/
policiesEntries.ts
127 lines (105 loc) · 3.87 KB
/
policiesEntries.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
import * as Utils from '../utils.js';
import * as API from '../api.js';
import { Observable } from '../utils/observable.js';
import { CrudOperation, CrudToolbar } from '../utils/crudToolbar.js';
import * as Policies from './policies.js';
export let observable = Observable();
export let selectedEntry: string;
type DomElements = {
tbodyPolicyEntries: HTMLTableElement,
tableValidationEntries: HTMLInputElement,
selectImportable: HTMLSelectElement,
crudEntry: CrudToolbar,
}
let dom: DomElements = {
tbodyPolicyEntries: null,
tableValidationEntries: null,
selectImportable: null,
crudEntry: null,
};
export function ready() {
Utils.getAllElementsById(dom);
Policies.observable.addChangeListener(onPolicyChanged);
Utils.addValidatorToTable(dom.tbodyPolicyEntries, dom.tableValidationEntries);
dom.crudEntry.editDisabled = true;
dom.crudEntry.addEventListener('onCreateClick', onCreateEntryClick);
dom.crudEntry.addEventListener('onDeleteClick', onDeleteEntryClick);
dom.crudEntry.addEventListener('onUpdateClick', onUpdateEntryClick);
dom.crudEntry.addEventListener('onEditToggle', onToggleEditEntry);
dom.tbodyPolicyEntries.onclick = onPolicyEntriesClick;
}
function onToggleEditEntry(event) {
dom.selectImportable.disabled = !event.detail.isEditing;
if (event.detail.isCancel) {
updateEditors(selectedEntry);
}
}
function onPolicyEntriesClick(event) {
if (selectedEntry === event.target.textContent) {
selectedEntry = null;
} else {
selectedEntry = event.target.textContent;
}
setEntry(selectedEntry);
}
function onCreateEntryClick() {
// validations(true);
Utils.assert(dom.crudEntry.idValue, 'Please enter a label for the entry', dom.crudEntry.validationElement);
Utils.assert(!Object.keys(Policies.thePolicy.entries).includes(dom.crudEntry.idValue),
`Entry with label ${dom.crudEntry.idValue} already exists`, dom.crudEntry.validationElement);
selectedEntry = dom.crudEntry.idValue;
putOrDeletePolicyEntry(dom.crudEntry.idValue, {
subjects: {},
resources: {},
importable: dom.selectImportable.value
}, Policies.finishEditing(dom.crudEntry, CrudOperation.CREATE));
}
function onUpdateEntryClick() {
Policies.thePolicy.entries[selectedEntry].importable = dom.selectImportable.value;
putOrDeletePolicyEntry(selectedEntry, Policies.thePolicy.entries[selectedEntry], Policies.finishEditing(dom.crudEntry, CrudOperation.UPDATE));
}
function onDeleteEntryClick() {
// validations(false, true);
validateSelected();
Utils.confirm(`Are you sure you want to delete policy entry<br>'${dom.crudEntry.idValue}'?`, 'Delete', () => {
putOrDeletePolicyEntry(selectedEntry, null, Policies.finishEditing(dom.crudEntry, CrudOperation.DELETE));
});
}
function putOrDeletePolicyEntry(entry, value, onSuccess) {
API.callDittoREST(value ? 'PUT' : 'DELETE',
`/policies/${Policies.thePolicy.policyId}/entries/${entry}`,
value
).then(onSuccess);
};
function onPolicyChanged(policy: Policies.Policy) {
dom.tbodyPolicyEntries.innerHTML = '';
dom.crudEntry.idValue = null;
dom.crudEntry.editDisabled = (policy === null);
if (policy) {
let policyHasEntry = false;
Object.keys(policy.entries).forEach((key) => {
Utils.addTableRow(dom.tbodyPolicyEntries, key, key === selectedEntry);
if (key === selectedEntry) {
setEntry(key);
policyHasEntry = true;
}
});
if (!policyHasEntry) {
selectedEntry = null;
setEntry(null);
}
} else {
setEntry(null);
}
}
function setEntry(entryLabel: string) {
updateEditors(entryLabel);
observable.notifyAll(selectedEntry);
};
function updateEditors(entryLabel: string) {
dom.crudEntry.idValue = entryLabel;
dom.selectImportable.value = entryLabel && Policies.thePolicy.entries[entryLabel].importable
}
export function validateSelected() {
Utils.assert(selectedEntry, 'Please select an entry', dom.tableValidationEntries);
}