-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframework.js
More file actions
226 lines (195 loc) · 6.69 KB
/
framework.js
File metadata and controls
226 lines (195 loc) · 6.69 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
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
const $ = () => {
console.log("muserve framework");
}
$._setup = function (element) {
if (!element || !(element instanceof HTMLElement)) {
console.warn("[framework] _setup(): provided element is not an HTMLElement or is null");
return null;
}
// attach element's prototype object's properties to element as function
// NOTE: each property is name -> `$${name}`
let prototype = Object.getPrototypeOf(element);
while (prototype) {
Object.getOwnPropertyNames(prototype).forEach((key) => {
const property = `$${key}`;
if (!element.hasOwnProperty(property)) {
element[property] = function (data) {
this[key] = data;
return this;
}
}
});
// move up the prototype chain
prototype = Object.getPrototypeOf(prototype);
}
// give element the $addChildren function if it has appendChild property
if ("appendChild" in element) {
element["$addChildren"] = function (children) {
if (Array.isArray(children)) {
children.forEach((child) => {
element.appendChild(child);
});
} else {
element.appendChild(children);
}
return this;
}
}
// give element $style function
element.$style = function (data) {
this.style.cssText = data;
return this;
}
// give element $on function which attaches an event listener to it
element.$on = function (eventType, callback) {
this.addEventListener(eventType, callback);
return this;
}
// Important NOTE: if you pass a arrow function, it will inherit the
// `this` from the lexical scope, if you pass a `function ()` function,
// `matching` will be used as `this` inside the function
element.$delegate = function (eventType, selector, callback) {
this.addEventListener(eventType, (event) => {
// get element that triggered the event
const target = event.target;
// check if target matches selector
// closest looks up DOM tree for closest ancestor
const matching = target.closest(selector);
// if a matching element is found, it is a child of the
// element the listener is on, then call the callback
if (matching && this.contains(matching)) {
callback.call(matching, event);
}
});
return this;
}
element.$addClass = function (className) {
element.classList.add(className);
return this;
}
element.$removeClass = function (className) {
element.classList.remove(className);
return this;
}
element.$toggleClass = function (className) {
if (!className) throw new Error("[framework] $toggleClass(): className must be defined");
if (element.classList.contains(className)) {
element.classList.remove(className);
} else {
element.classList.add(className);
}
return this;
}
return element;
}
$.$byId = function (id, target = document) {
if (!(target instanceof HTMLElement || target instanceof Document || target instanceof ShadowRoot)) {
throw new Error("[framework] $byId(): The target must be an HTMLElement, Document, or ShadowRoot.");
}
return this._setup(target.getElementById(id));
}
$.$byName = function (name, target = document) {
if (!(target instanceof HTMLElement || target instanceof Document || target instanceof ShadowRoot)) {
throw new Error("[framework] $byName(): The target must be an HTMLElement, Document, or ShadowRoot.");
}
return Array.from(target.getElementsByName(name)).map(element => this._setup(element));
}
$.$byTag = function (name, target = document) {
if (!(target instanceof HTMLElement || target instanceof Document || target instanceof ShadowRoot)) {
throw new Error("[framework] $byTag(): The target must be an HTMLElement, Document, or ShadowRoot.");
}
return Array.from(target.getElementsByTagName(name)).map(element => this._setup(element));
}
$.$byClass = function (name, target = document) {
if (!(target instanceof HTMLElement || target instanceof Document || target instanceof ShadowRoot)) {
throw new Error("[framework] $byClass(): The target must be an HTMLElement, Document, or ShadowRoot.");
}
return Array.from(target.getElementsByClassName(name)).map(element => this._setup(element));
}
$.$select = function (selector, target = document) {
if (!(target instanceof HTMLElement || target instanceof Document || target instanceof ShadowRoot)) {
throw new Error("[framework] $select(): The target must be an HTMLElement, Document, or ShadowRoot.");
}
return this._setup(target.querySelector(selector));
}
$.$selectAll = function (selector, target = document) {
if (!(target instanceof HTMLElement || target instanceof Document || target instanceof ShadowRoot)) {
throw new Error("[framework] $selectAll(): The target must be an HTMLElement, Document, or ShadowRoot.");
}
return Array.from(target.querySelectorAll(selector)).map(element => this._setup(element));
}
$._root = "uninitialized";
$.$registerRoot = function (element) {
if (element instanceof HTMLElement || element instanceof ShadowRoot) {
this._root = element;
} else {
throw new Error("[framework] $registerRoot(): root container can only be registered to an HTMLElement or ShadowRoot");
}
}
$.$create = function (elementType, props = {}, attachToElement = this._root) {
if (this._root === "uninitialized") {
throw new Error("[framework] $create(): you need to register the root container first");
}
const newElement = document.createElement(elementType);
for (const key in props) {
if (props.hasOwnProperty(key)) {
const value = props[key];
switch (key) {
case "children":
this._setup(newElement).$addChildren(value);
break;
case "onClick":
case "onInput":
case "onChange":
this._setup(newElement).$on(key.substring(2).toLowerCase(), value);
break;
case "style":
this._setup(newElement).$style(value);
break;
case "className":
newElement.className = value;
break;
case "textContent":
newElement.textContent = value;
break;
default:
if (typeof value === 'boolean') {
newElement[key] = value;
} else {
newElement.setAttribute(key, value);
}
break;
}
}
}
if (attachToElement) attachToElement.appendChild(newElement);
return this._setup(newElement);
}
$.$createState = function (initialState) {
const bindings = new Map();
const handler = {
set(target, key, value) {
// check if value is different
if (target[key] !== value) {
target[key] = value;
// trigger DOM update for any bound elements
if (bindings.has(key)) {
bindings.get(key).forEach((binding) => {
binding.update(value);
});
}
}
return true;
}
};
const proxy = new Proxy(initialState, handler);
proxy.$bind = function (key, element, updateFunction) {
if (!bindings.has(key)) {
bindings.set(key, []);
}
bindings.get(key).push({ element, update: updateFunction });
updateFunction(proxy[key]); // initial update
}
return proxy;
}
export default $;