-
Notifications
You must be signed in to change notification settings - Fork 218
/
policies.ts
182 lines (149 loc) · 5.31 KB
/
policies.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
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
/* eslint-disable comma-dangle */
/* eslint-disable prefer-const */
/* eslint-disable no-invalid-this */
/* eslint-disable require-jsdoc */
import * as Utils from '../utils.js';
import * as API from '../api.js';
import * as Things from '../things/things.js';
import * as Environments from '../environments/environments.js';
import {TabHandler} from '../utils/tabHandler.js';
import policyHTML from './policies.html';
import { Observable } from '../utils/observable.js';
import { CrudOperation, CrudToolbar } from '../utils/crudToolbar.js';
export interface Policy {
policyId: string,
entries: Object,
imports: Object,
}
export let observable = Observable();
export let thePolicy: Policy;
let tabHandler;
type DomElements = {
inputPolicyId: HTMLInputElement,
tbodyRecentPolicies: HTMLTableElement,
tbodyWhoami: HTMLTableElement,
buttonLoadPolicy: HTMLButtonElement,
collapsePolicies: HTMLElement,
tabPolicies: HTMLUListElement,
}
let dom: DomElements = {
inputPolicyId: null,
tbodyRecentPolicies: null,
tbodyWhoami: null,
buttonLoadPolicy: null,
collapsePolicies: null,
tabPolicies: null,
} ;
document.getElementById('policyHTML').innerHTML = policyHTML;
export function ready() {
Environments.addChangeListener(onEnvironmentChanged);
Utils.getAllElementsById(dom);
tabHandler = TabHandler(dom.tabPolicies, dom.collapsePolicies, refreshAll, 'disablePolicies');
dom.tbodyRecentPolicies.addEventListener('click', onRecentPoliciesTableClicked);
dom.buttonLoadPolicy.onclick = () => {
Utils.assert(dom.inputPolicyId.value, 'Please enter a policyId', dom.inputPolicyId);
refreshPolicy(dom.inputPolicyId.value);
};
// WhoAmI -------------
dom.tbodyWhoami.onclick = (event) => {
// dom.crudSubject.idValue = event.target.parentNode.id;
};
Things.addChangeListener(onThingChanged);
}
// function validations(entryFilled, entrySelected = false, subjectFilled = false, subjectSelected = false, resourceFilled = false, resourceSelected = false) {
// Utils.assert(thePolicy, 'Please load a policy', dom.inputPolicyId);
// if (entryFilled) {
// Utils.assert(dom.crudEntry.idValue, 'Please enter a label for the entry', dom.crudEntry.validationElement);
// }
// if (entrySelected) {
// Utils.assert(selectedEntry, 'Please select an entry', dom.tableValidationEntries);
// }
// if (subjectFilled) {
// Utils.assert(dom.crudSubject.idValue, 'Please enter a subject or select one above', dom.crudSubject.validationElement);
// };
// if (subjectSelected) {
// Utils.assert(selectedSubject, 'Please select a subject', dom.tableValidationSubjects);
// };
// if (resourceFilled) {
// Utils.assert(dom.crudResource.idValue, 'Please enter a resource', dom.crudResource.validationElement);
// };
// if (resourceSelected) {
// Utils.assert(selectedResource, 'Please select a resource', dom.tableValidationResources);
// };
// }
function onThingChanged(thing) {
dom.inputPolicyId.value = thing && thing.policyId;
tabHandler.viewDirty = true;
}
function refreshWhoAmI() {
dom.tbodyWhoami.innerHTML = '';
API.callDittoREST('GET', '/whoami')
.then((whoamiResult) => {
whoamiResult.subjects.forEach((subject) => {
Utils.addTableRow(dom.tbodyWhoami, subject, false, false,
subject === whoamiResult.defaultSubject ? 'default' : '');
});
})
.catch((error) => {
});
};
export function refreshPolicy(policyId) {
API.callDittoREST('GET', '/policies/' + policyId)
.then((policy) => setThePolicy(policy))
.catch(() => setThePolicy(null));
};
export function setThePolicy(policy: Policy) {
thePolicy = policy;
if (thePolicy) {
updateRecentPolicies(thePolicy.policyId);
} else {
dom.inputPolicyId.value = null;
}
observable.notifyAll(thePolicy);
};
function refreshAll(otherEnvironment: boolean) {
refreshWhoAmI();
if (otherEnvironment) {
setThePolicy(null);
}
if (dom.inputPolicyId.value) {
refreshPolicy(dom.inputPolicyId.value);
}
}
export function updateRecentPolicies(policyId: String) {
const oldIndex = Environments.current().recentPolicyIds.indexOf(policyId);
if (oldIndex >= 0) {
Environments.current().recentPolicyIds.splice(oldIndex,1);
}
Environments.current().recentPolicyIds.unshift(policyId);
if (Environments.current().recentPolicyIds.length >= 10) {
Environments.current().recentPolicyIds.pop();
}
Environments.environmentsJsonChanged('recentPolicyIds');
}
function onEnvironmentChanged(modifiedField: String) {
Environments.current()['recentPolicyIds'] = Environments.current()['recentPolicyIds'] || [];
dom.tbodyRecentPolicies.innerHTML = '';
Environments.current().recentPolicyIds.forEach(entry => {
Utils.addTableRow(dom.tbodyRecentPolicies, entry, thePolicy && thePolicy.policyId === entry, true);
});
}
function onRecentPoliciesTableClicked(event) {
if (event.target && event.target.nodeName === 'TD') {
const row = event.target.parentNode;
if (thePolicy && thePolicy.policyId === row.id) {
setThePolicy(null);
} else {
dom.inputPolicyId.value = row.id;
refreshPolicy(row.id);
}
}
}
export function finishEditing(editor: CrudToolbar, op: CrudOperation) {
return () => {
if (op in [CrudOperation.CREATE, CrudOperation.UPDATE]) {
editor.toggleEdit(false);
}
refreshPolicy(thePolicy.policyId);
}
}