-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorPallete.js
55 lines (40 loc) · 1.63 KB
/
colorPallete.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
// This module handles events on color pallete
var colorPallete = ( () => {
// Get color pallete element from navbar
var colorPallete = navbar.getNavbar().childNodes[9];
// Initialize list of available colors
var colors = ["#000", "#fff", "#99c2ff", "#ff6666", "#99ff99", "#ffff80", "#dea562"];
// This function sets listeners on all color elements
var setListeners = () => {
// Get all the color elements
var shades = colorPallete.getElementsByClassName('circular');
for(var i = 0; i < shades.length; i++) {
// Set listener on each element
shades[i].addEventListener('click', event => {
// Remove class from previous clicked element
colorPallete.getElementsByClassName('clicked')[0].classList.remove('clicked');
// Add clicked class to clicked element
event.target.classList.add('clicked');
// Publish setColor event with background color of the clicked button
pubsub.emit('setColor', event.target.style.backgroundColor);
});
}
}
// This function renders our color elements to HTML
var render = () => {
// Clear color pallete
colorPallete.innerHTML = "";
// Loop through the list of colors and add their HTML
for( var i in colors ) {
colorPallete.innerHTML += '<div class="circular" style="background-color: '+colors[i]+';"></div>';
}
// Set clicked class on first element
colorPallete.querySelector('.circular').classList.add('clicked');
console.log( colorPallete.querySelector('.circular').style.backgroundColor);
pubsub.emit('setColor', colorPallete.querySelector('.circular').style.backgroundColor);
}
// Render the elements
render();
// Set Listeners
setListeners();
})();