-
Notifications
You must be signed in to change notification settings - Fork 0
/
bview.js
201 lines (160 loc) · 5.19 KB
/
bview.js
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
/*!
BView v0.1.1
(c) 2023 github.com/splendidexponent
License: MIT
*/
export default class BView {
constructor(el){
this.el = el;
// https://backbonejs.org/#View-constructor
this.initialize(el);
if(BView.debug){
console.log(this.constructor.name, 'instantiated el:', this.el);
}
this.delegateEvents();
this.setEls();
this.render();
}
// https://backbonejs.org/#View-delegateEvents
delegateEvents(){
if(!this.events) return;
// Remove previous delegated events, if any
if(this.__events_fn && Object.keys(this.__events_fn).length > 0){
this.undelegateEvents();
} else {
this.__events_fn = {};
}
for (const event_key in this.events) {
const method_name = this.events[event_key];
const event_key_parts = event_key.match(/([^\s]+)\s(.*)/);
// handle "click" without any selector. click event on el itself. ie. event_key_parts == null
const event_on_el_itself = event_key_parts == null;
const event_name = event_on_el_itself ? event_key : event_key_parts[1];
const event_el_selector = event_on_el_itself ? null : event_key_parts[2];
this.__events_fn[method_name] = (e) => {
if(
event_on_el_itself ||
this.arrContains(this.qAll(event_el_selector), e.target)
){
if(BView.debug){
console.log(this.constructor.name, `"${event_key}: ${method_name}" triggered, target:`, e.target, 'el:', this.el);
}
this[method_name](e);
}
}
// Set events on el, handled with event bubbling.
this.el.addEventListener(event_name, this.__events_fn[method_name]);
}
if(BView.debug){
console.log(this.constructor.name, 'delegating events el:', this.el);
}
}
// https://backbonejs.org/#View-undelegateEvents
undelegateEvents(){
if(!this.events || !this.__events_fn) return;
for (const event_key in this.events) {
const method_name = this.events[event_key];
const event_key_parts = event_key.match(/([^\s]+)\s(.*)/);
// handle "click" without any selector. click event on el. ie. event_key_parts == null
const event_name = event_key_parts == null ? event_key : event_key_parts[1];
this.el.removeEventListener(event_name, this.__events_fn[method_name]);
delete this.__events_fn[method_name];
}
if(BView.debug){
console.log(this.constructor.name, 'undelegating events el:', this.el);
}
}
// Helper methods. KEY_el() will return reference element with KEY.
// Not in backbone
setEls(){
if(!this.els) return;
if(BView.debug){
var function_list = [];
}
for (const name in this.els) {
// This must be a function, as DOM within el could change at anytime.
Object.defineProperty(this, `${name}_el`, {
get: ()=>this.q(this.els[name])
});
if(BView.debug){
function_list.push('this.' + name + '_el');
}
}
if(BView.debug){
console.log(this.constructor.name, 'reference element functions:', function_list.join(), 'el:', this.el);
}
}
// Find element within el. Similar to jQuery this.$el.find().
// https://backbonejs.org/#View-$el
q(query){
return this.el.querySelector(query);
}
// Find all elements within el. Similar to jQuery this.$el.find().
// https://backbonejs.org/#View-$el
qAll(query){
return this.el.querySelectorAll(query);
}
// Checks if given element is present in an array. (Works on node_list)
arrContains(arr, element){
for (let i = 0; i < arr.length; i++) {
if(element === arr[i]){
return true;
}
}
return false;
}
initialize(){
// default no-op
}
render(){
// default no-op
}
// Sync
static debug = false;
static viewClasses = {};
static viewInstances = [];
static addViewClass(viewClass){
this.viewClasses[viewClass.name] = viewClass;
if(BView.debug){
console.log('BView Sync:', 'Added View Class', viewClass.name);
}
this.sync();
}
static sync(){
// Select all uninitialized views
const els = document.querySelectorAll('[data-view]:not([data-view-initialized])');
// Instantiate
els.forEach((el)=>{
if(!this.viewClasses[el.dataset.view]) return;
const instance = new this.viewClasses[el.dataset.view](el);
el.dataset.viewInitialized = 'true';
this.viewInstances.push({
instance: instance,
el: el
});
});
// Remove instances that are not in dom
this.viewInstances = this.viewInstances.filter((viewInstance)=>{
if(document.body.contains(viewInstance.el) == false){
if(BView.debug){
console.log('BView Sync:', 'Removing', viewInstance.instance.constructor.name , 'instance for el:', viewInstance.el);
}
viewInstance.instance.undelegateEvents();
delete viewInstance.instance;
delete viewInstance.el;
return false;
}
return true;
});
}
// Start
static start(){
const observer = new MutationObserver(function(){
BView.sync();
});
document.addEventListener('DOMContentLoaded', function(){
BView.sync();
observer.observe(document, { childList: true, subtree: true });
});
}
}