forked from tricia-holmes/ColoRandom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
96 lines (82 loc) · 2.76 KB
/
scripts.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// DOM Elements
var boxes = document.querySelectorAll(".swatch")
var hexes = document.querySelectorAll(".box-details > p")
var locks = document.querySelectorAll(".material-symbols-outlined")
var newButton = document.querySelector("#new")
var saveButton = document.querySelector("#save")
// Side Display
var asideSection = document.querySelector(".palettes")
var savedPaletteSection = document.querySelector(".saved-palettes")
// Event Listeners
window.addEventListener("load", createNewPalette)
newButton.addEventListener("click", randomizeColorPalette)
saveButton.addEventListener("click", savePalette)
for (var i = 0; i < boxes.length; i++) {
boxes[i].addEventListener("click", toggleLock)
}
savedPaletteSection.addEventListener("click", handleSavedPaletteSectionClick)
// Global Variables
var currentPalette
var savedPalettes = []
// Event Handlers
function createNewPalette() {
currentPalette = new Palette()
var colors = currentPalette.colors
for (var i = 0; i < colors.length; i++) {
var color = colors[i]
boxes[i].setAttribute("id", color.id)
boxes[i].style.backgroundColor = color.hexCode
hexes[i].innerText = color.hexCode
locks[i].innerText = "lock_open"
}
}
function randomizeColorPalette() {
currentPalette.refreshColors()
var colors = currentPalette.colors
for (var i = 0; i < colors.length; i++) {
var color = colors[i]
boxes[i].style.backgroundColor = color.hexCode
hexes[i].innerText = color.hexCode
}
}
function toggleLock(event) {
var boxId = event.target.id
var index = currentPalette.toggleColorLock(boxId)
locks[index].innerText = currentPalette.colors[index].lockIconValue()
}
function savePalette() {
savedPalettes.push(currentPalette)
asideSection.innerHTML = ""
for (var i = 0; i < savedPalettes.length; i++) {
var colors = savedPalettes[i].colors
var paletteId = savedPalettes[i].id
createSection(colors, paletteId)
}
}
function createSection(colors, paletteId) {
var newElement = document.createElement("section")
newElement.classList.add("palette")
for (var i = 0; i < colors.length; i++) {
newElement.innerHTML += `
<div style = "background-color:${colors[i].hexCode}"></div>`
asideSection.appendChild(newElement)
}
newElement.innerHTML += `
<button class="delete-button">
<span class="material-symbols-outlined" id="${paletteId}"> delete </span>
</button>`
createNewPalette()
}
function deletePalette(event){
for (var i = 0; i < savedPalettes.length; i++) {
if (savedPalettes[i].id === event.target.id) {
savedPalettes.splice(i, 1)
}
}
event.target.parentNode.parentNode.remove()
}
function handleSavedPaletteSectionClick(event) {
if (event.target.className === "material-symbols-outlined") {
deletePalette(event)
}
}