-
Notifications
You must be signed in to change notification settings - Fork 101
/
bliss.shy.js
888 lines (730 loc) · 21.1 KB
/
bliss.shy.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
(function() {
"use strict";
function overload(callback, start, end) {
start = start === undefined ? 1 : start;
end = end || start + 1;
if (end - start <= 1) {
return function() {
if (arguments.length <= start || $.type(arguments[start]) === "string") {
return callback.apply(this, arguments);
}
var obj = arguments[start];
var ret;
for (var key in obj) {
var args = Array.prototype.slice.call(arguments);
args.splice(start, 1, key, obj[key]);
ret = callback.apply(this, args);
}
return ret;
};
}
return overload(overload(callback, start + 1, end), start, end - 1);
}
// Copy properties from one object to another. Overwrites allowed.
// Subtle difference of array vs string whitelist: If property doesn't exist in from, array will not define it.
function extend(to, from, whitelist) {
var whitelistType = type(whitelist);
if (whitelistType === "string") {
// To copy gettters/setters, preserve flags etc
var descriptor = Object.getOwnPropertyDescriptor(from, whitelist);
if (descriptor && (!descriptor.writable || !descriptor.configurable || !descriptor.enumerable || descriptor.get || descriptor.set)) {
delete to[whitelist];
Object.defineProperty(to, whitelist, descriptor);
}
else {
to[whitelist] = from[whitelist];
}
}
else if (whitelistType === "array") {
whitelist.forEach(function(property) {
if (property in from) {
extend(to, from, property);
}
});
}
else {
for (var property in from) {
if (whitelist) {
if (whitelistType === "regexp" && !whitelist.test(property) ||
whitelistType === "function" && !whitelist.call(from, property)) {
continue;
}
}
extend(to, from, property);
}
}
return to;
}
/**
* Returns the [[Class]] of an object in lowercase (eg. array, date, regexp, string etc)
*/
function type(obj) {
if (obj === null) {
return "null";
}
if (obj === undefined) {
return "undefined";
}
var ret = (Object.prototype.toString.call(obj).match(/^\[object\s+(.*?)\]$/)[1] || "").toLowerCase();
if (ret == "number" && isNaN(obj)) {
return "nan";
}
return ret;
}
var $ = self.Bliss = extend(function(expr, context) {
if (arguments.length == 2 && !context || !expr) {
return null;
}
return $.type(expr) === "string"? (context || document).querySelector(expr) : expr || null;
}, self.Bliss);
extend($, {
extend: extend,
overload: overload,
type: type,
property: $.property || "_",
listeners: self.WeakMap? new WeakMap() : new Map(),
original: {
addEventListener: (self.EventTarget || Node).prototype.addEventListener,
removeEventListener: (self.EventTarget || Node).prototype.removeEventListener
},
sources: {},
noop: function() {},
$: function(expr, context) {
if (expr instanceof Node || expr instanceof Window) {
return [expr];
}
if (arguments.length == 2 && !context) {
return [];
}
return Array.prototype.slice.call(typeof expr == "string"? (context || document).querySelectorAll(expr) : expr || []);
},
/*
* Return first non-undefined value. Mainly used internally.
*/
defined: function () {
for (var i=0; i<arguments.length; i++) {
if (arguments[i] !== undefined) {
return arguments[i];
}
}
},
create: function (tag, o) {
if (tag instanceof Node) {
return $.set(tag, o);
}
// 4 signatures: (tag, o), (tag), (o), ()
if (arguments.length === 1) {
if ($.type(tag) === "string") {
o = {};
}
else {
o = tag;
tag = o.tag;
o = $.extend({}, o, function(property) {
return property !== "tag";
});
}
}
return $.set(document.createElement(tag || "div"), o);
},
each: function(obj, callback, ret) {
ret = ret || {};
for (var property in obj) {
ret[property] = callback.call(obj, property, obj[property]);
}
return ret;
},
ready: function(context, callback, isVoid) {
if (typeof context === "function" && !callback) {
callback = context;
context = undefined;
}
context = context || document;
if (callback) {
if (context.readyState !== "loading") {
callback();
}
else {
$.once(context, "DOMContentLoaded", function() {
callback();
});
}
}
if (!isVoid) {
return new Promise(function(resolve) {
$.ready(context, resolve, true);
});
}
},
// Helper for defining OOP-like “classes”
Class: function(o) {
var special = ["constructor", "extends", "abstract", "static"].concat(Object.keys($.classProps));
var init = o.hasOwnProperty("constructor")? o.constructor : $.noop;
var Class;
if (arguments.length == 2) {
// Existing class provided
Class = arguments[0];
o = arguments[1];
}
else {
Class = function() {
if (this.constructor.__abstract && this.constructor === Class) {
throw new Error("Abstract classes cannot be directly instantiated.");
}
if (Class.super && !isSuperClass) {
Class.super.apply(this, arguments);
}
init.apply(this, arguments);
};
Class.super = o.extends || null;
if (Class.super) {
// We cannot extend a native class, it will error
var isSuperClass = (Class.super + "").indexOf("class ") === 0;
if (isSuperClass) {
console.error(`You are using $.Class() to create a fake function-based class that extends a native JS class. This will not work.
You should convert your code to use native JS classes too. You can still pass a class into $.Class() to use its conveniences.`);
}
}
Class.prototype = $.extend(Object.create(Class.super? Class.super.prototype : Object), {
constructor: Class
});
// For easier calling of super methods
// This doesn't save us from having to use .call(this) though
Class.prototype.super = Class.super? Class.super.prototype : null;
Class.__abstract = !!o.abstract;
}
var specialFilter = function(property) {
return this.hasOwnProperty(property) && special.indexOf(property) === -1;
};
// Static methods
if (o.static) {
$.extend(Class, o.static, specialFilter);
for (var property in $.classProps) {
if (property in o.static) {
$.classProps[property](Class, o.static[property]);
}
}
}
// Instance methods
$.extend(Class.prototype, o, specialFilter);
for (var property in $.classProps) {
if (property in o) {
$.classProps[property](Class.prototype, o[property]);
}
}
return Class;
},
// Properties with special handling in classes
classProps: {
// Lazily evaluated properties
lazy: overload(function(obj, property, getter) {
Object.defineProperty(obj, property, {
get: function() {
var value = getter.call(this);
Object.defineProperty(this, property, {
value: value,
configurable: true,
enumerable: true,
writable: true
});
return value;
},
set: function(value) {
// Blind write: skip running the getter
Object.defineProperty(this, property, {
value: value,
configurable: true,
enumerable: true,
writable: true
});
},
configurable: true,
enumerable: true
});
return obj;
}),
// Properties that behave like normal properties but also execute code upon getting/setting
live: overload(function(obj, property, descriptor) {
if ($.type(descriptor) === "function") {
descriptor = {set: descriptor};
}
Object.defineProperty(obj, property, {
get: function() {
var value = this["_" + property];
var ret = descriptor.get && descriptor.get.call(this, value);
return ret !== undefined? ret : value;
},
set: function(v) {
var value = this["_" + property];
var ret = descriptor.set && descriptor.set.call(this, v, value);
this["_" + property] = ret !== undefined? ret : v;
},
configurable: descriptor.configurable,
enumerable: descriptor.enumerable
});
return obj;
})
},
// Includes a script, returns a promise
include: function() {
var url = arguments[arguments.length - 1];
var loaded = arguments.length === 2? arguments[0] : false;
var script = document.createElement("script");
return loaded? Promise.resolve() : new Promise(function(resolve, reject) {
$.set(script, {
async: true,
onload: function() {
resolve(script);
script.parentNode && script.parentNode.removeChild(script);
},
onerror: function() {
reject(script);
},
src: url,
inside: document.head
});
});
},
// Dynamically load a CSS or JS resource
load: function load(url, base) {
base = base? new URL(base, location.href) : location.href;
url = new URL(url, base);
// Prevent double loading
var loading = load.loading = load.loading || {};
if (loading[url + ""]) {
return loading[url + ""];
}
if (/\.css$/.test(url.pathname)) {
// CSS file
return loading[url + ""] = new Promise(function(resolve, reject) {
var link = $.create("link", {
"href": url,
"rel": "stylesheet",
"inside": document.head,
onload: function() {
resolve(link);
},
onerror: function() {
reject(link);
}
});
});
}
// JS file
return loading[url + ""] = $.include(url);
},
/*
* Fetch API inspired XHR wrapper. Returns promise.
*/
fetch: function(url, o) {
if (!url) {
throw new TypeError("URL parameter is mandatory and cannot be " + url);
}
// Set defaults & fixup arguments
var env = extend({
url: new URL(url, location),
data: "",
method: "GET",
headers: {},
xhr: new XMLHttpRequest()
}, o);
env.method = env.method.toUpperCase();
$.hooks.run("fetch-args", env);
// Start sending the request
if (env.method === "GET" && env.data) {
env.url.search += env.data;
}
document.body.setAttribute("data-loading", env.url);
env.xhr.open(env.method, env.url.href, env.async !== false, env.user, env.password);
for (var property in o) {
if (property === "upload") {
if (env.xhr.upload && typeof o[property] === "object") {
$.extend(env.xhr.upload, o[property]);
}
}
else if (property in env.xhr) {
try {
env.xhr[property] = o[property];
}
catch (e) {
self.console && console.error(e);
}
}
}
var headerKeys = Object.keys(env.headers).map(function(key) {
return key.toLowerCase();
});
if (env.method !== "GET" && headerKeys.indexOf("content-type") === -1) {
env.xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
}
for (var header in env.headers) {
if (env.headers[header] !== undefined) {
env.xhr.setRequestHeader(header, env.headers[header]);
}
}
var promise = new Promise(function(resolve, reject) {
env.xhr.onload = function() {
document.body.removeAttribute("data-loading");
if (env.xhr.status === 0 || env.xhr.status >= 200 && env.xhr.status < 300 || env.xhr.status === 304) {
// Success!
resolve(env.xhr);
}
else {
reject($.extend(Error(env.xhr.statusText), {
xhr: env.xhr,
get status() {
return this.xhr.status;
}
}));
}
};
env.xhr.onerror = function() {
document.body.removeAttribute("data-loading");
reject($.extend(Error("Network Error"), {xhr: env.xhr}));
};
env.xhr.ontimeout = function() {
document.body.removeAttribute("data-loading");
reject($.extend(Error("Network Timeout"), {xhr: env.xhr}));
};
env.xhr.send(env.method === "GET"? null : env.data);
});
// Hack: Expose xhr.abort(), by attaching xhr to the promise.
promise.xhr = env.xhr;
return promise;
},
value: function(obj) {
var hasRoot = typeof obj !== "string";
return $.$(arguments).slice(+hasRoot).reduce(function(obj, property) {
return obj && obj[property];
}, hasRoot? obj : self);
}
});
$.Hooks = new $.Class({
add: function (name, callback, first) {
if (typeof arguments[0] != "string") {
// Multiple hooks
for (var name in arguments[0]) {
this.add(name, arguments[0][name], arguments[1]);
}
return;
}
(Array.isArray(name)? name : [name]).forEach(function(name) {
this[name] = this[name] || [];
if (callback) {
this[name][first? "unshift" : "push"](callback);
}
}, this);
},
run: function (name, env) {
this[name] = this[name] || [];
this[name].forEach(function(callback) {
callback.call(env && env.context? env.context : env, env);
});
}
});
$.hooks = new $.Hooks();
var _ = $.property;
$.Element = function (subject) {
this.subject = subject;
// Author-defined element-related data
this.data = {};
// Internal Bliss element-related data
this.bliss = {};
};
$.Element.prototype = {
set: overload(function(property, value) {
if (property in $.setProps) {
$.setProps[property].call(this, value);
}
else if (property in this) {
this[property] = value;
}
else {
this.setAttribute(property, value);
}
}, 0),
// Run a CSS transition, return promise
transition: function(props, duration) {
return new Promise(function(resolve, reject) {
if ("transition" in this.style && duration !== 0) {
// Get existing style
var previous = $.extend({}, this.style, /^transition(Duration|Property)$/);
$.style(this, {
transitionDuration: (duration || 400) + "ms",
transitionProperty: Object.keys(props).join(", ")
});
$.once(this, "transitionend", function() {
clearTimeout(i);
$.style(this, previous);
resolve(this);
});
// Failsafe, in case transitionend doesn’t fire
var i = setTimeout(resolve, duration+50, this);
$.style(this, props);
}
else {
$.style(this, props);
resolve(this);
}
}.bind(this));
},
// Fire a synthesized event on the element
fire: function (type, properties) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent(type, true, true );
// Return the result of dispatching the event, so we
// can know if `e.preventDefault` was called inside it
return this.dispatchEvent($.extend(evt, properties));
},
bind: overload(function(types, options) {
if (arguments.length > 1 && ($.type(options) === "function" || options.handleEvent)) {
// options is actually callback
var callback = options;
options = $.type(arguments[2]) === "object"? arguments[2] : {
capture: !!arguments[2] // in case it's passed as a boolean 3rd arg
};
options.callback = callback;
}
var listeners = $.listeners.get(this) || {};
types.trim().split(/\s+/).forEach(function (type) {
if (type.indexOf(".") > -1) {
type = type.split(".");
var className = type[1];
type = type[0];
}
listeners[type] = listeners[type] || [];
if (listeners[type].filter(function(l) {
return l.callback === options.callback && l.capture == options.capture;
}).length === 0) {
listeners[type].push($.extend({className: className}, options));
}
$.original.addEventListener.call(this, type, options.callback, options);
}, this);
$.listeners.set(this, listeners);
}, 0),
unbind: overload(function(types, options) {
if (options && ($.type(options) === "function" || options.handleEvent)) {
var callback = options;
options = arguments[2];
}
if ($.type(options) == "boolean") {
options = {capture: options};
}
options = options || {};
options.callback = options.callback || callback;
var listeners = $.listeners.get(this);
(types || "").trim().split(/\s+/).forEach(function (type) {
if (type.indexOf(".") > -1) {
type = type.split(".");
var className = type[1];
type = type[0];
}
//if listeners exist, always go through listeners to clean up
if (!listeners) {
if (type && options.callback) {
return $.original.removeEventListener.call(this, type, options.callback, options.capture);
}
return;
}
// Mass unbinding, need to go through listeners
for (var ltype in listeners) {
if (!type || ltype === type) {
// No forEach, because we’re mutating the array
for (var i=0, l; l=listeners[ltype][i]; i++) {
if ((!className || className === l.className)
&& (!options.callback || options.callback === l.callback)
&& (!!options.capture == !!l.capture ||
!type && !options.callback && undefined === options.capture)
) {
listeners[ltype].splice(i, 1);
$.original.removeEventListener.call(this, ltype, l.callback, l.capture);
i--;
}
}
}
}
}, this);
}, 0),
// Return a promise that resolves when an event fires, then unbind
when: function(type, test) {
var me = this;
return new Promise(function(resolve) {
me.addEventListener(type, function callee(evt) {
if (!test || test.call(this, evt)) {
this.removeEventListener(type, callee);
resolve(evt);
}
});
});
},
toggleAttribute: function(name, value, test) {
if (arguments.length < 3) {
test = value !== null;
}
if (test) {
this.setAttribute(name, value);
}
else {
this.removeAttribute(name);
}
}
};
/*
* Properties with custom handling in $.set()
* Also available as functions directly on element._ and on $
*/
$.setProps = {
// Set a bunch of inline CSS styles
style: function (val) {
for (var property in val) {
if (property in this.style) {
// camelCase versions
this.style[property] = val[property];
}
else {
// This way we can set CSS Variables too and use normal property names
this.style.setProperty(property, val[property]);
}
}
},
// Set a bunch of attributes
attributes: function (o) {
for (var attribute in o) {
this.setAttribute(attribute, o[attribute]);
}
},
// Set a bunch of properties on the element
properties: function (val) {
$.extend(this, val);
},
// Bind one or more events to the element
events: function (val) {
if (arguments.length == 1 && val && val.addEventListener) {
// Copy events from other element (requires Bliss Full)
var me = this;
// Copy listeners
if ($.listeners) {
var listeners = $.listeners.get(val);
for (var type in listeners) {
listeners[type].forEach(function(l) {
$.bind(me, type, l.callback, l.capture);
});
}
}
// Copy inline events
for (var onevent in val) {
if (onevent.indexOf("on") === 0) {
this[onevent] = val[onevent];
}
}
}
else {
return $.bind.apply(this, [this].concat($.$(arguments)));
}
},
once: overload(function(types, callback) {
var me = this;
var once = function() {
$.unbind(me, types, once);
return callback.apply(me, arguments);
};
$.bind(this, types, once, {once: true});
}, 0),
// Event delegation
delegate: overload(function (type, selector, callback) {
$.bind(this, type, function(evt) {
if (evt.target.closest(selector)) {
callback.call(this, evt);
}
});
}, 0, 2),
// Set the contents as a string, an element, an object to create an element or an array of these
contents: function (val) {
if (val || val === 0) {
(Array.isArray(val)? val : [val]).forEach(function (child) {
var type = $.type(child);
if (/^(string|number)$/.test(type)) {
child = document.createTextNode(child + "");
}
else if (type === "object") {
child = $.create(child);
}
if (child instanceof Node) {
this.appendChild(child);
}
}, this);
}
},
// Append the element inside another element
inside: function (element) {
element && element.appendChild(this);
},
// Insert the element before another element
before: function (element) {
element && element.parentNode.insertBefore(this, element);
},
// Insert the element after another element
after: function (element) {
element && element.parentNode.insertBefore(this, element.nextSibling);
},
// Insert the element before another element's contents
start: function (element) {
element && element.insertBefore(this, element.firstChild);
},
// Wrap the element around another element
around: function (element) {
if (element && element.parentNode) {
$.before(this, element);
}
this.appendChild(element);
}
};
$.Array = function (subject) {
this.subject = subject;
};
$.Array.prototype = {
all: function(method) {
var args = $.$(arguments).slice(1);
return this[method].apply(this, args);
}
};
// Extends Bliss with more methods
$.add = overload(function(method, callback, on, noOverwrite) {
on = $.extend({$: true, element: true, array: true}, on);
if ($.type(callback) == "function") {
if (on.element && (!(method in $.Element.prototype) || !noOverwrite)) {
$.Element.prototype[method] = function () {
return this.subject && $.defined(callback.apply(this.subject, arguments), this.subject);
};
}
if (on.array && (!(method in $.Array.prototype) || !noOverwrite)) {
$.Array.prototype[method] = function() {
var args = arguments;
return this.subject.map(function(element) {
return element && $.defined(callback.apply(element, args), element);
});
};
}
if (on.$) {
$.sources[method] = $[method] = callback;
if (on.array || on.element) {
$[method] = function () {
var args = [].slice.apply(arguments);
var subject = args.shift();
var Type = on.array && Array.isArray(subject)? "Array" : "Element";
return $[Type].prototype[method].apply({subject: subject}, args);
};
}
}
}
}, 0);
$.add($.Array.prototype, {element: false});
$.add($.Element.prototype);
$.add($.setProps);
$.add($.classProps, {element: false, array: false});
// Add native methods on $ and _
var dummy = document.createElement("_");
$.add($.extend({}, HTMLElement.prototype, function(method) {
return $.type(dummy[method]) === "function";
}), null, true);
})();