-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesson_2_name_the_note.js
119 lines (100 loc) · 3.09 KB
/
lesson_2_name_the_note.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
/**
* Lesson: Name the note
* Objective: learn notes and their placement
*/
function runNameTheNote() {
disablePiano();
enableButtons();
renderButtons();
showButtons();
hideAllLessonSettings();
showLesson2Settings();
selectRandomOctave();
setRandomKey();
debug && console.log('task key: ', taskKey);
debug && findButton(taskKey.note).classList.add('debug');
findElement(taskKey).classList.add('active');
}
function selectNote(noteName) {
if (!buttonsClickable || !noteName) {
return;
}
disableButtons();
addClass(findElement(taskKey), 'correct');
debug && console.log('selected note: ' + noteName);
if (taskKey.note === noteName || taskKey.sameAs(noteName)) {
setTask(noteName + ' - Correct! :)');
addClass(findButton(noteName), 'correct');
correctAnswer();
} else {
setTask(noteName + ' - Wrong! :(');
const selectedKey = getKey(noteName, selectedOctave);
addClass(findElement(selectedKey), 'wrong');
addClass(findButton(noteName), 'wrong');
}
finishLesson();
setTimeout(() => {
startLesson();
}, TIMEOUT);
}
function renderButtons() {
const container = document.getElementById('buttons');
container.innerHTML = '';
const buttons = randomButtons ? getRandomButtons() : getSortedButtons();
for (let i = 0; i < buttons.length; i++) {
let button = document.createElement('div');
addClass(button, 'button');
button.setAttribute('id', 'button_' + buttons[i]);
button.setAttribute('title', getHint(buttons[i]));
button.addEventListener('click', () => selectNote(buttons[i]));
button.innerHTML = buttons[i];
container.appendChild(button);
}
}
function getHint(note) {
let prefix = '';
if (note.indexOf('#') !== -1) {
prefix = 'Shift + ';
} else if (note.indexOf('b') !== -1) {
prefix = 'Ctrl + ';
}
return 'Keyboard: ' + prefix + note.substr(0, 1);
}
function getRandomButtons() {
return shuffle(getButtons().map(k => k.note));
}
function getSortedButtons() {
return getButtons()
.sort((a, b) => b.order - a.order)
.map(k => k.note);
}
function getButtons() {
let buttons = [];
const octaveKeys = KEYS.filter(n => n.octave === 1);
buttons = buttons.concat(octaveKeys.filter(k => white ? k.white : false));
buttons = buttons.concat(octaveKeys.filter(k => sharp ? k.sharp : false));
buttons = buttons.concat(octaveKeys.filter(k => flat ? k.flat : false));
return buttons;
}
function findButton(noteName) {
return document.getElementById('button_' + noteName);
}
function handleKey(event, code, note, shiftNote, ctrlNote) {
if (event.code === code) {
if (sharp && event.shiftKey) {
selectNote(shiftNote);
} else if (flat && event.ctrlKey) {
selectNote(ctrlNote);
} else {
selectNote(note);
}
}
}
function showLesson2Settings() {
document.getElementById('lesson2-random').style.display = 'inline-block';
document.getElementById('lesson2-random').style.display = 'inline-block';
}
function hideLesson2Settings() {
document.getElementById('lesson2-random').style.display = 'none';
document.getElementById('lesson2-random').style.display = 'none';
}