@musodojo/note-collection-selector 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,122 @@
1
+ Creative Commons Legal Code
2
+
3
+ CC0 1.0 Universal
4
+
5
+ CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
6
+ LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
7
+ ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
8
+ INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
9
+ REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
10
+ PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
11
+ THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
12
+ HEREUNDER.
13
+
14
+ Statement of Purpose
15
+
16
+ The laws of most jurisdictions throughout the world automatically confer
17
+ exclusive Copyright and Related Rights (defined below) upon the creator
18
+ and subsequent owner(s) (each and all, an "owner") of an original work of
19
+ authorship and/or a database (each, a "Work").
20
+
21
+ Certain owners wish to permanently relinquish those rights to a Work for
22
+ the purpose of contributing to a commons of creative, cultural and
23
+ scientific works ("Commons") that the public can reliably and without fear
24
+ of later claims of infringement build upon, modify, incorporate in other
25
+ works, reuse and redistribute as freely as possible in any form whatsoever
26
+ and for any purposes, including without limitation commercial purposes.
27
+ These owners may contribute to the Commons to promote the ideal of a free
28
+ culture and the further production of creative, cultural and scientific
29
+ works, or to gain reputation or greater distribution for their Work in
30
+ part through the use and efforts of others.
31
+
32
+ For these and/or other purposes and motivations, and without any
33
+ expectation of additional consideration or compensation, the person
34
+ associating CC0 with a Work (the "Affirmer"), to the extent that he or she
35
+ is an owner of Copyright and Related Rights in the Work, voluntarily
36
+ elects to apply CC0 to the Work and publicly distribute the Work under its
37
+ terms, with knowledge of his or her Copyright and Related Rights in the
38
+ Work and the meaning and intended legal effect of CC0 on those rights.
39
+
40
+ 1. Copyright and Related Rights. A Work made available under CC0 may be
41
+ protected by copyright and related or neighboring rights ("Copyright and
42
+ Related Rights"). Copyright and Related Rights include, but are not
43
+ limited to, the following:
44
+
45
+ i. the right to reproduce, adapt, distribute, perform, display,
46
+ communicate, and translate a Work;
47
+ ii. moral rights retained by the original author(s) and/or performer(s);
48
+ iii. publicity and privacy rights pertaining to a person's image or
49
+ likeness depicted in a Work;
50
+ iv. rights protecting against unfair competition in regards to a Work,
51
+ subject to the limitations in paragraph 4(a), below;
52
+ v. rights protecting the extraction, dissemination, use and reuse of data
53
+ in a Work;
54
+ vi. database rights (such as those arising under Directive 96/9/EC of the
55
+ European Parliament and of the Council of 11 March 1996 on the legal
56
+ protection of databases, and under any national implementation
57
+ thereof, including any amended or successor version of such
58
+ directive); and
59
+ vii. other similar, equivalent or corresponding rights throughout the
60
+ world based on applicable law or treaty, and any national
61
+ implementations thereof.
62
+
63
+ 2. Waiver. To the greatest extent permitted by, but not in contravention
64
+ of, applicable law, Affirmer hereby overtly, fully, permanently,
65
+ irrevocably and unconditionally waives, abandons, and surrenders all of
66
+ Affirmer's Copyright and Related Rights and associated claims and causes
67
+ of action, whether now known or unknown (including existing as well as
68
+ future claims and causes of action), in the Work (i) in all territories
69
+ worldwide, (ii) for the maximum duration provided by applicable law or
70
+ treaty (including future time extensions), (iii) in any current or future
71
+ medium and for any number of copies, and (iv) for any purpose whatsoever,
72
+ including without limitation commercial, advertising or promotional
73
+ purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
74
+ member of the public at large and to the detriment of Affirmer's heirs and
75
+ successors, fully intending that such Waiver shall not be subject to
76
+ revocation, rescission, cancellation, termination, or any other legal or
77
+ equitable action to disrupt the quiet enjoyment of the Work by the public
78
+ as contemplated by Affirmer's express Statement of Purpose.
79
+
80
+ 3. Public License Fallback. Should any part of the Waiver for any reason
81
+ be judged legally invalid or ineffective under applicable law, then the
82
+ Waiver shall be preserved to the maximum extent permitted taking into
83
+ account Affirmer's express Statement of Purpose. In addition, to the
84
+ extent the Waiver is so judged Affirmer hereby grants to each affected
85
+ person a royalty-free, non transferable, non sublicensable, non exclusive,
86
+ irrevocable and unconditional license to exercise Affirmer's Copyright and
87
+ Related Rights in the Work (i) in all territories worldwide, (ii) for the
88
+ maximum duration provided by applicable law or treaty (including future
89
+ time extensions), (iii) in any current or future medium and for any number
90
+ of copies, and (iv) for any purpose whatsoever, including without
91
+ limitation commercial, advertising or promotional purposes (the
92
+ "License"). The License shall be deemed effective as of the date CC0 was
93
+ applied by Affirmer to the Work. Should any part of the License for any
94
+ reason be judged legally invalid or ineffective under applicable law, such
95
+ partial invalidity or ineffectiveness shall not invalidate the remainder
96
+ of the License, and in such case Affirmer hereby affirms that he or she
97
+ will not (i) exercise any of his or her remaining Copyright and Related
98
+ Rights in the Work or (ii) assert any associated claims and causes of
99
+ action with respect to the Work, in either case contrary to Affirmer's
100
+ express Statement of Purpose.
101
+
102
+ 4. Limitations and Disclaimers.
103
+
104
+ a. No trademark or patent rights held by Affirmer are waived, abandoned,
105
+ surrendered, licensed or otherwise affected by this document.
106
+ b. Affirmer offers the Work as-is and makes no representations or
107
+ warranties of any kind concerning the Work, express, implied,
108
+ statutory or otherwise, including without limitation warranties of
109
+ title, merchantability, fitness for a particular purpose, non
110
+ infringement, or the absence of latent or other defects, accuracy, or
111
+ the present or absence of errors, whether or not discoverable, all to
112
+ the greatest extent permissible under applicable law.
113
+ c. Affirmer disclaims responsibility for clearing rights of other persons
114
+ that may apply to the Work or any use thereof, including without
115
+ limitation any person's Copyright and Related Rights in the Work.
116
+ Further, Affirmer disclaims responsibility for obtaining any necessary
117
+ consents, permissions or other rights required for any use of the
118
+ Work.
119
+ d. Affirmer understands and acknowledges that Creative Commons is not a
120
+ party to this document and has no duty or obligation with respect to
121
+ this CC0 or use of the Work.
122
+
package/README.md ADDED
@@ -0,0 +1,6 @@
1
+ # Note Sequence Selector Web Component
2
+
3
+ A versatile web component for interactively selecting and exploring musical note
4
+ collections, such as diatonic modes, dominant variants, and more. This component
5
+ provides a user-friendly interface for musicians, educators, and developers
6
+ working with music theory concepts.
@@ -0,0 +1,48 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <title>Note collection Selector Example</title>
7
+ <style>
8
+ :root {
9
+ color-scheme: dark light;
10
+ font-family:
11
+ system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
12
+ Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue",
13
+ sans-serif;
14
+ font-size: 1.5em;
15
+ }
16
+
17
+ note-collection-selector::part(main-button) {
18
+ border: 0.2em solid currentColor;
19
+ border-radius: 0.6em;
20
+ padding: 0.3em 1em;
21
+ }
22
+ </style>
23
+ </head>
24
+ <body>
25
+ <h2>Select a Note Collection</h2>
26
+ <note-collection-selector></note-collection-selector>
27
+ <br />
28
+ <br />
29
+ <button id="random-button">Random Collection</button>
30
+
31
+ <script type="module" src="../dist/bundle.js"></script>
32
+ <script type="module">
33
+ document.body.addEventListener(
34
+ "note-collection-selected",
35
+ (event) => {
36
+ console.log("note collection selected:", event.detail);
37
+ },
38
+ );
39
+ const noteCollectionSelector = document.querySelector(
40
+ "note-collection-selector",
41
+ );
42
+ const randomButton = document.getElementById("random-button");
43
+ randomButton.addEventListener("click", () => {
44
+ noteCollectionSelector.setRandomNoteCollection();
45
+ });
46
+ </script>
47
+ </body>
48
+ </html>
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "@musodojo/note-collection-selector",
3
+ "version": "5.0.0",
4
+ "description": "A custom HTML element for selecting a note collection, and dispatching events with the details.",
5
+ "keywords": [
6
+ "music",
7
+ "theory"
8
+ ],
9
+ "license": "CC0-1.0",
10
+ "author": "Conor Dowdall",
11
+ "type": "module",
12
+ "main": "src/mod.ts",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/conor-dowdall/note-collection-selector.git"
16
+ },
17
+ "bugs": {
18
+ "url": "https://github.com/conor-dowdall/note-collection-selector/issues"
19
+ },
20
+ "dependencies": {
21
+ "@musodojo/music-theory-data": "^20.2.0"
22
+ },
23
+ "devDependencies": {
24
+ "esbuild": "^0.27.0",
25
+ "typescript": "^5.9.3"
26
+ },
27
+ "scripts": {
28
+ "bundle": "esbuild src/mod.ts --bundle --minify --platform=browser --outfile=dist/bundle.js"
29
+ }
30
+ }
package/src/mod.ts ADDED
@@ -0,0 +1,582 @@
1
+ import {
2
+ groupedNoteCollections,
3
+ type NoteCollection,
4
+ type NoteCollectionGroupKey,
5
+ noteCollectionGroupsMetadata,
6
+ type NoteCollectionKey,
7
+ noteCollections,
8
+ } from "@musodojo/music-theory-data";
9
+
10
+ const noteCollectionSelectorTemplate = document.createElement("template");
11
+ noteCollectionSelectorTemplate.innerHTML = /* HTML */ `
12
+ <style>
13
+ :host {
14
+ --_main-icon-size: var(--main-icon-size, 2.5ch);
15
+ --_close-dialog-icon-size: var(--close-dialog-icon-size, 2ch);
16
+
17
+ --_dialog-backdrop-background: var(
18
+ --dialog-backdrop-background,
19
+ light-dark(rgb(255 255 255 / 50%), rgb(0 0 0 / 50%))
20
+ );
21
+
22
+ --_default-spacing: var(--default-spacing, 0.5em);
23
+
24
+ display: inline-block;
25
+ font-size: inherit;
26
+ }
27
+
28
+ button {
29
+ font: inherit;
30
+ margin: 0;
31
+ padding: 0;
32
+ cursor: pointer;
33
+ background: none;
34
+ border: none;
35
+ }
36
+
37
+ svg {
38
+ fill: currentColor;
39
+ }
40
+
41
+ [part="main-button"] {
42
+ display: grid;
43
+ place-items: center;
44
+
45
+ min-width: var(--_main-icon-size);
46
+
47
+ > #main-button-text-span {
48
+ grid-area: 1 / 1;
49
+ }
50
+
51
+ > slot {
52
+ height: var(--_main-icon-size);
53
+ }
54
+
55
+ ::slotted(svg),
56
+ ::slotted(img),
57
+ > slot > svg {
58
+ grid-area: 1 / 1;
59
+ width: var(--_main-icon-size);
60
+ height: var(--_main-icon-size);
61
+ }
62
+ }
63
+
64
+ [part="dialog"] {
65
+ padding: var(--_default-spacing);
66
+
67
+ > [part="close-dialog-button"] {
68
+ display: grid;
69
+ place-items: center;
70
+ padding: var(--_default-spacing);
71
+ border: none;
72
+ margin-inline-start: auto;
73
+ margin-block-end: var(--_default-spacing);
74
+
75
+ /* Size icons, but let text content flow naturally */
76
+ ::slotted(svg),
77
+ ::slotted(img),
78
+ > slot[name="close-dialog-icon"] > svg {
79
+ width: var(--_close-dialog-icon-size);
80
+ height: var(--_close-dialog-icon-size);
81
+ /* Ensure icons are on the same grid cell if multiple are slotted */
82
+ grid-area: 1 / 1;
83
+ }
84
+ }
85
+
86
+ > #toggle-more-info-label {
87
+ padding: 0.5em;
88
+ border: 0.1em solid currentColor;
89
+ border-radius: 0.5em;
90
+ cursor: pointer;
91
+ }
92
+
93
+ > #note-collections-div {
94
+ display: flex;
95
+ flex-direction: column;
96
+ gap: 1em;
97
+ margin-block-start: 2em;
98
+
99
+ > #note-collection-group-wrapper {
100
+ > #note-collection-group-div {
101
+ margin-block: 0.5em;
102
+ display: flex;
103
+ flex-wrap: wrap;
104
+ gap: 1em;
105
+ }
106
+
107
+ > h3 {
108
+ margin: 0em;
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ [part="dialog"]::backdrop {
115
+ background: var(--_dialog-backdrop-background);
116
+ }
117
+
118
+ .note-collection-option {
119
+ padding: 0.5em;
120
+ min-width: 4ch;
121
+ max-width: 80ch;
122
+ border: 0.1em solid currentColor;
123
+ border-radius: 0.5em;
124
+ cursor: pointer;
125
+ text-align: left;
126
+ text-wrap: pretty; /* Enable smart text wrapping if supported */
127
+
128
+ > h4 {
129
+ margin-block: 0.2em;
130
+ }
131
+
132
+ /* When the "more info" div is hidden, center the text */
133
+ &:has(> .more-info-div.hidden) {
134
+ text-align: center;
135
+ }
136
+
137
+ > .more-info-div {
138
+ display: flex;
139
+ flex-direction: column;
140
+ gap: 0.5em;
141
+ }
142
+
143
+ > .more-info-div.hidden {
144
+ display: none;
145
+ }
146
+ }
147
+
148
+ .hidden {
149
+ display: none;
150
+ }
151
+
152
+ .visually-hidden {
153
+ clip: rect(0 0 0 0);
154
+ clip-path: inset(50%);
155
+ height: 1px;
156
+ overflow: hidden;
157
+ position: absolute;
158
+ white-space: nowrap;
159
+ width: 1px;
160
+ }
161
+ </style>
162
+
163
+ <button part="main-button">
164
+ <span id="main-button-text-span" style="display: none;"></span>
165
+ <slot>
166
+ <!-- Default icon when no note is selected. Can be overridden by the user. This
167
+ SVG is part of the project and is licensed under CC0 1.0 Universal. -->
168
+ <svg viewBox="0 -960 960 960">
169
+ <path
170
+ d="M 705 -793.04688 A 158.9 237.09 59.29 0 0 564.0625 -748.82812 A 158.9 237.09 59.29 0 0 423.51562 -551.09375 A 158.9 237.09 59.29 0 0 423.51562 -546.71875 L 423.51562 -530.3125 A 158.9 237.09 59.29 0 0 239.0625 -487.96875 A 158.9 237.09 59.29 0 0 113.125 -230.46875 A 158.9 237.09 59.29 0 0 255.78125 -167.73438 A 158.9 237.09 59.29 0 0 396.71875 -211.95312 A 158.9 237.09 59.29 0 0 537.26562 -409.6875 A 158.9 237.09 59.29 0 0 537.26562 -414.0625 L 537.26562 -430.46875 A 158.9 237.09 59.29 0 0 721.71875 -472.8125 A 158.9 237.09 59.29 0 0 847.65625 -730.3125 A 158.9 237.09 59.29 0 0 705 -793.04688 z M 642.89062 -610.78125 L 476.32812 -513.51562 A 158.9 237.09 59.29 0 0 476.25 -513.59375 L 642.89062 -610.78125 z M 484.45312 -447.26562 A 158.9 237.09 59.29 0 0 484.53125 -447.1875 L 317.89062 -350 L 484.45312 -447.26562 z"
171
+ />
172
+ </svg>
173
+ </slot>
174
+ </button>
175
+
176
+ <dialog part="dialog" aria-labelledby="dialog-heading">
177
+ <button part="close-dialog-button" aria-label="Close Dialog">
178
+ <slot name="close-dialog-icon">
179
+ <!-- Default icon when no note is selected. Can be overridden by the user.
180
+ This SVG is part of the project and is licensed under CC0 1.0 Universal. -->
181
+ <svg viewBox="0 -960 960 960">
182
+ <path
183
+ transform="rotate(-45)"
184
+ d="m638.82-400h80v800h-80zm-360 360h800v80h-800z"
185
+ />
186
+ </svg>
187
+ </slot>
188
+ </button>
189
+
190
+ <h2 id="dialog-heading" class="visually-hidden">
191
+ Select a Note Collection
192
+ </h2>
193
+
194
+ <label id="toggle-more-info-label">
195
+ <input type="checkbox" id="toggle-more-info-checkbox" />
196
+ more info
197
+ </label>
198
+
199
+ <div id="note-collections-div">
200
+ <!-- the buttons in here are dynamically generated
201
+ each with an attribute of part="note-collection-button" -->
202
+ </div>
203
+ </dialog>
204
+ `;
205
+
206
+ export interface NoteCollectionSelectedEventDetail {
207
+ noteCollectionKey: NoteCollectionKey | null;
208
+ noteCollection: NoteCollection | null;
209
+ }
210
+
211
+ export class NoteCollectionSelector extends HTMLElement {
212
+ #shadowRoot: ShadowRoot;
213
+
214
+ // cache these critical elements in the constructor with #cacheDomElements()
215
+ // and throw an error if they are not found
216
+ #mainButton!: HTMLButtonElement;
217
+ #mainButtonTextSpan!: HTMLSpanElement;
218
+ #mainButtonSlot!: HTMLSlotElement;
219
+ #dialog!: HTMLDialogElement;
220
+ #closeDialogButton!: HTMLButtonElement;
221
+ #toggleMoreInfoCheckbox!: HTMLInputElement;
222
+ #noteCollectionsDiv!: HTMLDivElement;
223
+
224
+ #abortController: AbortController | null = null;
225
+ #selectedNoteCollectionKey: NoteCollectionKey | null = null;
226
+ #selectedNoteCollection: NoteCollection | null = null;
227
+
228
+ static get observedAttributes(): string[] {
229
+ return ["selected-note-collection-key"];
230
+ }
231
+
232
+ constructor() {
233
+ super();
234
+ this.#shadowRoot = this.attachShadow({ mode: "open" });
235
+ this.#shadowRoot.appendChild(
236
+ noteCollectionSelectorTemplate.content.cloneNode(true),
237
+ );
238
+ this.#cacheDomElements();
239
+ }
240
+
241
+ connectedCallback() {
242
+ this.#populateNoteCollectionsDiv();
243
+ this.#addEventListeners();
244
+ this.#updateMainButton();
245
+ this.#syncSelectedNoteCollectionKeyAttribute();
246
+ }
247
+
248
+ disconnectedCallback() {
249
+ this.#abortController?.abort();
250
+ }
251
+
252
+ attributeChangedCallback(
253
+ name: string,
254
+ oldValue: string | null,
255
+ newValue: string | null,
256
+ ) {
257
+ // Only proceed if the attribute's value has actually changed
258
+ if (oldValue === newValue) return;
259
+ if (name === "selected-note-collection-key") {
260
+ // Update the internal state with the new key
261
+ this.selectedNoteCollectionKey = newValue as NoteCollectionKey | null;
262
+ // Dispatch the selection event to notify consumers of the change
263
+ this.#dispatchNoteCollectionSelectedEvent();
264
+ }
265
+ }
266
+
267
+ #cacheDomElements() {
268
+ const mainButton = this.#shadowRoot.querySelector<HTMLButtonElement>(
269
+ '[part="main-button"]',
270
+ );
271
+
272
+ const mainButtonTextSpan = this.#shadowRoot.querySelector<HTMLSpanElement>(
273
+ "#main-button-text-span",
274
+ );
275
+
276
+ const mainButtonSlot = mainButton?.querySelector<HTMLSlotElement>("slot");
277
+
278
+ const dialog = this.#shadowRoot.querySelector<HTMLDialogElement>(
279
+ '[part="dialog"]',
280
+ );
281
+
282
+ const closeDialogButton = this.#shadowRoot.querySelector<HTMLButtonElement>(
283
+ '[part="close-dialog-button"]',
284
+ );
285
+
286
+ const toggleMoreInfoCheckbox = this.#shadowRoot.querySelector<
287
+ HTMLInputElement
288
+ >(
289
+ "#toggle-more-info-checkbox",
290
+ );
291
+
292
+ const noteCollectionsDiv = this.#shadowRoot.querySelector<HTMLDivElement>(
293
+ "#note-collections-div",
294
+ );
295
+
296
+ if (
297
+ !mainButton ||
298
+ !mainButtonTextSpan ||
299
+ !mainButtonSlot ||
300
+ !dialog ||
301
+ !closeDialogButton ||
302
+ !noteCollectionsDiv ||
303
+ !toggleMoreInfoCheckbox
304
+ ) {
305
+ throw new Error(
306
+ "NoteCollectionSelector: Critical elements not found in shadow DOM.",
307
+ );
308
+ }
309
+
310
+ this.#mainButton = mainButton;
311
+ this.#mainButtonTextSpan = mainButtonTextSpan;
312
+ this.#mainButtonSlot = mainButtonSlot;
313
+ this.#dialog = dialog;
314
+ this.#closeDialogButton = closeDialogButton;
315
+ this.#toggleMoreInfoCheckbox = toggleMoreInfoCheckbox;
316
+ this.#noteCollectionsDiv = noteCollectionsDiv;
317
+ }
318
+
319
+ #addEventListeners() {
320
+ // abort any previous controllers before creating a new one
321
+ this.#abortController?.abort();
322
+ this.#abortController = new AbortController();
323
+ const { signal } = this.#abortController;
324
+
325
+ this.#mainButton.addEventListener(
326
+ "click",
327
+ () => {
328
+ this.#dialog.showModal();
329
+ },
330
+ { signal },
331
+ );
332
+
333
+ this.#closeDialogButton.addEventListener(
334
+ "click",
335
+ () => {
336
+ this.#dialog.close();
337
+ },
338
+ { signal },
339
+ );
340
+
341
+ this.#toggleMoreInfoCheckbox.addEventListener(
342
+ "change",
343
+ () => {
344
+ this.#updateMoreInfoVisibility();
345
+ },
346
+ { signal },
347
+ );
348
+ }
349
+
350
+ #populateNoteCollectionsDiv() {
351
+ const frag = document.createDocumentFragment();
352
+
353
+ // for each note collection group, add a wrapper div, which contains
354
+ // a heading and a note collection group div
355
+ Object.entries(noteCollectionGroupsMetadata).forEach(
356
+ ([groupKey, groupMetadata]) => {
357
+ // wrapper for heading and note-collection
358
+ const groupWrapper = document.createElement("div");
359
+ groupWrapper.id = "note-collection-group-wrapper";
360
+ groupWrapper.innerHTML = /* HTML */ `<h3>
361
+ ${groupMetadata.displayName}
362
+ </h3>`;
363
+
364
+ // more info on the group (can hide)
365
+ const groupMoreInfoDiv = document.createElement("div");
366
+ groupMoreInfoDiv.classList.add("more-info-div", "hidden");
367
+ groupMoreInfoDiv.textContent = `${groupMetadata.description}`;
368
+ groupWrapper.appendChild(groupMoreInfoDiv);
369
+
370
+ // div to group the current group of note collections
371
+ const noteCollectionGroupDiv = document.createElement("div");
372
+ noteCollectionGroupDiv.id = "note-collection-group-div";
373
+ groupWrapper.appendChild(noteCollectionGroupDiv);
374
+
375
+ const currentNoteCollectionGroup =
376
+ groupedNoteCollections[groupKey as NoteCollectionGroupKey];
377
+
378
+ Object.entries(currentNoteCollectionGroup).forEach(
379
+ ([key, collection]) => {
380
+ const noteCollectionDiv = document.createElement("div");
381
+ noteCollectionDiv.classList.add("note-collection-option");
382
+ noteCollectionDiv.innerHTML = /* HTML */ `<h4>
383
+ ${collection.primaryName}
384
+ </h4>`;
385
+
386
+ const collectionMoreInfoDiv = document.createElement("div");
387
+ collectionMoreInfoDiv.classList.add("more-info-div", "hidden");
388
+ collectionMoreInfoDiv.innerHTML = this.#renderMoreInfo(collection);
389
+ noteCollectionDiv.appendChild(collectionMoreInfoDiv);
390
+
391
+ noteCollectionDiv.addEventListener("click", () => {
392
+ this.#selectedNoteCollectionKey = key as NoteCollectionKey;
393
+ this.#selectedNoteCollection = collection;
394
+ this.#updateMainButton();
395
+ this.#syncSelectedNoteCollectionKeyAttribute();
396
+ this.#dialog.close();
397
+ });
398
+
399
+ noteCollectionGroupDiv.appendChild(noteCollectionDiv);
400
+ },
401
+ );
402
+
403
+ frag.appendChild(groupWrapper);
404
+ },
405
+ );
406
+
407
+ this.#noteCollectionsDiv.replaceChildren(frag);
408
+ }
409
+
410
+ /**
411
+ * Renders the detailed "more info" content for a given note collection.
412
+ * @private
413
+ * @param {NoteCollection} noteCollection - The note collection data object.
414
+ * @returns {string} An HTML string containing the detailed information.
415
+ */
416
+ #renderMoreInfo(noteCollection: NoteCollection): string {
417
+ // TODO: replace this with updated code
418
+ // <div>${noteCollection.exampleNotes.join(", ")}</div>
419
+ return /* HTML */ `
420
+ <div>${noteCollection.names.join(", ")}</div>
421
+ <div>${noteCollection.intervals.join(", ")}</div>
422
+ <div>${noteCollection.type.join(", ")}</div>
423
+ <div>${noteCollection.characteristics.join(", ")}</div>
424
+ <div>${noteCollection.patternShort.join("-")}</div>
425
+ <div>${noteCollection.pattern.join(", ")}</div>
426
+ `;
427
+ }
428
+
429
+ /**
430
+ * Toggles the visibility of all "more info" sections within the element
431
+ * based on the state of the `toggleMoreInfoCheckbox`.
432
+ * @private
433
+ */
434
+ #updateMoreInfoVisibility() {
435
+ const moreInfoElements = this.#shadowRoot?.querySelectorAll(
436
+ ".more-info-div",
437
+ ) as NodeListOf<HTMLDivElement>;
438
+ moreInfoElements.forEach((el) => {
439
+ el.classList.toggle("hidden", !this.#toggleMoreInfoCheckbox!.checked);
440
+ });
441
+ }
442
+
443
+ /**
444
+ * Updates the text content of the main note selector button to reflect
445
+ * the `primaryName` of the currently selected note collection, or a default text.
446
+ * @private
447
+ */
448
+ #updateMainButton() {
449
+ if (
450
+ this.#selectedNoteCollectionKey !== null &&
451
+ this.#selectedNoteCollection !== null
452
+ ) {
453
+ // State when a note is selected
454
+ this.#mainButtonTextSpan.textContent =
455
+ this.#selectedNoteCollection.primaryName;
456
+ this.#mainButtonTextSpan.style.display = "initial";
457
+ this.#mainButtonSlot.style.display = "none";
458
+ this.#mainButton.ariaLabel =
459
+ `${this.#selectedNoteCollection.primaryName} selected`;
460
+ } else {
461
+ // Default state when no note is selected
462
+ this.#mainButtonTextSpan.style.display = "none";
463
+ this.#mainButtonSlot.style.display = "initial";
464
+ this.#mainButton.ariaLabel = "Select Note Collection";
465
+ }
466
+ }
467
+
468
+ /**
469
+ * Synchronizes the `selected-note-collection-key` attribute on the host element
470
+ * with the component's internal state.
471
+ * @private
472
+ */
473
+ #syncSelectedNoteCollectionKeyAttribute() {
474
+ if (this.#selectedNoteCollectionKey) {
475
+ this.setAttribute(
476
+ "selected-note-collection-key",
477
+ this.#selectedNoteCollectionKey,
478
+ );
479
+ } else {
480
+ this.removeAttribute("selected-note-collection-key");
481
+ }
482
+ }
483
+
484
+ /**
485
+ * Dispatches a custom event named 'note-collection-selected' when a note collection is chosen.
486
+ * The event bubbles and composes, carrying the key and the full data object of the selected note collection.
487
+ * @private
488
+ * @fires NoteSequenceSelectedEvent
489
+ */
490
+ #dispatchNoteCollectionSelectedEvent() {
491
+ this.dispatchEvent(
492
+ new CustomEvent<NoteCollectionSelectedEventDetail>(
493
+ "note-collection-selected",
494
+ {
495
+ detail: {
496
+ noteCollectionKey: this.#selectedNoteCollectionKey,
497
+ noteCollection: this.#selectedNoteCollection,
498
+ },
499
+ bubbles: true,
500
+ composed: true, // Allows the event to cross the Shadow DOM boundary
501
+ },
502
+ ),
503
+ );
504
+ }
505
+
506
+ /**
507
+ * Selects a random note collection from all available and updates
508
+ * the component's state and display.
509
+ * This method programmatically sets the `selectedNoteCollectionKey` property.
510
+ * Ensures the newly selected note is different from the current one.
511
+ * @public
512
+ */
513
+ setRandomNoteCollection() {
514
+ const noteCollectionKeys = Object.keys(
515
+ noteCollections,
516
+ ) as NoteCollectionKey[];
517
+
518
+ let randomNoteCollectionKey: NoteCollectionKey;
519
+
520
+ // Keep selecting at random until it's different from the current one.
521
+ do {
522
+ const randomIndex = Math.floor(Math.random() * noteCollectionKeys.length);
523
+ randomNoteCollectionKey = noteCollectionKeys[randomIndex];
524
+ } while (randomNoteCollectionKey === this.selectedNoteCollectionKey);
525
+
526
+ this.selectedNoteCollectionKey = randomNoteCollectionKey;
527
+ }
528
+
529
+ /**
530
+ * Gets the unique key of the currently selected note collection.
531
+ * @prop {NoteCollectionKey | null} selectedNoteCollectionKey
532
+ * @returns {NoteCollectionKey | null} The note collection key (e.g., "ionian") or `null` if no collection is selected.
533
+ */
534
+ get selectedNoteCollectionKey(): NoteCollectionKey | null {
535
+ return this.#selectedNoteCollectionKey;
536
+ }
537
+
538
+ /**
539
+ * Sets the currently selected note collection by its unique key.
540
+ * This will update the component's display and internal state. If a valid key
541
+ * is provided, the corresponding `NoteCollection` object will be looked up
542
+ * and stored internally. Setting to `null` clears the selection.
543
+ * @param {NoteCollectionKey | null} newNoteCollectionKey - The unique key of the note collection to select.
544
+ * @prop {NoteCollectionKey | null} selectedNoteCollectionKey
545
+ */
546
+ set selectedNoteCollectionKey(
547
+ newNoteCollectionKey: NoteCollectionKey | null,
548
+ ) {
549
+ // Look up the full note collection object based on the key,
550
+ // set values to null if key is null or invalid
551
+ if (
552
+ newNoteCollectionKey !== null &&
553
+ noteCollections[newNoteCollectionKey] !== undefined
554
+ ) {
555
+ this.#selectedNoteCollectionKey = newNoteCollectionKey;
556
+ this.#selectedNoteCollection = noteCollections[newNoteCollectionKey];
557
+ } else {
558
+ this.#selectedNoteCollectionKey = null;
559
+ this.#selectedNoteCollection = null;
560
+ }
561
+ this.#updateMainButton();
562
+ this.#syncSelectedNoteCollectionKeyAttribute();
563
+ // No need to dispatch event here, as attributeChangedCallback will do it
564
+ }
565
+
566
+ /**
567
+ * Gets the full data object for the currently selected note collection.
568
+ * This property is read-only and is derived from `selectedNoteCollectionKey`.
569
+ * @prop {NoteCollection | null} selectedNoteCollection
570
+ * @returns {NoteCollection | null} The full note collection object or `null` if no collection is selected.
571
+ * @readonly
572
+ */
573
+ get selectedNoteCollection(): NoteCollection | null {
574
+ return this.#selectedNoteCollection;
575
+ }
576
+ }
577
+
578
+ /**
579
+ * Defines the custom element 'note-collection-selector' in the browser's CustomElementRegistry.
580
+ * This makes the element available for use in HTML documents.
581
+ */
582
+ customElements.define("note-collection-selector", NoteCollectionSelector);