Skip to content

Commit fbd275c

Browse files
committed
MAGETWO-71622: [UI Component] Color Picker
1 parent 88857f6 commit fbd275c

File tree

14 files changed

+86
-68
lines changed

14 files changed

+86
-68
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function getComponentName(): string
6565
*
6666
* @return void
6767
*/
68-
public function prepare()
68+
public function prepare() : void
6969
{
7070
$modes = $this->modesProvider->getModes();
7171
$colorPickerModeSetting = $this->getData('config/colorPickerMode');

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magento\Ui\Model\ColorPicker;
1010

1111
/**
12-
* Class ColorModesProvider
12+
* Collect all modes by configuration
1313
*/
1414
class ColorModesProvider
1515
{
@@ -27,6 +27,10 @@ class ColorModesProvider
2727
*/
2828
private $objectManager;
2929

30+
/**
31+
* @param array $colorModesPool
32+
* @param \Magento\Framework\ObjectManagerInterface $objectManager
33+
*/
3034
public function __construct(
3135
array $colorModesPool,
3236
\Magento\Framework\ObjectManagerInterface $objectManager
@@ -58,9 +62,6 @@ public function getModes(): array
5862
*/
5963
private function createModeProvider(string $instance): ModeInterface
6064
{
61-
if (!is_subclass_of($instance, ModeInterface::class)) {
62-
throw new \InvalidArgumentException($instance . ' does not implement ' . ModeInterface::class);
63-
}
6465
return $this->objectManager->create($instance);
6566
}
6667
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class FullMode implements ModeInterface
1616
/**
1717
* {@inheritdoc}
1818
*
19-
* @return array
2019
*/
2120
public function getConfig(): array
2221
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ interface ModeInterface
1818
*
1919
* @return array
2020
*/
21-
public function getConfig();
21+
public function getConfig() : array ;
2222
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Magento\Ui\Model\ColorPicker;
1010

1111
/**
12-
* Returns config parameters for paletteonly mode
12+
* Returns config parameters for palette only mode
1313
*/
1414
class PaletteOnlyMode implements ModeInterface
1515
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
</imageUploader>
152152
<colorPicker class="Magento\Ui\Component\Form\Element\ColorPicker" component="Magento_Ui/js/form/element/color-picker" template="ui/form/field">
153153
<settings>
154-
<elementTmpl>ui/form/element/colorPicker</elementTmpl>
154+
<elementTmpl>ui/form/element/color-picker</elementTmpl>
155155
<colorFormat>rgb</colorFormat>
156156
<colorPickerMode>full</colorPickerMode>
157157
</settings>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
/**
6+
* @api
7+
*/
8+
define([], function () {
9+
'use strict';
10+
11+
return [
12+
[
13+
'rgb(0,0,0)', 'rgb(52,52,52)', 'rgb(83,83,83)', 'rgb(135,135,135)', 'rgb(193,193,193)',
14+
'rgb(234,234,234)', 'rgb(240,240,240)', 'rgb(255,255,255)'
15+
],
16+
[
17+
'rgb(252,0,9)', 'rgb(253,135,10)', 'rgb(255,255,13)', 'rgb(35,255,9)', 'rgb(33,255,255)',
18+
'rgb(0,0,254)', 'rgb(132,0,254)', 'rgb(251,0,255)'
19+
],
20+
[
21+
'rgb(240,192,194)', 'rgb(251,223,194)', 'rgb(255,241,193)', 'rgb(210,230,201)',
22+
'rgb(199,217,220)', 'rgb(197,219,240)', 'rgb(208,200,227)', 'rgb(229,199,212)'
23+
],
24+
[
25+
'rgb(228,133,135)', 'rgb(246,193,139)', 'rgb(254,225,136)', 'rgb(168,208,152)',
26+
'rgb(146,184,190)', 'rgb(143,184,227)', 'rgb(165,148,204)', 'rgb(202,147,175)'
27+
],
28+
[
29+
'rgb(214,78,83)', 'rgb(243,163,88)', 'rgb(254,211,83)', 'rgb(130,187,106)',
30+
'rgb(99,149,159)', 'rgb(93,150,211)', 'rgb(123,100,182)', 'rgb(180,100,142)'
31+
],
32+
[
33+
'rgb(190,0,5)', 'rgb(222,126,44)', 'rgb(236,183,39)', 'rgb(89,155,61)', 'rgb(55,110,123)',
34+
'rgb(49,112,185)', 'rgb(83,55,150)', 'rgb(147,55,101)'
35+
],
36+
[
37+
'rgb(133,0,3)', 'rgb(163,74,10)', 'rgb(177,127,7)', 'rgb(45,101,23)', 'rgb(18,62,74)',
38+
'rgb(14,62,129)', 'rgb(40,15,97)', 'rgb(95,16,55)'
39+
],
40+
[
41+
'rgb(81,0,1)', 'rgb(100,48,7)', 'rgb(107,78,3)', 'rgb(31,63,16)',
42+
'rgb(13,39,46)', 'rgb(10,40,79)', 'rgb(24,12,59)', 'rgb(59,10,36)'
43+
]
44+
];
45+
});

app/code/Magento/Ui/view/base/web/js/form/element/color-picker.js

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,11 @@
88
*/
99
define([
1010
'mage/translate',
11-
'Magento_Ui/js/form/element/abstract'
12-
], function ($t, Abstract) {
11+
'Magento_Ui/js/form/element/abstract',
12+
'Magento_Ui/js/form/element/color-picker-palette'
13+
], function ($t, Abstract, palette) {
1314
'use strict';
1415

15-
var defaultColorPalette = [
16-
[
17-
'rgb(0,0,0)', 'rgb(52,52,52)', 'rgb(83,83,83)', 'rgb(135,135,135)', 'rgb(193,193,193)',
18-
'rgb(234,234,234)', 'rgb(240,240,240)', 'rgb(255,255,255)'
19-
],
20-
[
21-
'rgb(252,0,9)', 'rgb(253,135,10)', 'rgb(255,255,13)', 'rgb(35,255,9)', 'rgb(33,255,255)',
22-
'rgb(0,0,254)', 'rgb(132,0,254)', 'rgb(251,0,255)'
23-
],
24-
[
25-
'rgb(240,192,194)', 'rgb(251,223,194)', 'rgb(255,241,193)', 'rgb(210,230,201)',
26-
'rgb(199,217,220)', 'rgb(197,219,240)', 'rgb(208,200,227)', 'rgb(229,199,212)'
27-
],
28-
[
29-
'rgb(228,133,135)', 'rgb(246,193,139)', 'rgb(254,225,136)', 'rgb(168,208,152)',
30-
'rgb(146,184,190)', 'rgb(143,184,227)', 'rgb(165,148,204)', 'rgb(202,147,175)'
31-
],
32-
[
33-
'rgb(214,78,83)', 'rgb(243,163,88)', 'rgb(254,211,83)', 'rgb(130,187,106)',
34-
'rgb(99,149,159)', 'rgb(93,150,211)', 'rgb(123,100,182)', 'rgb(180,100,142)'
35-
],
36-
[
37-
'rgb(190,0,5)', 'rgb(222,126,44)', 'rgb(236,183,39)', 'rgb(89,155,61)', 'rgb(55,110,123)',
38-
'rgb(49,112,185)', 'rgb(83,55,150)', 'rgb(147,55,101)'
39-
],
40-
[
41-
'rgb(133,0,3)', 'rgb(163,74,10)', 'rgb(177,127,7)', 'rgb(45,101,23)', 'rgb(18,62,74)',
42-
'rgb(14,62,129)', 'rgb(40,15,97)', 'rgb(95,16,55)'
43-
],
44-
[
45-
'rgb(81,0,1)', 'rgb(100,48,7)', 'rgb(107,78,3)', 'rgb(31,63,16)',
46-
'rgb(13,39,46)', 'rgb(10,40,79)', 'rgb(24,12,59)', 'rgb(59,10,36)'
47-
]
48-
];
49-
5016
return Abstract.extend({
5117

5218
defaults: {
@@ -56,8 +22,8 @@ define([
5622
maxSelectionSize: 8,
5723
clickoutFiresChange: true,
5824
allowEmpty: true,
59-
localStorageKey: "magento.spectrum",
60-
palette: defaultColorPalette
25+
localStorageKey: 'magento.spectrum',
26+
palette: palette
6127
}
6228
},
6329

@@ -69,8 +35,8 @@ define([
6935
this._super();
7036

7137
this.colorPickerConfig.value = this.value;
38+
7239
return this;
7340
}
7441
});
7542
});
76-

app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/bootstrap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ define(function (require) {
3838
tooltip: require('./tooltip'),
3939
repeat: require('knockoutjs/knockout-repeat'),
4040
fastForEach: require('knockoutjs/knockout-fast-foreach'),
41-
colorPicker: require('./colorPicker')
41+
colorPicker: require('./color-picker')
4242
};
4343
});

app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/colorPicker.js renamed to app/code/Magento/Ui/view/base/web/js/lib/knockout/bindings/color-picker.js

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ define([
1212
'use strict';
1313

1414
ko.bindingHandlers.colorPicker = {
15-
1615
/**
1716
* Binding init callback.
1817
*
@@ -22,26 +21,27 @@ define([
2221
* @param {Object} viewModel
2322
*/
2423
init: function (element, valueAccessor, allBindings, viewModel) {
25-
var config = valueAccessor();
24+
var config = valueAccessor(),
2625

27-
if (!viewModel.disabled()) {
28-
config.change = function (value) {
26+
/** change value */
27+
changeValue = function (value) {
2928
if (value == null) {
3029
value = '';
3130
}
3231
config.value(value.toString());
3332
};
3433

35-
config.hide = function (value) {
36-
if (value == null) {
37-
value = '';
38-
}
39-
config.value(value.toString());
40-
};
34+
if (!viewModel.disabled()) {
35+
config.change = changeValue;
36+
37+
config.hide = changeValue;
38+
39+
/** show value */
4140
config.show = function () {
4241
if (!viewModel.focused()) {
4342
viewModel.focused(true);
4443
}
44+
4545
return true;
4646
};
4747
$(element).spectrum(config);
@@ -52,11 +52,18 @@ define([
5252
}
5353
},
5454

55-
update: function(element, valueAccessor) {
55+
/**
56+
* Reads params passed to binding, parses component declarations.
57+
* Fetches for those found and attaches them to the new context.
58+
*
59+
* @param {HTMLElement} element
60+
* @param {Function} valueAccessor
61+
*/
62+
update: function (element, valueAccessor) {
5663
var config = valueAccessor();
5764

5865
if (tinycolor(config.value()).isValid() || config.value() === '') {
59-
$(element).spectrum("set", config.value());
66+
$(element).spectrum('set', config.value());
6067
}
6168
}
6269
};

0 commit comments

Comments
 (0)