forked from dli/vortexspheres
-
Notifications
You must be signed in to change notification settings - Fork 1
/
buttons.js
41 lines (32 loc) · 1004 Bytes
/
buttons.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
'use strict'
var Buttons = function (elements, changeCallback) {
var activeElement = elements[0];
var color;
this.setColor = function (newColor) {
color = newColor;
refresh();
};
var refresh = function () {
for (var i = 0; i < elements.length; ++i) {
if (elements[i] === activeElement) {
elements[i].style.color = '#111111';
} else {
elements[i].style.color = '#777777';
}
}
};
for (var i = 0; i < elements.length; ++i) {
(function () { //create closure to store index
var index = i;
var clickedElement = elements[i];
elements[i].addEventListener('click', function () {
if (activeElement !== clickedElement) {
activeElement = clickedElement;
changeCallback(index);
refresh();
}
});
}());
}
refresh();
};