-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinker_script.js
600 lines (508 loc) · 20.1 KB
/
tinker_script.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/**
* This file contains functions to handle various UI interactions including button actions, slider operations,
* and element manipulation within the dev-container and slider interfaces. Write all components in #dev-container.
* Css For #dev-container may need small adjustments.
*
* Tree/Leaf explained:
* To facilitate the outlines and matching of elements a leaf global object named leafs is used.
* The leafs object tracks the state of each leaf and its relevant id variations.
* Each element/leaf pair is given a "leafId". The number is then stored in a custom
* attribute named leadId on each leaf and element. Elements leafIds are suffixed by "-e"
* and leafs are suffixed by "-l"
*
* Global Variables:
* - leafs: object to track the state of leafs. Each leaf matches an html element.
*
* Functions:
* - attachEventListeners(): Attaches event listeners to elements
* - attachRangeEventListeners(): Attaches event listeners to range input elements
* - attachButtonEventListeners(): Attaches event listeners to button elements
* - loadCssToTextarea(): Loads CSS from the tinker CSS file into the text area
* - applyTextAreaCss(): Applies CSS in the textarea to the page
* - saveCssToStorage(cssContent): Saves textarea CSS to storage (prevents removal on refresh)
* - loadCssFromStorage(): Loads textarea CSS from storage (runs on page load)
* - resetCssToBase(): Resets the page CSS and storage CSS back to default
* - setIndicator(min, max, value, indicator): Sets the indicator for a range element
* - setCSS(selector, property, units, value): Sets CSS properties for a given selector
* - printTree(element, indent, leafCounter = { co }): Prints a tree structure of the elements
* - darkMode(): Activates dark mode for the page
* - lightMode(): Activates light mode for the page
* - addTinkerSlider(): Adds a slider element to the page
* - saveSlidersToStorage(): Saves slider states to storage
* - loadSlidersFromStorage(): Loads slider states from storage
* - resetTinkerSliders(): Resets the sliders to their default state
* - addTinkerButton(): Adds a button element to the page
* - saveButtonsToStorage(): Saves button states to storage
* - loadButtonsFromStorage(): Loads button states from storage
* - resetTinkerButtons(): Resets the buttons to their default state
* - saveSettingsToStorage(): Saves settings to storage
* - loadSettingsFromStorage(): Loads settings from storage
*/
/* All Dev JS below here */
function foo() {
console.log("Hello World!");
}
/* All Dev JS above here */
// Save input values before refresh
$(window).on("beforeunload", function () {
saveSlidersToStorage();
saveButtonsToStorage();
saveSettingsToStorage();
});
// Load needed information from storage and attach event listeners
$(window).on("load", function () {
loadSlidersFromStorage();
loadButtonsFromStorage();
loadSettingsFromStorage();
loadCssFromStorage();
loadCssToTextarea();
attachEventListeners();
});
var leafs = {};
/**
* Function to attach event listeners
*/
function attachEventListeners() {
$(".tab").on("click", function () {
let index = $(this).index();
// Sets proper tab as active
$(".active-tab").toggleClass("active-tab inactive-tab");
$(this).toggleClass("active-tab inactive-tab");
// Sets matching slide as active
$(".active-slide").toggleClass("active-slide inactive-slide");
$("#tinker-slides .slide").eq(index).toggleClass("active-slide inactive-slide");
});
$("#css-textarea").on("keydown", function (e) {
if (e.key == "Tab") {
e.preventDefault();
var cursorPos = this.selectionStart;
var textBefore = this.value.substring(0, cursorPos);
var textAfter = this.value.substring(cursorPos);
this.value = textBefore + " " + textAfter;
this.selectionStart = this.selectionEnd = cursorPos + 4;
} else if (e.key == "{") {
e.preventDefault();
var cursorPos = this.selectionStart;
var textBefore = this.value.substring(0, cursorPos);
var textAfter = this.value.substring(cursorPos);
this.value = textBefore + "{}" + textAfter;
this.selectionStart = this.selectionEnd = cursorPos + 1;
}
});
$(".leaf").on("mouseenter", function () {
let leafLeafId = $(this).attr("leafId");
let elementLeafId = leafs[leafLeafId.slice(0, -2)].elementLeafId;
$(`[leafId='${elementLeafId}']`).addClass("blinking-outline");
});
$(".leaf").on("mouseleave", function () {
let leafLeafId = $(this).attr("leafId");
let elementLeafId = leafs[leafLeafId.slice(0, -2)].elementLeafId;
$(`[leafId='${elementLeafId}']`).removeClass("blinking-outline");
});
$(".leaf").on("click", function () {
let leafLeafId = $(this).attr("leafId");
let elementLeafId = leafs[leafLeafId.slice(0, -2)].elementLeafId;
$(`[leafId='${elementLeafId}']`).toggleClass("constant-outline");
});
attachRangeEventListeners();
attachButtonEventListeners();
$(".tinker-background-setting").on("input", function () {
$("#dev-container").css("background-color", $(this).val());
});
$("#run-css-button").on("click", applyTextAreaCss);
$("#reset-css-button").on("click", resetCssToBase);
}
/**
* Attaches relevany event listeners to sliders
*/
function attachRangeEventListeners() {
// Update the indicator position when the slider value changes
$(".css-range-slider")
.off("input")
.on("input", function () {
let value = $(this).val();
let indicator = $(this).parent().children(1);
let inputs = $(this).parent().parent().children(1);
let selector = inputs.find(".css-selector-input").val();
let property = inputs.find(".css-property-input").val();
let units = inputs.find(".css-units-input").val();
setIndicator($(this).attr("min"), $(this).attr("max"), value, indicator);
setCSS(selector, property, units, value);
});
$(".css-range-start-input, .css-range-end-input")
.off("input")
.on("input", function () {
let cluster = $(this).closest(".slider-cluster");
let slider = cluster.find(".css-range-slider");
if ($(this).hasClass("css-range-start-input")) {
slider.attr("min", $(this).val());
} else if ($(this).hasClass("css-range-end-input")) {
slider.attr("max", $(this).val());
}
});
}
/**
* Attaches relevant event listeners to function buttons
*/
function attachButtonEventListeners() {
$(".button-function-input")
.off("input")
.on("input", function () {
let cluster = $(this).closest(".button-cluster");
let button = cluster.find(".tinker-function-button");
let value = $(this).val();
button.off("click").on("click", function () {
eval(value);
});
});
}
/**
* Loads css from tinker_style.css link into the text area.
*/
function loadCssToTextarea() {
const linkElement = document.querySelector('link[rel="stylesheet"][href="tinker_style.css"]');
if (linkElement) {
const stylesheetUrl = linkElement.href;
// Fetch file and load into textarea
fetch(stylesheetUrl)
.then((response) => response.text())
.then((cssContent) => {
$("#css-textarea").val(cssContent);
})
.catch((error) => {
console.error("Error loading CSS file:", error);
});
} else {
console.error("Stylesheet not found");
}
}
/**
* Applys the css contained within the text are to the page.
*/
function applyTextAreaCss() {
const cssContent = $("#css-textarea").val();
const styleElement = $("<style></style>");
styleElement.html(cssContent);
$("head").append(styleElement);
saveCssToStorage(cssContent);
}
/**
* Saves the passed variable to a local storage item named "textareaCss"
*
* @param {*} cssContent
*/
function saveCssToStorage(cssContent) {
sessionStorage.setItem("textareaCss", cssContent);
}
/**
* Loads an item called "textareaCss" from local storage, loads it into the text
* area, and applies the css.
*/
function loadCssFromStorage() {
const savedCss = sessionStorage.getItem("textareaCss");
if (savedCss) {
$("#css-textarea").val(savedCss);
applyTextAreaCss();
}
}
/**
* Resets the pages css back to whatever is contained within "tinker_style.css"
* This function will also clear the "textareaCss" from local storage.
*/
function resetCssToBase() {
// Remove all dynamically added styles
$("style").remove();
sessionStorage.removeItem("textareaCss");
// Get base link
let baseLink = $('link[rel="stylesheet"][href*="tinker_style.css"]');
// Add link if no longer present
if (baseLink.length === 0) {
$("head").append('<link rel="stylesheet" href="{% static \'css/tinker_style.css\' %}" />');
}
loadCssToTextarea();
}
/**
* Calculates and sets the proper offset and
* value for the indicator on a slider
*
* @param {int} min - minimum value of the slider range
* @param {int} max - maximum value of the slider range
* @param {int} value - Current value of the slider
* @param {$element} indicator - Indicator element for the slider
*/
function setIndicator(min, max, value, indicator) {
let percentThrough = ((value - min) / (max - min)) * 100;
// Offset to avoid additional movement due to thumb width
let leftOffset = (percentThrough / 100) * 15;
indicator.css("left", `calc(${percentThrough}% - ${leftOffset}px)`);
$(indicator).find(".indicator-text").text(value);
}
/**
* Uses jquery to set the css of a DOM element using the input information
*
* @param {string} selector - CSS selector to select the jquery element
* @param {string} property - CSs property to change
* @param {string} units - Units of the CSS property (eq. rem, px, vh,...)
* @param {int} value - The value to set the property to
*/
function setCSS(selector, property, units, value) {
$(selector).css(`${property}`, `${value}${units}`);
}
printTree($("#dev-container"), "");
/**
* Recursive function to print the DOM elements in the tinker tree
*
* The function works by creating a "leaf" that correlates to a real
* DOM element by use of a leaf-id. The leafs are then stored in a
* global object that tracks their relevant states.
*
* @param {$element} element - The current element to print the children of
* @param {string} indent - The amount to indent the text by for formatting
* @param {object} leafCounter - The leaf number that correlates to the current element
* @returns
*/
function printTree(element, indent, leafCounter = { count: 0 }) {
let numOfChildren = element.children().length;
// Base Case: If no children, stop recursion
if (numOfChildren === 0) {
return;
}
let newIndent = indent + " ";
element.children().each(function () {
let leafId = leafCounter.count++;
let tagName = $(this).prop("nodeName").toLowerCase();
let leaf = `<div class="leaf" leafId="${leafId}-l"><p>${newIndent}${tagName}</p></div>`;
$(this).attr("leafId", `${leafId}-e`);
leafs[leafId] = {
leafLeafId: leafId + "-l",
elementLeafId: leafId + "-e",
outline: false,
};
$("#tinker-tree").append(leaf);
// Recursive call
printTree($(this), newIndent, leafCounter);
});
}
/**
* Sets all necessary css variables to dark mode variations
*/
function darkMode() {
$("html").css("--current-display-mode", "dark");
$("html").css("--tinker-primary-color", "orange");
$("html").css("--tinker-darker-primary", "rgb(255, 140, 0)");
$("html").css("--tinker-background-color", "rgb(22, 22, 22)");
$("html").css("--tinker-background-shadow-color", "rgb(15, 15, 15)");
$("html").css("--tinker-text-color", "white");
$("html").css("--tinker-slider-background", "black");
$("html").css("--tinker-slider-thumb", "#333");
$("html").css("--tinker-slider-shadow", "#333");
}
/**
* Sets all necessary css variables to light mode variations
*/
function lightMode() {
$("html").css("--current-display-mode", "light");
$("html").css("--tinker-primary-color", "rgb(94, 94, 248)");
$("html").css("--tinker-darker-primary", "rgb(94, 94, 248)");
$("html").css("--tinker-background-color", "white");
$("html").css("--tinker-background-shadow-color", "rgb(0, 0, 0, 0)");
$("html").css("--tinker-text-color", "black");
$("html").css("--tinker-slider-background", "white");
$("html").css("--tinker-slider-thumb", "rgb(0, 0, 0, 0)");
$("html").css("--tinker-slider-shadow", "rgb(0, 0, 0, 0.2)");
}
/**
* Adds default slider html to the slider container and attaches event listeners to
* new slider.
*/
function addTinkerSlider() {
let sliderHtml = `
<div class="slider-cluster">
<div class="slider-cluster__options">
<input
type="text"
class="css-selector-input tinker-text-input tinker-text-input__full"
spellcheck="false"
placeholder=".example-class"
/>
<input
type="text"
class="css-property-input tinker-text-input tinker-text-input__full"
spellcheck="false"
placeholder="padding"
/>
<span>
<input
type="text"
class="css-range-start-input tinker-text-input tinker-text-input__half"
spellcheck="false"
placeholder="Start"
/>
<input
type="text"
class="css-range-end-input tinker-text-input tinker-text-input__half"
spellcheck="false"
placeholder="End"
/>
<input
type="text"
class="css-units-input tinker-text-input tinker-text-input__half"
spellcheck="false"
placeholder="Units"
/>
</span>
</div>
<div class="slider-cluster__slider">
<div class="indicator"><p class="indicator-text">0</p></div>
<input type="range" min="0.00" max="50" step="0.1" value="0" class="css-range-slider" />
</div>
</div>
<div class="sep-h"></div>
`;
$("#sliders").append(sliderHtml);
attachRangeEventListeners();
}
/**
* Saves sliders information to sessionStorage
*/
function saveSlidersToStorage() {
let sliderValues = [];
$(".slider-cluster").each(function () {
const cluster = $(this);
const selectorInput = cluster.find(".css-selector-input").val();
const propertyInput = cluster.find(".css-property-input").val();
const rangeStartInput = cluster.find(".css-range-start-input").val();
const rangeEndInput = cluster.find(".css-range-end-input").val();
const unitsInput = cluster.find(".css-units-input").val();
const sliderValue = cluster.find(".css-range-slider").val();
const clusterData = {
selector: selectorInput,
property: propertyInput,
rangeStart: rangeStartInput,
rangeEnd: rangeEndInput,
units: unitsInput,
sliderValue: sliderValue,
};
sliderValues.push(clusterData);
});
sessionStorage.setItem("sliderData", JSON.stringify(sliderValues));
}
/**
* Loads sliders information from storage and creates needed sliders
*/
function loadSlidersFromStorage() {
const savedSliderData = JSON.parse(sessionStorage.getItem("sliderData"));
if (savedSliderData && savedSliderData.length > 0) {
savedSliderData.forEach((sliderData, index) => {
addTinkerSlider();
let cluster = $(".slider-cluster").eq(index);
if (cluster.length) {
cluster.find(".css-selector-input").val(sliderData.selector);
cluster.find(".css-property-input").val(sliderData.property);
cluster.find(".css-range-start-input").val(sliderData.rangeStart);
cluster.find(".css-range-end-input").val(sliderData.rangeEnd);
cluster.find(".css-units-input").val(sliderData.units);
cluster.find(".css-range-slider").val(sliderData.sliderValue);
cluster.find(".slider-cluster__slider input").attr("min", sliderData.rangeStart);
cluster.find(".slider-cluster__slider input").attr("max", sliderData.rangeEnd);
let indicator = cluster.find(".indicator");
setIndicator(sliderData.rangeStart, sliderData.rangeEnd, sliderData.sliderValue, indicator);
setCSS(sliderData.selector, sliderData.property, sliderData.units, sliderData.sliderValue);
}
});
} else {
addTinkerSlider();
}
}
/**
* Clears sliders container and resets the session storage
* that holds sliders information
*/
function resetTinkerSliders() {
sessionStorage.removeItem("sliderData");
$("#sliders").empty();
addTinkerSlider();
}
/**
* Adds default function button to button container and attaches event listeners to new
* button
*/
function addTinkerButton() {
let buttonHtml = `
<div class="button-cluster">
<div class="button-cluster__inputs">
<input
type="text"
class="button-function-input tinker-text-input tinker-text-input__full"
spellcheck="false"
placeholder="Function(args)"
/>
</div>
<button class="tinker-function-button">Run Function</button>
</div>
<div class="sep-h"></div>
`;
$("#tinker-buttons").append(buttonHtml);
attachButtonEventListeners();
}
/**
* Saves function button information to sesstion storage
*/
function saveButtonsToStorage() {
let buttonValues = [];
$(".button-cluster").each(function () {
const cluster = $(this);
const functionInfo = cluster.find(".button-function-input").val();
const clusterData = {
functionInfo: functionInfo,
};
buttonValues.push(clusterData);
});
sessionStorage.setItem("buttonData", JSON.stringify(buttonValues));
}
/**
* Loads function button information from storage and adds needed buttons to the page.
*/
function loadButtonsFromStorage() {
const savedButtonData = JSON.parse(sessionStorage.getItem("buttonData"));
if (savedButtonData && savedButtonData.length > 0) {
savedButtonData.forEach((buttonData, index) => {
addTinkerButton();
let cluster = $(".button-cluster").eq(index);
cluster.find(".button-function-input").val(buttonData.functionInfo);
});
} else {
addTinkerButton();
}
}
/**
* Clears function button container and resets the session storage
* that holds buttons information
*/
function resetTinkerButtons() {
sessionStorage.removeItem("buttonData");
$("#tinker-buttons").empty();
addTinkerButton();
}
/**
* Saves settings to session storage
*/
function saveSettingsToStorage() {
const settingsData = {
displayMode: $("html").css("--current-display-mode"),
backgroundColor: $("#dev-container").css("background-color"),
};
sessionStorage.setItem("settingsData", JSON.stringify(settingsData));
}
/**
* Loads settings from session storage and applies the stored information
*/
function loadSettingsFromStorage() {
const settingsData = JSON.parse(sessionStorage.getItem("settingsData"));
if (settingsData && settingsData.displayMode && settingsData.displayMode == "light") {
lightMode();
}
if (settingsData && settingsData.backgroundColor) {
$("#dev-container").css("background-color", settingsData.backgroundColor);
}
}