-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
1102 lines (846 loc) · 35 KB
/
index.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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
global objects from 3rd party js libraries
window.zip
https://github.com/gildas-lormeau/zip.js
window.idbKeyval
https://github.com/jakearchibald/idb-keyval
*/
const app = {
//#region ******************************* SETTINGS *******************************
// this shows the debug button on the home page and
// the json object below the form on the form page
debugMode: false,
// show a confirm dialog when deleting things
confirmDelete: true,
// the currently selected text translation
currentLanguage: "en",
// text translations
translations: {
en: { // english
// html strings
homePageHeading: "My Forms",
homePageCreateForm: "Create Form",
formPageHeading: "Edit Form",
// javascript strings
createFormPrompt: "Enter a name",
invalidFormNameLength: "Invalid Filename. Maximum of 50 characters allowed",
invalidFormNameChars: "Invalid Filename. Only letters, numbers, spaces and underscrore are allowed",
deleteFormPrompt: "Delete this form?",
homePageNoForms: "No Forms",
formPageLoading: "Loading",
formNotFoundError: "Data for this form not found",
deleteFormItemPrompt: "Delete this item?",
selectFileButtonLabel: "Select a file",
updatingElementNameIndexesError: "Error updating element name indexes",
overwriteExistingForm: "A file with the name ${} already exists. Would you like to overwrite it?",
importFileTypeError: "Only ${} files can be imported"
},
nl: { // dutch
// html strings
homePageHeading: "Documenten",
homePageCreateForm: "Nieuw document",
formPageHeading: "Bewerk document",
// javascript strings
createFormPrompt: "Voer document naam in",
invalidFormNameLength: "De naam van dit bestand is te lang. Maximaal 50 letters.",
invalidFormNameChars: "De bestandsnaam bevat illegale tekens. Alleen letters, nummers, spaties en underscores mogen.",
deleteFormPrompt: "Verwijder dit document?",
homePageNoForms: "Geen documenten",
formPageLoading: "Laden...",
formNotFoundError: "Geen data voor dit document gevonden",
deleteFormItemPrompt: "Verwijder dit item?",
selectFileButtonLabel: "< Kies afbeelding",
updatingElementNameIndexesError: "Error updating element name indexes",
overwriteExistingForm: "Er bestaat al een bestand met de naam ${}. Wil je het overschrijven?",
importFileTypeError: "Alleen ${} bestanden kunnen geimporteerd worden"
}
},
// this returns the json schema which is used to define a default form. It can be changed
// at any time because when creating a new form a copy of this schema is included with
// the form data. There are a few rules to follow when updating the schema as shown below
getJsonSchema: function () {
/*
JSON SCHEMA RULES
- property names cannot contain dot "." symbols
- property names cannot be numbers
- properties where the value is a file must be named "image" or "file" or end with "_file"
- properties must have a sample value of the correct type (file type is just a string)
- arrays must contain at least one sample object
- array objects in the same array must have the same property names
- array objects in the same array must have the same number of properties
*/
// start schema
const jsonSchema = {
"addressBook": {
"owner": "ExampleOwnerName",
"image": "exampleImage.jpg"
},
"people": [
{
"name": "exampleName1",
"last_name": "exampleLastName1",
"age": 32,
"image": "exampleImage1.png",
"hobbies": [
{
"name": "hobby1",
"hoursPerWeek": 1
},
{
"name": "hobby2",
"hoursPerWeek": 2
}
]
},
]
};
// end schema
return jsonSchema;
},
//#endregion *********************************************************************
// selected translation object
i18n: null,
// throttles saving to database
debounceTimeout: null,
// form currently being edited
currentForm: {},
// references to frequestly used html elements
homePageEl: null,
homePageListEl: null,
formPageEl: null,
formPageFormEl: null,
formPageImageDialogEl: null,
formPageImageDialogImageEl: null,
debugPageEl: null,
// runs when window loaded
init: async function () {
this.i18n = this.translations[this.currentLanguage];
// get page elements
this.homePageEl = document.getElementById("home-page");
this.homePageListEl = document.getElementById("home-form-list");
this.formPageEl = document.getElementById("form-page");
this.formPageFormEl = document.getElementById("form-page-form");
this.formPageImageDialogEl = document.getElementById("form-page-image-dialog");
this.formPageImageDialogImageEl = document.getElementById("form-page-image-dialog-image");
this.debugPageEl = document.getElementById("debug-page");
this.debugDataEl = document.getElementById("debug-form-data");
// add strings to html
this.homePageEl.querySelector(".page-header > h1").textContent = this.i18n.homePageHeading;
this.homePageEl.querySelector("#home-create-form-button").textContent = this.i18n.homePageCreateForm;
this.formPageEl.querySelector(".page-header > h1").textContent = this.i18n.formPageHeading;
// add delegated event handlers
this.homePageEl.addEventListener("click", this.homePageClick.bind(this));
this.formPageEl.addEventListener("click", this.formPageClick.bind(this));
this.formPageEl.addEventListener("input", this.formPageInput.bind(this));
this.formPageEl.addEventListener("change", this.formPageChange.bind(this));
this.formPageEl.addEventListener("submit", (e) => { e.preventDefault(); });
this.debugPageEl.addEventListener("click", this.debugPageClick.bind(this));
// load home page forms
await this.goToHomePage();
// await this.goToFormsPage("asdf");
this.updateFormDebug();
},
//#region Home Page
goToHomePage: async function () {
this.debugPageEl.style.display = "none";
this.formPageEl.style.display = "none";
this.homePageEl.style.display = "block";
this.debugDataEl.style.display = "none";
document.getElementById("home-debug-button").style.display = this.debugMode ? "inline-block" : "none";
// empty home page form list
this.homePageListEl.innerHTML = "";
// get form names from database
const formNames = await this.loadKeysFromDb();
if (formNames.length) {
// add home page list elements
const frag = document.createDocumentFragment();
formNames.forEach(formName => {
const template = this.getTemplate("home-list-item-template");
template.querySelector("label").textContent = formName;
frag.appendChild(template);
});
this.homePageListEl.appendChild(frag);
} else {
this.showHomePageListEmpty();
}
},
homePageClick: async function (e) {
if (e.target.id === "home-create-form-button") {
const name = this.getHomePageCreateFormName();
if (!name) return;
this.goToFormsPage(name, true);
} else if (e.target.id === "home-import-button") {
if (await this.importZipFile()) {
await this.goToHomePage(); // reloads home page which reloads list items
}
} else if (e.target.classList.contains("home-list-item-edit-button")) {
const formName = e.target.parentElement.querySelector("label").textContent;
await this.goToFormsPage(formName);
} else if (e.target.classList.contains("home-list-item-download-button")) {
const formName = e.target.parentElement.querySelector("label").textContent;
await this.exportZipFile(formName);
} else if (e.target.classList.contains("home-list-item-delete-button")) {
this.homePageDeleteListItem(e.target);
} else if (e.target.id === "home-debug-button") {
this.goToDebugPage();
}
},
getHomePageCreateFormName: function () {
let name = prompt(this.i18n.createFormPrompt)?.trim();
if (!name) return;
if (name.length > 50) {
alert(this.i18n.invalidFormNameLength);
return;
}
if (~name.indexOf("/") || ~name.indexOf(":") || ~name.indexOf("\"")) {
alert(this.i18n.invalidFormNameChars);
return;
}
return name;
},
homePageDeleteListItem: async function (el) {
if (this.confirmDelete && !confirm(this.i18n.deleteFormPrompt)) return;
const listItem = el.closest(".home-list-item");
const formName = el.parentElement.querySelector("label").textContent;
listItem.remove();
await this.deleteFromDb(formName);
this.showHomePageListEmpty();
},
showHomePageListEmpty: function () {
if (!this.homePageListEl.children.length) {
this.homePageListEl.insertAdjacentHTML("afterbegin",
`<label class='center' style='font-weight: bold;'>${this.i18n.homePageNoForms}</label>`);
}
},
//#endregion
//#region Form Page
goToFormsPage: async function (formName, isCreate) {
this.debugPageEl.style.display = "none";
this.homePageEl.style.display = "none";
this.formPageEl.style.display = "block";
// clear existing form
this.formPageFormEl.innerHTML = "";
// show loading label
this.formPageFormEl.insertAdjacentHTML("afterbegin",
`<label class='center'>${this.i18n.formPageLoading}</label>`);
// create new form
if (isCreate) {
// create new form object
this.currentForm = this.getNewFormObject(formName);
await this.saveCurrentFormToDb();
// load existing form
} else if (formName) {
const formData = await this.loadFromDb(formName);
if (!formData) {
alert(this.i18n.formNotFoundError);
this.goToHomePage();
return;
}
this.currentForm = formData;
}
// create form elements
const frag = document.createDocumentFragment();
this.createFormElementsFromObj(this.currentForm.form, this.currentForm.schema, frag);
this.formPageFormEl.innerHTML = "";
this.formPageFormEl.appendChild(frag);
// apply folded state to elements
if (!isCreate) this.applyFoldedStates(this.currentForm.foldedStates);
// print form to console
// console.log(this.currentForm);
// console.log(JSON.stringify(this.currentForm.schema, null, 2));
// console.log(JSON.stringify(this.currentForm.form, null, 2));
this.debugDataEl.style.display = this.debugMode ? "inline-block" : "none";
this.updateFormDebug();
},
formPageClick: async function (e) {
if (e.target.id === "form-back-button") {
this.goToHomePage();
} else if (e.target.id === "form-download-button") {
this.exportZipFile(this.currentForm.name, this.currentForm);
} else if (e.target.classList.contains("form-add-item-button")) {
this.formPageAddItem(e.target);
} else if (e.target.classList.contains("form-delete-item-button")) {
this.formPageDeleteItem(e.target);
} else if (e.target.classList.contains("form-toggle-item-button")) {
e.target.closest(".form-list-item").classList.toggle("folded");
this.currentForm.foldedStates = this.getFoldedStates();
this.saveCurrentFormToDb();
} else if (e.target.classList.contains("form-file-upload-button")) {
this.formPageUploadFile(e.target);
} else if (e.target.classList.contains("form-file-open-button")) {
this.showImageDialog(e.target);
} else if (e.target.id === "form-page-image-dialog-close-button") {
this.hideImageDialog();
}
},
formPageInput: function (e) {
if (e.target.classList.contains("form-input")) {
const objPath = e.target.name;
// get value from input
let value = e.target.value?.trim() || null;
if (e.target.type === "number") {
if (e.target.value === "") {
value = e.target.value = null;
} else {
value = +e.target.value;
}
}
this.updateCurrentFormObjectProperty(objPath, value);
}
},
formPageChange: function (e) {
if (e.target.classList.contains("form-select")) {
this.updateCurrentFormObjectProperty(e.target.name, e.target.value);
}
},
formPageAddItem: async function (el) {
const path = el.name;
const typePath = path.replace(/\.\d+\./gi, ".0.");
// add to json
const schemaObj = this.findInObjectByKey(typePath, this.currentForm.schema, true);
const formObj = this.parseJsonSchema(structuredClone(schemaObj)); // convert type values to null values
const currentFormObj = this.findInObjectByKey(path, this.currentForm.form);
currentFormObj.push(formObj);
// add form elements
const nameParts = path.split(".");
const propertyName = this.getFormFormattedHeading(nameParts[nameParts.length - 1]);
const parentFormListEl = el.closest(".form-list");
const parentFormListContentEl = parentFormListEl.querySelector(".form-list-content");
const formListItemIndex = parentFormListContentEl.children.length.toString();
this.createFormElementsFromObj({ [formListItemIndex]: formObj },
{ "0": schemaObj }, parentFormListContentEl, path, {
isArray: true,
name: propertyName,
element: parentFormListEl
});
this.currentForm.foldedStates = this.getFoldedStates();
await this.saveCurrentFormToDb();
this.updateFormDebug();
},
formPageDeleteItem: async function (el) {
if (this.confirmDelete && !confirm(this.i18n.deleteFormItemPrompt)) return;
const path = el.name.substring(0, el.name.lastIndexOf("."));
// remove from json
const itemsContainerEl = el.closest(".form-list");
const formListContentEl = itemsContainerEl.querySelector(".form-list-content");
const itemEl = el.closest(".form-list-item");
const itemIndex = Array.from(formListContentEl.children).indexOf(itemEl);
const formObj = this.findInObjectByKey(path, this.currentForm.form);
formObj.splice(itemIndex, 1);
itemEl.remove();
// update headings
for (let i = 0; i < formListContentEl.children.length; i++) {
const labelEl = formListContentEl.children[i].querySelector(".form-list-item-heading > label");
labelEl.textContent = labelEl.textContent.substring(0, labelEl.textContent.lastIndexOf(" ")) + " " + (i + 1);
}
// update form name indexes
this.updateFormElementNameIndexes(formListContentEl);
// get list of file names in use
let fileList = [];
const formFileUploadLabelEls = this.formPageFormEl.querySelectorAll(".form-file-upload-label");
formFileUploadLabelEls.forEach(x => fileList.push(x.textContent));
fileList = Array.from(new Set(fileList));
// delete unused images
for (const key in this.currentForm.files) {
if (!~fileList.indexOf(key)) delete this.currentForm.files[key];
}
this.currentForm.foldedStates = this.getFoldedStates();
await this.saveCurrentFormToDb();
this.updateFormDebug();
},
formPageUploadFile: async function (el) {
// get file as string
const file = await this.loadFile();
if (!file) return;
const fileString = await this.convertFileToString(file);
const fileName = file.name;
const objPath = el.name;
const filenameLabel = el.nextElementSibling;
const openFileButton = filenameLabel.nextElementSibling;
// save form
await this.saveCurrentFormToDb();
// update label and show open file button
filenameLabel.textContent = fileName;
openFileButton.style.display = "inline-block";
// update currentForm object
this.updateCurrentFormObjectProperty(objPath, fileName);
this.currentForm.files[fileName] = fileString;
},
createFormElementsFromObj: function (formObj, schemaObj, parentEl, path, parentInfo) {
for (let formKey in formObj) {
const schemaKey = isNaN(+formKey) ? formKey : "0"; // use first item of array from schema obj
const formValue = formObj[formKey];
const schemaValue = schemaObj[schemaKey];
const isSchemaValuePrimitiveArray = this.isPrimitiveArray(schemaValue);
const currentPath = path ? (`${path}.${formKey}`) : path;
const nextPath = path ? (`${path}.${formKey}`) : formKey;
let template = null;
let nextEl = null;
let isArray = false;
let currentPropertyName = null;
if (!path) path = "";
// form-list-item-template
if (parentInfo && parentInfo.isArray) {
const labelNumber = parentInfo.element.querySelector(".form-list-content").children.length + 1;
const templateListItemEl = this.getTemplate("form-list-item-template");
templateListItemEl.querySelector("label").textContent = `${parentInfo.name} ${labelNumber}`;
templateListItemEl.querySelector(".form-delete-item-button").name = currentPath;
parentInfo.element.querySelector(".form-list-content").appendChild(templateListItemEl);
nextEl = templateListItemEl.querySelector(".form-list-item-content");
// form-list-template
} else if (Array.isArray(schemaValue) && !isSchemaValuePrimitiveArray) {
isArray = true;
currentPropertyName = this.getFormFormattedHeading(formKey);
template = this.getTemplate("form-list-template");
template.querySelector("label").textContent = currentPropertyName;
template.querySelector(".form-add-item-button").name = nextPath;
// form-heading-template
} else if (typeof schemaValue === "object" && !isSchemaValuePrimitiveArray) {
if (isNaN(+formKey)) {
template = this.getTemplate("form-heading-template");
template.querySelector("label").textContent = this.getFormFormattedHeading(formKey);
}
// text-input-template
} else if (schemaValue === "%string%") {
template = this.getTemplate("text-input-template");
template.querySelector("label").textContent = this.getFormFormattedHeading(formKey, false);
const inputEl = template.querySelector("input");
inputEl.name = currentPath;
inputEl.value = formValue;
// zip code uses a different kind of autocomplete to disable autofill properly
if (formKey.match(/zip|post ?code/i)) inputEl.setAttribute("autocomplete", "no");
// number-input-template
} else if (schemaValue === "%number%") {
template = this.getTemplate("number-input-template");
template.querySelector("label").textContent = this.getFormFormattedHeading(formKey, false);
const inputEl = template.querySelector("input");
inputEl.name = currentPath;
inputEl.value = (formValue !== null && formValue !== undefined) ? +formValue : "";
// file-upload-template
} else if (schemaValue === "%file%") {
template = this.getTemplate("file-upload-template");
const buttonEl = template.querySelector(".form-file-upload-button");
buttonEl.name = currentPath;
buttonEl.previousElementSibling.textContent = this.getFormFormattedHeading(formKey.replace("_file", ""), false);
buttonEl.nextElementSibling.textContent = formValue || this.i18n.selectFileButtonLabel;
buttonEl.nextElementSibling.nextElementSibling.style.display = formValue ? "inline-block" : "none";
// dropdown-template
} else if (isSchemaValuePrimitiveArray) {
template = this.getTemplate("dropdown-template");
template.querySelector("label").textContent = this.getFormFormattedHeading(formKey, false);
const select = template.querySelector("select");
select.name = currentPath;
const options = schemaValue.map(x => `<option value="${x}">${x}</option>`).join("\n");
select.insertAdjacentHTML("afterbegin", options);
select.value = formValue;
}
if (template) parentEl.appendChild(template);
// go to next item
if (!isSchemaValuePrimitiveArray && (Array.isArray(schemaValue) || typeof schemaValue === "object")) {
this.createFormElementsFromObj(formValue, schemaValue, nextEl || parentEl, nextPath, {
isArray: isArray,
name: currentPropertyName,
element: template
});
}
}
},
getFormFormattedHeading: function (text, upperCaseFirst=true) {
let result = text.replace(/([A-Z])/g, " $1");
result = result.replace("_", " ");
result = result.toLowerCase();
result = result.trim();
if(upperCaseFirst){
result = result.charAt(0).toUpperCase() + result.slice(1);
}
console.log("Converted " + text + " to " + result);
return result;
},
updateCurrentFormObjectProperty: async function (objPath, value) {
// This updates the value of a single property of the forms json object
// it updates a property like this
// locations.0.air sample.yeast_fungi.2.species
// not like this
// locations.0.air sample.yeast_fungi.2
const path = objPath.substring(0, objPath.lastIndexOf("."));
const prop = objPath.substring(objPath.lastIndexOf(".") + 1);
const obj = this.findInObjectByKey(path, this.currentForm.form);
obj[prop] = value;
await this.saveCurrentFormToDb();
this.updateFormDebug();
},
getFoldedStates: function () {
const foldedStates = [];
this.formPageFormEl.querySelectorAll(".form-list-item").forEach(x => {
foldedStates.push(x.classList.contains("folded") ? 1 : 0);
});
return foldedStates;
},
applyFoldedStates: function (foldedStates) {
if (!foldedStates) return;
this.formPageFormEl.querySelectorAll(".form-list-item").forEach((x, i) => {
if (foldedStates[i]) x.classList.add("folded");
});
},
updateFormElementNameIndexes: function (rootEl) {
// this recursively updates various html name attributes that are used to identify
// which html element is related to which json property. So when an element is
// deleted a html name attribute might go from locations.1.air sample.yeast_fungi.2, to
// locations.0.air sample.yeast_fungi.2, if the first locations array item was deleted.
// Check the name attributes of say the Delete buttons on the forms page in the
// browser devtools if it's not clear, you should see a name attribute similar to
// locations.0.air sample.yeast_fungi.2, depending on the current jsonSchema value
Array.from(rootEl.children).forEach(el => {
if (el.name) {
const indexes = [];
// count how many list items deep the current item is and add to indexes array
let count = 0;
let parentFormListItem = el.closest(".form-list-item");
let parentFormListContent = el.closest(".form-list-content");
while (parentFormListContent) {
if (parentFormListContent) {
indexes.push(Array.from(parentFormListContent.children).indexOf(parentFormListItem));
}
parentFormListItem = parentFormListContent.parentElement.closest(".form-list-item");
parentFormListContent = parentFormListContent.parentElement.closest(".form-list-content");
count++;
if (count > 100) { // artificial count to detect infinite loop if it ever happens
alert("Error updating element name indexes");
throw "Infinite loop in updateFormElementNameIndexes";
}
}
// update the indexes in the elements name, e.g. the numbers 1 and 2 here
// in this example: locations.1.air sample.yeast_fungi.2
if (indexes.length) {
indexes.reverse();
let newName = [];
let nameCount = 0;
el.name.split(".").forEach((x, i) => {
if (isNaN(+x)) {
newName.push(x);
} else {
newName.push(indexes[nameCount++]);
}
});
el.name = newName.join(".");
}
}
if (el.children) {
this.updateFormElementNameIndexes(el);
}
});
},
showImageDialog: function (el) {
// shows the selected image in a popup dialog
const imageName = el.previousElementSibling.textContent;
const imgContent = this.currentForm.files[imageName];
this.formPageImageDialogImageEl.src = imgContent;
this.formPageImageDialogEl.style.display = "block";
},
hideImageDialog: function () {
this.formPageImageDialogImageEl.src = "";
this.formPageImageDialogEl.style.display = "none";
},
//#endregion
//#region Database
saveCurrentFormToDb: async function () {
await this.saveToDb(this.currentForm.name, this.currentForm);
},
loadKeysFromDb: async function () {
return await idbKeyval.keys();
},
loadFromDb: async function (key) {
return await idbKeyval.get(key);
},
saveToDb: function (key, data) {
return new Promise(resolve => {
if (this.debounceTimeout) clearTimeout(this.debounceTimeout);
this.debounceTimeout = setTimeout(async () => {
await idbKeyval.set(key, data);
resolve();
}, 1000);
});
},
deleteFromDb: async function (key) {
return await idbKeyval.del(key);
},
//#endregion
//#region Files
importZipFile: async function () {
// load file
const file = await this.loadFile(".zip");
if (!file) return false;
// get files in zip file. Example:
// https://github.com/gildas-lormeau/zip.js/blob/2df1c48acff9ee87ee9d8dce0fe738103ca84796/tests/all/test-parallel-reads.js#L21
const zipReader = new zip.ZipReader(new zip.BlobReader(file));
const entries = await zipReader.getEntries();
// data from zip file is loaded into this object
const loadedForm = this.getEmptyFormObject();
// load individual files from zip file
await Promise.all(entries.map(async entry => {
const parts = entry.filename.split("/");
const fileName = parts[parts.length - 1];
const mimeType = this.getMimeType(fileName);
// form
if (fileName === "form.json") {
loadedForm.form = JSON.parse(await entry.getData(new zip.TextWriter(mimeType)));
// metadata
} else if (fileName === "metadata.json") {
const metadata = JSON.parse(await entry.getData(new zip.TextWriter(mimeType)));
loadedForm.schema = metadata.schema;
loadedForm.created = metadata.created;
loadedForm.name = metadata.name;
loadedForm.foldedStates = metadata.foldedStates;
// images and other files
} else {
loadedForm.files[fileName] = await entry.getData(new zip.Data64URIWriter(mimeType));
}
}));
await zipReader.close();
// check if a form with the same name already exists
const existingFormNames = await this.loadKeysFromDb();
if (~existingFormNames.indexOf(loadedForm.name)) {
if (!confirm(this.i18n.overwriteExistingForm.replace("${}", loadedForm.name))) return;
}
// save form to database
await this.saveToDb(loadedForm.name, loadedForm);
return true;
},
exportZipFile: async function (fileName, formObj) {
// get form from db
if (!formObj) {
formObj = await this.loadFromDb(fileName);
}
// zip file
const name = formObj.name;
const zipWriter = new zip.ZipWriter(new zip.BlobWriter("application/zip"));
await Promise.all([
// add form
zipWriter.add(name + "/form.json", new zip.TextReader(JSON.stringify(formObj.form, null, 2))),
// add schema, name and created time to a single file
zipWriter.add(name + "/metadata.json", new zip.TextReader(JSON.stringify({
name: name,
created: formObj.created,
schema: formObj.schema,
foldedStates: formObj.foldedStates
}, null, 2))),
// images and other files
...Object.entries(formObj.files).map(([key, value]) =>
zipWriter.add(name + "/" + key, new zip.Data64URIReader(value)))
]);
const zipFile = await zipWriter.close();
// save file
this.saveFile(fileName + ".zip", zipFile);
},
loadFile: function (fileType) {
// load a file from disk
return new Promise(resolve => {
// remove any existing temporary hidden file inputs. They might get
// orphaned if user cancels file select dialog so this cleans them up
const existing = document.querySelectorAll(".temporary-hidden-file-input");
Array.from(existing).forEach(x => x.remove());
// ask user to choose file
// this creates a temporary file type input
const input = document.createElement("input");
input.type = "file";
input.classList.add("temporary-hidden-file-input");
if (fileType) input.accept = fileType;
input.style.display = "none";
document.body.appendChild(input);
input.addEventListener("change", async () => {
// check file was selected
if (!input.files || !input.files.length) {
input.remove();
return resolve();
}
// check file has valid extension
const fileName = input.files[0].name;
if (fileType) {
if (!fileName.endsWith(fileType)) {
input.remove();
alert(this.i18n.importFileTypeError.replace("${}", fileType));
return resolve();
}
}
input.remove();
return resolve(input.files[0]);
});
input.click();
});
},
saveFile: function (fileName, file) {
// save a file to disk
// this creates a temporary anchor element
const a = document.createElement("a");
document.body.appendChild(a);
a.style = "display: none";
const url = window.URL.createObjectURL(file);
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
a.remove();
},
convertFileToString: function (file) {
// read a file into a base64 string
return new Promise(resolve => {
const reader = new FileReader();
reader.addEventListener("load", async () => {
resolve(reader.result);
});
reader.readAsDataURL(file);
});
},
getMimeType: function (fileName) {
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
if (fileName.endsWith(".jpg")) {
return "image/jpeg";
} else if (fileName.endsWith(".jpeg")) {
return "image/jpeg";
} else if (fileName.endsWith(".png")) {
return "image/png";
} else if (fileName.endsWith(".bmp")) {
return "image/bmp";
} else if (fileName.endsWith(".webp")) {
return "image/webp";
} else if (fileName.endsWith(".gif")) {
return "image/gif";
} else if (fileName.endsWith(".heic")) {
return "image/heic";
} else if (fileName.endsWith(".json")) {
return "application/json";
} else {
return "application/octet-stream";
}
},
//#endregion
//#region Other
getEmptyFormObject() {
// returns a new form object
return {
name: "",
created: "",
schema: {},
form: {},
files: {},
foldedStates: []
};
},
getNewFormObject: function (formName) {
// returns a default new form object
const formObj = this.getEmptyFormObject();
formObj.name = formName;
formObj.created = this.getFormattedDateTime();
formObj.schema = this.parseJsonSchema(structuredClone(this.getJsonSchema()), true);
formObj.form = this.parseJsonSchema(structuredClone(this.getJsonSchema()));
formObj.foldedStates = [];
return formObj;
},
parseJsonSchema: function (jsonObj, setTypes) {
// Parses user provided json schema into an object with property values replaced
// with type names or null values. The output with types is the schema, the output
// with null values is an empty form object for when the user creates a form
for (let key in jsonObj) {
if (!key) continue;
const value = jsonObj[key];
const isValueAPrimitiveArray = this.isPrimitiveArray(value);
if (Array.isArray(value)) {
if (isValueAPrimitiveArray && !setTypes) {
jsonObj[key] = value[0];
} else if (!isValueAPrimitiveArray && setTypes) {
// remove additional array items so there's only 1, only for type schema.
// When new form itmes are added, only one schema item is required to copy from
jsonObj[key] = value.slice(0, 1);
}
// string type (might be a file type too if using a file keyword)
} else if (typeof value === "string") {
if (key.includes("image") || key.includes("file") || key.includes("afbeelding")) {
jsonObj[key] = setTypes ? "%file%" : null;
} else {
jsonObj[key] = setTypes ? "%string%" : null;
}
// number type
} else if (typeof value === "number") {
jsonObj[key] = setTypes ? "%number%" : null;
}
if (!isValueAPrimitiveArray && (Array.isArray(value) || typeof value === "object")) {
this.parseJsonSchema(value, setTypes);
}
}
return jsonObj;
},
findInObjectByKey: function (path, objToSearch, isArrayResult) {
// "path" here is the value of the name attribute from one of the html
// elements, for example: locations.0.air sample.yeast_fungi.2.species
const pathParts = path.split(".");