-
Notifications
You must be signed in to change notification settings - Fork 1
/
foojitsu.js
2356 lines (2188 loc) · 80 KB
/
foojitsu.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
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* FooJitsu - A lightweight JavaScript framework for plugins.
* @version 1.0.5
* @link https://github.com/fooplugins/foojitsu
* @copyright FooPlugins 2016
* @license Released under the MIT license.
*/
(function(){
function $(){
var self = this, args = Array.prototype.slice.call(arguments);
if (!(this instanceof $)){
function FooJitsu(args) { return $.apply(this, args); }
FooJitsu.prototype = $.prototype;
return new FooJitsu(args);
}
this.context = $.getContext(args);
this.length = 0;
var arg1 = args.shift();
if ($.is.self(arg1)) {
return arg1;
} else if ($.is.selector(arg1)) {
return $(this.context.querySelectorAll(arg1), this.context);
} else if ($.is.html(arg1)) {
return $($.parseHTML(arg1), this.context);
} else if ($.is.element(arg1) || arg1 === window) {
this[0] = arg1;
this.length = 1;
} else if ($.is.array(arg1) || $.is.arrayLike(arg1)) {
var index = 0;
$.each(arg1, function(i, el){
if ($.is.element(el) && $.inArray(el, self) === -1){
self[index] = el; index++;
self.length = index;
}
});
} else if ($.is.fn(arg1)){
$.ready(arg1);
}
}
$.version = '1.0.5';
var ver = /[\d\.]/;
/**
* Compares two version numbers. 0 if the versions are equal, -1 if v1 < v2, 1 if v1 > v2 and NaN if either version is invalid.
* @param {string} v1 The first version to be compared.
* @param {string} v2 The second version to be compared.
* @returns {(number|NaN)}
*/
$.versionCompare = function(v1, v2){
function split(v){
var res = v.split('.');
for(var i = 0, len = res.length; i < len; i++){
res[i] = parseInt(res[i]);
if (isNaN(res[i])) res[i] = 0;
}
return res;
}
if (!(ver.test(v1) && ver.test(v2))) return NaN;
var v1parts = split(v1),
v2parts = split(v2);
while (v1parts.length < v2parts.length) v1parts.push(0);
while (v2parts.length < v1parts.length) v2parts.push(0);
for (var i = 0; i < v1parts.length; ++i) {
if (v2parts.length == i) return 1;
if (v1parts[i] == v2parts[i]) continue;
if (v1parts[i] > v2parts[i]) return 1;
else return -1;
}
if (v1parts.length != v2parts.length) return -1;
return 0;
};
var exists = !!window.FooJitsu;
if (!exists || (exists && $.versionCompare($.version, window.FooJitsu.version) > 0)){
window.FooJitsu = $;
}
})();
(function($){
if ($.version !== '1.0.5') return;
/**
* Checks if the element matches the supplied selector.
* @param {HTMLElement} el - The element to test.
* @param {string} selector - The selector to test with.
* @returns {boolean}
*/
$.is = function(el, selector){
if (!$.is.element(el)) return false;
var p = selector.split(','), result = false;
$.each(p, function (i, s) {
s = s.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
if (s === '') return;
var root = $.is.element(el.parentNode) ? el.parentNode.cloneNode(false) : document.createElement('div'),
clone = el.cloneNode(true);
root.appendChild(clone);
if (root.querySelector(s) === clone) {
result = true;
return false;
}
});
return result;
};
/**
* Checks if the supplied object is an instance of a FooPlugins object.
* @param {*} obj - The object to check.
* @returns {boolean}
*/
$.is.self = function(obj){
return $.is.defined(obj) && obj instanceof $;
};
/**
* Checks if the value is defined.
* @param {*} value - The value to check is defined.
* @returns {boolean}
*/
$.is.defined = function (value) {
return typeof value !== 'undefined';
};
/**
* Checks if the value is defined.
* @param {*} value - The value to check is defined.
* @returns {boolean}
*/
$.is.undef = function (value) {
return typeof value === 'undefined';
};
/**
* Checks if the value is a string.
* @param {*} value - The value to check is a string.
* @returns {boolean}
*/
$.is.string = function (value) {
return typeof value === 'string';
};
/**
* Checks if the value is a number.
* @param {*} value - The value to check is a number.
* @returns {boolean}
*/
$.is.number = function (value) {
return typeof value === 'number' && !isNaN(value);
};
/**
* Checks if the value is an array.
* @param {*} value - The value to check.
* @returns {boolean}
*/
$.is.array = function (value) {
return '[object Array]' === Object.prototype.toString.call(value);
};
/**
* Checks if the value is an array like value.
* @param {object} obj - The value to check.
* @returns {boolean}
*/
$.is.arrayLike = function(obj){
return !$.is.fn(obj) && $.is.defined(obj) && obj !== null && $.is.number(obj.length);
};
/**
* Checks if the value is a boolean.
* @param {*} value - The value to check.
* @returns {boolean}
*/
$.is.boolean = function (value) {
return '[object Boolean]' === Object.prototype.toString.call(value);
};
/**
* Checks if the value is a function.
* @param {*} value - The value to check.
* @returns {boolean}
*/
$.is.fn = function (value) {
var isAlert = typeof window !== 'undefined' && value === window.alert;
return isAlert || '[object Function]' === Object.prototype.toString.call(value);
};
/**
* Checks if the value is an object.
* @param {*} value - The value to check.
* @returns {boolean}
*/
$.is.object = function (value) {
return $.is.defined(value) && '[object Object]' === Object.prototype.toString.call(value);
};
/**
* Checks if the value is a hash.
* @param {*} value - The value to check.
* @returns {boolean}
*/
$.is.hash = function (value) {
return $.is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval;
};
/**
* Checks if the supplied object is an HTMLElement
* @param {object} obj - The object to check.
* @returns {boolean}
*/
$.is.element = function (obj) {
return typeof HTMLElement === 'object'
? obj instanceof HTMLElement
: obj && typeof obj === 'object' && obj !== null && obj.nodeType === 1 && typeof obj.nodeName === 'string';
};
/**
* Checks if the supplied object is an Event
* @param {object} obj - The object to check.
* @returns {boolean}
*/
$.is.event = function (obj) {
return $.is.defined(window.Event) && obj instanceof window.Event;
};
/**
* Checks if the supplied string is a CSS selector.
* @param {string} str - The string to check.
* @returns {boolean}
*/
$.is.selector = function (str) {
try {
if (!$.is.string(str)) return false;
document.querySelector(str);
return true;
} catch (e) {
return false;
}
};
/**
* Checks if the supplied string contains HTML and should be interpreted as an HTMLString.
* @param {string} str - The string to check.
* @returns {boolean}
* @description This is an exceedingly simplified check for an HTML string, a far more reliable check can be performed by switching the Regex to the following:
* /<(br|basefont|hr|input|source|frame|param|area|meta|!--|col|link|option|base|img|wbr|!DOCTYPE).*?>|<(a|abbr|acronym|address|applet|article|aside|audio|b|bdi|bdo|big|blockquote|body|button|canvas|caption|center|cite|code|colgroup|command|datalist|dd|del|details|dfn|dialog|dir|div|dl|dt|em|embed|fieldset|figcaption|figure|font|footer|form|frameset|head|header|hgroup|h1|h2|h3|h4|h5|h6|html|i|iframe|ins|kbd|keygen|label|legend|li|map|mark|menu|meter|nav|noframes|noscript|object|ol|optgroup|output|p|pre|progress|q|rp|rt|ruby|s|samp|script|section|select|small|span|strike|strong|style|sub|summary|sup|table|tbody|td|textarea|tfoot|th|thead|time|title|tr|track|tt|u|ul|var|video).*?<\/\2>/i
*/
$.is.html = function (str) {
try {
return /^<.*?>$/i.test(str);
} catch (e) {
return false;
}
};
/**
* Checks if the supplied value is a member or value depending on if the target is an object or an array.
* @param {*} value - The value to check for.
* @param {*} target - Either the object or array to check.
* @returns {boolean}
*/
$.is.any = function (value, target) {
if ($.is.array(target)) {
var l = target.length + 1;
while (l -= 1) if (target[l - 1] === value) return true;
} else if ($.is.object(target)) {
return value in target;
}
return false;
};
})(FooJitsu);
(function($){
if ($.version !== '1.0.5') return;
var prefixes = ['Webkit', 'Moz', 'ms', 'O', 'Khtml'],
check = ['transition','transform','transformOrigin','userSelect'],
elem = document.createElement('div'),
cssReg = /^-(moz|webkit|khtml|o|ms)-([a-z])/,
jsReg = /^(Moz|Webkit|Khtml|O|ms)([A-Z])/,
ua = navigator.userAgent.toLowerCase(),
regexReplacer = function(match, $1, $2){ return $2.toLowerCase(); },
cssTextReplacer = function(match, $1, $2){ return '-'+$1+'-'+$2.toLowerCase(); },
cleanName = function(name){
if (cssReg.test(name)) name = name.replace(cssReg, regexReplacer);
if (jsReg.test(name)) name = name.replace(jsReg, regexReplacer);
return $.toCamelCase(name);
};
function supports(name){
if ($.is.defined(elem.style[name])) return true;
for (var i = 0, len = prefixes.length; i < len; i++){
var n = prefixes[i] + name.charAt(0).toUpperCase() + name.substr(1);
if ($.is.defined(elem.style[n])) return true;
}
return false;
}
function prefixed(name){
if ($.inArray(name, check) != -1){
if ($.is.defined(elem.style[name])) return name;
for (var i = 0, len = prefixes.length; i < len; i++){
var n = prefixes[i] + name.charAt(0).toUpperCase() + name.substr(1);
if ($.is.defined(elem.style[n])) return n;
}
}
return name;
}
function test(props, def){
if ($.is.hash(props)){
for (var name in props){
if (!props.hasOwnProperty(name)) continue;
if ($.is.defined(elem.style[name])) return props[name];
}
}
return def;
}
$.browser = {
isIE: ua.indexOf('msie ') > -1 || ua.indexOf('trident/') > -1 || ua.indexOf('edge/') > -1,
ltEqIE10: Function('/*@cc_on return true@*/')() // this works as only IE10 and below support the @cc_on syntax
};
var __supports__ = {
storage: 'localStorage' in window && window['localStorage'] !== null,
touch: 'ontouchstart' in window || navigator.msMaxTouchPoints > 0
};
/**
* Checks if the browser supports a particular feature. At present this tests the style of an element for supported CSS properties or a small list of common features such as;
* "storage" - Whether or not the browser supports localStorage.
* "touch" - Whether or not the browser supports touch events.
* @param {string} name - The name of the property or feature to check.
* @returns {boolean}
*/
$.browser.supports = function(name){
name = cleanName(name);
if ($.is.defined(__supports__[name])) return __supports__[name];
return (__supports__[name] = supports(name));
};
var __name__ = {};
/**
* Adds the appropriate browser prefix to the beginning of the supplied Javascript or CSS property name.
* @param {string} name - The name of the property to prefix.
* @param {boolean} [cssText] - Whether or not the name should be returned in CSS text format.
* @returns {string}
*/
$.browser.prefixed = function(name, cssText){
name = cleanName(name);
name = $.is.defined(__name__[name]) ? __name__[name] : (__name__[name] = prefixed(name));
return !!cssText ? $.toHyphen(name.replace(jsReg, cssTextReplacer)) : name;
};
var __events__ = {
transitionend: test({ 'transition': 'transitionend', 'OTransition': 'otransitionend', 'MozTransition': 'transitionend', 'WebkitTransition': 'webkitTransitionEnd' }),
animationstart: test({ 'animation': 'animationstart', 'OAnimation': 'oanimationstart', 'MozAnimation': 'animationstart', 'WebkitAnimation': 'webkitAnimationStart' }),
animationiteration: test({ 'animation': 'animationiteration', 'OAnimation': 'oanimationiteration', 'MozAnimation': 'animationiteration', 'WebkitAnimation': 'webkitAnimationIteration' }),
animationend: test({ 'animation': 'animationend', 'OAnimation': 'oanimationend', 'MozAnimation': 'animationend', 'WebkitAnimation': 'webkitAnimationEnd' })
};
/**
* Returns the browser specific event using the supplied name.
* @param {string} name - The event name to check.
* @returns {string}
*/
$.browser.event = function(name){
return $.is.defined(__events__[name]) ? __events__[name] : name;
};
})(FooJitsu);
(function ($) {
if ($.version !== '1.0.5') return;
/**
* Empty function
*/
$.noop = function(){};
/**
* Waits for the DOM to be accessible and then executes the callback.
* @param {function} callback - The function to execute once ready.
*/
$.ready = function (callback) {
function onready(){
try { callback.call(window, $); }
catch(err) { console.error(err); }
}
if ($.browser.ltEqIE10 ? document.readyState === "complete" : document.readyState !== "loading") onready();
else document.addEventListener('DOMContentLoaded', onready, false);
};
/**
* Used by the FooJitsu constructor, when given an array of args this function returns the correct context to use.
* @param {Array} args - The arguments to check.
* @returns {Node}
*/
$.getContext = function(args){
if ($.is.array(args) && args.length > 1){
var ctx = args[args.length - 1];
if ($.is.selector(args[0])){
if ($.is.self(ctx) && ctx.length == 1) return ctx.get(0);
if ($.is.element(ctx)) return ctx;
}
}
return document;
};
/**
* Gets the width of the current viewport.
* @param {boolean} [density=false] - If true the screens pixel density is factored into the result.
* @returns {number}
*/
$.viewportWidth = function(density){
var ratio = !!density && $.is.number(window['devicePixelRatio']) ? window['devicePixelRatio'] : 1;
return Math.max(document.documentElement.clientWidth, window.innerWidth, 0) / ratio;
};
/**
* @callback eachCallback
* @param {(number|string)} indexOrKey - The index or property key of the current item.
* @param {*} value - The current item's value.
* @this window
*/
/**
* A generic iterator function, which can be used to seamlessly iterate over both objects and arrays.
* Arrays and array-like objects with a length property (such as a function's arguments object) are
* iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
* @param {(Array|object)} target - The array or object to iterate over.
* @param {(eachCallback)} callback - The function that will be executed on every value.
*/
$.each = function (target, callback) {
var result;
if ($.is.hash(target)) {
for (var name in target) {
if (!target.hasOwnProperty(name)) continue;
result = callback.call(window, name, target[name]);
if ($.is.boolean(result) && result === false) break;
}
} else if ($.is.array(target) || $.is.self(target) || $.is.arrayLike(target)) {
for (var i = 0, len = target.length; i < len; i++) {
result = callback.call(window, i, target[i]);
if ($.is.boolean(result) && result === false) break;
}
}
};
/**
* @callback filterCallback
* @param {(number|string)} indexOrKey - The index or property key of the current item.
* @param {*} value - The current item's value.
* @returns {boolean}
* @this window
*/
/**
* Reduce the items or properties of the supplied target to those that match the callback's test.
* @param {(Array|object)} target - The array or object to iterate over.
* @param {filterCallback} callback - The function that is used to test each value.
* @returns {(Array|object)}
*/
$.filter = function(target, callback){
var result;
if ($.is.hash(target)){
result = {};
$.each(target, function(name, value){
if (callback.call(window, name, value) === true) result[name] = value;
});
} else if ($.is.array(target) || $.is.self(target) || $.is.arrayLike(target)){
result = [];
$.each(target, function(i, item){
if (callback.call(window, i, item) === true) result.push(item);
});
}
return result;
};
/**
* @callback mapCallback
* @param {*} value - The current item's value.
* @param {(number|string)} indexOrKey - The index or property key of the current value.
* @returns {*}
* @this window
*/
/**
* Translate all items in an array or object to new array of items.
* @param {(Array|object)} target - The array or object to iterate over.
* @param {mapCallback} callback - The function to process each item against.
* @returns {Array}
*/
$.map = function(target, callback){
var result = [], tmp;
$.each(target, function(indexOrKey, value){
tmp = callback.call(window, value, indexOrKey);
if ($.is.defined(tmp) && tmp !== null){
if ($.is.array(tmp)) result = result.concat(tmp);
else result.push(tmp);
}
});
return result;
};
/**
* Merge the contents of two or more objects together into the first object.
* @param {(object|boolean)} deepOrTarget - Whether to perform a deep copy or the target object to receive additional properties.
* @param {object} targetOrObject1 - Either the target object to receive additional properties or an object containing additional properties to merge in.
* @param {...object} [objectN] - Additional objects containing properties to merge in.
* @returns {object}
*/
$.extend = function (deepOrTarget, targetOrObject1, objectN) {
var args = Array.prototype.slice.call(arguments),
target = args.shift(), deep = false, src, clone;
if ($.is.boolean(target)){
deep = target;
target = args.shift() || {};
}
$.each(args, function(i, obj){
if (!$.is.hash(obj) && !$.is.array(obj)) return;
$.each(obj, function(name, value){
src = target[name];
if (target === value) return;
if (deep && $.is.array(value)){
clone = $.is.array(src) ? src : [];
target[name] = $.extend(deep, clone, value);
} else if (deep && $.is.hash(value)){
clone = $.is.hash(src) ? src : {};
target[name] = $.extend(deep, clone, value);
} else if ($.is.defined(value)) {
target[name] = value;
}
});
});
return target;
};
var mouseEvent = /^(?:mouse|pointer|contextmenu|drag|drop)|click/,
keyEvent = /^key/,
touchEvent = /^touch/,
transitionEvent = /^transition/,
animationEvent = /^animation/;
/**
* Given the name of an event, such as click or submit, this returns the type of Event object that should be instantiated.
* @param {string} name - The name of the event.
* @returns {string}
*/
$.getEventType = function(name){
return (mouseEvent.test(name)
? 'Mouse' : (keyEvent.test(name)
? 'Keyboard' : (touchEvent.test(name)
? 'Touch' : ($.browser.supports('transition') && transitionEvent.test(name)
? 'Transition' : ($.browser.supports('animation') && animationEvent.test(name)
? 'Animation' : ''))))) + 'Event'
};
/**
* Remove the whitespace from the beginning and end of a string and optional replace multiple spaces with a single space.
* @param {string} str - The string to trim.
* @param {boolean} [replaceMultiple=false] - Whether or not to replace multiple spaces with a single space.
* @returns {string}
*/
$.trim = function (str, replaceMultiple) {
if ($.is.undef(str) || str == null) return '';
if (!!replaceMultiple) str = str.replace(/[\s\uFEFF\xA0]{2,}/g, ' ');
return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
};
/**
* Convert an array-like object into a true JavaScript array.
* @param {object} arrayLike - The object to create an array from.
* @returns {Array}
*/
$.makeArray = function (arrayLike) {
var array = [];
if ($.is.arrayLike(arrayLike)) {
for (var i = 0, len = arrayLike.length, tmp; i < len; i++) {
tmp = arrayLike[i];
if ($.is.defined(tmp)) array.push(tmp);
}
}
return array;
};
/**
* Search for a specified value within an array and return its index (or -1 if not found).
* @param {*} value - The value to search for.
* @param {Array} array - An array through which to search.
* @returns {number}
*/
$.inArray = function(value, array){
var result = -1;
$.each(array, function(i, item){
if (item === value){
result = i;
return false;
}
});
return result;
};
/**
* Splits a string into an array using the separator and removes empty values from the result.
* @param {string} str - The string to split.
* @param {string} [separator= ] - The separator to use to split the string. Defaults to a single space ( ).
* @param {boolean} [removeEmpty=true] - Whether or not to remove empty values from the final result. Defaults to true.
* @param {boolean} [trimResults=true] - Whether or not to trim the values in the final result. Defaults to true.
* @returns {Array.<string>}
*/
$.split = function(str, separator, removeEmpty, trimResults){
if (!$.is.string(str)) return [];
separator = $.is.defined(separator) ? separator : ' ';
removeEmpty = $.is.boolean(removeEmpty) ? removeEmpty : true;
trimResults = $.is.boolean(trimResults) ? trimResults : true;
var result = str.split(separator);
for (var i = result.length; i >= 0; i--){
if (trimResults) result[i] = $.trim(result[i]);
if (removeEmpty && result[i] === '') result.splice(i, 1);
}
return result;
};
/**
* Parses an attributes value into either a string, boolean, number or JSON value.
* @param {(string|boolean|number)} val - The attribute's string value.
* @returns {(string|boolean|number|object)}
*/
$.parseValue = function(val){
var result = val;
if (/^false$/i.test(val)) result = false;
else if (/^true$/i.test(val)) result = true;
else if (!isNaN(val)) result = +val;
else if ($.is.string(val) && ((val.charAt(0) == '{' && val.charAt(val.length - 1) == '}') || (val.charAt(0) == '[' && val.charAt(val.length - 1) == ']'))) result = JSON.parse(val);
return result;
};
/**
* Converts the supplied string to a camel cased variant both trimming the string and replacing multiple spaces.
* @param {string} str - The string to convert.
* @returns {string}
*/
$.toCamelCase = function (str) {
return $.trim(str, true).replace(/^([A-Z])|[-\s_](\w)/g, function (match, p1, p2) {
if (p2) return p2.toUpperCase();
return p1.toLowerCase();
});
};
/**
* Converts the supplied string to a hyphen cased variant both trimming the string and replacing multiple spaces.
* @param {string} str - The string to convert.
* @returns {string}
*/
$.toHyphen = function (str) {
return $.trim(str, true).replace(/([a-z])(?:[-\s_])([A-Za-z])|([a-z])([A-Z])/g, '$1$3-$2$4').toLowerCase();
};
/**
* Checks if the first element is a child of the second.
* @param {Node} child - The child element to check.
* @param {Node} parent - The parent element to check within.
* @returns {boolean}
*/
$.isChildOf = function(child, parent){
while ((child = child.parentNode) && child !== parent) {}
return !!child;
};
/**
* Get an integer indicating the position of the supplied element within it's parent element relative to its sibling elements.
* @returns {number}
*/
$.index = function(element){
var k = 0, e = element;
while (e = e.previousElementSibling) { ++k; }
return k;
};
var elem = document.createElement('div');
/**
* Parses a string into a NodeList.
* @param {string} htmlString - The HTML string to be parsed.
* @returns {(NodeList|null)}
*/
$.parseHTML = function(htmlString){
if (!$.is.html(htmlString)) return null;
elem.innerHTML = htmlString;
return elem.childNodes;
};
/**
* Convert between an object and a parameter string using PHP array notation. Spaces are converted to plus "+" characters.
* @param {(string|object|Array)} obj - The object to convert. If a string is passed in the parameters are converted into an object.
* @param {string} [prefix] - The prefix to apply to the name.
* @returns {(string|object)}
*/
$.param = function(obj, prefix) {
if ($.is.string(obj)){
return $.param.parse(obj, prefix);
}
return $.param.stringify(obj, prefix);
};
/**
* Convert an object into a parameter string using PHP notation. Spaces are converted to plus "+" characters.
* @param {object} obj - The object to convert.
* @param {string} [prefix] - The prefix to apply to parameter names.
* @returns {string}
*/
$.param.stringify = function(obj, prefix){
var str = [], arr = $.is.arrayLike(obj);
prefix = $.is.string(prefix) ? prefix : '';
$.each(obj, function(name, value){
name = arr ? prefix + '[]' : (prefix !== '' ? prefix + '[' + name + ']' : name);
str.push(typeof value === 'object' ? $.param(value, name) : $.param.encode(name) + "=" + $.param.encode(value));
});
return str.join("&");
};
/**
* Convert a PHP notation parameter string to an object or array. "%20" and "+" characters are converted to spaces.
* @param {string} str - The parameter string to convert, this can be a full url.
* @param {string} [prefix] - The prefix to use when generating properties.
* @returns {(Array|object)}
*/
$.param.parse = function(str, prefix){
str = str.split('#')[0];
str = '?' + (str.indexOf('?') !== -1 ? str.split('?')[1] : str);
var result = {};
$.each(str.substr(1).split('&'), function(i, param){
var parts = param.split('=');
if (parts.length !== 2) return;
var name = $.param.decode(parts[0]),
value = $.param.decode(parts[1]),
regex = /\[(.+?)?]/g;
if (regex.test(name)){
regex.lastIndex = 0;
var arr = /\[]/.test(name), current = result, path = [], match, last;
while ((match = regex.exec(name)) !== null) {
if ($.is.string(match[1])) path.push(match[1]);
}
var prefixed = $.is.string(prefix);
name = name.replace(regex, '');
if (!prefixed) path.unshift(name);
if (prefixed) path.unshift(prefix);
last = path.length - 1;
$.each(path, function(i, n){
if (i === last){
if (arr){
if (n === '' && !prefixed){
n = 'array';
current.ARRAY_RESULT = true;
}
if ($.is.array(current[n])) current[n].push(value);
else current[n] = [value];
} else {
current[n] = value;
}
} else {
if ($.is.undef(current[n])) current[n] = {};
current = current[n];
}
});
} else {
result[name] = value;
}
});
return result.ARRAY_RESULT ? result.array : result;
};
/**
* URL parameter encodes the supplied string. Spaces are converted to plus "+" characters.
* @param {string} str - The string to encode.
* @returns {string}
*/
$.param.encode = function(str){
return encodeURIComponent(str).replace(/%20/g, '+');
};
/**
* URL parameter decodes the supplied string. Pluses are converted to space " " characters.
* @param {string} str - The string to decode.
* @returns {string}
*/
$.param.decode = function(str){
return $.is.string(str) ? decodeURIComponent(str.replace(/\+/g, '%20')) : str + '';
};
})(FooJitsu);
(function ($) {
if ($.version !== '1.0.5') return;
/**
* @callback beforeStartCallback
* @param {FooJitsu.Deferred} deferred - The current deferred object.
*/
/**
* A chainable utility object with methods to register multiple callbacks into callback queues, invoke callback queues,
* and relay the success or failure state of any synchronous or asynchronous function.
* @param {beforeStartCallback} beforeStart - A function that is called just before the constructor returns.
* @returns {FooJitsu.Deferred}
* @constructor
*/
$.Deferred = function(beforeStart){
if (!(this instanceof $.Deferred)) return new $.Deferred(beforeStart);
this.__callbacks__ = [];
this.currentState = 'pending';
if ($.is.fn(beforeStart)){
try {
beforeStart(this);
} catch (err) {
this.reject(err);
}
}
};
/**
* Used internally by the object to set it's state and execute callbacks.
* @param {string} type - The type to apply, can be one of the following; "resolve", "reject" or "progress"
* @param {Array} args - The arguments to execute any callbacks with.
* @private
*/
$.Deferred.prototype.__apply__ = function(type, args){
var self = this, finalize = type === 'resolve' || type === 'reject';
if (finalize){
if (type === 'reject'){
this.currentState = 'rejected';
this.then = function (resolve, reject) { reject.apply(self, args); };
}
if (type === 'resolve'){
this.currentState = 'resolved';
this.then = function (resolve) { resolve.apply(self, args); };
}
}
function execute(reg, type, args){
var callbacks = $.is.fn(reg[type]) ? [reg[type]] : ($.is.array(reg[type]) ? reg[type] : []);
$.each(callbacks, function(i, callback){
if ($.is.fn(callback)) callback.apply(self, args);
});
}
function safe_execute(reg, type, args){
try { execute(reg, type, args); }
catch (err) { execute(reg, 'reject', [err]); }
}
$.each(this.__callbacks__, function(i, reg){
safe_execute(reg, type, args);
});
if (finalize){
$.each(this.__callbacks__, function(i, reg){
safe_execute(reg, 'always', args);
});
this.__callbacks__ = [];
this.resolve = this.reject = this.notify = $.noop;
}
};
/**
* Determine the current state of a deferred object, can be one of the following; "pending", "resolved" or "rejected"
* @returns {string}
*/
$.Deferred.prototype.state = function(){
return this.currentState;
};
/**
* Resolve a deferred object and call any doneCallbacks with the given args.
* @param {...*} args - Any number of arguments to supply to the doneCallbacks.
*/
$.Deferred.prototype.resolve = function(args){
this.__apply__('resolve', Array.prototype.slice.call(arguments));
};
/**
* Reject a deferred object and call any failCallbacks with the given args.
* @param {...*} args - Any number of arguments to supply to the failCallbacks.
*/
$.Deferred.prototype.reject = function(args){
this.__apply__('reject', Array.prototype.slice.call(arguments));
};
/**
* Call the progressCallbacks on a deferred object with the given args.
* @param {...*} args - Any number of arguments to supply to the progressCallbacks.
*/
$.Deferred.prototype.notify = function(args){
this.__apply__('progress', Array.prototype.slice.call(arguments));
};
/**
* @callback alwaysCallback
* @param {...*} [args] - Any number of arguments to supply to the alwaysCallbacks.
*/
/**
* @callback doneCallback
* @param {...*} [args] - Any number of arguments to supply to the doneCallbacks.
*/
/**
* @callback failCallback
* @param {...*} [args] - Any number of arguments to supply to the failCallbacks.
*/
/**
* @callback progressCallback
* @param {...*} [args] - Any number of arguments to supply to the progressCallbacks.
*/
/**
* Add handlers to be called when the deferred object is resolved, rejected, or still in progress.
* @param {(doneCallback|Array.<doneCallback>)} doneCallbacks - A function, or array of functions, called when the deferred is resolved.
* @param {(failCallback|Array.<failCallback>)} failCallbacks - A function, or array of functions, called when the deferred is rejected.
* @param {(progressCallback|Array.<progressCallback>)} progressCallbacks - A function, or array of functions, called when the deferred notifies progress.
* @returns {FooJitsu.Deferred}
*/
$.Deferred.prototype.then = function(doneCallbacks, failCallbacks, progressCallbacks){
this.__callbacks__.push({resolve: doneCallbacks, reject: failCallbacks, progress: progressCallbacks});
return this;
};
/**
* Add handlers to be called when the deferred object is either resolved or rejected.
* @param {(alwaysCallback|Array.<alwaysCallback>)} alwaysCallbacks - A function, or array of functions, that is called when the deferred is resolved or rejected.
* @returns {FooJitsu.Deferred}
*/
$.Deferred.prototype.always = function(alwaysCallbacks){
this.__callbacks__.push({always: alwaysCallbacks});
return this;
};
/**
* Add handlers to be called when the deferred object is resolved.
* @param {(doneCallback|Array.<doneCallback>)} doneCallbacks - A function, or array of functions, called when the deferred is resolved.
* @returns {FooJitsu.Deferred}
*/
$.Deferred.prototype.done = function(doneCallbacks){
this.__callbacks__.push({resolve: doneCallbacks});
return this;
};
/**
* Add handlers to be called when the deferred object is rejected.
* @param {(failCallback|Array.<failCallback>)} failCallbacks - A function, or array of functions, called when the deferred is rejected.
* @returns {FooJitsu.Deferred}
*/
$.Deferred.prototype.fail = function(failCallbacks){
this.__callbacks__.push({reject: failCallbacks});
return this;
};
/**
* Add handlers to be called when the deferred object generates progress notifications.
* @param {(progressCallback|Array.<progressCallback>)} progressCallbacks - A function, or array of functions, to be called when the deferred generates progress notifications.
* @returns {FooJitsu.Deferred}
*/
$.Deferred.prototype.progress = function(progressCallbacks){
this.__callbacks__.push({progress: progressCallbacks});
return this;
};
/**
* Provides a way to execute callback functions based on one or more deferred objects that represent asynchronous events.
* @param {...FooJitsu.Deferred} deferreds - One or more deferred objects.
* @returns {FooJitsu.Deferred}
*/
$.when = function(deferreds){
var args = Array.prototype.slice.call(arguments);
return new $.Deferred(function(d){
var expected = args.length, results = {length: 0};
$.each(args, function(i, deferred){
deferred.then(function(result){
results.length++;
results[i] = result;
if (results.length === expected) d.resolve($.makeArray(results));
}, function(err){
d.reject(err);
});
});
});
};
})(FooJitsu);
(function($){
if ($.version !== '1.0.5') return;
/**
* The expando property name used by this instance of FooJitsu.
* @type {string}
*/
$.expando = 'foojitsu-'+Math.random().toString(36).substring(7);
/**
* This class is to make working with per element data storage simple.
* @returns {FooJitsu.CacheManager}
* @constructor
*/
$.CacheManager = function(){