-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
315 lines (273 loc) · 7.57 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<title>Random Jazz Chords</title>
<meta name="viewport" content="width=device-width">
<meta name="description" content="Random Jazz Chords is a simple chord practice tool. Select a combination of notes and chord types, and generate as many bars of random chords as you want.">
<style>
.clearfix:after {
visibility: hidden;
display: block;
font-size: 0;
content: " ";
clear: both;
height: 0;
}
html, body {
height: 100%;
}
html {
font-family: Sans-Serif;
font-size: 16pt;
}
body {
margin: 0;
background-color: #000;
}
#app {
/* Selfishly optimized for iPad mini width (1024px) */
max-width: 924px;
margin: 0 auto;
padding: 10px;
min-height: calc(100% - 20px);
background-color: #fff;
}
button, label, select, input {
display: inline-block;
vertical-align: middle;
font-size: 14pt;
margin: 10px;
}
select {
/* Enough to show 'N items' on iPad */
min-width: 120px;
}
input {
width: 50px;
}
header {
margin-bottom: 25px;
text-align: center;
}
h1 {
display: inline-block;
font-size: 20pt;
margin: 0;
}
/* Force these to wrap */
h1, #controls {
min-width: 275px;
}
#controls button {
margin: 10px 5px;
}
#options {
background-color: #eee;
margin-top: 15px;
}
#bars {
font-size: 20pt;
}
#bars {
margin-top: 15px;
}
.chord {
float: left;
width: calc(50% - 1px);
margin-bottom: 20px;
/* Prevent double-wide borders */
margin-left: -1px;
/* Force constant line height, vertically center with line-height == height == X */
height: 1.5em;
line-height: 1.5em;
text-align: center;
border: 1px solid #000;
}
/* Prevent <sup> from affecting line height */
sup {
vertical-align: baseline;
position: relative;
top: -0.4em;
}
/* Desktop/tablet layout */
@media (min-width: 650px) {
#app {
padding: 50px;
min-height: calc(100% - 100px);
}
header {
text-align: left;
}
h1, #controls {
min-width: 0;
}
#controls {
float: right;
}
#controls button {
margin: 0;
margin-left: 20px;
}
.chord {
width: calc(33.33% - 1px);
}
}
@media (min-width: 800px) {
.chord {
width: calc(25% - 1px);
}
}
</style>
<script>
// Utils
var randomInt = function (min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
var hideElementById = function (id) {
document.getElementById(id).style.setProperty("display", "none");
}
var showElementById = function (id) {
document.getElementById(id).style.setProperty("display", "");
}
var setHTMLById = function (id, html) {
document.getElementById(id).innerHTML = html;
}
// Useful symbols
var flatChar = '♭';
var sharpChar = '♯';
var delta = 'Δ';
var halfDimChar = '<sup>ø</sup>';
var dimChar = '<sup>o</sup>';
// Input rendering/reading
var renderSelect = function (id, options) {
var content = Object.keys(options).map(function (val) {
return '<option value="' + val + '">' + options[val] + '</option>';
}).join('');
return '<select multiple id="' + id + '">' + content + '</select>';
};
var getSelected = function (id) {
var selected = document.getElementById(id).selectedOptions;
return Array.from(selected).map(function (option) {
return option.value; });
};
// Chord/note data (immutable)
var notes = {
f: 'F',
c: 'C',
g: 'G',
d: 'D',
a: 'A',
e: 'E',
b: 'B',
gflat: 'G' + flatChar,
dflat: 'D' + flatChar,
aflat: 'A' + flatChar,
eflat: 'E' + flatChar,
bflat: 'B' + flatChar
};
var chords = {
dom7: '7',
maj7: delta + '7',
min7: '-7',
halfdim7: halfDimChar + '7',
dim7: dimChar + '7'
};
// Actually generate random chords
var renderChord = function (chordName) {
return '<div class="chord">' + chordName + '</div>';
};
var renderChords = function (allowedNotes, allowedChords, numBars) {
if (allowedNotes.length && allowedChords.length) {
var text = '';
for (i = 0; i < numBars; i++) {
note = allowedNotes[randomInt(0, allowedNotes.length)];
chord = allowedChords[randomInt(0, allowedChords.length)];
text += renderChord(notes[note] + chords[chord]);
}
return text;
} else {
return 'Please select at least one root note and chord type.';
}
};
// Analytics
var initAnalytics = function () {
if (window.location.hostname === 'randomjazzchords.com') {
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-85512574-1', 'auto');
ga('send', 'pageview');
// See how common old browsers are
if (!Array.from) {
sendEvent('Compatibility', 'no-array-from');
}
}
};
var sendEvent = function (category, action) {
if (window.ga) {
ga('send', {
hitType: 'event',
eventCategory: category,
eventAction: action
});
}
};
// Event handlers
var hideOptions = function () {
sendEvent('Options', 'hide');
hideElementById('options');
hideElementById('hideoptions');
showElementById('showoptions');
};
var showOptions = function () {
sendEvent('Options', 'show');
showElementById('options');
showElementById('hideoptions');
hideElementById('showoptions');
};
var _refresh = function () {
setHTMLById('bars', renderChords(getSelected('notes'), getSelected('chords'),
parseInt(document.getElementById('numbars').value)));
}
var refresh = function () {
sendEvent('App', 'refresh');
_refresh();
};
var initApp = function () {
setHTMLById('notes', renderSelect('notes', notes));
setHTMLById('chords', renderSelect('chords', chords));
var noteOptions = Array.from(document.getElementById('notes').children);
var chordOptions = Array.from(document.getElementById('chords').children);
noteOptions.concat(chordOptions).forEach(function (option) { option.selected = true; });
document.getElementById('numbars').value = 32;
_refresh();
};
var init = function () {
initAnalytics();
initApp();
}
</script>
</head>
<body>
<div id="app">
<header class="clearfix">
<h1>Random Jazz Chords</h1>
<div id="controls">
<button onclick="refresh()">Refresh</button>
<button id="showoptions" onclick="showOptions()">Show Options</button>
<button id="hideoptions" onclick="hideOptions()" style="display: none">Hide Options</button>
</div>
</header>
<div id="options" style="display: none">
<label>Root notes:</label> <select id="notes" multiple></select>
<label>Chord types:</label> <select id="chords" multiple></select>
<label>Number of bars:</label> <input id="numbars" type="text"></input>
</div>
<div id="bars" class="clearfix"></div>
</div>
<script>init()</script>
</body>
</html>