Skip to content

Commit f19bf0b

Browse files
committed
Rename name variable. Update demo. Update test.
1 parent d813708 commit f19bf0b

File tree

4 files changed

+93
-94
lines changed

4 files changed

+93
-94
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414
],
1515
"dependencies": {
1616
"dxjs": "devex-web-frontend/dxjs#^2.0.3",
17-
"dropdown": "devex-web-frontend/dropdown#empty-title-group"
17+
"dropdown": "devex-web-frontend/dropdown#^2.4.1"
1818
}
1919
}

src/js/colorpicker.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -124,56 +124,55 @@ var Colorpicker = (function(DX, window, document, undefined) {
124124
};
125125
}
126126

127-
function getColorGroups(colors) {
127+
function getGroups(colors) {
128128

129-
var colorGroups = [];
129+
var groups = [];
130130

131131
if (!colors || colors.length === 0) {
132132
colorList = defaults.colorList;
133-
colorGroups.push(createGroup(colorList));
133+
groups.push(createGroup(colorList));
134134
} else {
135135
var colorsWithoutGroup = [];
136136

137-
colors.forEach(function(color) {
138-
if (isObject(color) || isString(color)) {
139-
var isItGroup = Array.isArray(color.colors);
137+
colors.forEach(function(item) {
138+
if (isObject(item) || isString(item)) {
139+
var isItGroup = Array.isArray(item.colors);
140140
if (isItGroup) {
141-
colorGroups.push(color);
141+
groups.push(item);
142142
} else {
143-
colorsWithoutGroup.push(color);
143+
colorsWithoutGroup.push(item);
144144
}
145145
}
146146
});
147147

148-
if (colorsWithoutGroup.length) {
149-
colorGroups.unshift(
150-
createGroup(colorsWithoutGroup)
151-
);
152-
}
148+
groups.unshift(
149+
createGroup(colorsWithoutGroup)
150+
);
151+
153152
}
154153

155-
return removeEmptyColorGroup(colorGroups);
154+
return removeEmptyGroups(groups);
156155
}
157156

158157
/**
159158
* Remove group without colors
160-
* @param {Array} colorGroups
159+
* @param {Array} groups
161160
* @returns {Array}
162161
*/
163-
function removeEmptyColorGroup(colorGroups) {
164-
return colorGroups.filter(function(colorGroup) {
165-
return colorGroup.colors.length > 0;
162+
function removeEmptyGroups(groups) {
163+
return groups.filter(function(group) {
164+
return group.colors.length > 0;
166165
});
167166
}
168167

169168
/**
170169
* Get all colors from groups
171-
* @param {Array} colorGroups
170+
* @param {Array} groups
172171
* @returns {Array}
173172
*/
174-
function getAllColorsFromGroups(colorGroups) {
175-
return colorGroups.reduce(function(colors, colorGroup) {
176-
return colors.concat(colorGroup.colors);
173+
function getAllColorsFromGroups(groups) {
174+
return groups.reduce(function(colors, group) {
175+
return colors.concat(group.colors);
177176
},[]);
178177
}
179178

@@ -182,18 +181,20 @@ var Colorpicker = (function(DX, window, document, undefined) {
182181
* @param {Array} colors
183182
*/
184183
function setColorList(colors) {
185-
var colorGroups = getColorGroups(colors);
186-
colorGroups.forEach(function(colorGroup) {
187-
colorGroup.colors = formatColors(colorGroup.colors);
184+
var groups = getGroups(colors);
185+
groups.forEach(function(group) {
186+
group.colors = formatColors(group.colors);
188187
});
189-
allColors = getAllColorsFromGroups(colorGroups);
190-
191-
var dataForDropDown = prepareDataForDropDown(colorGroups);
192-
dropDown.setDataList(dataForDropDown);
188+
allColors = getAllColorsFromGroups(groups);
193189

194-
if (allColors.length) {
190+
if (!allColors.length) {
191+
setColorList(defaults.colorList);
192+
} else {
193+
var dataForDropDown = prepareDataForDropDown(groups);
194+
dropDown.setDataList(dataForDropDown);
195195
setColor(allColors[0]);
196196
}
197+
197198
}
198199

199200
/**
@@ -215,15 +216,15 @@ var Colorpicker = (function(DX, window, document, undefined) {
215216

216217
/**
217218
* Get data for dropDown
218-
* @param {{groupTitle:string, options:Array}[]} colorGroups
219+
* @param {{groupTitle:string, options:Array}[]} groups
219220
* @returns {Array}
220221
*/
221-
function prepareDataForDropDown(colorGroups) {
222+
function prepareDataForDropDown(groups) {
222223

223-
return colorGroups.map(function(colorGroup) {
224+
return groups.map(function(group) {
224225
return {
225-
title: colorGroup.groupTitle || '',
226-
options: colorGroup.colors.map(function(value) {
226+
title: group.groupTitle || '',
227+
options: group.colors.map(function(value) {
227228
return {
228229
value: value
229230
};

test/html/default.case.html

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,32 @@
2323
<script>
2424

2525
Colorpicker.colorList = [
26-
['#dasdasd','#dsadasd'],
27-
'#FFFFFF', '#e5e5e5', '#cccccc', '#b5b5b5', '#999999', '#828282',
28-
'#666666', '#4f4f4f', '#333333', '#1c1c1c', '#161412',
2926
{
30-
groupTitle: 'Basic and Studies',
31-
colors: [
32-
'#962d3e','#d9372c','#de7d2c','#ffc973','#d9d96c','#60994d',
33-
'#79bd8f','#00b892','#348899','#14a0b3','#92c4e0'
34-
]
35-
},
36-
'#ff6666','#ffaf66','#fffb66','#b8ff66','#66ff66','#66ffaf',
37-
'#66fffb','#66beff','#6666ff','#bc66ff','#fb66ff',
38-
{
39-
groupTitle: 'Common',
40-
colors: [
41-
'#ff9999','#ffc799','#fffa99','#d3ff99','#99ff99','#66ffaf',
42-
'#99fffa','#99d8ff','#9999ff','#cd99ff','#fa99ff',
43-
'#ff6666','#ffaf66','#fffb66','#b8ff66','#66ff66','#66ffaf',
44-
'#66fffb','#66beff','#6666ff','#bc66ff','#fb66ff',
45-
'#cc2929','#cc7229','#ccc429','#85cc29','#29cc29','#29cc72',
46-
'#29ccc4','#298ecc','#5e5ee5','#8b54cc','#c429cc',
47-
'#a50000','#a54300','#998e00','#5a9900','#009900','#009941',
48-
'#00998e','#006699','#4646af','#5f3c96','#8e0099'
49-
]
27+
groupTitle: 'test',
28+
colors: []
5029
}
5130
];
5231
</script>
5332
<script type="text/javascript">
5433
var colorPickers = [];
5534
window.onload = function() {
5635
var inputColorPicker = document.getElementById('Input_ColorPicker');
36+
var inputColorPickerGroupWithoutTitle = document.getElementById('Input_ColorPicker_GroupWithoutTitle');
5737
inputColorPicker.colorList = [
58-
'#66fffb','#66beff','#6666ff','#bc66ff','#fb66ff',
5938
{
6039
groupTitle: 'Group 1',
6140
colors: [
6241
'#ffffff', '#e5e5e5', '#cccccc', '#b5b5b5', '#999999', '#828282'
6342
]
6443
}
6544
];
45+
inputColorPickerGroupWithoutTitle.colorList = [
46+
{
47+
colors: [
48+
'#ffffff', '#e5e5e5', '#cccccc', '#b5b5b5', '#999999', '#828282'
49+
]
50+
}
51+
];
6652
Array.prototype.forEach.call(document.querySelectorAll('[data-colorpicker]'), function(input) {
6753
colorPickers.push(new Colorpicker(input));
6854
})
@@ -74,6 +60,10 @@
7460
<legend>Color List from object Colorpicker.colorList</legend>
7561
<input data-ng-model="default" data-colorpicker type="text" value=""/>
7662
</fieldset>
63+
<fieldset>
64+
<legend>Color List with group without title</legend>
65+
<input data-ng-model="default" data-colorpicker type="text" value="" id="Input_ColorPicker_GroupWithoutTitle"/>
66+
</fieldset>
7767
<fieldset>
7868
<legend>Color List from property DOM Element</legend>
7969
<input data-ng-model="default" data-colorpicker type="text" value="" id="Input_ColorPicker"/>

test/js/colorpicker.unit.spec.js

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ describe('Colorpicker', function() {
55
var elementTmpl = [
66
'<input type="text" value="" id="test" />'
77
].join(''),
8+
defaultColorList = ['#511717', '#000000', '#fefefe'],
89
colorPicker,
910
testElement;
1011

1112
beforeEach(function() {
1213
document.body.innerHTML = elementTmpl;
1314
testElement = document.getElementById('test');
1415
window.DropDown = DropDownMock;
15-
window.Colorpicker.colorList = ['#511717', '#000000', '#fefefe'];
16+
window.Colorpicker.colorList = defaultColorList;
1617
});
1718

1819
afterEach(function() {
@@ -24,61 +25,68 @@ describe('Colorpicker', function() {
2425

2526
describe('dropDownData', function() {
2627

27-
it('should create one group from colors without group', function() {
28-
var dropDown,
29-
dataList;
28+
var dropDown,
29+
dataList,
30+
temp,
31+
initColorPicker;
32+
33+
beforeEach(function(){
3034

31-
new Colorpicker(testElement);
35+
initColorPicker = function(colorList) {
36+
colorList = colorList || defaultColorList;
37+
window.Colorpicker.colorList = colorList;
38+
new Colorpicker(testElement);
3239

33-
dropDown = DropDown.___instance;
34-
dataList = dropDown.getDataList();
40+
dropDown = DropDown.___instance;
41+
dataList = dropDown.getDataList();
42+
};
43+
44+
afterEach(function() {
45+
window.Colorpicker.colorList = [];
46+
});
3547

36-
expect(dataList.length).toEqual(1);
37-
expect(dataList[0].title).toEqual('');
3848
});
3949

40-
it('should create group with passed title' , function() {
41-
window.Colorpicker.colorList = [{
42-
groupTitle: 'test',
43-
colors: ['#000', '#fff']
44-
}];
50+
it('should create one group from colors without group', function() {
4551

46-
new Colorpicker(testElement);
52+
initColorPicker();
53+
54+
expect(dataList.length).toEqual(1);
55+
expect(dataList[0].title).toEqual('');
56+
});
4757

48-
dropDown = DropDown.___instance;
49-
dataList = dropDown.getDataList();
58+
it('should create group with passed title' , function() {
59+
initColorPicker([
60+
{
61+
groupTitle: 'test',
62+
colors: ['#000', '#fff']
63+
}
64+
]);
5065
expect(dataList.length).toEqual(1);
5166
expect(dataList[0].title).toEqual('test');
5267

5368
});
5469

5570
it('should not create group with blank colors', function() {
56-
window.Colorpicker.colorList = [{
57-
groupTitle: 'test',
58-
colors: []
59-
}];
60-
61-
new Colorpicker(testElement);
62-
63-
dropDown = DropDown.___instance;
64-
dataList = dropDown.getDataList();
65-
expect(dataList.length).toEqual(0);
71+
initColorPicker([
72+
'#000', '#fff',
73+
{
74+
groupTitle: 'test',
75+
colors: []
76+
}
77+
]);
78+
expect(dataList.length).toEqual(1);
6679
});
6780

6881
it('should create the same number of groups from mixed config', function() {
69-
window.Colorpicker.colorList = [
82+
initColorPicker([
7083
'#000', '#fff',
7184
{
7285
groupTitle: 'test',
7386
colors: ['#000', '#fff']
7487
},
7588
'#000', '#fff'
76-
];
77-
78-
new Colorpicker(testElement);
79-
80-
dropDown = DropDown.___instance;
81-
dataList = dropDown.getDataList();
89+
]);
8290
expect(dataList.length).toEqual(2);
8391
})
8492

0 commit comments

Comments
 (0)