forked from intel/appframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jq.mobi.js
1896 lines (1765 loc) · 71 KB
/
jq.mobi.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
/**
* jqMobi is a query selector class for HTML5 mobile apps on a WebkitBrowser.
* Since most mobile devices (Android, iOS, webOS) use a WebKit browser, you only need to target one browser.
* We are able to increase the speed greatly by removing support for legacy desktop browsers and taking advantage of browser features, like native JSON parsing and querySelectorAll
* MIT License
* @author AppMobi
* @api private
*/
if (!window.jq || typeof (jq) !== "function") {
/**
* This is our master jq object that everything is built upon.
* $ is a pointer to this object
* @title jqMobi
* @api private
*/
var jq = (function(window) {
"use strict";
var undefined, document = window.document,
emptyArray = [],
slice = emptyArray.slice,
classCache = [],
eventHandlers = [],
_eventID = 1,
jsonPHandlers = [],
_jsonPID = 1,
fragementRE=/^\s*<(\w+)[^>]*>/,
_attrCache={};
/**
* Internal function to test if a class name fits in a regular expression
* @param {String} name to search against
* @return {Boolean}
* @api private
*/
function classRE(name) {
return name in classCache ? classCache[name] : (classCache[name] = new RegExp('(^|\\s)' + name + '(\\s|$)'));
}
/**
* Internal function that returns a array of unique elements
* @param {Array} array to compare against
* @return {Array} array of unique elements
* @api private
*/
function unique(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr.indexOf(arr[i]) != i) {
arr.splice(i, 1);
continue;
}
}
return arr;
}
/**
* Given a set of nodes, it returns them as an array. Used to find
* siblings of an element
* @param {Nodelist} Node list to search
* @param {Object} [element] to find siblings off of
* @return {Array} array of sibblings
* @api private
*/
function siblings(nodes, element) {
var elems = [];
if (nodes == undefined)
return elems;
for (; nodes; nodes = nodes.nextSibling) {
if (nodes.nodeType == 1 && nodes !== element) {
elems.push(nodes);
}
}
return elems;
}
/**
* This is the internal jqMobi object that gets extended and added on to it
* This is also the start of our query selector engine
* @param {String|Element|Object|Array} selector
* @param {String|Element|Object} [context]
*/
var $jqm = function(toSelect, what) {
this.length = 0;
if (!toSelect) {
return this;
} else if (toSelect instanceof $jqm && what == undefined) {
return toSelect;
} else if ($.isFunction(toSelect)) {
return $(document).ready(toSelect);
} else if ($.isArray(toSelect) && toSelect.length != undefined) { //Passing in an array or object
for (var i = 0; i < toSelect.length; i++)
this[this.length++] = toSelect[i];
return this;
} else if ($.isObject(toSelect) && $.isObject(what)) { //var tmp=$("span"); $("p").find(tmp);
if (toSelect.length == undefined) {
if (toSelect.parentNode == what)
this[this.length++] = toSelect;
} else {
for (var i = 0; i < toSelect.length; i++)
if (toSelect[i].parentNode == what)
this[this.length++] = toSelect[i];
}
return this;
} else if ($.isObject(toSelect) && what == undefined) { //Single object
this[this.length++] = toSelect;
return this;
} else if (what !== undefined) {
if (what instanceof $jqm) {
return what.find(toSelect);
}
} else {
what = document;
}
var dom = this.selector(toSelect, what);
if (!dom) {
return this;
}
//reverse the query selector all storage
else if ($.isArray(dom)) {
for (var j = 0; j < dom.length; j++) {
this[this.length++] = dom[j];
}
} else {
this[this.length++] = dom;
return this;
}
return this;
};
/**
* This calls the $jqm function
* @param {String|Element|Object|Array} selector
* @param {String|Element|Object} [context]
*/
var $ = function(selector, what) {
return new $jqm(selector, what);
};
/**
* this is the query selector library for elements
* @param {String} selector
* @param {String|Element|Object} [context]
* @api private
*/
function _selector(selector, what) {
var dom;
try {
selector=selector.trim();
if (selector[0] === "#" && selector.indexOf(" ") === -1 && selector.indexOf(">") === -1) {
if (what == document)
dom = what.getElementById(selector.replace("#", ""));
else
dom = [].slice.call(what.querySelectorAll(selector));
} else if (selector[0] === "<" && selector[selector.length - 1] === ">") //html
{
var tmp = document.createElement("div");
tmp.innerHTML = selector.trim();
dom = [].slice.call(tmp.childNodes);
} else {
dom = [].slice.call(what.querySelectorAll(selector));
}
} catch (e) {
}
return dom;
}
/**
* Map takes in elements and executes a callback function on each and returns a collection
```
$.map([1,2],function(ind){return ind+1});
```
* @param {Array|Object} elements
* @param {Function} callback
* @return {Object} jqMobi object with elements in it
* @title $.map(elements,callback)
*/
$.map = function(elements, callback) {
var value, values = [],
i, key;
if ($.isArray(elements))
for (i = 0; i < elements.length; i++) {
value = callback(elements[i], i);
if (value !== undefined)
values.push(value);
}
else if ($.isObject(elements))
for (key in elements) {
if (!elements.hasOwnProperty(key))
continue;
value = callback(elements[key], key);
if (value !== undefined)
values.push(value);
}
return $([values]);
};
/**
* Iterates through elements and executes a callback. Returns if false
```
$.each([1,2],function(ind){console.log(ind);});
```
* @param {Array|Object} elements
* @param {Function} callback
* @return {Array} elements
* @title $.each(elements,callback)
*/
$.each = function(elements, callback) {
var i, key;
if ($.isArray(elements))
for (i = 0; i < elements.length; i++) {
if (callback(i, elements[i]) === false)
return elements;
}
else if ($.isObject(elements))
for (key in elements) {
if (!elements.hasOwnProperty(key))
continue;
if (callback(key, elements[key]) === false)
return elements;
}
return elements;
};
/**
* Extends an object with additional arguments
```
$.extend({foo:'bar'});
$.extend(element,{foo:'bar'});
```
* @param {Object} [target] element
* @param any number of additional arguments
* @return {Object} [target]
* @title $.extend(target,{params})
*/
$.extend = function(target) {
if (target == undefined)
target = this;
if (arguments.length === 1) {
for (var key in target)
this[key] = target[key];
return this;
} else {
slice.call(arguments, 1).forEach(function(source) {
for (var key in source)
target[key] = source[key];
});
}
return target;
};
/**
* Checks to see if the parameter is an array
```
var arr=[];
$.isArray(arr);
```
* @param {Object} element
* @return {Boolean}
* @example $.isArray([1]);
* @title $.isArray(param)
*/
$.isArray = function(obj) {
return obj != undefined && obj instanceof Array && obj['push'] != undefined; //ios 3.1.3 doesn't have Array.isArray
};
/**
* Checks to see if the parameter is a function
```
var func=function(){};
$.isFunction(func);
```
* @param {Object} element
* @return {Boolean}
* @title $.isFunction(param)
*/
$.isFunction = function(obj) {
return obj != undefined && typeof obj === "function";
};
/**
* Checks to see if the parameter is a object
```
var foo={bar:'bar'};
$.isObject(foo);
```
* @param {Object} element
* @return {Boolean}
* @title $.isObject(param)
*/
$.isObject = function(obj) {
return obj != undefined && typeof obj === "object";
}
/**
* Prototype for jqm object. Also extens $.fn
*/
$.fn = $jqm.prototype = {
constructor: $jqm,
forEach: emptyArray.forEach,
reduce: emptyArray.reduce,
push: emptyArray.push,
indexOf: emptyArray.indexOf,
concat: emptyArray.concat,
selector: _selector,
oldElement: undefined,
slice: emptyArray.slice,
/**
* This is a utility function for .end()
* @param {Object} params
* @return {Object} a jqMobi with params.oldElement set to this
* @api private
*/
setupOld: function(params) {
if (params == undefined)
return $();
params.oldElement = this;
return params;
},
/**
* This is a wrapper to $.map on the selected elements
```
$().map(function(){this.value+=ind});
```
* @param {Function} callback
* @return {Object} a jqMobi object
* @title $().map(function)
*/
map: function(fn) {
return $.map(this, function(el, i) {
return fn.call(el, i, el);
});
},
/**
* Iterates through all elements and applys a callback function
```
$().each(function(){console.log(this.value)});
```
* @param {Function} callback
* @return {Object} a jqMobi object
* @title $().each(function)
*/
each: function(callback) {
this.forEach(function(el, idx) {
callback.call(el, idx, el);
});
return this;
},
/**
* This is executed when DOMContentLoaded happens, or after if you've registered for it.
```
$(document).ready(function(){console.log('I'm ready');});
```
* @param {Function} callback
* @return {Object} a jqMobi object
* @title $().ready(function)
*/
ready: function(callback) {
if (document.readyState === "complete" || document.readyState === "loaded")
callback();
document.addEventListener("DOMContentLoaded", callback, false);
return this;
},
/**
* Searches through the collection and reduces them to elements that match the selector
```
$("#foo").find('.bar');
$("#foo").find($('.bar'));
$("#foo").find($('.bar').get());
```
* @param {String|Object|Array} selector
* @return {Object} a jqMobi object filtered
* @title $().find(selector)
*/
find: function(sel) {
if (this.length === 0)
return undefined;
var elems = [];
var tmpElems;
for (var i = 0; i < this.length; i++) {
tmpElems = ($(sel, this[i]));
for (var j = 0; j < tmpElems.length; j++) {
elems.push(tmpElems[j]);
}
}
return $(unique(elems));
},
/**
* Gets or sets the innerHTML for the collection.
* If used as a get, the first elements innerHTML is returned
```
$("#foo").html(); //gets the first elements html
$("#foo").html('new html');//sets the html
```
* @param {String} html to set
* @return {Object} a jqMobi object
* @title $().html([html])
*/
html: function(html) {
if (this.length === 0)
return undefined;
if (html === undefined)
return this[0].innerHTML;
for (var i = 0; i < this.length; i++) {
this[i].innerHTML = html;
}
return this;
},
/**
* Gets or sets the innerText for the collection.
* If used as a get, the first elements innerText is returned
```
$("#foo").text(); //gets the first elements text;
$("#foo").text('new text'); //sets the text
```
* @param {String} text to set
* @return {Object} a jqMobi object
* @title $().text([text])
*/
text: function(text) {
if (this.length === 0)
return undefined;
if (text === undefined)
return this[0].textContent;
for (var i = 0; i < this.length; i++) {
this[i].textContent = text;
}
return this;
},
/**
* Gets or sets a css property for the collection
* If used as a get, the first elements css property is returned
```
$().css("background"); // Gets the first elements background
$().css("background","red") //Sets the elements background to red
```
* @param {String} attribute to get
* @param {String} value to set as
* @return {Object} a jqMobi object
* @title $().css(attribute,[value])
*/
css: function(attribute, value, obj) {
var toAct = obj != undefined ? obj : this[0];
if (this.length === 0)
return undefined;
if (value == undefined && typeof (attribute) === "string") {
var styles = window.getComputedStyle(toAct);
return toAct.style[attribute] ? toAct.style[attribute]: window.getComputedStyle(toAct)[attribute] ;
}
for (var i = 0; i < this.length; i++) {
if ($.isObject(attribute)) {
for (var j in attribute) {
this[i].style[j] = attribute[j];
}
} else {
this[i].style[attribute] = value;
}
}
return this;
},
/**
* Sets the innerHTML of all elements to an empty string
```
$().empty();
```
* @return {Object} a jqMobi object
* @title $().empty()
*/
empty: function() {
for (var i = 0; i < this.length; i++) {
this[i].innerHTML = '';
}
return this;
},
/**
* Sets the elements display property to "none".
* This will also store the old property into an attribute for hide
```
$().hide();
```
* @return {Object} a jqMobi object
* @title $().hide()
*/
hide: function() {
if (this.length === 0)
return this;
for (var i = 0; i < this.length; i++) {
if (this.css("display", null, this[i]) != "none") {
this[i].setAttribute("jqmOldStyle", this.css("display", null, this[i]));
this[i].style.display = "none";
}
}
return this;
},
/**
* Shows all the elements by setting the css display property
* We look to see if we were retaining an old style (like table-cell) and restore that, otherwise we set it to block
```
$().show();
```
* @return {Object} a jqMobi object
* @title $().show()
*/
show: function() {
if (this.length === 0)
return this;
for (var i = 0; i < this.length; i++) {
if (this.css("display", null, this[i]) == "none") {
this[i].style.display = this[i].getAttribute("jqmOldStyle") ? this[i].getAttribute("jqmOldStyle") : 'block';
this[i].removeAttribute("jqmOldStyle");
}
}
return this;
},
/**
* Toggle the visibility of a div
```
$().toggle();
$().toggle(true); //force showing
```
* @param {Boolean} [show] -force the hiding or showing of the element
* @return {Object} a jqMobi object
* @title $().toggle([show])
*/
toggle: function(show) {
var show2 = show === true ? true : false;
for (var i = 0; i < this.length; i++) {
if (window.getComputedStyle(this[i])['display'] !== "none" || (show !== undefined && show2 === false)) {
this[i].setAttribute("jqmOldStyle", this[i].style.display)
this[i].style.display = "none";
} else {
this[i].style.display = this[i].getAttribute("jqmOldStyle") != undefined ? this[i].getAttribute("jqmOldStyle") : 'block';
this[i].removeAttribute("jqmOldStyle");
}
}
return this;
},
/**
* Gets or sets an elements value
* If used as a getter, we return the first elements value. If nothing is in the collection, we return undefined
```
$().value; //Gets the first elements value;
$().value="bar"; //Sets all elements value to bar
```
* @param {String} [value] to set
* @return {String|Object} A string as a getter, jqMobi object as a setter
* @title $().val([value])
*/
val: function(value) {
if (this.length === 0)
return undefined;
if (value == undefined)
return this[0].value;
for (var i = 0; i < this.length; i++) {
this[i].value = value;
}
return this;
},
/**
* Gets or sets an attribute on an element
* If used as a getter, we return the first elements value. If nothing is in the collection, we return undefined
```
$().attr("foo"); //Gets the first elements 'foo' attribute
$().attr("foo","bar");//Sets the elements 'foo' attribute to 'bar'
$().attr("foo",{bar:'bar'}) //Adds the object to an internal cache
```
* @param {String|Object} attribute to act upon. If it's an object (hashmap), it will set the attributes based off the kvp.
* @param {String|Array|Object|function} [value] to set
* @return {String|Object|Array|Function} If used as a getter, return the attribute value. If a setter, return a jqMobi object
* @title $().attr(attribute,[value])
*/
attr: function(attr, value) {
if (this.length === 0)
return undefined;
if (value === undefined && !$.isObject(attr)) {
var val = (this[0].jqmCacheId&&_attrCache[this[0].jqmCacheId][attr])?(this[0].jqmCacheId&&_attrCache[this[0].jqmCacheId][attr]):this[0].getAttribute(attr);
return val;
}
for (var i = 0; i < this.length; i++) {
if ($.isObject(attr)) {
for (var key in attr) {
$(this[i]).attr(key,attr[key]);
}
}
else if($.isArray(value)||$.isObject(value)||$.isFunction(value))
{
if(!this[i].jqmCacheId)
this[i].jqmCacheId=$.uuid();
if(!_attrCache[this[i].jqmCacheId])
_attrCache[this[i].jqmCacheId]={}
_attrCache[this[i].jqmCacheId][attr]=value;
}
else if (value == null && value !== undefined)
{
this[i].removeAttribute(attr);
if(this[i].jqmCacheId&&_attrCache[this[i].jqmCacheId][attr])
delete _attrCache[this[i].jqmCacheId][attr];
}
else{
this[i].setAttribute(attr, value);
}
}
return this;
},
/**
* Removes an attribute on the elements
```
$().removeAttr("foo");
```
* @param {String} attributes that can be space delimited
* @return {Object} jqMobi object
* @title $().removeAttr(attribute)
*/
removeAttr: function(attr) {
var that = this;
for (var i = 0; i < this.length; i++) {
attr.split(/\s+/g).forEach(function(param) {
that[i].removeAttribute(param);
if(that[i].jqmCacheId&&_attrCache[that[i].jqmCacheId][attr])
delete _attrCache[that[i].jqmCacheId][attr];
});
}
return this;
},
/**
* Removes elements based off a selector
```
$().remove(".foo");//Remove off a string selector
var element=$("#foo").get();
$().remove(element); //Remove by an element
$().remove($(".foo")); //Remove by a collection
```
* @param {String|Object|Array} selector to filter against
* @return {Object} jqMobi object
* @title $().remove(selector)
*/
remove: function(selector) {
var elems = $(this).filter(selector);
if (elems == undefined)
return this;
for (var i = 0; i < elems.length; i++) {
elems[i].parentNode.removeChild(elems[i]);
}
return this;
},
/**
* Adds a css class to elements.
```
$().addClass("selected");
```
* @param {String} classes that are space delimited
* @return {Object} jqMobi object
* @title $().addClass(name)
*/
addClass: function(name) {
for (var i = 0; i < this.length; i++) {
var cls = this[i].className;
var classList = [];
var that = this;
name.split(/\s+/g).forEach(function(cname) {
if (!that.hasClass(cname, that[i]))
classList.push(cname);
});
this[i].className += (cls ? " " : "") + classList.join(" ");
this[i].className = this[i].className.trim();
}
return this;
},
/**
* Removes a css class from elements.
```
$().removeClass("foo"); //single class
$().removeClass("foo selected");//remove multiple classess
```
* @param {String} classes that are space delimited
* @return {Object} jqMobi object
* @title $().removeClass(name)
*/
removeClass: function(name) {
for (var i = 0; i < this.length; i++) {
if (name == undefined) {
this[i].className = '';
return this;
}
var classList = this[i].className;
name.split(/\s+/g).forEach(function(cname) {
classList = classList.replace(classRE(cname), " ");
});
if (classList.length > 0)
this[i].className = classList.trim();
else
this[i].className = "";
}
return this;
},
/**
* Checks to see if an element has a class.
```
$().hasClass('foo');
$().hasClass('foo',element);
```
* @param {String} class name to check against
* @param {Object} [element] to check against
* @return {Boolean}
* @title $().hasClass(name,[element])
*/
hasClass: function(name, element) {
if (this.length === 0)
return false;
if (!element)
element = this[0];
return classRE(name).test(element.className);
},
/**
* Appends to the elements
* We boil everything down to a jqMobi object and then loop through that.
* If it's HTML, we create a dom element so we do not break event bindings.
* if it's a script tag, we evaluate it.
```
$().append("<div></div>"); //Creates the object from the string and appends it
$().append($("#foo")); //Append an object;
```
* @param {String|Object} Element/string to add
* @param {Boolean} [insert] insert or append
* @return {Object} jqMobi object
* @title $().append(element,[insert])
*/
append: function(element, insert) {
if (element && element.length != undefined && element.length === 0)
return this;
if ($.isArray(element) || $.isObject(element))
element = $(element);
var i;
for (i = 0; i < this.length; i++) {
if (element.length && typeof element != "string") {
element = $(element);
for (var j = 0; j < element.length; j++)
insert != undefined ? this[i].insertBefore(element[j], this[i].firstChild) : this[i].appendChild(element[j]);
} else {
var obj =fragementRE.test(element)?$(element):undefined;
if (obj == undefined || obj.length == 0) {
obj = document.createTextNode(element);
}
if (obj.nodeName != undefined && obj.nodeName.toLowerCase() == "script" && (!obj.type || obj.type.toLowerCase() === 'text/javascript')) {
window.eval(obj.innerHTML);
} else if(obj instanceof $jqm) {
for(var k=0;k<obj.length;k++)
{
insert != undefined ? this[i].insertBefore(obj[k], this[i].firstChild) : this[i].appendChild(obj[k]);
}
}
else {
insert != undefined ? this[i].insertBefore(obj, this[i].firstChild) : this[i].appendChild(obj);
}
}
}
return this;
},
/**
* Prepends to the elements
* This simply calls append and sets insert to true
```
$().prepend("<div></div>");//Creates the object from the string and appends it
$().prepend($("#foo")); //Prepends an object
```
* @param {String|Object} Element/string to add
* @return {Object} jqMobi object
* @title $().prepend(element)
*/
prepend: function(element) {
return this.append(element, 1);
},
/**
* Inserts collection before the target (adjacent)
```
$().insertBefore(jq("#target"));
```
* @param {String|Object} Target
* @title $().insertBefore(target);
*/
insertBefore: function(target, after) {
if (this.length == 0)
return this;
target = $(target).get(0);
if (!target || target.length == 0)
return this;
for (var i = 0; i < this.length; i++)
{
after ? target.parentNode.insertBefore(this[i], target.nextSibling) : target.parentNode.insertBefore(this[i], target);
}
return this;
},
/**
* Inserts collection after the target (adjacent)
```
$().insertAfter(jq("#target"));
```
* @param {String|Object} target
* @title $().insertAfter(target);
*/
insertAfter: function(target) {
this.insertBefore(target, true);
},
/**
* Returns the raw DOM element.
```
$().get(); //returns the first element
$().get(2);// returns the third element
```
* @param {Int} [index]
* @return {Object} raw DOM element
* @title $().get([index])
*/
get: function(index) {
index = index == undefined ? 0 : index;
if (index < 0)
index += this.length;
return (this[index]) ? this[index] : undefined;
},
/**
* Returns the offset of the element, including traversing up the tree
```
$().offset();
```
* @return {Object} with left, top, width and height properties
* @title $().offset()
*/
offset: function() {
if (this.length === 0)
return undefined;
var obj = this[0].getBoundingClientRect();
return {
left: obj.left + window.pageXOffset,
top: obj.top + window.pageYOffset,
width: parseInt(this[0].style.width),
height: parseInt(this[0].style.height)
};
},
/**
* Returns the parent nodes of the elements based off the selector
```
$("#foo").parent('.bar');
$("#foo").parent($('.bar'));
$("#foo").parent($('.bar').get());
```
* @param {String|Array|Object} [selector]
* @return {Object} jqMobi object with unique parents
* @title $().parent(selector)
*/
parent: function(selector) {
if (this.length == 0)
return undefined;
var elems = [];
for (var i = 0; i < this.length; i++) {
if (this[i].parentNode)
elems.push(this[i].parentNode);
}
return this.setupOld($(unique(elems)).filter(selector));
},
/**
* Returns the child nodes of the elements based off the selector
```
$("#foo").children('.bar'); //Selector
$("#foo").children($('.bar')); //Objects
$("#foo").children($('.bar').get()); //Single element
```
* @param {String|Array|Object} [selector]
* @return {Object} jqMobi object with unique children
* @title $().children(selector)
*/
children: function(selector) {
if (this.length == 0)
return undefined;
var elems = [];
for (var i = 0; i < this.length; i++) {
elems = elems.concat(siblings(this[i].firstChild));
}
return this.setupOld($((elems)).filter(selector));
},
/**
* Returns the siblings of the element based off the selector
```
$("#foo").siblings('.bar'); //Selector
$("#foo").siblings($('.bar')); //Objects
$("#foo").siblings($('.bar').get()); //Single element
```
* @param {String|Array|Object} [selector]
* @return {Object} jqMobi object with unique siblings
* @title $().siblings(selector)
*/
siblings: function(selector) {
if (this.length == 0)
return undefined;
var elems = [];
for (var i = 0; i < this.length; i++) {
if (this[i].parentNode)
elems = elems.concat(siblings(this[i].parentNode.firstChild, this[i]));
}
return this.setupOld($(elems).filter(selector));
},
/**
* Returns the closest element based off the selector and optional context
```
$("#foo").closest('.bar'); //Selector
$("#foo").closest($('.bar')); //Objects
$("#foo").closest($('.bar').get()); //Single element
```
* @param {String|Array|Object} selector
* @param {Object} [context]
* @return {Object} Returns a jqMobi object with the closest element based off the selector
* @title $().closest(selector,[context]);
*/
closest: function(selector, context) {
if (this.length == 0)
return undefined;
var elems = [],
cur = this[0];
var start = $(selector, context);
if (start.length == 0)
return $();
while (cur && start.indexOf(cur) == -1) {
cur = cur !== context && cur !== document && cur.parentNode;
}
return $(cur);
},
/**
* Filters elements based off the selector
```
$("#foo").filter('.bar'); //Selector
$("#foo").filter($('.bar')); //Objects
$("#foo").filter($('.bar').get()); //Single element
```
* @param {String|Array|Object} selector
* @return {Object} Returns a jqMobi object after the filter was run
* @title $().filter(selector);
*/
filter: function(selector) {
if (this.length == 0)
return undefined;
if (selector == undefined)
return this;
var elems = [];
for (var i = 0; i < this.length; i++) {
var val = this[i];
if (val.parentNode && $(selector, val.parentNode).indexOf(val) >= 0)
elems.push(val);
}