forked from PrismJS/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utopia.js
463 lines (375 loc) · 10.8 KB
/
utopia.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
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
* Utopia: A JavaScript util library that assumes modern standards support and doesn't fix any browser bugs
* @author Lea Verou (http://lea.verou.me)
* MIT license (http://www.opensource.org/licenses/mit-license.php)
* Last update: 2012-4-29
*/
function $(expr, con) {
return typeof expr === 'string'? (con || document).querySelector(expr) : expr;
}
function $$(expr, con) {
var elements = (con || document).querySelectorAll(expr);
try {
return Array.prototype.slice.call(elements);
}
catch(e) {
var arr = Array(elements.length);
for (var i = elements.length; i-- > 0;) {
arr[i] = elements[i];
}
return arr;
}
}
if (!Array.prototype.forEach) {
Array.prototype.forEach = function(fn, scope) {
for (var i = 0, len = this.length; i < len; ++i) {
fn.call(scope || this, this[i], i, this);
}
}
}
// Make each ID a global variable
// Many browsers do this anyway (it’s in the HTML5 spec), so it ensures consistency
$$('[id]').forEach(function(element) { window[element.id] = element; });
// Array#splice but for strings
String.prototype.splice = function(i, remove, add) {
remove = +remove || 0;
add = add || '';
return this.slice(0,i) + add + this.slice(i + remove);
};
(function(){
var _ = window.Utopia = {
/**
* Returns the [[Class]] of an object in lowercase (eg. array, date, regexp, string etc)
* Caution: Results for DOM elements and collections aren't reliable.
* @param {Object} obj
*
* @return {String}
*/
type: function(obj) {
if(obj === null) { return 'null'; }
if(obj === undefined) { return 'undefined'; }
var ret = Object.prototype.toString.call(obj).match(/^\[object\s+(.*?)\]$/)[1];
ret = ret? ret.toLowerCase() : '';
if(ret == 'number' && isNaN(obj)) {
return 'NaN';
}
return ret;
},
/**
* Iterate over the properties of an object. Checks whether the properties actually belong to it.
* Can be stopped if the function explicitly returns a value that isn't null, undefined or NaN.
*
* @param obj {Object} The object to iterate over
* @param func {Function} The function used in the iteration. Can accept 2 parameters: one of the
* value of the object and one for its name.
* @param context {Object} Context for the above function. Default is the object being iterated.
*
* @return {Object} Null or the return value of func, if it broke the loop at some point.
*/
each: function(obj, func, context) {
if(!_.type(func) == 'function') {
throw Error('The second argument in Utopia.each() must be a function');
};
context = context || obj;
for (var i in obj) {
if(obj.hasOwnProperty && obj.hasOwnProperty(i)) {
var ret = func.call(context, obj[i], i);
if(!!ret || ret === 0 || ret === '') {
return ret;
}
}
}
return null;
},
/**
* Copies the properties of one object onto another.
* When there is a collision, the later one wins
*
* @return {Object} destination object
*/
merge: function(objects) {
var ret = {};
for(var i=0; i<arguments.length; i++) {
var o = arguments[i];
for(var j in o) {
ret[j] = o[j];
}
}
return ret;
},
/**
* Copies the properties of one or more objects onto the first one
* When there is a collision, the first object wins
*/
attach: function(object, objects) {
for(var i=0; i<arguments.length; i++) {
var o = arguments[i];
for(var j in o) {
if(!(j in object)) {
object[j] = o[j];
}
}
}
return object;
},
element: {
/**
* Creates a new DOM element
* @param options {Object} A set of key/value pairs for attributes, properties, contents, placement in the DOM etc
* @return The new DOM element
*/
create: function() {
var options;
if(_.type(arguments[0]) === 'string') {
if(_.type(arguments[1]) === 'object') {
// Utopia.element.create('div', { ... });
options = arguments[1];
options.tag = arguments[0];
}
else {
// Utopia.element.create('div', ...);
options = {
tag: arguments[0]
};
// Utopia.element.create('div', [contents]);
if(_.type(arguments[1]) === 'array') {
options.contents = arguments[1];
}
// Utopia.element.create('div', 'Text contents');
else if(_.type(arguments[1]) === 'string' || _.type(arguments[1]) === 'number') {
options.contents = ['' + arguments[1]];
}
}
}
else {
options = arguments[0];
}
var namespace = options.namespace || '', element;
if(namespace) {
element = document.createElementNS(namespace, options.tag);
}
else {
element = document.createElement(options.tag);
}
if (options.className || options.id) {
options.properties = options.properties || {};
options.properties.className = options.className;
options.properties.id = options.id;
}
// Set properties, attributes and contents
_.element.set(element, options);
// Place the element in the DOM (inside, before or after an existing element)
// This could be a selector
if(options.before) {
var before = $(options.before);
if (before && before.parentNode) {
before.parentNode.insertBefore(element, before);
}
}
if (options.after && element.parentNode === null) {
var after = $(options.after);
if (after && after.parentNode) {
after.parentNode.insertBefore(element, after.nextSibling)
}
}
if (options.inside && element.parentNode === null) {
$(options.inside).appendChild(element);
}
return element;
},
set: function(element, options) {
_.element.prop(element, options.properties || options.prop);
_.element.attr(element, options.attributes || options.attr);
_.element.contents(element, options.contents);
return element;
},
prop: function (element, properties) {
if (properties) {
for (var prop in properties) {
element[prop] = properties[prop];
}
}
return element;
},
attr: function (element, attributes) {
if (attributes) {
for (attr in attributes) {
element.setAttribute(attr, attributes[attr]);
}
}
return element;
},
/**
* Sets an element’s contents
* Contents could be: One or multiple (as an array) of the following:
* - An object literal that will be passed through Utopia.element.create
* - A string or number, which will become a text node
* - An existing DOM element
*/
contents: function (element, contents, where) {
if(contents || contents === 0) {
if (_.type(contents) !== 'array') {
contents = [contents];
}
var firstChild = element.firstChild;
for (var i=0; i<contents.length; i++) {
var content = contents[i], child;
switch(_.type(content)) {
case 'string':
if(content === '') {
continue;
}
// fall through
case 'number':
child = document.createTextNode(content);
break;
case 'object':
child = _.element.create(content);
break;
default:
child = content;
}
if(child) {
if (!where || where === 'end') {
element.appendChild(child);
}
else if (where === 'start') {
element.insertBefore(child, firstChild);
}
}
}
}
return element;
}
},
elements: {
// set, attr, prop, contents functions from Utopia.element, but for multiple elements
},
event: {
/**
* Binds one or more events to one or more elements
*/
bind: function(target, event, callback, traditional) {
if(_.type(target) === 'string' || _.type(target) === 'array') {
var elements = _.type(target) === 'string'? $$(target) : target;
elements.forEach(function(element) {
_.event.bind(element, event, callback, traditional);
});
}
else if(_.type(event) === 'string') {
if(traditional) {
target['on' + event] = callback;
}
else {
target.addEventListener(event, callback, false);
}
}
else if(_.type(event) === 'array') {
for (var i=0; i<event.length; i++) {
_.event.bind(target, event[i], callback, arguments[2]);
}
}
else {
for (var name in event) {
_.event.bind(target, name, event[name], arguments[2]);
}
}
},
/**
* Fire a custom event
*/
fire: function(target, type, properties) {
if(_.type(target) === 'string' || _.type(target) === 'array') {
var elements = _.type(target) === 'string'? $$(target) : target;
elements.forEach(function(element) {
_.event.fire(element, type, properties);
});
}
else if (document.createEvent) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(type, true, true );
evt.custom = true;
if(properties) {
_.attach(evt, properties);
}
target.dispatchEvent(evt);
}
}
},
/**
* Helper for XHR requests
*/
xhr: function(o) {
document.body.setAttribute('data-loading', '');
var xhr = new XMLHttpRequest(),
method = o.method || 'GET',
data = o.data || '';
xhr.open(method, o.url + (method === 'GET' && data? '?' + data : ''), true);
o.headers = o.headers || {};
if(method !== 'GET' && !o.headers['Content-type'] && !o.headers['Content-Type']) {
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
for (var header in o.headers) {
xhr.setRequestHeader(header, o.headers[header]);
}
xhr.onreadystatechange = function(){
if(xhr.readyState === 4) {
document.body.removeAttribute('data-loading');
o.callback(xhr);
}
};
xhr.send(method === 'GET'? null : data);
return xhr;
},
/**
* Lazy loads an external script
*/
script: function(url, callback, doc) {
doc = doc || document;
return _.element.create({
tag: 'script',
properties: {
src: url,
async: true,
onload: callback
},
inside: doc.documentElement
});
},
/**
* Returns the absolute X, Y offsets for an element
*/
offset: function(element) {
var left = 0, top = 0, el = element;
if (el.parentNode) {
do {
left += el.offsetLeft;
top += el.offsetTop;
} while ((el = el.offsetParent) && el.nodeType < 9);
el = element;
do {
left -= el.scrollLeft;
top -= el.scrollTop;
} while ((el = el.parentNode) && !/body/i.test(el.nodeName));
}
return {
top: top,
right: innerWidth - left - element.offsetWidth,
bottom: innerHeight - top - element.offsetHeight,
left: left,
};
}
};
['set', 'prop', 'attr', 'contents'].forEach(function(method) {
_.elements[method] = function(elements) {
elements = _.type(elements) === 'string'? $$(elements) : Array.prototype.slice.call(elements);
var args = Array.prototype.slice.call(arguments);
args.shift(); // Remove the elements argument
elements = elements.map(function(element) {
return _.element[method](element, args);
});
return elements;
}
});
})();
window.$u = window.$u || Utopia;