-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathverse.js
417 lines (362 loc) · 11.3 KB
/
verse.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import {findAncestor, nbsp} from "./utils.js";
import {SideChordsInit} from "./sidechords.js";
import {pushRowChords} from "./songbody.js";
export class SongVerse extends HTMLElement {
constructor() {
super();
const template = document.createElement('template');
template.innerHTML = `
<link rel="stylesheet" href="./verse.css"/>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<div class="verse">
<div class="verse_meta">
<span id="nr"></span>
<div>
<label><input id="blocklink" name="blocklink" type="checkbox"/>już było</label>
<label for="bt_verse"><input type="radio" id="bt_verse" value="verse" name="block_type"/>zwrotka</label>
<label for="bt_chorus"><input type="radio" id="bt_chorus" value="chorus" name="block_type"/>refren</label>
<label for="bt_other"><input type="radio" id="bt_other" value="other" name="block_type"/>wstawka</label>
<button id="delete" class="material-icons">delete</button>
</div>
</div>
<div id="verse_link" class="verse_link">
<select id="verse_link_sel">
<option>Foo</option>
<option>Bar</option>
</select>
</div>
<div id="verse_sidechords"></div>
<div id="verse_main" class="verse_main">
<slot id="slot0"/>
</div>
</div>
`;
const shadow = this.attachShadow({ mode: "closed" });
shadow.appendChild(template.content.cloneNode(true));
this.nr=shadow.getElementById("nr");
this.slot0 = shadow.getElementById("slot0");
this.blocklink=shadow.getElementById("blocklink");
this.main=shadow.getElementById("verse_main");
this.link=shadow.getElementById("verse_link");
this.sidechords=shadow.getElementById("verse_sidechords");
this.linkSel=shadow.getElementById("verse_link_sel");
this.buttonDelete=shadow.getElementById("delete");
this.blocklink.addEventListener("input", (e) => this.blocklinkoninput(e, this));
this.linkSel.addEventListener("input", (e) => this.inputLinkVerse(e, this));
this.buttonDelete.addEventListener("click", (e) => this.remove());
this.btRadios={}
for (const bt_radio of shadow.querySelectorAll('input[name="block_type"]')) {
bt_radio.addEventListener("input", (e) => this.refoninput(e, this));
this.btRadios[bt_radio.value]=bt_radio;
}
const observer = new MutationObserver(()=> {this.refreshSidechords();});
observer.observe(this, { attributes: false, childList: true, subtree: true, characterData:false });
this.resizeObserver = new ResizeObserver( (entries) => {
for (const e of entries) {
if (e.target.siblingSide) {
e.target.siblingSide.style.height = e.borderBoxSize[0].blockSize + "px";
}
}
});
}
onChangedChord() {
this.refreshSidechords()
}
refreshSidechords() {
this.resizeObserver.disconnect();
// Trick to not jump too much
this.sidechords.style.width = this.sidechords.getBoundingClientRect().width + "px";
let newsc = document.createDocumentFragment()
if (!this.getAttribute("blocknb")) {
let rows = this.getElementsByTagName("song-row");
for (let r of rows) {
const ed = document.createElement("song-side-chords")
ed.setRow(r);
newsc.appendChild(ed);
r.siblingSide = ed;
this.resizeObserver.observe(r);
}
}
this.sidechords.replaceChildren(newsc)
this.sidechords.style.removeProperty("width");
}
refoninput(e, verse) {
if (this.btRadios.chorus.checked) {
verse.setAttribute("type", "chorus");
}
if (this.btRadios.verse.checked) {
verse.setAttribute("type", "verse");
}
if (this.btRadios.other.checked) {
verse.setAttribute("type", "other");
}
verse.refresh();
}
refresh() {
this.updateClass()
this.updateVisibility();
this.refreshSidechords();
}
blocklinkoninput(e, verse) {
if (verse.blocklink.checked) {
verse.setAttribute("blocknb", "?");
} else {
const referred = this.getReferred();
verse.removeAttribute("blocknb");
if (referred) {
verse.innerHTML=referred.innerHTML;
}
}
verse.refresh();
}
inputLinkVerse(e, verse) {
let r = document.getElementById(verse.linkSel.value);
if (r) {
verse.setAttribute("blocknb", r.id);
verse.setRadioToType(r.getAttribute('type'));
} else {
verse.setAttribute("blocknb", "?");
verse.setRadioToType('?');
}
this.refoninput(e, verse);
}
updateClass() {
if (this.isChorus()) {
this.main.classList.add("chorus");
this.link.classList.add("chorus");
} else {
this.main.classList.remove("chorus");
this.link.classList.remove("chorus");
}
}
updateVisibility() {
if (this.getAttribute("blocknb")) {
this.main.hidden = true;
this.link.hidden = false;
this.btRadios.chorus.disabled = true;
this.btRadios.verse.disabled = true;
this.btRadios.other.disabled = true;
} else {
this.main.hidden = false;
this.link.hidden = true;
this.btRadios.chorus.disabled = false;
this.btRadios.verse.disabled = false;
this.btRadios.other.disabled = false;
}
}
connectedCallback() {
if (this.getAttribute("blocknb") &&
!isNaN(this.getAttribute("blocknb"))) {
let offset=parseInt(this.getAttribute("blocknb"));
if (offset && !isNaN(offset)) {
let j=0;
let toSet='?';
for (let i=0; i < this.parentNode.childNodes.length; ++i) {
let v=this.parentNode.childNodes[i];
if (v.nodeName==='SONG-VERSE' && !v.getAttribute('blocknb')) {
j++;
}
if (j==offset) {
toSet=v.id;
break;
}
}
this.setAttribute("blocknb", toSet);
}
}
this.observer = new MutationObserver((mutations) => this.refreshPosition(this, mutations));
console.log("Observing:", this.parentNode);
this.refreshPosition(this)
this.observer.observe(this.parentNode, { attributes: true, childList: true, subtree: true });
}
disconnectedCallback() {
this.observer.disconnect();
}
refreshPosition(verse, mutations) {
if (!verse.parentNode) {
console.warn("refresh-position disconnected");
return;
}
verse.blocklink.disabled=!verse.previousSibling;
if (verse.isVerse()) {
let j=0;
for (let i=0; i < verse.parentNode.childNodes.length; ++i) {
let v =verse.parentNode.childNodes[i];
if (v.nodeName!=='SONG-VERSE') {
continue;
}
if (v.isVerse()) {
j=j+1;
}
if (v===this) {
verse.nr.innerText = j+".";
break;
}
}
} else if (verse.isChorus()) {
verse.nr.innerText='Ref:';
} else {
verse.nr.innerText='';
}
while(verse.linkSel.options.length>0) {
verse.linkSel.options.remove(0);
}
verse.linkSel.add(new Option("[ wybierz zwrotkę ]", "?"));
for (let i=0; i < verse.parentNode.childNodes.length; ++i) {
let v = verse.parentNode.childNodes[i];
if (v == this) {
break;
}
if (v.nodeName==='SONG-VERSE'&& !v.getAttribute("blocknb")) {
verse.linkSel.add(new Option(v.shortRep(), v.id, false, v.id===verse.getAttribute("blocknb")));
if (v.id===verse.getAttribute("blocknb")) {
verse.setRadioToType(v.getAttribute('type'));
}
}
}
}
isChorus() {
let r=this.getReferred();
if (r) {
return r.isChorus();
} else {
return this.getAttribute("type") == 'chorus';;
}
}
isVerse() {
let r=this.getReferred();
if (r) {
return r.isVerse();
} else {
return this.getAttribute("type") == 'verse';;
}
}
getReferred() {
if (this.getAttribute("blocknb")) {
return document.getElementById(this.getAttribute("blocknb"));
} else {
return null;
}
}
shortRep() {
if (this.childNodes.length<1) {
return null;
}
let rows = this.getElementsByTagName("song-row");
return rows.length>0? rows[0].innerText : null;
}
static get observedAttributes() {
return ["blocknb", "type"]
}
attributeChangedCallback(attr) {
console.log("Attribute changed...", attr)
if (attr=='type') {
this.refreshAttributes();
}
if (attr=="blocknb") {
this.blocklink.checked = this.getAttribute("blocknb");
}
this.updateClass();
this.updateVisibility();
}
refreshAttributes() {
this.setRadioToType(this.getAttribute("type"));
}
setRadioToType(t) {
this.btRadios.chorus.checked = t=='chorus';
this.btRadios.verse.checked = t=='verse' || t=='';
this.btRadios.other.checked = t=='other';
}
}
export class SongBis extends HTMLElement {
constructor() {
super();
const template = document.createElement('template');
template.innerHTML = `
<link rel="stylesheet" href="verse.css"/>
<div class="bis">
<div class="bis_main"><slot/></div>
<div class="bis_meta"><label id='xl' for="x">x</label><input type="number" id="x" min="1" max="99"/></div>
</div>
`;
const shadow = this.attachShadow({ mode: "closed" });
shadow.appendChild(template.content.cloneNode(true));
this.x=shadow.getElementById("x");
this.x.addEventListener("input", (e) => {this.input(e, this)});
}
input(e, songbis) {
songbis.setAttribute("x", e.target.value);
}
connectedCallback() {
this.attributeChangedCallback();
}
attributeChangedCallback() {
this.x.value = this.getAttribute("x");
}
static get observedAttributes() {
return ["x"]
}
focus() {
this.x.focus();
}
}
export function getChordsFromRow(row) {
let text="";
for (let i=0; i < row.childNodes.length; ++i) {
let n = row.childNodes[i];
if (n.nodeName==='SONG-CH') {
text+=n.getAttribute("a") + " ";
}
}
return text.trim().replaceAll(nbsp, " ")
}
export function setSideChordsForRow(row, ch) {
const chr=getChordsFromRow(row);
if (row.getAttribute('type')=='instr') {
pushRowChords(row, ch)
}
if (chr===ch.trim()) {
console.log("Side and main back in sync")
row.removeAttribute("sidechords");
} else {
console.log(`Side and main not in sync: '${chr}'!='${ch.trim()}'`)
row.setAttribute("sidechords", ch.trim());
}
}
export function getSideChordsForRow(row) {
let sch = row.getAttribute("sidechords");
if (sch) { // Even empty sch - means explicitly no children in the subtree.
return sch.trim();
} else {
return getChordsFromRow(row);
}
}
export function SongVerseBisInit() {
SideChordsInit();
customElements.define("song-verse", SongVerse);
customElements.define("song-bis", SongBis);
}
export function getNextRow(row) {
if (row.nextSibling && row.nextSibling.nodeName=='SONG-ROW') {
return row.nextSibling;
}
let songbody = findAncestor(row, "SONG-BODY");
let rows = songbody.getElementsByTagName("song-row");
for (let i=0; i < rows.length; ++i) {
if (rows[i]==row) {
return i<rows.length-1 ? rows[i+1] : null;
}
}
}
export function getPrevRow(row) {
if (row.previousSibling && row.previousSibling.nodeName=='SONG-ROW') {
return row.previousSibling;
}
let songbody = findAncestor(row, "SONG-BODY");
let rows = songbody.getElementsByTagName("song-row");
for (let i=rows.length-1; i>=0; --i) {
if (rows[i]==row) {
return i>1 ? rows[i-1] : null;
}
}
}