-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEdoPrompt.ts
More file actions
97 lines (82 loc) · 3.6 KB
/
EdoPrompt.ts
File metadata and controls
97 lines (82 loc) · 3.6 KB
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
// Copyright (c) 2012-2022 John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file.
import { HTML } from "imperative-html/dist/esm/elements-strict";
import { SongDocument } from "./SongDocument";
import { Prompt } from "./Prompt";
import { ChangeEdo } from "./changes";
const {button, div, h2, input} = HTML;
export class EdoPrompt implements Prompt {
private readonly _edo: HTMLInputElement = input({style: "width: 3em; margin-left: 1em;", type: "number", step: "1"});
private readonly _cancelButton: HTMLButtonElement = button({class: "cancelButton"});
private readonly _okayButton: HTMLButtonElement = button({class: "okayButton", style: "width:45%;"}, "Okay");
public readonly container: HTMLDivElement = div({class: "prompt noSelection", style: "width: 250px;"},
h2("EDO"),
div({style: "display: flex; flex-direction: row; align-items: center; height: 2em; justify-content: flex-end;"},
div({style: "display: inline-block; text-align: left;"},
"EDO:",
),
this._edo,
),
div({style: "display: flex; flex-direction: row; align-items: center; height: 2em; justify-content: flex-end;"},
),
div({style: "display: flex; flex-direction: row-reverse; justify-content: space-between;"},
this._okayButton,
),
this._cancelButton,
);
constructor(private _doc: SongDocument) {
this._edo.value = this._doc.song.edo + "";
this._edo.min = "5";
this._edo.max = "53";
this._edo.select();
setTimeout(()=>this._edo.focus());
this._okayButton.addEventListener("click", this._saveChanges);
this._cancelButton.addEventListener("click", this._close);
this._edo.addEventListener("keypress", EdoPrompt._validateKey);
this._edo.addEventListener("blur", EdoPrompt._validateNumber);
this.container.addEventListener("keydown", this._whenKeyPressed);
}
private _close = (): void => {
this._doc.undo();
}
public cleanUp = (): void => {
this._okayButton.removeEventListener("click", this._saveChanges);
this._cancelButton.removeEventListener("click", this._close);
this._edo.removeEventListener("keypress", EdoPrompt._validateKey);
this._edo.removeEventListener("blur", EdoPrompt._validateNumber);
this.container.removeEventListener("keydown", this._whenKeyPressed);
}
private _whenKeyPressed = (event: KeyboardEvent): void => {
if ((<Element> event.target).tagName != "BUTTON" && event.keyCode == 13) { // Enter key
this._saveChanges();
}
}
private static _validateKey(event: KeyboardEvent): boolean {
const charCode = (event.which) ? event.which : event.keyCode;
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57)) {
event.preventDefault();
return true;
}
return false;
}
private static _validateNumber(event: Event): void {
const input: HTMLInputElement = <HTMLInputElement>event.target;
input.value = String(EdoPrompt._validate(input));
}
private static _validate(input: HTMLInputElement): number {
return Math.floor(Math.max(Number(input.min), Math.min(Number(input.max), Number(input.value))));
}
private _saveChanges = (): void => {
this._doc.prompt = null;
this._doc.record(new ChangeEdo(this._doc, EdoPrompt._validate(this._edo)), true);
let numChannels: number = this._doc.song.channels.length;
let numPatterns: number;
for (let i=0; i<numChannels; i++) {
numPatterns = this._doc.song.channels[i].patterns.length;
for (let j=0; j<numPatterns; j++) {
this._doc.song.channels[i].patterns[j].notes = [];
}
}
// Reload but make sure changes get saved or changes will not save
setTimeout(() => { location.reload(); }, 50);
}
}