-
Notifications
You must be signed in to change notification settings - Fork 224
/
policiesImports.ts
149 lines (120 loc) · 5.02 KB
/
policiesImports.ts
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
import * as ace from 'ace-builds/src-noconflict/ace';
import * as Utils from '../utils.js';
import * as API from '../api.js';
import * as Policies from './policies.js';
import * as PolicyEntries from './policiesEntries.js';
import { CrudOperation, CrudToolbar } from '../utils/crudToolbar.js';
let selectedImport: string;
type DomElements = {
tbodyPolicyImports: HTMLTableElement,
tbodyPolicyImportEntries: HTMLTableElement,
crudImport: CrudToolbar,
}
let dom: DomElements = {
tbodyPolicyImports: null,
tbodyPolicyImportEntries: null,
crudImport: null,
} ;
// let importEditor: ace.Editor;
export function ready() {
Utils.getAllElementsById(dom);
Policies.observable.addChangeListener(onPolicyChanged);
// Utils.addValidatorToTable(dom.tbodyPolicySubjects, dom.tableValidationSubjects);
// importEditor = Utils.createAceEditor('importEditor', 'ace/mode/json');
dom.tbodyPolicyImports.onclick = onPolicyImportsClick;
dom.crudImport.editDisabled = true;
dom.crudImport.addEventListener('onCreateClick', onCreatePolicyImportClick);
dom.crudImport.addEventListener('onUpdateClick', onUpdatePolicyImportClick);
dom.crudImport.addEventListener('onDeleteClick', onDeletePolicyImportClick);
dom.crudImport.addEventListener('onEditToggle', onEditToggleImport);
dom.crudImport.addEventListener('onIdValueChange', onIdValueChange);
}
async function onIdValueChange() {
await setImport(dom.crudImport.idValue);
setExplicitCheckboxesDisabledState(false);
}
function onPolicyImportsClick(event: MouseEvent): any {
const target = event.target as HTMLElement;
if (selectedImport === target.parentElement.id) {
selectedImport = null;
} else {
selectedImport = target.parentElement.id;
}
setImport(selectedImport);
}
function modifyImport(key: string, value: object, onSuccess: (value: any) => any) {
API.callDittoREST(value ? 'PUT' : 'DELETE',
`/policies/${Policies.thePolicy.policyId}/imports/${key}`, value
).then(onSuccess);
};
function onCreatePolicyImportClick() {
selectedImport = dom.crudImport.idValue;
modifyImport(selectedImport, importValueFromCheckboxes(), Policies.finishEditing(dom.crudImport, CrudOperation.CREATE));
}
function onUpdatePolicyImportClick() {
modifyImport(selectedImport, importValueFromCheckboxes(), Policies.finishEditing(dom.crudImport, CrudOperation.UPDATE));
}
function onDeletePolicyImportClick() {
Utils.confirm(`Are you sure you want to delete policy import<br>'${dom.crudImport.idValue}'?`, 'Delete', () => {
modifyImport(selectedImport, null, Policies.finishEditing(dom.crudImport, CrudOperation.DELETE));
});
}
function importValueFromCheckboxes() {
const importValue = {};
const checkedBoxes = document.querySelectorAll('#tbodyPolicyImportEntries input:checked[data-importable="explicit"]');
if (checkedBoxes.length > 0) {
importValue['entries'] = Array.from(checkedBoxes).map((checkbox) => checkbox.id);
}
return importValue;
}
function onEditToggleImport(event: CustomEvent) {
setExplicitCheckboxesDisabledState(!event.detail.isEditing)
if (event.detail.isCancel) {
setImport(selectedImport);
}
}
function setExplicitCheckboxesDisabledState(disabled: boolean) {
document.querySelectorAll('#tbodyPolicyImportEntries input[data-importable="explicit"]')
.forEach((e: HTMLInputElement) => e.disabled = disabled);
}
function onPolicyChanged(policy: Policies.Policy) {
dom.tbodyPolicyImports.innerHTML = '';
dom.crudImport.idValue = null;
dom.crudImport.editDisabled = (policy === null);
if (policy) {
let policyHasImport = false;
Object.keys(policy.imports).forEach((key) => {
Utils.addTableRow(dom.tbodyPolicyImports, key, key === selectedImport);
if (key === selectedImport) {
setImport(key);
policyHasImport = true;
}
});
if (!policyHasImport) {
selectedImport = null;
setImport(null);
}
} else {
setImport(null);
}
}
async function setImport(importedPolicyId: string) {
dom.crudImport.idValue = importedPolicyId;
dom.tbodyPolicyImportEntries.innerHTML = '';
if (importedPolicyId) {
const importedPolicy: Policies.Policy = await API.callDittoREST('GET', '/policies/' + importedPolicyId)
Object.keys(importedPolicy.entries).forEach((entry) => {
const row = dom.tbodyPolicyImportEntries.insertRow();
const checkbox: HTMLInputElement = Utils.addCheckboxToRow(row, entry, isImportedOrImplicit(entry, importedPolicy), true);
checkbox.dataset.importable = importedPolicy.entries[entry].importable;
Utils.addCellToRow(row, entry, JSON.stringify(importedPolicy.entries[entry], null, 4));
Utils.addCellToRow(row, importedPolicy.entries[entry].importable);
});
}
function isImportedOrImplicit(entry: string, importedPolicy: Policies.Policy): boolean {
const policyImport = Policies.thePolicy.imports[importedPolicyId];
const isImported = policyImport && policyImport['entries'] && policyImport.entries.includes(entry);
const isImplicit = importedPolicy.entries[entry].importable === 'implicit';
return isImported || isImplicit;
}
};