xonecas.io
142
St

startsInvasion / files

startsInvasion / libs / node_modules / bonzo / src / ender.js

2.8 KB
1!function ($) {
2
3  var b = bonzo;
4  b.setQueryEngine($);
5  $.ender(b);
6  $.ender(b(), true);
7  $.ender({
8    create: function (node) {
9      return $(b.create(node));
10    }
11  });
12
13  $.id = function (id) {
14    return $([document.getElementById(id)]);
15  };
16
17  function indexOf(ar, val) {
18    for (var i = 0; i < ar.length; i++) {
19      if (ar[i] === val) {
20        return i;
21      }
22    }
23    return -1;
24  }
25
26  function uniq(ar) {
27    var a = [], i, j;
28    label:
29    for (i = 0; i < ar.length; i++) {
30      for (j = 0; j < a.length; j++) {
31        if (a[j] == ar[i]) {
32          continue label;
33        }
34      }
35      a[a.length] = ar[i];
36    }
37    return a;
38  }
39
40  $.ender({
41    parents: function (selector, closest) {
42      var collection = $(selector), j, k, p, r = [];
43      for (j = 0, k = this.length; j < k; j++) {
44        p = this[j];
45        while (p = p.parentNode) {
46          if (indexOf(collection, p) !== -1) {
47            r.push(p);
48            if (closest) break;
49          }
50        }
51      }
52      return $(uniq(r));
53    },
54
55    closest: function (selector) {
56      return this.parents(selector, true);
57    },
58
59    first: function () {
60      return $(this[0]);
61    },
62
63    last: function () {
64      return $(this[this.length - 1]);
65    },
66
67    next: function () {
68      return $(b(this).next());
69    },
70
71    previous: function () {
72      return $(b(this).previous());
73    },
74
75    appendTo: function (t) {
76      return b(this.selector).appendTo(t, this);
77    },
78
79    prependTo: function (t) {
80      return b(this.selector).prependTo(t, this);
81    },
82
83    insertAfter: function (t) {
84      return b(this.selector).insertAfter(t, this);
85    },
86
87    insertBefore: function (t) {
88      return b(this.selector).insertBefore(t, this);
89    },
90
91    siblings: function () {
92      var i, l, p, r = [];
93      for (i = 0, l = this.length; i < l; i++) {
94        p = this[i];
95        while (p = p.previousSibling) {
96          p.nodeType == 1 && r.push(p);
97        }
98        p = this[i];
99        while (p = p.nextSibling) {
100          p.nodeType == 1 && r.push(p);
101        }
102      }
103      return $(r);
104    },
105
106    children: function () {
107      var i, el, r = [];
108      for (i = 0, l = this.length; i < l; i++) {
109        if (!(el = b.firstChild(this[i]))) {
110          continue;
111        }
112        r.push(el);
113        while (el = el.nextSibling) {
114          el.nodeType == 1 && r.push(el);
115        }
116      }
117      return $(uniq(r));
118    },
119
120    height: function (v) {
121      return dimension(v, this, 'height')
122    },
123
124    width: function (v) {
125      return dimension(v, this, 'width')
126    }
127  }, true);
128
129  function dimension(v, self, which) {
130    return v ?
131      self.css(which, v) :
132      function (r) {
133        r = parseInt(self.css(which), 10);
134        return isNaN(r) ? self[0]['offset' + which.replace(/^\w/, function (m) {return m.toUpperCase()})] : r
135      }()
136  }
137
138}(ender || $);