-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtier-row.js
184 lines (148 loc) · 4.23 KB
/
tier-row.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/** @import TierElement from "./tier-element.js" */
import Pickr from "@simonwep/pickr";
import Sortable from "sortablejs";
export default class TierRow extends HTMLElement {
static sortableGroup = "tiers";
static clearColor = "#778899";
static defaultColors = [
"#ff7f7e",
"#ffbf7f",
"#feff7f",
"#7eff80",
"#7fffff",
"#807fff",
"#ff7ffe",
];
// #region Properties
get color() {
return this.getAttribute("color") ?? TierRow.clearColor;
}
get name() {
return this.getAttribute("name") ?? "New tier";
}
set color(value) {
this.pickr.setColor(value);
// If pickr is not initialized, set the color when it is
this.pickr.on("init", () => {
this.pickr.setColor(value);
});
}
set name(value) {
this.querySelector(".tier-label-content").textContent = value;
this.nameChanged();
}
// #endregion
constructor() {
super();
this.innerHTML = /* HTML */ `
<div class="tier-label" style="background-color: ${this.color};">
<div class="tier-label-text" contenteditable="true">
<span class="tier-label-content">${this.name}</span>
</div>
<div class="tier-tooltip" data-visibility="hidden"></div>
</div>
<div class="tier-content sort"></div>
<div class="tier-options">
<div class="tier-options-container">
<div class="tier-option-container tier-option-container-span">
<button class="tier-option tier-option-delete"></button>
</div>
<div class="tier-option-container">
<button class="tier-option tier-option-up"></button>
</div>
<div class="tier-option-container">
<button class="tier-option tier-option-down"></button>
</div>
</div>
</div>
`;
}
// #region Lifecycle
connectedCallback() {
// Color picker
this.createColorPicker();
// Sortable
const tierSort = /** @type {HTMLElement} */ (this.querySelector(".sort"));
this.sort = new Sortable(tierSort, { group: TierRow.sortableGroup });
// Options
const deleteButton = this.querySelector(".tier-option-delete");
const upButton = this.querySelector(".tier-option-up");
const downButton = this.querySelector(".tier-option-down");
deleteButton.addEventListener("click", this.deleteRow);
upButton.addEventListener("click", this.moveUp);
downButton.addEventListener("click", this.moveDown);
// Update tier name
const tierName = this.querySelector(".tier-label-text");
tierName.addEventListener("blur", this.nameChanged);
}
disconnectedCallback() {
this.pickr.destroyAndRemove();
this.sort.destroy();
}
// #endregion
// #region Methods
createColorPicker = () => {
// It needs to be created as it's deleted when the row is disconnected
const colorPicker = document.createElement("div");
const tooltip = this.querySelector(".tier-tooltip");
tooltip.appendChild(colorPicker);
this.pickr = new Pickr({
el: colorPicker,
theme: "monolith",
default: this.color,
swatches: TierRow.defaultColors,
components: {
preview: true,
hue: true,
interaction: {
input: true,
clear: true,
save: true,
},
},
});
this.pickr.on("save", this.colorChanged);
};
/**
* @param {Pickr.HSVaColor} color
*/
colorChanged = (color) => {
if (color === null) {
this.pickr.setColor(TierRow.clearColor);
return;
}
const hsl = color.toHSLA();
const lightness = hsl[2];
const tierLabel = /** @type {HTMLElement} */ (
this.querySelector(".tier-label")
);
this.setAttribute("color", color.toHEXA().toString());
tierLabel.style.backgroundColor = color.toHEXA().toString();
tierLabel.style.color = lightness < 50 ? "white" : "black";
this.pickr.hide();
};
nameChanged = () => {
const tierName = this.querySelector(".tier-label-content");
this.setAttribute("name", tierName.textContent);
};
deleteRow = () => {
/** @type {NodeListOf<TierElement>} */
const elements = this.querySelectorAll("tier-element");
for (const element of elements) {
element.revokeImageUrl();
}
this.remove();
};
moveUp = () => {
if (this.previousElementSibling) {
this.parentElement.insertBefore(this, this.previousElementSibling);
}
};
moveDown = () => {
if (this.nextElementSibling) {
this.parentElement.insertBefore(this.nextElementSibling, this);
}
};
// #endregion
}
customElements.define("tier-row", TierRow);