Skip to content

Commit 65df5cb

Browse files
author
Robert He
committed
MAGETWO-89658: Add validation check and rule
- added validation for color inputs
1 parent 13eac48 commit 65df5cb

File tree

12 files changed

+1239
-27
lines changed

12 files changed

+1239
-27
lines changed

app/code/Magento/Ui/Component/Form/Element/ColorPicker.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
/**
1717
* @api
18-
* @since 100.0.2
1918
*/
2019
class ColorPicker extends AbstractElement
2120
{
@@ -46,7 +45,7 @@ public function __construct(
4645
*
4746
* @return string
4847
*/
49-
public function getComponentName()
48+
public function getComponentName(): string
5049
{
5150
return static::NAME;
5251
}

app/code/Magento/Ui/Model/ColorPicker/FullMode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function getConfig()
2222
{
2323
return [
2424
'showInput' => true,
25-
'allowEmpty' => false,
25+
'allowEmpty' => true,
2626
'showInitial' => false,
2727
'showPalette' => true,
28-
'showAlpha' => false,
28+
'showAlpha' => true,
2929
'showSelectionPalette' => true
3030
];
3131
}

app/code/Magento/Ui/view/base/requirejs-config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var config = {
1212
paths: {
1313
'ui/template': 'Magento_Ui/templates',
1414
'tinymce4': 'tiny_mce_4/tinymce.min',
15-
'spectrum': 'jquery/spectrum',
15+
'spectrum': 'jquery/spectrum/spectrum',
16+
'tinycolor': 'jquery/spectrum/tinycolor',
1617
'wysiwygAdapter': 'mage/adminhtml/wysiwyg/tiny_mce/tinymce4Adapter'
1718
},
1819
map: {

app/code/Magento/Ui/view/base/ui_component/etc/definition.map.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@
107107
<schema name="current">
108108
<argument name="data" xsi:type="array">
109109
<item name="config" xsi:type="array">
110-
<item name="initialColor" type="string" xsi:type="xpath">settings/initialColor</item>
111110
<item name="colorPickerMode" type="string" xsi:type="xpath">settings/colorPickerMode</item>
112111
<item name="colorFormat" type="string" xsi:type="xpath">settings/colorFormat</item>
113112
</item>

app/code/Magento/Ui/view/base/ui_component/etc/definition.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@
151151
</imageUploader>
152152
<colorPicker class="Magento\Ui\Component\Form\Element\ColorPicker" component="Magento_Ui/js/form/element/colorPicker" template="ui/form/element/colorPicker">
153153
<settings>
154-
<initialColor>rgb(0,0,0)</initialColor>
155154
<colorFormat>rgb</colorFormat>
156155
<colorPickerMode>full</colorPickerMode>
157156
</settings>

app/code/Magento/Ui/view/base/ui_component/etc/definition/colorPicker.xsd

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,11 @@
2525
<xs:group name="componentColorPickerSettings">
2626
<xs:choice>
2727
<xs:group ref="abstractSettings"/>
28-
<xs:element name="initialColor" type="xs:string">
29-
<xs:annotation>
30-
<xs:documentation>
31-
Defines the initial color that appears in the color selection dropdown and input field.
32-
</xs:documentation>
33-
</xs:annotation>
34-
</xs:element>
3528
<xs:element name="colorFormat" type="xs:string">
3629
<xs:annotation>
3730
<xs:documentation>
3831
Defines the color format that is displayed in selection tool as well as in input field.
39-
Valid formats: hex, rgb, hsl, hsv
32+
Valid formats: hex, rgb, hsl, name, none
4033
</xs:documentation>
4134
</xs:annotation>
4235
</xs:element>

app/code/Magento/Ui/view/base/web/js/form/element/colorPicker.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ define([
88
'uiLayout',
99
'mage/translate',
1010
'Magento_Ui/js/form/element/abstract',
11-
'spectrum'
12-
], function ($, _, layout, $t, Abstract, spectrum) {
11+
'spectrum',
12+
'tinycolor'
13+
], function ($, _, layout, $t, Abstract, spectrum, tinycolor) {
1314
'use strict';
1415

1516
var defaultColorPalette = [
@@ -27,10 +28,12 @@ define([
2728

2829
defaults: {
2930
colorPickerConfig: {
30-
chooseText: "Apply",
31-
cancelText: "Cancel",
31+
chooseText: $t('Apply'),
32+
cancelText: $t('Cancel'),
3233
maxSelectionSize: 8,
3334
clickoutFiresChange: true,
35+
allowEmpty: true,
36+
localStorageKey: "magento.spectrum",
3437
palette: defaultColorPalette
3538
}
3639
},
@@ -61,6 +64,19 @@ define([
6164
this._super();
6265

6366
return this;
67+
},
68+
69+
showColorPicker: function () {
70+
jQuery('#colorPicker-spectrum').spectrum("show");
71+
},
72+
73+
setInputValue: function() {
74+
var inputValue = jQuery('#colorPicker-input').val(),
75+
inputColor = tinycolor(inputValue);
76+
77+
if (inputColor.isValid()) {
78+
jQuery('#colorPicker-spectrum').spectrum("set", inputValue);
79+
}
6480
}
6581

6682
});

app/code/Magento/Ui/view/base/web/js/lib/validation/rules.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ define([
1111
'underscore',
1212
'./utils',
1313
'moment',
14+
'tinycolor',
1415
'jquery/validate',
1516
'jquery/ui',
1617
'mage/translate'
17-
], function ($, _, utils, moment) {
18+
], function ($, _, utils, moment, tinycolor) {
1819
'use strict';
1920

2021
/**
@@ -963,6 +964,13 @@ define([
963964
return moment.utc(value, params.dateFormat).unix() <= maxValue;
964965
},
965966
$.mage.__('The date is not within the specified range.')
967+
],
968+
'validate-color': [
969+
function (value) {
970+
if (value === 'No Color')
971+
return tinycolor(value).isValid();
972+
},
973+
$.mage.__('Wrong color format.')
966974
]
967975
}, function (data) {
968976
return {

app/code/Magento/Ui/view/base/web/templates/form/element/colorPicker.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<div class="admin__field-control"
1414
css="'_with-tooltip': $data.tooltip, '_with-reset': $data.showFallbackReset && $data.isDifferedFromDefault">
1515

16-
<input type="hidden" colorPicker="colorPickerConfig" ko-value="value" style="display: none"/>
17-
<input type="text" ko-value="value"/>
16+
<input type="hidden" id="colorPicker-spectrum" colorPicker="colorPickerConfig" ko-value="value" style="display: none"/>
17+
<input type="text" id="colorPicker-input" ko-value="value" data-bind="event: { focus: showColorPicker, mouseout: setInputValue }"/>
18+
19+
<label class="admin__field-error" if="error" attr="for: uid" text="error"/>
1820
</div>
1921
</div>

app/design/adminhtml/Magento/backend/Magento_Ui/web/css/source/module/spectrum.less

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,13 @@ See http://bgrins.github.io/spectrum/themes/ for instructions.
371371
display:inline-block;
372372
*zoom: 1;
373373
*display: inline;
374-
border: solid 1px #91765d;
374+
border: solid 1px #1979c3;
375375
background: #eee;
376376
color: #333;
377377
vertical-align: middle;
378378
}
379379
.sp-replacer:hover, .sp-replacer.sp-active {
380-
border-color: #F0C49B;
380+
border-color: #1979c3;
381381
color: #111;
382382
}
383383
.sp-replacer.sp-disabled {
@@ -395,9 +395,8 @@ See http://bgrins.github.io/spectrum/themes/ for instructions.
395395
.sp-preview {
396396
position:relative;
397397
width:25px;
398-
height: 20px;
399-
border: solid 1px #222;
400-
margin-right: 5px;
398+
height: 24px;
399+
border: solid 1px #1979c3;
401400
float:left;
402401
z-index: 0;
403402
}
@@ -415,6 +414,7 @@ See http://bgrins.github.io/spectrum/themes/ for instructions.
415414

416415
.sp-container {
417416
padding-bottom:0;
417+
box-shadow: 1px 1px 5px rgba(0,0,0,0.5);
418418
}
419419

420420

0 commit comments

Comments
 (0)