-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-component.js
More file actions
66 lines (51 loc) · 1.53 KB
/
Copy pathbase-component.js
File metadata and controls
66 lines (51 loc) · 1.53 KB
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
const { v4: uuidv4 } = require('uuid');
module.exports = class BaseComponent {
constructor(properties) {
this.binding = properties.binding;
this.debug = properties.debug;
this._parent = undefined;
this._rendered = false;
this._id = properties._id || uuidv4();
}
initializeComponent(data) {}
generateComponent(document, data) {
this._rendered = true;
}
afterGenerateComponent(document) {}
getBinding(data, binding) {
if (binding) {
binding = binding.replace(/{{|}}/g, '');
if (this.binding) {
binding = this.binding + '.' + binding;
}
} else {
binding = this.binding;
}
if (!binding || binding == '' || binding == '.') {
return data;
}
const parts = binding.split('.');
return this._getSubBinding(data, parts);
}
_getSubBinding(data, parts) {
const head = data[parts[0].trim()];
if (parts.length == 1 || head === undefined) {
return head;
}
return this._getSubBinding(head, parts.splice(1));
}
getStringBinding(data, text) {
if (data !== undefined && typeof(text) == 'string') {
while (text.indexOf('{{') != -1 && text.indexOf('}}') != -1) {
const startIndex = text.indexOf('{{');
const endIndex = text.indexOf('}}') + 2;
const bindingResult = this.getBinding(data, text.substring(startIndex, endIndex));
text = text.substring(0, startIndex) + bindingResult + text.substring(endIndex);
}
}
return text;
}
clone() {
return new this.constructor(this);
}
}