/
chikuwa.js
1666 lines (1590 loc) · 36.4 KB
/
chikuwa.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
/**
* chikuwa.js
* Copyright (c) 2011 CyberAgent, Inc.
* License: MIT (http://www.opensource.org/licenses/mit-license)
* GitHub: https://github.com/suguru/chikuwa.js
*/
(function(w) {
var doc = w.document,
root = w.document.documentElement,
cssnum = {'column-count':1,columns:1,'font-weight': 1,'line-height':1,opacity:1,'z-index':1,zoom:1},
slice = function(args) {
return Array.prototype.slice.apply(args);
}
// is string
function isString(o) { return typeof o === 'string' || o instanceof String; }
// is function
function isFunction(o) { return typeof o === 'function' || o instanceof Function; }
// is number
function isNumber(o) { return typeof o === 'number' || o instanceof Number; }
// is chikuwa object
function isChikuwa(o) { return o instanceof chikuwa; }
// is object
function isObject(o) { return o instanceof Object; }
// is array
function isArray(o) { return o instanceof Array; }
// is undefined or null
function isNull(o) { return o === undefined || o === null; }
// is empty object
function isEmptyObject(o) { for(var b in o){return false;}return true; }
// convert undefined/null to empty string
function nullToEmpty(o) { return isNull(o) ? '' : o; }
// trim string
// function trim(str) { return str.replace(/^\s+|\s+$/g,''); }
// convert camel case
function camelcase(str) { return str.replace(/-([a-z])/g, function(c) {return c.toUpperCase().replace('-','');}); }
// revert camel case
function dashed(str) { return str.replace(/([A-Z])/g,function(c) {return '-'+c.toLowerCase();}); }
// add px string to css property if required
function addpx(name,value) { return (isNumber(value) && isNull(cssnum[name])) ? value + 'px' : value; }
// convert array to map names
function arrayToMap(array) {
var o = {};
for (var i = 0; i < array.length; i++) {
o[array[i]] = 1;
}
return o;
}
// trim string
function trim(string) {
return string ? string.replace(/^\s+|\s+$/g,'') : '';
}
// escape html
function escapeHtml(string) {
return tag('span').text(string).inner();
}
// cross browser css applicants
function xbcss(css,name,value) {
if (!value) return;
['-webkit-'+name,'-moz-'+name,'-ms-'+name,'-o-'+name,name].forEach(function(n) {
css[n] = value;
});
}
var _cid = 0;
function cid(element) {
return element._cid || (element._cid = ++_cid);
}
// create an event object
function createEvent(type,target,source) {
try {
var evt = new Event(type);
evt.target = target;
return evt;
} catch (e) {
var currentTarget = (target instanceof chikuwa) ? target.get(0) : null;
return {
type: type,
target: target,
currentTarget: currentTarget,
custom: true,
source: source,
stopPropagation: function() {
if (source) {
source.stopPropagation();
}
},
preventDefault: function() {
if (source) {
source.preventDefault();
}
}
};
}
}
/**
* main constructor of chikuwa object
* @param s selector
* @param p parent
*
* chikuwa class
* property
* s: selector
* p: parent
* c: context
*/
function chikuwa(selector,parent) {
var self = this;
if (isString(selector)) {
self.c = parent.querySelectorAll(selector);
} else if (isArray(selector)) {
self.c = selector;
} else {
self.c = [selector];
}
}
/**
* shortcut function for creating chikwua object
* @param s selector
*/
function $(selector) {
if (isNull(selector))
return null;
if (selector instanceof chikuwa)
return selector;
else
return new chikuwa(selector,doc);
}
// event shortcuts
var eventShortcut = {
// added to DOM tree
'added' : 'DOMNodeInsertedIntoDocument',
// removed from DOM Tree
'removed': 'DOMNodeRemovedFromDocument',
// added to DOM node
'addedToNode' : 'DOMNodeInserted',
// removed from DOM node
'removedFromNode' : 'DOMNodeRemoved'
};
function eventName(name) {
if (name in eventShortcut) {
return eventShortcut[name];
} else {
return name;
}
}
/**
* Prototyping of chikuwa object
*/
chikuwa.prototype = {
constructor: chikuwa,
/**
* get length of contexts.
* @returns
*/
length: function() {
return this.c.length;
},
/**
* check existence by index.
* @param i index
* @returns {Boolean}
*/
exist: function(i) {
var c = this.c;
return i >= 0 && i < c.length && (isArray(c) ? !isNull(c[i]) : !isNull(c.item(i)));
},
/**
* get element by index.
* @param i index
* @returns {Element}
*/
get: function(i) {
if (isArray(this.c))
return this.c[i];
else
return this.c.item(i);
},
/**
* iterate all object in contexts
* @param cb callback function (element)
* @returns {chikuwa}
*/
each: function(cb) {
var a = this.c instanceof Array;
for (var i = 0; i < this.c.length; i++) {
if (a)
cb.call(this.c[i]);
else
cb.call(this.c.item(i));
}
return this;
},
/**
* find from children
* @param s selector
*/
find: function(s) {
var r = [];
this.each(function() {
var l = this.querySelectorAll(s);
var length = l.length;
for (var i = 0; i < length; i++) {
r.push(l.item(i));
}
});
return $(r);
},
/**
* get offset values of the first element.
* default coordinates contain scrollLeft/scrollTop.
* @param o options (scroll=do not contain scrollLeft/scrollTop)
* @return
*/
bound: function(o) {
var b = this.get(0).getBoundingClientRect();
// check scroll coordinate
var sx = (o && o.screen) ? 0 : doc.body.scrollLeft;
var sy = (o && o.screen) ? 0 : doc.body.scrollTop;
return {
left: Math.floor(b.left + sx),
right: Math.floor(b.right + sx),
top: Math.floor(b.top + sy),
bottom: Math.floor(b.bottom + sy),
width: Math.floor(b.width),
height: Math.floor(b.height)
};
},
/**
* get/set width of elements.
* @param w width
* @returns width
*/
width: function(w) {
if (w === undefined) {
return this.bound().width;
} else {
if (typeof w === 'number') {
w = w + 'px';
}
return this.css({width:w});
}
},
/**
* get/set height of elements.
* @param h height
* @returns height
*/
height: function(h) {
if (h === undefined) {
return this.bound().height;
} else {
if (typeof h === 'number') {
h = h + 'px';
}
return this.css({height:h});
}
},
/**
* get/set position of elements
*/
position: function(pos) {
if (pos) {
this.css({
left: pos.x,
top: pos.y
});
} else {
var bounds = this.bound();
return {
x: bounds.left,
y: bounds.top
};
}
return this;
},
/**
* get/set css values of the first/all elements
* @param name
* @returns
*/
css: function(name) {
var css;
if (isString(name)) {
return this.get(0).style[camelcase(name)] || getComputedStyle(this.get(0), '').getPropertyValue(name);
} else if (arguments.length === 0) {
css = {};
this.get(0).style.cssText.split(';').forEach(function(cssline) {
var match = cssline.match(/\s*(.+)\s*:\s*(.+)\s*/);
if (match)
css[match[1]] = match[2];
});
return css;
} else {
css='';
var reset = name.reset;
delete name.reset;
for (var n in name) {
var d = dashed(n);
css += (d + ':' + addpx(n,name[n]) + ';');
}
this.each(function() {
if (reset)
this.style.cssText = css;
else
this.style.cssText += css;
});
return this;
}
},
/**
* check css property existence of the first element.
* @param name
* @returns
*/
hasClass: function(name) {
if (!this.exist(0)) return undefined;
var has = false;
this.each(function() {
var className = this.className;
// check clas existence
has = (' ' + nullToEmpty(className) + ' ').indexOf(' ' + name + ' ') >= 0;
return has;
});
return has;
},
/**
* apply class changes of the map
*/
cls: function(values) {
if (isNull(values)) {
var o = {};
this.each(function() {
$.merge(o, arrayToMap(this.className.split(/\s+/g)));
});
return o;
}
var add = [], remove = [], i = 0;
if (isString(values)) {
add = [values];
} else if (isArray(values)) {
add = arrayToMap(values);
} else {
$.map(values, function(name,value) {
if (value === 1)
add.push(name);
else
remove.push(name);
});
}
// add classes
this.each(function() {
var classmap = arrayToMap(this.className.split(/\s+/g));
// add classes
for (i = 0; i < add.length; i++) {
var name = add[i];
if (name)
classmap[name] = 1;
}
// remove classes
for (i = 0; i < remove.length; i++) {
delete classmap[remove[i]];
}
var cl = '';
$.map(classmap, function(name) {
if (cl.length === 0)
cl += name;
else
cl += ' ' + name;
});
this.className = cl;
});
return this;
},
/**
* bind event listener to the elements
* @param name name
* @param cb callback
* @param uc useCapture
* @returns {chikuwa}
*/
on: function(name,callback,useCapture) {
name = eventName(name);
this.each(function() {
function ccb(e) {
var target = e.currentTarget;
var ret = callback.call(target,e);
return ret;
}
ccb._cb = callback;
ccb._uc = useCapture;
this.addEventListener(name,ccb,useCapture);
var handlers = this._handlers || (this._handlers = {});
var handler = handlers[name] || (handlers[name] = []);
if (handler.indexOf(ccb) < 0) {
handler.push(ccb);
} else {
alert(callback);
}
});
return this;
},
/**
* trigger event of the elements.
* @param name
*/
trigger: function(name,e) {
name = eventName(name);
var self = this;
var evt = createEvent(name,self,e);
this.each(function() {
if (evt.custom) {
var h = this._handlers;
if (h && name in h) {
var list = h[name];
list.forEach(function(cb) {
return cb.call(self,evt);
});
}
} else {
this.dispatchEvent(evt);
}
});
},
/**
* add event listener which invoke once.
* @param name event type
* @param cb callback
* @param uc (optional) useCapture flag
* @returns {chikuwa}
*/
once: function(name,cb,uc) {
name = eventName(name);
this.each(function() {
var self = this;
self.addEventListener(name,function(e) {
self.removeEventListener(name,arguments.callee,uc);
return cb.call(self,e);
},uc);
});
return this;
},
/**
* remove event listener of elements.
* @param name event type
* @param cb (optional) callback
* @returns {chikuwa}
*/
off : function(name,cb) {
name = eventName(name);
this.each(function() {
var h = this._handlers;
if (h && name in h) {
var i;
var a = h[name];
if (cb) {
for (i = 0; i<a.length; i++)
if (a[i]._cb === cb) {
this.removeEventListener(name,a[i],a[i]._uc);
a.splice(i,1);
if (a.length === 0)
delete h[name];
break;
}
} else {
for (i=0; i<a.length; i++)
this.removeEventListener(name,a[i],a[i]._uc);
delete h[name];
}
}
});
return this;
},
/**
* get/set text of the elements.
* @param text text to set
* @returns
*/
text: function(text) {
var alen = arguments.length;
if (alen === 0) {
var t = '';
this.each(function() {
t += this.textContent;
});
return t;
} else {
if (alen > 1)
text = $.format.apply($,arguments);
text = text || '';
return this.each(function() { this.textContent = text; });
}
},
/**
* get or set value
*/
value: function(v) {
var f = this.get(0);
if(v !== null && v !== undefined) {
//setter
if(f) f.value = v;
return this;
} else {
//getter
if(f) return f.value || '';
return '';
}
},
/**
* checked or not
*/
checked: function(){
var f = this.get(0);
return f.checked || false;
},
/**
* get the parent node.
*/
parent: function() {
var f = this.get(0);
if (f) {
return $(f.parentNode);
} else {
return null;
}
},
/**
* append element to the first one.
* @param e
* @returns {chikuwa}
*/
append: function(e) {
if (isNull(e)) return this;
var a = $(e);
var f = this.get(0);
if (f)
a.each(function() {
f.appendChild(this);
});
return this;
},
/**
* prepend element to the first one.
* @param e
*/
prepend: function(e) {
if (isNull(e)) return this;
var a = $(e);
var f = this.get(0);
if (f)
a.each(function() {
if (f.childNodes.length > 0 && f.firstChild)
f.insertBefore(this,f.firstChild);
else
f.appendChild(this);
});
return this;
},
replace: function(e, t) {
if (isNull(e)) return this;
var a = $(e);
var f = this.get(0);
var r = t.get(0);
if (f)
a.each(function() {
if (f.childNodes.length > 0 && r && r.parentNode == f)
f.insertBefore(this,r);
else
f.appendChild(this);
f.removeChild(r);
});
return this;
},
/**
* remove elements from the content.
* @returns {chikuwa}
*/
remove: function() {
this.each(function() {
if (this.parentNode)
this.parentNode.removeChild(this);
});
return this;
},
/**
* set empty string
*/
empty: function() {
return this.inner('');
},
/**
* get the html of this tag
*/
html: function() {
var args = slice(arguments)
if (args.length === 0) {
var html = '';
this.each(function() {
if (this.outerHTML)
html += this.outerHTML;
});
return html;
} else {
return this.each(function() {
this.outerHTML = args[0];
});
}
},
inner: function() {
var args = slice(arguments);
if (args.length === 0) {
var html = '';
this.each(function() {
if (this.innerHTML)
html += this.innerHTML;
});
return html;
} else {
return this.each(function() {
this.innerHTML = args[0];
});
}
},
/**
* generate tagged content.
* @param name tag name
* @param opts (optional) attribute map
* @returns
*/
tag: function(name,opts) {
var t = $.tag(name,opts);
t._p = this;
return t;
},
/**
* end generation and append to the parent.
* @returns
*/
gat: function() {
var p = this._p;
delete this._p;
return p.append(this);
},
/**
* append textnode
* @returns
*/
appendText: function(text) {
var alen = arguments.length;
if (alen > 1){
text = $.format.apply($,arguments);
}
text = text || '';
this.append(doc.createTextNode(text));
return this;
},
/**
* execute function in method chain
* @param cb
*/
exec: function(cb) {
cb.call(this);
return this;
},
/**
* get/set attribute to elements.
* @param name
* @param value
* @returns
*/
attr: function(name,value) {
var self = this;
var f;
if (name === undefined && value === undefined) {
if (arguments.length === 1)
return this;
f = self.get(0);
var o = {};
if (f && f.attributes) {
var length = f.attributes.length;
for (var i = 0; i < length; i++) {
var attr = f.attributes[i];
o[attr.name] = attr.value;
}
}
return o;
} else if (value === undefined && isObject(name)) {
for (var n in name)
self.attr(n,name[n]);
} else if (isString(name) && value === undefined) {
f = self.get(0);
if (f.hasAttribute(name)) {
return f.getAttribute(name);
} else {
return undefined;
}
} else {
this.each(function() {
if (isNull(value)) {
this.removeAttribute(name);
} else {
if (name === 'cls') {
self.cls(value);
} else {
this.setAttribute(name,value);
}
}
});
}
return this;
},
/**
* set data attributes to the element
*/
data: function(name,value) {
var self = this;
var n;
if (arguments.length === 0) {
var attrs = this.attr();
var obj = {};
for (n in attrs) {
if (n.indexOf('data-') === 0)
obj[n.substring(5)] = attrs[n];
}
return obj;
} else if (arguments.length === 1) {
if (isString(name)) {
return this.attr('data-'+name);
} else {
var attr = {};
for (n in name) {
attr['data-'+n] = name[n];
}
this.attr(attr);
}
} else {
this.attr('data-'+name,value);
}
return this;
},
/**
* tap event support touches
*/
tap: function(cb) {
var self = this;
if (cb) {
self.on('tap', function(e) {
return cb.call(this,e);
});
// bind click if not support touch event
if ($.os.touch) {
var target = null;
self
// start detecting touches
.on('touchstart', function(e) {
target = $(this);
target.__tap = {};
var finger = e.changedTouches[0];
target.__tap.x = finger.clientX;
target.__tap.y = finger.pageY + doc.body.scrollTop;
})
.on('touchmove', function(e) {
var finger = e.changedTouches[0];
target.__tap.toX = finger.clientX;
target.__tap.toY = finger.pageY + doc.body.scrollTop;
var __tap = target.__tap;
if (!(Math.abs((__tap.toX || __tap.x) - __tap.x) <= 10 &&
Math.abs((__tap.toY || __tap.y) - __tap.y) <= 10)) {
// trigger tapout
target.trigger('tapout',e);
return false;
}
})
.on('touchend', function(e) {
e.preventDefault();
e.stopPropagation();
// get the rectangle bounds of the element
var bounds = target.bound();
// var bounds = self.bound({screen:1});
// get the finger
var finger = e.changedTouches[0];
// calculate touch point of the element
var x = finger.clientX - bounds.left;
var y = finger.pageY - bounds.top;
var __tap = target.__tap;
if (x >= 0 &&
Math.abs((__tap.toX || __tap.x) - __tap.x) <= 5 &&
Math.abs((__tap.toY || __tap.y) - __tap.y) <= 5 &&
x <= bounds.width &&
y >= 0 &&
y <= bounds.height &&
!target.attr('disabled')
) {
// trigger tap
target.trigger('tap',e);
}
delete target.__tap;
target = null;
});
} else {
self.click(function(e) {
$(this).trigger('tap',e);
});
}
} else {
self.trigger('tap');
}
return self;
},
/**
* over event support touches
*/
over: function(cb) {
var self = this;
self.on('over',function(e) {
var target = this;
target.__over=true;
cb.call(target,e);
});
function triggerOver(e) {
$(this).trigger('over',e);
}
if ($.os.touch)
if (cb)
self
.on('touchstart', triggerOver);
else
triggerOver();
else
self.mouseover(triggerOver);
return self;
},
/**
* tapout event support touches
*/
tapout: function(cb) {
var self = this;
self.on('tapout',function(e) {
var target = this;
// target.__tapout=true;
cb.call(target,e);
});
return self;
},
/**
* out event support touches
*/
out: function(cb) {
var self = this;
self.on('out',function(e) {
var target = this;
if (target.__over) {
delete target.__over;
cb.call(target,e);
}
});
function triggerOut(e) {
$(this).trigger('out',e);
}
if ($.os.touch)
if (cb)
self
.on('touchend', triggerOut);
else
triggerOut();
else
self.mouseout(triggerOut);
return self;
},
/**
* transform
*/
transform: function(transform, options) {
var css = {};
var opts = options || {};
xbcss(css,'transform', transform || 'none');
xbcss(css,'transform-origin', opts.origin);
return this.css(css);
},
/**
* transition
*/
transition: function(options, callback) {
var that = this;
var opts = options || {};
var duration = 'duration' in opts ? opts.duration : 0.5;
var css = {};
if (!options.no3d) {
xbcss(css,'transform-style', 'preserve-3d');
}
xbcss(css,'transition', 'all ' + duration + 's ' + (opts.ease || ''));
that.css(css);
if (typeof options === 'function') callback = options;
var handler = function() {
if (callback) {
callback.apply(that);
}
};
if (handler)
if ($.os.webkit)
that.once('webkitTransitionEnd',handler);
else
setTimeout(handler,duration*1000);
return this;
},
/**
* rotate transform
*/
rotate: function(deg,options,callback) {
return this.transform('rotate('+deg+'deg)',options,callback);
},
/**
* translate transform
*/
translate: function(x,y,options,callback) {
return this.transform('translate('+x+'px,'+y+'px)',options,callback);
},
/**
* scale transform
*/
scale: function(scale,options,callback) {
return this.transform('scale('+scale+','+scale+')',options,callback);
},
/**
* visibility
*/
visible: function(visible) {
return this.css({visibility:visible ? 'visible':'hidden'});
},
/**
* disable all events
*/
disable: function() {
var self = this;
function prevent(e) {
e.stopPropagation();
e.preventDefault();
}
['touchstart','touchend','touchmove','mouseover','mouseout','mousedown','mouseup','click']
.forEach(function(n) {
self.on(n, prevent);
});
return self;
}
};
// register shorthand for bind/trigger
['click','blur','change','error','focus',
'keydown','keyup','keypress','keyup',
'load','unload',
'mousedown','mouseup','mousemove','mouseout','mouseover',
'resize','scroll','select','submit',
'touchstart','touchmove','touchend']
.forEach(function(name) {
chikuwa.prototype[name] = function(cb) {
if (cb)
this.on(name, cb);
else
this.trigger(name);
return this;
};
});
// window
$.win = $(w);
// document
$.doc = $(doc);
// root element
$.root = $(root);
// browser detection
var ua = navigator.userAgent;
$.os = {
ua: ua,
touch: 'ontouchstart' in document.documentElement,
ios: /ip(hone|od|ad)/i.test(ua),
iphone: /iphone/i.test(ua),
ipad: /ipad/i.test(ua),