-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmain.js
More file actions
310 lines (255 loc) · 11 KB
/
main.js
File metadata and controls
310 lines (255 loc) · 11 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
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
/* -------------------------------------------------------------------------- */
/* main.js */
/* -------------------------------------------------------------------------- */
/* Author: Archie Adams */
/* -------------------------------------------------------------------------- */
var activeColour = "";
/* --------------------------- Handle keypresses. --------------------------- */
$(document).on("click", ".key", function () {
var key = $(this);
if (activeColour == "") {
// If a colour button is not selected add new text.
var currentText = $(key).find(".userText").text();
var newKeyText = prompt("Please enter new text for the key.", currentText);
if (newKeyText != null) {
$(key).find(".userText").text(newKeyText);
}
} else {
// If a colour button is selected change key colour.
// Remove akk classes containing substring 'key--colour'
$(key).removeClass(function (index, css) {
return (css.match(/(^|\s)key--colour\S+/g) || []).join(' ');
});
// Add the activeColour.
if (activeColour != "NONE") {
$(key).addClass(activeColour);
}
}
// Set key to be pressed for 200ms.
key.addClass("key--pressed");
setTimeout(() => { key.removeClass("key--pressed"); }, 200);
});
/* -------------------------------------------------------------------------- */
/* ---------------------------- Colour selection ---------------------------- */
$(document).on("click", ".colourkey", function () {
var dataKey = $(this).attr('data-key');
switch (dataKey) {
case "ColourCoral":
activeColour = "key--colour--coral";
break;
case "ColourBlue":
activeColour = "key--colour--blue";
break;
case "ColourNavy":
activeColour = "key--colour--navy";
break;
case "ColourGreen":
activeColour = "key--colour--green";
break;
case "ColourOrange":
activeColour = "key--colour--orange";
break;
case "ColourNone":
activeColour = "NONE";
break;
}
// Creates a toggle effect for each colour button.
if ($(this).hasClass("key--pressed")) {
$("[data-key*='Colour']").removeClass("key--pressed");
activeColour = "";
}
else {
$("[data-key*='Colour']").removeClass("key--pressed");
$(this).addClass("key--pressed");
}
});
/* -------------------------------------------------------------------------- */
/* ---------------------- Add/Remove element functions. --------------------- */
$(document).on("click", "#AddSectionDivider", function () {
var template = $(document).find("#templateSectionDivider").clone();
template.removeAttr('hidden');
template.removeAttr('id');
$(template).find('hr').prop('id', 'divider' + new Date().getTime());
template.appendTo("#KeyboardTable");
});
$(document).on("click", "#AddKeyboard", function () {
var template = $(document).find("#template10Keyless").clone();
template.removeAttr('hidden');
template.removeAttr('id');
$(template).find('div:first').prop('id', 'kbd' + new Date().getTime());
template.appendTo("#KeyboardTable");
});
// TODO: Implement addChordSection()
$(document).on("click", "#AddChordSection", function () {
});
function removeSectionDivider(SectionDividerLi) {
if (confirm('Are you sure you want to delete this section divider?')) {
SectionDividerLi.remove();
}
}
function removeKeyboard(keyboardLi) {
if (confirm('Are you sure you want to delete this keyboard?')) {
keyboardLi.remove();
}
}
function removeChordSection(ChordLi) {
if (confirm('Are you sure you want to delete this chord section?')) {
ChordLi.remove();
}
}
/* -------------------------------------------------------------------------- */
/* ------------------------ List ordering functions. ------------------------ */
$(document).on("click", ".up", function () {
before = $(this.parentNode).prev();
$(this.parentNode).insertBefore(before);
});
$(document).on("click", ".down", function () {
after = $(this.parentNode).next();
$(this.parentNode).insertAfter(after);
});
// TODO: Implement drag and drop.
/* -------------------------------------------------------------------------- */
/* ---------------------------- Editable headers ---------------------------- */
$(document).on("click", "#loadedFileHeader", function () {
var currentText = $(this).text().substring(1, $(this).text().length - 5);
var newText = prompt("Please enter a new filename.", currentText);
if (newText != null) {
$(this).text(newText + ".html");
}
});
$(document).on("click", ".editable:not(#loadedFileHeader)", function () {
var currentText = $(this).text().substring(1, $(this).text().length);
var newText = prompt("Please enter a new name.", currentText);
if (newText != null) {
$(this).text(newText);
}
});
$(document).on("mouseover", ".editable", function () {
$(this).text("✎" + $(this).text());
});
$(document).on("mouseleave", ".editable", function () {
let text = $(this).text();
if (text[0] == "✎") {
$(this).text(text.substring(1, text.length));
}
});
/* -------------------------------------------------------------------------- */
/* -------------------------- Save to HTML function ------------------------- */
function saveToHtml() {
// Create new document.
var parser = new DOMParser();
var outfile = parser.parseFromString("", "text/html");
// Add comment at head of file telling user how to edit.
$(outfile).find('html').prepend(
'<!-- This file was made using: ' +
'https://archie-adams.github.io/keyboard-shortcut-map-maker/ It can be ' +
'edited by loading it with the same website. -->');
// Add all meta tags to the head.
$('head').find('meta').appendTo($(outfile).find('head'));
// Add title to the head.
var title = $(document).find('#loadedFileHeader').text();
$(outfile).find('head').append("<title>" + title + "</title>");
// Add css styling to the head.
$.when($.get("site.css"))
.done(function (response) {
$('<style />').text(response).appendTo($(outfile).find('head'));
$.when($.get("keyboard.css"))
.done(function (response) {
$('<style />').text(response).appendTo($(outfile).find('head'));
$.when($.get("key-colours.css"))
.done(function (response) {
$('<style />').text(response).appendTo($(outfile).find('head'));
// TODO: Nested due to async? function timings.
// Create the header.
$(outfile).find('head').after('<header></header>');
$('header').find('h1').clone().appendTo($(outfile).find('header'));
$('header').find('h2').clone().appendTo($(outfile).find('header'));
$(outfile).find('header').append('<div style=" padding-top: 1px;"></div>');
// Create the body.
$(outfile).find('body').append('<div style=" margin-top: 30px;" class="top-padding-50px"></div>');
$(outfile).find('body').append('<div class="bodyStyle"></div>');
$(outfile).find('.bodyStyle').append($(document).find('#KeyboardTable').clone());
// Visually remove UI from keyboards.
$(outfile).find('li span:not(.userText)').addClass('hidden');
$(outfile).find('li p').addClass('hidden');
// Create and download the file
let data = new XMLSerializer().serializeToString(outfile);
const textToBLOB = new Blob([data], { type: 'text/html' });
let newLink = document.createElement("a");
newLink.download = title;
if (window.webkitURL != null) {
newLink.href = window.webkitURL.createObjectURL(textToBLOB);
}
else {
newLink.href = window.URL.createObjectURL(textToBLOB);
newLink.style.display = "none";
document.body.appendChild(newLink);
}
newLink.click();
});
});
});
}
/* -------------------------------------------------------------------------- */
/* -------------------------- Save to PNG function -------------------------- */
function saveToPng() {
var pngTable = $("#KeyboardTable").clone();
pngTable.removeAttr('id');
$(pngTable).prop('id', 'pngTable');
$(pngTable).find('li span:not(.userText)').addClass('hidden');
$(pngTable).find('li p').addClass('hidden');
$(pngTable).prop('style', 'background-color: white;');
$(pngTable).find('h2').prop('style', 'margin-top:10px;');
$(pngTable).find('h3').prop('style', 'margin-top:20px;');
$("#KeyboardTable").after(pngTable);
domtoimage.toBlob(document.getElementById('pngTable'))
.then(function (blob) {
var name = $("#loadedFileHeader").text();
name = name.substring(0, name.length - 5) + ".png";
window.saveAs(blob, name);
$("#pngTable").remove();
});
}
/* -------------------------------------------------------------------------- */
/* --------------------------- Load HTML Function --------------------------- */
// FIXME: Only loads on input file change, if user loads a file, edits it,
// decides to start from scratch and attempts to reopen the same file, the
// selected file will not have changed and nothing will happen.
$(document).on("change", "#inputfile", function () {
// Clear the current keyboard list.
$(document).find("#KeyboardTable").empty();
var fr = new FileReader();
fr.onload = function () {
// Set the document keyboard list to the read in keyboard list.
var string = fr.result;
var object = $('<div/>').html(string).contents();
$(document).find("#KeyboardTable").html(object.find("#KeyboardTable").html());
// Set the filename header.
$(document).find("#loadedFileHeader").text(object.find("#loadedFileHeader").text());
// Unhide the controls on the keyboards.
$(document).find("#KeyboardTable li span").removeClass('hidden');
$(document).find("#KeyboardTable li p").removeClass('hidden');
}
fr.readAsText(this.files[0]);
})
/* -------------------------------------------------------------------------- */
/* ---------------------------- New Set Function ---------------------------- */
function newSet() {
if (confirm('If you start a new set unsaved changes will be lost.')) {
$(document).find("#KeyboardTable").empty();
$(document).find(loadedFileHeader).text("New-Set.html");
$(document).find('#AddKeyboard').click();
}
}
/* -------------------------------------------------------------------------- */
/* --------------- Stop user from accidentally losing changes --------------- */
window.onbeforeunload = function (e) {
return "Sure you want to leave?";
};
/* -------------------------------------------------------------------------- */
// TODO: Add mouse over enlarge and cursor click image.
/* ---------------------- Toggle the introduction text. --------------------- */
$(document).on("click", "#close-introduction, #open-introduction", function () {
$("#introduction, #open-introduction").toggleClass("hidden");
});
/* -------------------------------------------------------------------------- */