-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpicker.js
65 lines (53 loc) · 1.83 KB
/
picker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
var color_options = [
'#00A0B0',
'#CC333F',
'#EB6841',
'#FFC640',
'#A0F235',
'#6EA73C',
'#13131A'
];
// A constructor for a color picker
// Takes a wrapper element and an array of CSS colors
var ColorBox = function(wrapper, colors) {
// Get the width and height of the wrapper element so we can properly
// size the color boxes
var wrapper_width = wrapper.offsetWidth;
var wrapper_height = wrapper.offsetHeight;
var box_width = String(Math.floor(wrapper_width / colors.length)) + "px";
var box_height = String(wrapper_height) + "px";
this.cboxes = [];
var self = this;
// Create a span for each color
for (var color_index in colors) {
(function () {
var cbox = document.createElement('span');
cbox.className += 'colorbox-option';
cbox.style.display = 'inline-block';
cbox.style.width = box_width;
cbox.style.height = box_height;
var box_color = colors[color_index];
cbox.style.backgroundColor = box_color;
cbox.onclick = function() {
self._setValue(cbox);
}
wrapper.appendChild(cbox);
self.cboxes.push(cbox);
})()
}
this._setValue = function(selected_cbox) {
// Set the property of the object
this.value = selected_cbox.style.backgroundColor;
// Now go through and update the UI to indicate whih box
// was selected
for (var cbox_index in this.cboxes) {
var target_cbox = this.cboxes[cbox_index];
if (target_cbox == selected_cbox) {
target_cbox.classList.add('colorbox-selected');
} else {
target_cbox.classList.remove('colorbox-selected');
}
}
}
this._setValue(this.cboxes[0]);
};