Skip to content

Commit 7803803

Browse files
committed
Remove recursion. Update demo
1 parent f19bf0b commit 7803803

File tree

2 files changed

+51
-33
lines changed

2 files changed

+51
-33
lines changed

src/js/colorpicker.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -126,32 +126,33 @@ var Colorpicker = (function(DX, window, document, undefined) {
126126

127127
function getGroups(colors) {
128128

129-
var groups = [];
130-
131-
if (!colors || colors.length === 0) {
132-
colorList = defaults.colorList;
133-
groups.push(createGroup(colorList));
134-
} else {
135-
var colorsWithoutGroup = [];
136-
137-
colors.forEach(function(item) {
138-
if (isObject(item) || isString(item)) {
139-
var isItGroup = Array.isArray(item.colors);
140-
if (isItGroup) {
141-
groups.push(item);
142-
} else {
143-
colorsWithoutGroup.push(item);
144-
}
129+
colors = colors || [];
130+
131+
var groups = [],
132+
colorsWithoutGroup = [];
133+
134+
colors.forEach(function(item) {
135+
if (isObject(item) || isString(item)) {
136+
var isItGroup = Array.isArray(item.colors);
137+
if (isItGroup) {
138+
groups.push(item);
139+
} else {
140+
colorsWithoutGroup.push(item);
145141
}
146-
});
142+
}
143+
});
147144

148-
groups.unshift(
149-
createGroup(colorsWithoutGroup)
150-
);
145+
groups.unshift(
146+
createGroup(colorsWithoutGroup)
147+
);
151148

152-
}
149+
groups = removeEmptyGroups(groups);
153150

154-
return removeEmptyGroups(groups);
151+
if (!groups.length) {
152+
colorList = defaults.colorList;
153+
groups.push(createGroup(colorList));
154+
}
155+
return groups;
155156
}
156157

157158
/**
@@ -187,13 +188,9 @@ var Colorpicker = (function(DX, window, document, undefined) {
187188
});
188189
allColors = getAllColorsFromGroups(groups);
189190

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

198195
}
199196

test/html/default.case.html

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,30 @@
2323
<script>
2424

2525
Colorpicker.colorList = [
26+
['#dasdasd','#dsadasd'],
27+
'#FFFFFF', '#e5e5e5', '#cccccc', '#b5b5b5', '#999999', '#828282',
28+
'#666666', '#4f4f4f', '#333333', '#1c1c1c', '#161412',
2629
{
27-
groupTitle: 'test',
28-
colors: []
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+
]
2950
}
3051
];
3152
</script>
@@ -38,14 +59,14 @@
3859
{
3960
groupTitle: 'Group 1',
4061
colors: [
41-
'#ffffff', '#e5e5e5', '#cccccc', '#b5b5b5', '#999999', '#828282'
62+
'#00998e','#006699','#4646af','#5f3c96','#8e0099'
4263
]
4364
}
4465
];
4566
inputColorPickerGroupWithoutTitle.colorList = [
4667
{
4768
colors: [
48-
'#ffffff', '#e5e5e5', '#cccccc', '#b5b5b5', '#999999', '#828282'
69+
'#cc2929','#cc7229','#ccc429','#85cc29','#29cc29','#29cc72',
4970
]
5071
}
5172
];

0 commit comments

Comments
 (0)