-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathModifyElement.js
More file actions
311 lines (299 loc) · 8.7 KB
/
ModifyElement.js
File metadata and controls
311 lines (299 loc) · 8.7 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
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
304
305
306
307
308
309
310
311
(function(){
var attributes = {};
// ignored property names
attributes.tagName =
attributes.alloyName =
attributes.alloyProperties =
attributes.copies =
function () {
return;
};
// touch events
attributes.onTap =
attributes.ontap =
function (fn) {
return Doom.touch(this, 'tap', fn);
};
attributes.onSwipe =
attributes.onswipe =
function (fn) {
return Doom.touch(this, 'swipe', fn);
};
attributes.onhold =
attributes.onHold =
function (fn) {
return Doom.touch(this, 'hold', fn);
};
attributes.onpan =
attributes.onPan =
function (fn) {
return Doom.touch(this, 'pan', fn);
};
attributes.ontouchstart =
attributes.onTouchStart =
function (fn) {
return Doom.touch(this, 'touchstart', fn);
};
attributes.ontouchend =
attributes.onTouchEnd =
function (fn) {
return Doom.touch(this, 'touchend', fn);
};
// mutation events
attributes.onConnected =
attributes.onconnected =
function (fn) {
return Doom.addMutationEvent(this, 'connect', fn);
};
attributes.onDisconnected =
attributes.ondisconnected =
function (fn) {
return Doom.addMutationEvent(this, 'disconnect', fn);
};
attributes.onAdopted =
attributes.onadopted =
function (fn) {
return Doom.addMutationEvent(this, 'adopt', fn);
};
// element append
attributes.parentNode =
attributes.parent =
function (node) {
return node.appendChild(this);
};
attributes.insertBefore =
function (node) {
node.parentNode.insertBefore(this, node);
};
attributes.insertAfter =
function (node) {
if (node.nextSibling) {
node.parentNode.insertBefore(this, node.nextSibling);
} else {
node.parentNode.appendChild(this);
}
};
attributes.insertFirst =
function (node) {
node.insertBefore(this, node.firstChild);
};
// style
attributes.style =
function (css) {
if (typeof css === 'object') {
for (var i in css ) {
this.style[i] = css[i];
}
} else if ( typeof css === 'string' ) {
this.style.cssText += css;
} else {
throw new Error('Style TypeError - ' + typeof css);
}
};
// Accepts a single class or a space delimited list of classes
attributes.addClass =
function (cls) {
this.classList.add.apply(this.classList, cls.split(' ')); // Oh magical spread operator, I wish you were part of ES5
}
attributes.toggleClass =
function (cls) {
var clsArr = cls.split(' ');
for (var i = 0, l = clsArr.length; i < l; i++)
this.classList.toggle( clsArr[i] );
}
attributes.removeClass =
function (cls) {
this.classList.remove.apply(this.classList, cls.split(' '));
}
// Accepts only arrays of classes
attributes.addClasses =
function (clsArr) {
this.classList.add.apply(this.classList, clsArr);
}
attributes.toggleClasses =
function (clsArr) {
for (var i = 0, l = clsArr.length; i < l; i++)
this.classList.toggle( clsArr[i] );
}
attributes.removeClasses =
function (clsArr) {
this.classList.remove.apply(this.classList, clsArr);
}
// recursion
attributes.childNodes =
attributes.children =
function (node) {
while (this.firstChild) {
this.removeChild(this.firstChild);
}
for (var i = 0; i < node.length; i++) {
if (node[i] instanceof HTMLElement) { //child is already element
this.appendChild(node[i]);
} else if (typeof node[i] === 'string') {
var temp = Doom.create({
innerHTML: node[i]
});
while (temp.firstChild) {
this.appendChild(temp.firstChild);
}
} else if (typeof node[i] === 'object') { //child is an element stub
node[i].parentNode = this;
node[i] = Doom.create(node[i]);
} else {
throw new Error('Child TypeError - ' + typeof node[i]);
}
}
return node;
};
attributes.child =
function (node) {
if (this.children.length === 1 && node === this.firstChild) {
return;
}
while (this.firstChild) {
this.removeChild(this.firstChild);
}
if (node instanceof HTMLElement) { //child is already element
this.appendChild(node);
} else if (typeof node === 'string') {
var temp = Doom.create({
innerHTML: node
});
while (temp.firstChild) {
this.appendChild(temp.firstChild);
}
} else if (typeof node === 'object') { //child is an element stub
node.parentNode = this;
node = Doom.create(node);
} else {
throw new Error('Child TypeError - ' + typeof node[i]);
}
return node;
};
attributes.removeChild =
function (node) {
if (typeof node === 'number') {
this.removeChild(this.childNodes[node]);
} else {
this.removeChild(node);
}
}
attributes.removeChildren =
attributes.removeChildNodes =
function (nodes) {
var i = nodes.length;
while (i--) {
if (typeof nodes[i] === 'number') {
nodes[i] = this.childNodes[nodes[i]];
}
}
n = nodes.length;
while (i--) {
this.removeChild(nodes[i]);
}
}
attributes.innerHTML =
function (str) {
if (this.innerHTML !== str) {
this.innerHTML = str
}
}
attributes.partialNoCache =
function (bool) {
if (!this.partial) {
this.partial = {
href: null
};
}
// if enabling nocache then clear the existing cache
this.partial.map = bool ? {} : this.partial.map;
this.partial.noCache = !!bool;
};
attributes.partial =
function (url) {
// quick reference for current elements partial object
var part = this.partial;
// if no partial object create one
if (!part) {
part = this.partial = {
href: url,
map: {},
noCache: false
};
// else check if the partial url has actually changed
} else if (part.href !== url) {
// if nocache is enabled then don't store the old template
if (part.noCache === false)
{
// try and reuse the previous fragment if possible, else create a new one
var fragment = part.map[part.href] || document.createDocumentFragment();
// move all child elements into the fragment
while (this.firstChild)
fragment.appendChild(this.firstChild);
// store the fragment in the partial map
part.map[part.href] = fragment;
}
// if no change, bail
} else {
return;
}
// update the partial url with the new one
part.href = url;
// if theres a cached fragment then append it
if (part.map[url]) {
this.appendChild(part.map[url]);
// else request the partial content and append it
} else {
var element = this;
Doom.fetch(url)
.then(function (res) {
element.innerHTML = res;
});
}
};
// data attributes
attributes.dataset =
function dataset(data) {
for (var i in prop) {
element.dataset[i] = prop[i];
}
};
/**
* Creates a HTML element from a stub
* @param {Object<string, *>} obj
*/
function modifyElement(obj) {
var element = obj["element"], i, l;
if (!element) { // no element
throw new Error("Element not defined");
} else if (typeof element === "string") {
element = Doom.search(element);
obj.element = element;
modifyElement(obj);
} else if (element instanceof HTMLElement === false && element[0]) { // presume array of elements or similar
for (i = 0, l = ~~element.length; i < l; i++) {
obj.element = element[i];
modifyElement(obj);
}
return element;
} else if (element instanceof HTMLElement) {
if (obj["delay"]) {
i = obj["delay"];
obj["delay"] = null;
setTimeout(modifyElement, i, obj);
return;
}
for (i in obj) {
if (i in attributes) {
attributes[i].apply(element, [obj[i]]);
} else {
element[i] = obj[i];
}
}
return element;
} else {
throw new Error("Element TypeError - " + typeof element);
}
};
Doom.modify = modifyElement;
}())