This repository has been archived by the owner on Jan 20, 2024. It is now read-only.
/
component.ts
303 lines (278 loc) · 9.71 KB
/
component.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
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
import { Node, NodeCallbacks, NodeType } from './models';
import { Utils } from './utils';
import { ClassNames, Errors, Selectors } from './variables';
/**
* Component used to render a node.
*/
export class ExpNodeComponent {
/**
* Creates a new instance of ExpNodeComponent and adds it to a container.
*
* @param node Node object that should be rendered in the ExpNodeComponent.
* @param containerEl Element that should be a container for the ExpNodeComponent.
* @param callbacks Object with callback functions.
* @param types Array containing types that can be used for a node.
*/
public static create(
node: Node,
containerEl: Element,
callbacks?: NodeCallbacks,
types?: ReadonlyArray<NodeType>
): ExpNodeComponent {
return new ExpNodeComponent(node, containerEl, callbacks, types);
}
/**
* Defines an element where the ExpNodeComponent will be appended to.
*/
private readonly containerEl: Element;
/**
* Defines a node object that should be rendered in the ExpNodeComponent.
*/
private readonly node: Node;
/**
* Defines an object containing callbacks for all buttons defined for a node.
*/
private readonly callbacks?: NodeCallbacks;
/**
* Defines an array of types that can be used for a node.
*/
private readonly types?: ReadonlyArray<NodeType>;
/**
* Initializes [[node]], [[containerEl]], [[callbacks]] and [[types]] variable. Calls [[render]] to render passed node.
*
* @param node Node object that should be rendered in the ExpNodeComponent.
* @param containerEl Element that should be a container for the ExpNodeComponent.
* @param callbacks Object with callback functions.
* @param types Array containing types that can be used for a node.
*/
constructor(
node: Node,
containerEl: Element,
callbacks?: NodeCallbacks,
types?: ReadonlyArray<NodeType>
) {
this.node = node;
this.containerEl = containerEl;
this.callbacks = callbacks;
this.types = types;
this.render();
}
/**
* Creates a node and adds it to the [[containerEl]].
*/
private render(): void {
const { id } = this.node;
const { description } = this.node;
const { enableEditBtn } = this.node;
const { childNodes } = this.node;
const expNodeComponent: Element = document.createElement('div');
expNodeComponent.id = id;
expNodeComponent.classList.add(ClassNames.CONTAINER);
expNodeComponent.innerHTML = `
<div class="exp-node-row">
<div class="exp-node-element">
<div class="exp-node-shape ${Utils.getCssClassForAssignedType(
this.node,
this.types
)}"></div>
<div class="exp-node-shape-selection"></div>
</div>
<div class="exp-node-element">
<div class="exp-node-actions">
<button type="button" class="exp-node-delete-btn"></button>
<button type="button" class="exp-node-edit-btn exp-node-hide"></button>
</div>
<div class="exp-node-description">
${description}
</div>
</div>
</div>
<div class="exp-node-row">
<div class="exp-node-element exp-node-children-actions-wrapper"></div>
<div class="exp-node-element exp-node-children-wrapper">
<div class="exp-node-children"></div>
</div>
</div>`;
if (
Utils.checkIfElementContainsClassName(
this.containerEl,
ClassNames.WRAPPER
)
) {
this.containerEl.appendChild(expNodeComponent);
} else {
const childrenContainerEl = this.containerEl.querySelector(
Selectors.CHILDREN
);
if (childrenContainerEl !== null) {
childrenContainerEl.appendChild(expNodeComponent);
}
}
if (Utils.arrayNotEmpty(childNodes)) {
this.enableChildrenActions(expNodeComponent);
this.renderChildNodes(expNodeComponent);
}
if (enableEditBtn) {
this.registerEditBtnClickListener(expNodeComponent);
}
this.registerDeleteBtnClickListener(expNodeComponent);
this.registerSelectClickListener(expNodeComponent);
}
/**
* Enables children actions for a given node.
*
* @param expNodeComponent Node which should have children actions enabled.
*/
private enableChildrenActions(expNodeComponent: Element): void {
const childrenActionsContainerEl = expNodeComponent.querySelector(
Selectors.CHILDREN_ACTIONS_WRAPPER
);
if (childrenActionsContainerEl !== null) {
childrenActionsContainerEl.innerHTML = `<div class="exp-node-children-actions">
<button type="button" class="exp-node-expand-btn exp-node-expand-less-btn"></button>
</div>`;
this.registerExpandBtnClickListener(expNodeComponent);
}
}
/**
* Renders children nodes of a node.
*
* @param expNodeComponent Node which children should be rendered out.
*/
private renderChildNodes(expNodeComponent: Element): void {
const { childNodes = [] } = this.node;
childNodes.forEach(childNode =>
ExpNodeComponent.create(
childNode,
expNodeComponent,
this.callbacks,
this.types
)
);
}
/**
* Registers an event listener for the children action button to show/collapse the children nodes.
*
* @param expNodeComponent Node for which the expand button should be registered.
*/
private registerExpandBtnClickListener(expNodeComponent: Element): void {
const expandBtnEl = expNodeComponent.querySelector(Selectors.EXPAND_BTN);
const childrenContainerEl = expNodeComponent.querySelector(
Selectors.CHILDREN
);
if (expandBtnEl !== null && childrenContainerEl !== null) {
expandBtnEl.addEventListener('click', () => {
childrenContainerEl.classList.toggle(ClassNames.HIDE);
if (
Utils.checkIfElementContainsClassName(
childrenContainerEl,
ClassNames.HIDE
)
) {
expandBtnEl.classList.remove('exp-node-expand-less-btn');
expandBtnEl.classList.add('exp-node-expand-more-btn');
} else {
expandBtnEl.classList.remove('exp-node-expand-more-btn');
expandBtnEl.classList.add('exp-node-expand-less-btn');
}
if (Utils.checkIfObjectHasProperty(this.callbacks, 'expandBtnCb')) {
const { expandBtnCb } = this.callbacks!;
if (Utils.isDefined(expandBtnCb)) {
expandBtnCb!(this.node);
} else {
throw new Error(Errors.EXPAND_BTN_CB_UNDEFINED);
}
}
});
} else {
throw new Error(Errors.EXPAND_BTN_NOT_FOUND);
}
}
/**
* Registers an event listener and enables the edit button for a given node.
*
* @param expNodeComponent Node for which the edit button should be registered.
*/
private registerEditBtnClickListener(expNodeComponent: Element): void {
const editBtnEl = expNodeComponent.querySelector(Selectors.EDIT_BTN);
if (editBtnEl != null) {
editBtnEl.classList.remove(ClassNames.HIDE);
editBtnEl.addEventListener('click', () => {
if (Utils.checkIfObjectHasProperty(this.callbacks, 'editBtnCb')) {
const { editBtnCb } = this.callbacks!;
if (Utils.isDefined(editBtnCb)) {
editBtnCb!(this.node);
} else {
throw new Error(Errors.EDIT_BTN_CB_UNDEFINED);
}
}
});
} else {
throw new Error(Errors.EDIT_BTN_NOT_FOUND);
}
}
/**
* Registers an event listener and enables the edit button for a given node.
*
* @param expNodeComponent Node for which the edit button should be registered.
*/
private registerDeleteBtnClickListener(expNodeComponent: Element): void {
const deleteBtnEl = expNodeComponent.querySelector(Selectors.DELETE_BTN);
const parentEl = (expNodeComponent as HTMLElement).parentElement;
if (deleteBtnEl != null && parentEl != null) {
deleteBtnEl.addEventListener('click', () => {
parentEl.removeChild(expNodeComponent);
const childrenActionsContainerEl = this.containerEl.querySelector(
Selectors.CHILDREN_ACTIONS_WRAPPER
);
const childrenContainerEl = this.containerEl.querySelector(
Selectors.CHILDREN
);
if (
childrenActionsContainerEl != null &&
childrenContainerEl != null &&
childrenContainerEl.children.length === 0
) {
childrenActionsContainerEl.innerHTML = '';
}
if (Utils.checkIfObjectHasProperty(this.callbacks, 'deleteBtnCb')) {
const { deleteBtnCb } = this.callbacks!;
if (Utils.isDefined(deleteBtnCb)) {
deleteBtnCb!(this.node);
} else {
throw new Error(Errors.DELETE_BTN_CB_UNDEFINED);
}
}
});
} else {
throw new Error(Errors.DELETE_BTN_NOT_FOUND);
}
}
/**
* Registers an event listener for the selection of a node.
*
* @param expNodeComponent Node for which the edit button should be registered.
*/
private registerSelectClickListener(expNodeComponent: Element): void {
const shapeEl = expNodeComponent.querySelector(Selectors.SHAPE);
const shapeSelectionEl = expNodeComponent.querySelector(
Selectors.SHAPE_SELECTION
);
if (shapeEl != null && shapeSelectionEl != null) {
shapeEl.addEventListener('click', () => {
Utils.removeSelectionFromAllShapes();
shapeSelectionEl.classList.add(ClassNames.SHAPE_SELECTION_ACTIVE);
if (Utils.checkIfObjectHasProperty(this.callbacks, 'selectCb')) {
const { selectCb } = this.callbacks!;
if (Utils.isDefined(selectCb)) {
selectCb!(this.node);
} else {
throw new Error(Errors.SELECT_BTN_CB_UNDEFINED);
}
}
});
} else {
throw new Error(Errors.SHAPE_NOT_FOUND);
}
}
}