-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageCustom.js
393 lines (387 loc) · 13.6 KB
/
pageCustom.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
var PageCustom = function () {
var _currentEditId = 0;
var _currentUseForSample = false;
var _samplesAddedToFirstPage = false;
var _itemsForDefaultSelects = [];
function preparePartsList() {
var parts = [];
var source = shallowCloneOf(_di);
var partsToSkip = ";upcomingHtml;bNow;frag1SunTimes;frag2SunTimes;frag1;frag2;currentTime;stamp;";
for (var partName in source) {
// TODO verify this is correct
if (Object.prototype.hasOwnProperty.call(source, partName)) {
if (partsToSkip.search(";".concat(partName, ";")) !== -1) {
continue;
}
parts.push({
name: partName,
type: typeof source[partName],
});
}
}
parts.sort(function (a, b) {
return a.name < b.name ? -1 : 1;
});
var template =
"<div><span class=customPart>{name}</span>" +
"<button type=button class=button>Add</button>" +
'<span class="customPartSample part_{type}" data-part="#{name}*"></span></div>';
var html = template.filledWithEach(parts).replace(/\#/g, "{").replace(/\*/g, "}");
$("#partsList").html(html);
setTimeout(function () {
showForCurrentDate();
}, 0);
}
function showForCurrentDate() {
$("#partsList .customPartSample, .customFormats .customSample").each(function (i, el) {
var span = $(el);
var part = span.data("part");
span.html(part.filledWith(_di));
});
updateActive();
updateFirstPageSamples();
updateDefaultDropdowns();
}
function inputChanged() {
updateActive();
}
function updateActive() {
var rawSource = $("#customBuilderInput");
var rawText = rawSource.val();
_nextFilledWithEach_UsesExactMatchOnly = true;
var converted = rawText.filledWith(_di);
_nextFilledWithEach_UsesExactMatchOnly = false;
var echo = $("#customBuilderEcho");
echo.html(converted || " ");
var hasError = converted.search(/{/) !== -1 || converted.search(/}/) !== -1;
rawSource.toggleClass("hasError", hasError);
updateEditButtons();
}
function addPart(ev) {
var btn = $(ev.target);
var template = btn.next().data("part");
var input = $("#customBuilderInput");
var rawInput = input[0];
var startPos = rawInput.selectionStart;
var endPos = rawInput.selectionEnd;
if (endPos === rawInput.value.length && endPos > 0) {
template = " ".concat(template); // if at the end of the input, add a space
}
var before = rawInput.value.substring(0, startPos);
rawInput.value = before + template + rawInput.value.substring(endPos, rawInput.value.length);
input.focus().trigger("change");
rawInput.selectionStart = rawInput.selectionEnd = startPos + template.length;
}
function saveEdits() {
var editInput = $("#customBuilderInput");
var value = editInput.val();
if (!value && !_currentEditId) {
return;
}
var data = {
f: value,
checked: _currentUseForSample ? "checked" : "",
};
var templateDiv = getCustomSample();
_nextFilledWithEach_UsesExactMatchOnly = true;
var newDiv = $(templateDiv.filledWith(data));
_nextFilledWithEach_UsesExactMatchOnly = false;
var sample = newDiv.find(".customSample");
sample.html(data.f.filledWith(_di));
if (!_currentEditId) {
newDiv.addClass("inEdit");
$(".customFormats").append(newDiv);
_currentEditId = renumberSamples();
} else {
var id = "customFormat_".concat(_currentEditId);
newDiv.attr("id", id);
$("#".concat(id)).replaceWith(newDiv);
}
saveFormats();
updateEditButtons();
}
function renumberSamples() {
var lastNum = 0;
$(".customFormats .customFormatDiv").each(function (i, el) {
lastNum = 1 + i;
el.id = "customFormat_".concat(lastNum);
});
return lastNum;
}
function deleteSample() {
var div = $("#customFormat_".concat(_currentEditId));
div.remove();
cancelEditing();
saveFormats();
}
function copySample(ev) {
var btn = $(ev.target);
var div = btn.closest(".customFormatDiv");
var format = div.find(".customFormat").html();
var text = div.find(".customSample").html();
// $("#sampleCopy").val(text).focus().select();
// document.execCommand("copy");
// console.log("copying", text);
navigator.clipboard.writeText(text).catch((error) => {
console.warn("Copy failed", error);
});
btn.text(getMessage("copied"));
setTimeout(function () {
btn.text(getMessage("customBtnCopy"));
}, 1000);
}
function editSample(ev) {
var btn = $(ev.target);
var div = btn.closest(".customFormatDiv");
$(".customFormats .customFormatDiv").removeClass("inEdit");
div.addClass("inEdit");
// remember setting while editing
_currentEditId = +div.attr("id").split("_")[1];
_currentUseForSample = div.find(".customIsSample input").is(":checked");
$("#customBuilderInput").val(div.find(".customFormat").html()).trigger("change");
updateEditButtons();
}
function loadCustom(ev) {
cancelEditing();
var btn = $(ev.target);
$("#customBuilderInput").val(btn.data("format")).trigger("change");
updateEditButtons();
}
function updateEditButtons() {
var notEditing = !_currentEditId;
var notEditingAndBlank = notEditing && $("#customBuilderInput").val() === "";
$("#btnCustomBuilderSave").prop("disabled", notEditingAndBlank);
$("#btnCustomBuilderDelete").prop("disabled", notEditing);
$("#btnCustomBuilderCancel").prop("disabled", notEditingAndBlank);
$(".customDefinedTitleDiv").toggleClass("show", $(".customFormats .customFormatDiv").length > 0);
}
function cancelEditing() {
$(".customFormats .customFormatDiv").removeClass("inEdit");
_currentEditId = 0;
_currentUseForSample = false;
$("#customBuilderInput").val("").removeClass("hasError");
$("#customBuilderEcho").html(" ");
updateEditButtons();
}
function addFromFirstPage(letter, format) {
var button = '<button type=button class="button btnLoadCustom" data-format="'.concat(format, '">').concat(letter, "</button>");
$(".customLettersFromFirstPage").append(button);
// if (!_samplesAddedToFirstPage) {
_itemsForDefaultSelects.push({
letter: letter,
format: format,
});
// }
}
function clearFromFirstPage() {
$(".customLettersFromFirstPage").html("");
}
function updateFirstPageSamples(forceRefresh) {
if (!forceRefresh) {
forceRefresh = null;
}
if (!_samplesAddedToFirstPage || forceRefresh) {
addSamplesToFirstPage();
}
$("#sampleList2 span").each(function (i, el) {
var span = $(el);
span.html(span.data("template").filledWith(_di));
});
}
function addSamplesToFirstPage() {
var selected = [];
var nextItemNumber = 1 + $("#sampleList1 > div").length;
if (nextItemNumber === 1) {
addSamples(_di);
_samplesAddedToFirstPage = true;
return;
}
$(".customFormats .customFormatDiv").each(function (i, el) {
var div = $(el);
var span = div.find(".customIsSample span");
var checked = div.find(".customIsSample input").is(":checked");
if (nextItemNumber > 26) {
div.find(".customIsSample input").prop("checked", false);
checked = false;
}
if (!checked) {
span.html("");
return;
}
var letter = String.fromCharCode(64 + nextItemNumber);
span.html(letter);
selected.push({
currentNote: "",
letter: letter,
tooltip: getMessage("pressKeyOrClick", letter),
template: div.find(".customFormat").text(),
});
nextItemNumber++;
});
_nextFilledWithEach_UsesExactMatchOnly = true;
var host = $("#samples").find("#sampleList2");
host.html(
(
'<div><button title="{tooltip}"' +
' type=button data-letter={letter} id="key{letter}">{letter}{currentNote}</button>' +
' <span data-template="{template}"></span></div>'
).filledWithEach(selected)
);
host.toggleClass("hasSamples", selected.length > 0);
if (!_samplesAddedToFirstPage) {
fillSelectForDefaults();
}
_samplesAddedToFirstPage = true;
}
function saveFormats() {
var formats = [];
$(".customFormats .customFormatDiv").each(function (i, el) {
var div = $(el);
formats.push({
f: div.find(".customFormat").text(),
s: div.find(".customIsSample input").is(":checked"),
});
});
$(".customDefinedTitleDiv").toggleClass("show", formats.length > 0);
tracker.sendEvent("modifiedSampleFormats", formats);
putInStorageSyncAsync(syncStorageKey.customFormats, formats);
common.customFormats = formats;
updateFirstPageSamples(true);
fillSelectForDefaults();
}
function getCustomSample() {
return $("#customSampleTemplate").html().replace('data-x=""', "{checked}");
}
function recallSettings(formats) {
var formats2 = [];
if (typeof formats === "string") {
formats2 = JSON.parse(formats);
} else {
formats2 = formats || common.customFormats;
}
if (formats2?.length) {
formats2.forEach((el) => {
el.checked = el.s ? "checked" : "";
//log(el);
});
var templateDiv = getCustomSample();
_nextFilledWithEach_UsesExactMatchOnly = true;
var result = templateDiv.filledWithEach(formats2);
_nextFilledWithEach_UsesExactMatchOnly = false;
$(".customFormats").html(result);
setTimeout(function () {
addSamplesToFirstPage();
showForCurrentDate();
}, 0);
renumberSamples();
updateEditButtons();
}
}
function isSampleChanged() {
saveFormats();
updateEditButtons();
}
function attachHandlers() {
$("#customBuilderInput").on("change keyup paste", inputChanged);
$("#partsList").on("click", "button", addPart);
$(".customFormats").on("click", ".btnCopy", copySample);
$(".customFormats").on("click", ".btnEdit", editSample);
$(".customFormats").on("change", ".cbIsSample", isSampleChanged);
$("#pageCustom").on("click", ".btnLoadCustom", loadCustom);
$("#btnCustomBuilderSave").on("click", saveEdits);
$("#btnCustomBuilderCancel").on("click", cancelEditing);
$("#btnCustomBuilderDelete").on("click", deleteSample);
$("#customLoadTopDay").on("change", saveTopDayFormat);
$("#customLoadToolTip1").on("change", saveTopToolTipFormat1);
$("#customLoadToolTip2").on("change", saveTopToolTipFormat2);
}
function fillSelectForDefaults() {
// each item in list is: letter:'A',format:'{format}'
fillSelectDefault("customLoadToolTip1", common.formatToolTip1, getMessage("formatIconToolTip"));
fillSelectDefault("customLoadToolTip2", common.formatToolTip2, getMessage("nearestSunset"));
fillSelectDefault("customLoadTopDay", common.formatTopDay, getMessage("bTopDayDisplay"));
updateDefaultDropdowns();
}
function fillSelectDefault(id, value, message) {
var defaultFormat = message;
var defaultFound = false;
var optionsHtml = ['<optgroup label="{0}">'.filledWith(getMessage("standardFormats"))];
_itemsForDefaultSelects.forEach((el) => {
var format = el.format;
var isDefault = false;
if (format === defaultFormat) {
defaultFound = true;
isDefault = true;
}
optionsHtml.push(
'<option value="'
.concat(format.replace(/"/g, """), '" data-prefix="')
.concat(isDefault ? getMessage("defaultFormat") : "")
.concat(el.letter, ' - " data-format="')
.concat(format, '"></option>')
);
});
optionsHtml.push("</optgroup>");
// add local custom formats
var formats = common.customFormats;
if (formats?.length > 0) {
optionsHtml.push('<optgroup label="{0}">'.filledWith(getMessage("customFormats")));
formats.forEach((el, i) => {
var format = el.f;
optionsHtml.push(
'<option value="'
.concat(format.replace(/"/g, """), '" data-prefix="')
.concat(i + 1, ' - " data-format="')
.concat(format, '"></option>')
);
});
optionsHtml.push("</optgroup>");
}
// fill select
var ddl = $("#".concat(id)).html(
(defaultFound ? "" : '<option value="'.concat(defaultFormat, '" data-prefix="Default - " data-format="').concat(defaultFormat, '"></option>')) +
optionsHtml.join("")
);
ddl.val(value);
if (!ddl.val()) {
ddl.val(defaultFormat);
}
}
function saveTopToolTipFormat1(ev) {
const value = $(ev.target).find("option:selected").data("format");
common.formatToolTip1 = value;
showIcon();
putInStorageSyncAsync(syncStorageKey.formatToolTip1, value);
}
function saveTopToolTipFormat2(ev) {
const value = $(ev.target).find("option:selected").data("format");
common.formatToolTip2 = value;
showIcon();
putInStorageSyncAsync(syncStorageKey.formatToolTip2, value);
}
function saveTopDayFormat(ev) {
const value = $(ev.target).find("option:selected").data("format");
putInStorageSyncAsync(syncStorageKey.formatTopDay, value);
common.formatTopDay = value;
showInfo();
}
function updateDefaultDropdowns() {
$(".customLoadDefaults option").each(function (i, el) {
var option = $(el);
option.html(option.data("prefix") + option.data("format").filledWith(_di));
});
}
function startup() {
recallSettings(); // do local storage quickly... let sync storage overwrite later
preparePartsList();
attachHandlers();
$(".customSelect").html(getMessage("customSelectForFrontPage").filledWith(getMessage("pick_pageDay")));
}
startup();
return {
updateDate: showForCurrentDate,
updateFirstPage: updateFirstPageSamples,
clearFromFirstPage: clearFromFirstPage,
addFromFirstPage: addFromFirstPage,
};
};