-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathconfigurator.js
1339 lines (1130 loc) · 44.2 KB
/
configurator.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
'use strict';
/* global output, policies, policymanager */
const DOWNLOAD_PERMISSION = { permissions : ['downloads'] };
const FILTER_ANIMATION_DELAY_IN_MS = 1500;
const elActionLinks = document.getElementById('action-links');
const elDownloadLink = document.getElementById('download');
const elGrantDownloadPermissionLink = document.getElementById('grant-download-permission');
const elPolicyGeneratorButton = document.getElementById('generate');
const elPolicyOutput = document.getElementById('policy-output');
const elSelectAllLink = document.getElementById('select-all');
/**
* @exports configurator
*/
const configurator = {
/**
* Contains the UI categories.
*
* @type {Array.<string>}
*/
uiCategoryElements : [],
/**
* The init() method. Defines UI categories, adds policies to UI, setup all the event listeners.
*
* @param {boolean} unserializeStep - (optional) whether the extra step for the unserializer should be executed or not
*
* @returns {void}
*/
init (unserializeStep) {
// define ui categories
const categories = [
'block-access', 'disable-features', 'customization', 'network', 'privacy', 'security',
'updates-and-data', 'others'
];
const categoriesLength = categories.length;
for (let i = 0; i < categoriesLength; i++) {
// remove all policies from generator so that it can be re-added (used by unserializer)
if (unserializeStep) {
const node = document.getElementById('options-' + categories[i]);
while (node.firstChild) {
node.removeChild(node.firstChild);
}
}
configurator.uiCategoryElements[categories[i]] = document.getElementById('options-' + categories[i]);
}
// iterate over polices from policies.js and call the appropriate option type for each policy
for (const key in policies) {
if (policies[key].type === 'array') {
configurator.addArrayOption(key, policies[key]);
}
else if (policies[key].type === 'boolean') {
configurator.addBooleanOption(key, policies[key]);
}
else if (policies[key].type === 'boolean-inverse') {
configurator.addBooleanOption(key, policies[key], true);
}
else if (policies[key].type === 'enum') {
configurator.addEnumOption(key, policies[key]);
}
else if (policies[key].type === 'flat-array') {
configurator.addFlatArrayOption(key, policies[key]);
}
else if (policies[key].type === 'key-value-pairs') {
configurator.addKeyValuePairsOption(key, policies[key]);
}
else if (policies[key].type === 'object') {
configurator.addObjectOption(key, policies[key]);
}
else if (policies[key].type === 'string') {
configurator.addStringOption(key, policies[key], false);
}
else if (policies[key].type === 'url') {
configurator.addStringOption(key, policies[key], true);
}
}
// test if the download permission is granted or not
configurator.testDownloadPermission();
// show suboptions for enabled policies and hide suboptions for disabled policies
[...document.querySelectorAll('.primary-checkbox')].forEach((el) => {
el.addEventListener('change', () => {
const elSubOptions = el.parentNode.getElementsByClassName('sub-options');
if (elSubOptions.length > 0) {
elSubOptions[0].classList.toggle('disabled');
// set focus to first input or select field after a policy checkbox has been checked
if (!elSubOptions[0].classList.contains('disabled')) {
const firstInputField = elSubOptions[0].querySelector('input[type=text], input[type=url], select');
if (firstInputField) {
firstInputField.focus();
}
}
}
});
});
// add event listener for array field actions (plus / minus icons)
[...document.querySelectorAll('.array-action')].forEach((el) => {
el.addEventListener('click', configurator.executeArrayFieldActions);
});
// add event listener for mandatory field validation
[...document.querySelectorAll('input[data-mandatory]')].forEach((el) => {
el.addEventListener('input', configurator.validateMandatoryFields);
});
// add event listener for URL field validation
[...document.querySelectorAll('input[type=url]')].forEach((el) => {
el.addEventListener('input', configurator.validateUrlFields);
});
// action for clicking the "generate policies" button
elPolicyGeneratorButton.onclick = (e) => {
e.preventDefault();
elPolicyOutput.innerText = output.generatePoliciesOutput();
elActionLinks.classList.remove('hidden');
};
// action for clicking the "select all" link
elSelectAllLink.onclick = (e) => {
e.preventDefault();
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(elPolicyOutput);
selection.removeAllRanges();
selection.addRange(range);
};
// action for clicking the "download policies.json" link if downloads permission is not granted
elGrantDownloadPermissionLink.onclick = async (e) => {
e.preventDefault();
const granted = await browser.permissions.request(DOWNLOAD_PERMISSION);
// immediately prompt for download after the downloads permission has been granted
if (granted) {
configurator.downloadPolicy();
}
};
// action for clicking the "download policies.json" link if downloads permission is granted
elDownloadLink.onclick = (e) => {
e.preventDefault();
configurator.downloadPolicy();
};
// filter field
configurator.filterfield();
},
/**
* Tests if the downloads permission has been granted or not. If granted, the link for granting the permission
* will be hidden and the real download link will be shown.
*
* @returns {void}
*/
async testDownloadPermission () {
const granted = await browser.permissions.contains(DOWNLOAD_PERMISSION);
// if the downloads permission is granted hide the link for granting permission and show the
// real download link instead
if (granted) {
elGrantDownloadPermissionLink.classList.add('hidden');
elDownloadLink.classList.remove('hidden');
}
},
/**
* Method for downloading the "policies.json" file.
*
* @returns {void}
*/
downloadPolicy () {
browser.downloads.download({
saveAs : true,
url : URL.createObjectURL(new Blob([elPolicyOutput.innerText])),
filename : 'policies.json'
});
},
/**
* Executes the add and remove actions for array fields.
*
* @param {MouseEvent} e - event
*
* @returns {void}
*/
executeArrayFieldActions (e) {
e.preventDefault();
switch (e.target.getAttribute('data-action')) {
case 'add':
configurator.addArrayField(e.target);
break;
case 'remove':
configurator.removeArrayField(e.target);
break;
default:
// do nothing
}
},
/**
* Executes the add action for array fields, called by executeArrayFieldActions().
*
* @param {HTMLElement} el - the DOM element of the array field which should be duplicated
* @param {string} key - (optional) the array field index which should be used for DOM IDs and names
* @param {number} overrideCountValue - (optional) use this value for the data-count attribute
*
* @returns {void}
*/
addArrayField (el, key, overrideCountValue) {
// if the key parameter is used, this method is called by the unserializer. Let's make sure that we append new
// items always to the end to preserve the original order
if (key) {
const closest = el.closest('.array-action-links');
if (closest) {
const arrayActionElements = closest.getElementsByClassName('array-action');
// eslint-disable-next-line no-param-reassign
el = arrayActionElements[arrayActionElements.length - 1];
}
}
// after adding a new array item the remove link of the first one shouldn't be disabled
if (el.parentNode.parentNode.querySelector('.disabled-link')) {
el.parentNode.parentNode.querySelector('.disabled-link').classList.remove('disabled-link');
}
// increment array field counter
const count = overrideCountValue ? parseInt(overrideCountValue) : parseInt(el.getAttribute('data-count')) + 1;
el.closest('.checkbox').querySelectorAll('[data-count]').forEach((el) => {
el.setAttribute('data-count', count);
});
// copy the array item
const addedNode = el.parentNode.cloneNode(true);
// we want an empty input field for the copied array item, we also need a new DOM ID
addedNode.querySelectorAll('input').forEach((el) => {
const id = el.id.replace(/^(\w+)_(\d+)$/i, (fullMatch, name) => name + '_' + (key ? key : count));
el.value = '';
el.setAttribute('id', id);
el.setAttribute('name', id);
});
// for select fields we also need a new DOM ID
addedNode.querySelectorAll('select').forEach((el) => {
const id = el.id.replace(/^(\w+)_(\d+)$/i, (fullMatch, name) => name + '_' + (key ? key : count));
el.setAttribute('id', id);
el.setAttribute('name', id);
});
// we have to re-add our event listener for executing the array field actions
addedNode.querySelectorAll('a').forEach((el) => {
const id = el.id.replace(/^(\w+)_(\d+)$/i, (fullMatch, name) => name + '_' + (key ? key : count));
el.setAttribute('id', id);
el.addEventListener('click', configurator.executeArrayFieldActions);
});
// we also have to re-add the event listener for the validation of mandatory fields
addedNode.querySelectorAll('input[data-mandatory]').forEach((el) => {
el.addEventListener('input', configurator.validateMandatoryFields);
el.classList.add('mandatory-style');
el.parentNode.querySelector('.mandatory-label').classList.remove('hidden');
});
// we also have to re-add the event listener for the validation of URL fields
addedNode.querySelectorAll('input[type=url]').forEach((el) => {
el.addEventListener('input', configurator.validateUrlFields);
el.parentNode.querySelector('.invalid-url-label').classList.add('hidden');
});
// add the copied array item to the DOM
el.parentNode.after(addedNode);
// set focus to first input or select field of the newly added array field
const firstInputField = addedNode.querySelector('input[type=text], input[type=url], select');
if (firstInputField) {
firstInputField.focus();
}
},
/**
* Executes the remove action for array fields, called by executeArrayFieldActions().
*
* @param {HTMLElement} el - the DOM element of the array field which should be removed
*
* @returns {void}
*/
removeArrayField (el) {
// different object types have different DOM structures, we need to know the number of total array items
const elGrandParent = el.parentNode.parentNode;
const isObjectArray = elGrandParent.classList.contains('object-array');
const hasSubOptions = elGrandParent.querySelectorAll('.sub-options').length > 0;
let newLength = 0;
// object-array property of object type
if (isObjectArray) {
newLength = elGrandParent.querySelectorAll(':scope > div').length - 2;
}
// array type
else if (hasSubOptions) {
newLength = elGrandParent.querySelectorAll('.sub-options').length - 1;
}
// array property of object type
else {
newLength = elGrandParent.querySelectorAll('.input').length - 1;
}
// remove the array item from the DOM
if (!el.classList.contains('disabled-link')) {
// set focus to previous input or select field
const previousField = el.parentNode.previousSibling.querySelector('input[type=text], input[type=url], select');
if (previousField) {
previousField.focus();
}
el.parentNode.remove();
}
// if there is only one array item after removing another array item the remove link should be disabled
if (newLength === 1) {
elGrandParent.querySelector('[data-action="remove"]').classList.add('disabled-link');
}
},
/**
* Validation for mandatory fields.
*
* @param {InputEvent} e - event
*
* @returns {void}
*/
validateMandatoryFields (e) {
// the mandatory field has a value, hide visual indication
if (e.target.value) {
e.target.classList.remove('mandatory-style');
e.target.parentNode.querySelector('.mandatory-label').classList.add('hidden');
}
// the mandatory field is empty, add visual indication
else {
e.target.classList.add('mandatory-style');
e.target.parentNode.querySelector('.mandatory-label').classList.remove('hidden');
}
},
/**
* Validation for URL fields.
*
* @param {InputEvent} e - event
*
* @returns {void}
*/
validateUrlFields (e) {
// the URL field has a valid URL, hide visual indication
if (!e.target.value || configurator.isValidURL(e.target.value)) {
e.target.classList.remove('invalid-url-style');
e.target.parentNode.querySelector('.invalid-url-label').classList.add('hidden');
}
// the URL field has not a valid URL, add visual indication
else {
e.target.classList.add('invalid-url-style');
e.target.parentNode.querySelector('.invalid-url-label').classList.remove('hidden');
}
},
/**
* Tests if a given string is a valid URL.
*
* @param {string} string - the string to check
*
* @returns {boolean} - whether the given string is a valid URL or not
*/
isValidURL (string) {
const pattern = new RegExp(/^https?:\/\//, 'gi');
return pattern.test(encodeURI(string));
},
/**
* Appends policy element to the appropriate UI category in the DOM.
*
* @param {HTMLElement} el - the DOM element
* @param {string} category - the name of the category
*
* @returns {void}
*/
addOptionToUi (el, category) {
configurator.uiCategoryElements[category].appendChild(el);
},
/**
* Adds policy of the type "array" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addArrayOption (key, policy) {
const elObjectWrapper = configurator.addPolicyNode(key, policy, 'array');
const elSubOptions = configurator.addSubOptions(elObjectWrapper);
// add array properties
const optionsLength = policy.items.length;
for (let i = 0; i < optionsLength; i++) {
configurator.addProperty(elSubOptions, key + '_' + policy.items[i].name, policy.items[i], true, true);
}
// add array field action links
elSubOptions.parentNode.classList.add('array-action-links');
configurator.addArrayFieldActionLinks(elSubOptions, key + '_1');
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds policy of the type "boolean" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
* @param {boolean} inverse - if true, the value for the policy will be false instead of true
*
* @returns {void}
*/
addBooleanOption (key, policy, inverse) {
const elObjectWrapper = configurator.addPolicyNode(key, policy, 'boolean', inverse);
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds policy of the type "enum" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addEnumOption (key, policy) {
const elObjectWrapper = configurator.addPolicyNode(key, policy, 'enum');
const elSelectWrapper = document.createElement('div');
elSelectWrapper.classList.add('enum', 'sub-options', 'select-wrapper', 'disabled');
// label
if (policy.label) {
configurator.addSelectLabel(elSelectWrapper, key + '_select', policy);
}
// add options to select element
const elSelect = document.createElement('select');
elSelect.setAttribute('id', key + '_Select');
elSelect.setAttribute('name', key + '_Select');
elSelect.setAttribute('data-name', key);
elSelectWrapper.appendChild(elSelect);
elObjectWrapper.appendChild(elSelectWrapper);
const optionsLength = policy.options.length;
for (let i = 0; i < optionsLength; i++) {
const elOptionLabel = document.createTextNode(policy.options[i].label);
const elOption = document.createElement('option');
elOption.setAttribute('value', policy.options[i].value);
elOption.appendChild(elOptionLabel);
if (policy.options[i].value === policy.default) {
elOption.setAttribute('selected', 'selected');
}
elSelect.appendChild(elOption);
}
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds policy of the type "flat-array" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addFlatArrayOption (key, policy) {
const elObjectWrapper = configurator.addPolicyNode(key, policy, 'flat-array');
const elSubOptions = configurator.addSubOptions(elObjectWrapper);
const elInputWrapper = document.createElement('div');
elInputWrapper.classList.add('input');
const elInput = document.createElement('input');
elInput.setAttribute('type', 'text');
elInput.setAttribute('id', key + '_Value_1');
elInput.setAttribute('name', key + '_Value_1');
elInput.setAttribute('data-name', key);
elInput.setAttribute('placeholder', policy.value.label);
// mandatory field
if (policy.value.mandatory) {
configurator.addMandatoryLabel(elInput, elSubOptions);
}
elSubOptions.appendChild(elInput);
// add array field action links
elSubOptions.parentNode.classList.add('array-action-links');
configurator.addArrayFieldActionLinks(elSubOptions, key + '_1');
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds policy of the type "key-value-pairs" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addKeyValuePairsOption (key, policy) {
const elObjectWrapper = configurator.addPolicyNode(key, policy, 'key-value-pairs');
const elSubOptions = configurator.addSubOptions(elObjectWrapper);
const elInputWrapperKey = document.createElement('div');
elInputWrapperKey.classList.add('input');
const elInputKey = document.createElement('input');
elInputKey.setAttribute('type', 'text');
elInputKey.setAttribute('id', key + '_Key_Value_1');
elInputKey.setAttribute('name', key + '_Key_Value_1');
elInputKey.setAttribute('data-name', key);
elInputKey.setAttribute('placeholder', policy.label_key);
elInputKey.classList.add('key');
configurator.addMandatoryLabel(elInputKey, elInputWrapperKey);
elInputWrapperKey.appendChild(elInputKey);
elSubOptions.appendChild(elInputWrapperKey);
const elInputWrapperValue = document.createElement('div');
elInputWrapperValue.classList.add('input');
const elInputValue = document.createElement('input');
elInputValue.setAttribute('type', 'text');
elInputValue.setAttribute('id', key + '_Value_Value_1');
elInputValue.setAttribute('name', key + '_Value_Value_1');
elInputValue.setAttribute('data-name', key);
elInputValue.setAttribute('placeholder', policy.label_value);
elInputValue.classList.add('value');
configurator.addMandatoryLabel(elInputValue, elInputWrapperValue);
elInputWrapperValue.appendChild(elInputValue);
elSubOptions.appendChild(elInputWrapperValue);
// add array field action links
elSubOptions.parentNode.classList.add('array-action-links');
configurator.addArrayFieldActionLinks(elSubOptions, key + '_1');
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds policy of the type "object" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addObjectOption (key, policy) {
const elObjectWrapper = configurator.addPolicyNode(key, policy, 'object');
const elSubOptions = configurator.addSubOptions(elObjectWrapper);
// policy can be locked
if (policy.is_lockable) {
configurator.addLockableLink(elSubOptions, key);
}
// add properties
if (policy.properties) {
const optionsLength = policy.properties.length;
for (let i = 0; i < optionsLength; i++) {
configurator.addProperty(elSubOptions, key, policy.properties[i]);
}
}
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds policy of the type "string" to the DOM.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
* @param {boolean} isUrl - if true, the option is of the type "url", otherwise it's of the type "string"
*
* @returns {void}
*/
addStringOption (key, policy, isUrl) {
const type = isUrl ? 'url' : 'string';
const elObjectWrapper = configurator.addPolicyNode(key, policy, type);
const elSubOptions = configurator.addSubOptions(elObjectWrapper);
// input field
const elInputWrapper = document.createElement('div');
elInputWrapper.classList.add('input');
const elInput = document.createElement('input');
if (isUrl) {
elInput.setAttribute('type', 'url');
}
else {
elInput.setAttribute('type', 'text');
}
elInput.setAttribute('id', key + '_Text');
elInput.setAttribute('name', key + '_Text');
elInput.setAttribute('data-name', key);
elInput.setAttribute('placeholder', policy.label);
// mandatory field
if (policy.mandatory) {
configurator.addMandatoryLabel(elInput, elSubOptions);
}
// URL validation label
if (isUrl) {
configurator.addInvalidUrlLabel(elSubOptions);
}
elSubOptions.appendChild(elInput);
// add option to UI
configurator.addOptionToUi(elObjectWrapper, policy.ui_category);
},
/**
* Adds a property to a policy field, calls the appropriate method.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
* @param {boolean} isArrayProperty - whether this call is within an array field or not
* @param {boolean} hideArrayActionLinks - whether this is an array item but no action links should be added
*
* @returns {void}
*/
addProperty (el, parentName, policy, isArrayProperty, hideArrayActionLinks) {
switch (policy.type) {
case 'array':
configurator.addArrayProperty(el, parentName, policy);
break;
case 'boolean':
configurator.addBooleanProperty(el, parentName, policy);
break;
case 'enum':
configurator.addEnumProperty(el, parentName, policy, isArrayProperty);
break;
case 'object':
configurator.addObjectProperty(el, parentName, policy);
break;
case 'object-array':
configurator.addObjectArrayProperty(el, parentName, policy);
break;
case 'string':
configurator.addStringProperty(el, parentName, policy, false, isArrayProperty, hideArrayActionLinks);
break;
case 'url':
configurator.addStringProperty(el, parentName, policy, true, isArrayProperty, hideArrayActionLinks);
break;
default:
// do nothing
}
},
/**
* Adds property of the type "array" to a policy.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addArrayProperty (el, parentName, policy) {
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('array');
elObjectWrapper.setAttribute('data-name', policy.name);
const elCaptionWrapper = document.createElement('div');
elCaptionWrapper.classList.add('label');
elObjectWrapper.appendChild(elCaptionWrapper);
const elCaption = document.createTextNode(policy.label);
elCaptionWrapper.appendChild(elCaption);
// add array items
if (policy.items) {
configurator.addProperty(elObjectWrapper, parentName + '_' + policy.name, policy.items, true);
}
el.appendChild(elObjectWrapper);
},
/**
* Adds property of the type "boolean" to a policy.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addBooleanProperty (el, parentName, policy) {
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('checkbox');
// property name
const name = parentName ? parentName + '_' + policy.name : policy.name;
// checkbox
const elInput = document.createElement('input');
elInput.setAttribute('type', 'checkbox');
elInput.setAttribute('id', name);
elInput.setAttribute('name', name);
elInput.setAttribute('data-name', policy.name);
elInput.classList.add('property-checkbox');
// mandatory field
if (policy.mandatory) {
configurator.addMandatoryLabel(elInput, elObjectWrapper);
}
elObjectWrapper.appendChild(elInput);
// label
const elLabel = document.createElement('label');
elLabel.setAttribute('for', name);
elLabel.textContent = policy.label;
elObjectWrapper.appendChild(elLabel);
el.appendChild(elObjectWrapper);
},
/**
* Adds property of the type "enum" to a policy.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
* @param {boolean} isArrayProperty - whether this call is within an array field or not
*
* @returns {void}
*/
addEnumProperty (el, parentName, policy, isArrayProperty) {
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('enum');
// label
if (policy.label) {
configurator.addSelectLabel(elObjectWrapper, policy.name, policy);
}
// we need an unique and deterministic name as DOM id and name
const domName = isArrayProperty ? parentName + '_1' : parentName + '_' + policy.name;
// add select element
const elSelectWrapper = document.createElement('div');
elSelectWrapper.classList.add('select-wrapper');
elObjectWrapper.appendChild(elSelectWrapper);
const elSelect = document.createElement('select');
elSelect.setAttribute('id', domName);
elSelect.setAttribute('name', domName);
elSelect.setAttribute('data-name', policy.name);
// mandatory field
if (policy.mandatory) {
configurator.addMandatoryLabel(elSelect, elSelectWrapper);
}
// add options to select element
const optionsLength = policy.options.length;
for (let i = 0; i < optionsLength; i++) {
const elOptionLabel = document.createTextNode(policy.options[i].label);
const elOption = document.createElement('option');
elOption.setAttribute('value', policy.options[i].value);
elOption.appendChild(elOptionLabel);
if (policy.options[i].value === policy.default) {
elOption.setAttribute('selected', 'selected');
}
elSelect.appendChild(elOption);
}
elSelectWrapper.appendChild(elSelect);
el.appendChild(elObjectWrapper);
},
/**
* Adds property of the type "object" to a policy.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addObjectProperty (el, parentName, policy) {
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('object');
elObjectWrapper.setAttribute('data-name', policy.name);
// label
const elCaptionWrapper = document.createElement('div');
elCaptionWrapper.classList.add('label');
elObjectWrapper.appendChild(elCaptionWrapper);
const elCaption = document.createTextNode(policy.label);
elCaptionWrapper.appendChild(elCaption);
el.appendChild(elObjectWrapper);
// add properties
const elSubOptions = document.createElement('div');
elObjectWrapper.appendChild(elSubOptions);
// property can be locked
if (policy.is_lockable) {
configurator.addLockableLink(elSubOptions, parentName + '_' + policy.name);
}
const optionsLength = policy.properties.length;
for (let i = 0; i < optionsLength; i++) {
let name = '';
if (policy.properties[i].type === 'boolean') {
name = parentName + '_' + policy.name;
}
else {
name = parentName + '_' + policy.name + '_' + policy.properties[i].name;
}
configurator.addProperty(elSubOptions, name, policy.properties[i]);
}
},
/**
* Adds property of the type "object-array" to a policy.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
*
* @returns {void}
*/
addObjectArrayProperty (el, parentName, policy) {
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('object-array');
elObjectWrapper.setAttribute('data-name', policy.name);
// label
const elCaptionWrapper = document.createElement('div');
elCaptionWrapper.classList.add('label');
elObjectWrapper.appendChild(elCaptionWrapper);
const elCaption = document.createTextNode(policy.label);
elCaptionWrapper.appendChild(elCaption);
el.appendChild(elObjectWrapper);
// add array items
const elSubOptions = document.createElement('div');
elObjectWrapper.appendChild(elSubOptions);
const optionsLength = policy.items.length;
for (let i = 0; i < optionsLength; i++) {
const name = parentName + '_' + policy.name + '_' + policy.items[i].name;
configurator.addProperty(elSubOptions, name, policy.items[i], true, true);
}
// add array field action links
const arrayAddName = parentName + '_' + policy.name + '_1';
elSubOptions.parentNode.classList.add('array-action-links');
configurator.addArrayFieldActionLinks(elSubOptions, arrayAddName);
},
/**
* Adds property of the type "string" or "url" to a policy.
*
* @param {HTMLElement} el - the DOM element of the policy
* @param {string} parentName - the name of the parent policy object
* @param {Object} policy - the policy object
* @param {boolean} isUrl - if true, the property is of the type "url", otherwise it's of the type "string"
* @param {boolean} isArrayProperty - whether this call is within an array field or not
* @param {boolean} hideArrayActionLinks - whether this is an array item but no action links should be added
*
* @returns {void}
*/
addStringProperty (el, parentName, policy, isUrl, isArrayProperty, hideArrayActionLinks) {
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('input');
// we need an unique and deterministic name as DOM id and name
const domName = isArrayProperty ? parentName + '_1' : parentName + '_' + policy.name;
// input field
const elInput = document.createElement('input');
if (isUrl) {
elInput.setAttribute('type', 'url');
}
else {
elInput.setAttribute('type', 'text');
}
elInput.setAttribute('id', domName);
elInput.setAttribute('name', domName);
elInput.setAttribute('placeholder', policy.label);
if (!isArrayProperty || hideArrayActionLinks) {
elInput.setAttribute('data-name', policy.name);
}
// mandatory field
if (policy.mandatory) {
configurator.addMandatoryLabel(elInput, elObjectWrapper);
}
// URL validation label
if (isUrl) {
configurator.addInvalidUrlLabel(elObjectWrapper);
}
elObjectWrapper.appendChild(elInput);
// add array field action links if property of an array
if (isArrayProperty && !hideArrayActionLinks) {
el.classList.add('array-action-links');
configurator.addArrayFieldActionLinks(elObjectWrapper, domName);
}
el.appendChild(elObjectWrapper);
},
/**
* Adds a policy node the DOM. This method generates the common code which is the same for all object types.
*
* @param {string} key - the name of the policy
* @param {Object} policy - the policy object
* @param {string} type - the type of the policy
* @param {boolean} inverse - if true, the value for the policy will be false instead of true
*
* @returns {HTMLElement} - the DOM node containing the policy option
*/
addPolicyNode (key, policy, type, inverse) {
// start node for each policy
const elObjectWrapper = document.createElement('div');
elObjectWrapper.classList.add('checkbox', 'policy-container');
// checkbox
const elCheckbox = document.createElement('input');
elCheckbox.setAttribute('type', 'checkbox');
elCheckbox.setAttribute('id', key);
elCheckbox.setAttribute('name', key);
elCheckbox.setAttribute('data-name', key);
elCheckbox.setAttribute('data-type', type);
elCheckbox.classList.add('primary-checkbox');
// set reverse attribute, can be used for boolean options with false instead of true as value
if (inverse) {
elCheckbox.setAttribute('data-inverse', 'true');
}