142
St
startsInvasion / files
startsInvasion / libs / node_modules / bonzo / src / bonzo.js
15.1 KB
1!function (context, win) { 2 3 var doc = context.document, 4 html = doc.documentElement, 5 parentNode = 'parentNode', 6 query = null, 7 byTag = 'getElementsByTagName', 8 specialAttributes = /^checked|value|selected$/, 9 specialTags = /select|fieldset|table|tbody|tfoot|td|tr|colgroup/i, 10 table = 'table', 11 tagMap = { thead: table, tbody: table, tfoot: table, tr: 'tbody', th: 'tr', td: 'tr', fieldset: 'form', option: 'select' }, 12 stateAttributes = /^checked|selected$/, 13 ie = /msie/i.test(navigator.userAgent), 14 uidList = [], 15 uuids = 0, 16 digit = /^-?[\d\.]+$/, 17 px = 'px', 18 // commonly used methods 19 setAttribute = 'setAttribute', 20 getAttribute = 'getAttribute', 21 trimReplace = /(^\s*|\s*$)/g, 22 unitless = { lineHeight: 1, zoom: 1, zIndex: 1, opacity: 1 }; 23 24 function classReg(c) { 25 return new RegExp("(^|\\s+)" + c + "(\\s+|$)"); 26 } 27 28 function each(ar, fn, scope) { 29 for (var i = 0, l = ar.length; i < l; i++) { 30 fn.call(scope || ar[i], ar[i], i, ar); 31 } 32 return ar; 33 } 34 35 var trim = String.prototype.trim ? 36 function (s) { 37 return s.trim(); 38 } : 39 function (s) { 40 return s.replace(trimReplace, ''); 41 }; 42 43 function camelize(s) { 44 return s.replace(/-(.)/g, function (m, m1) { 45 return m1.toUpperCase(); 46 }); 47 } 48 49 function is(node) { 50 return node && node.nodeName && node.nodeType == 1; 51 } 52 53 function some(ar, fn, scope) { 54 for (var i = 0, j = ar.length; i < j; ++i) { 55 if (fn.call(scope, ar[i], i, ar)) { 56 return true; 57 } 58 } 59 return false; 60 } 61 62 var getStyle = doc.defaultView && doc.defaultView.getComputedStyle ? 63 function (el, property) { 64 var value = null; 65 if (property == 'float') { 66 property = 'cssFloat'; 67 } 68 var computed = doc.defaultView.getComputedStyle(el, ''); 69 computed && (value = computed[camelize(property)]); 70 return el.style[property] || value; 71 72 } : (ie && html.currentStyle) ? 73 74 function (el, property) { 75 property = camelize(property); 76 property = property == 'float' ? 'styleFloat' : property; 77 78 if (property == 'opacity') { 79 var val = 100; 80 try { 81 val = el.filters['DXImageTransform.Microsoft.Alpha'].opacity; 82 } catch (e1) { 83 try { 84 val = el.filters('alpha').opacity; 85 } catch (e2) {} 86 } 87 return val / 100; 88 } 89 var value = el.currentStyle ? el.currentStyle[property] : null; 90 return el.style[property] || value; 91 } : 92 93 function (el, property) { 94 return el.style[camelize(property)]; 95 }; 96 97 function insert(target, host, fn) { 98 var i = 0, self = host || this, r = [], 99 nodes = query && typeof target == 'string' && target.charAt(0) != '<' ? function (n) { 100 return (n = query(target)) && (n.selected = 1) && n; 101 }() : target; 102 each(normalize(nodes), function (t) { 103 each(self, function (el) { 104 var n = !el[parentNode] || (el[parentNode] && !el[parentNode][parentNode]) ? 105 function () { 106 var c = el.cloneNode(true); 107 self.$ && self.cloneEvents && self.$(c).cloneEvents(el); 108 return c; 109 }() : 110 el; 111 fn(t, n); 112 r[i] = n; 113 i++; 114 }); 115 }, this); 116 each(r, function (e, i) { 117 self[i] = e; 118 }); 119 self.length = i; 120 return self; 121 } 122 123 function xy(el, x, y) { 124 var $el = bonzo(el), 125 style = $el.css('position'), 126 offset = $el.offset(), 127 rel = 'relative', 128 isRel = style == rel, 129 delta = [parseInt($el.css('left'), 10), parseInt($el.css('top'), 10)]; 130 131 if (style == 'static') { 132 $el.css('position', rel); 133 style = rel; 134 } 135 136 isNaN(delta[0]) && (delta[0] = isRel ? 0 : el.offsetLeft); 137 isNaN(delta[1]) && (delta[1] = isRel ? 0 : el.offsetTop); 138 139 x !== null && (el.style.left = x - offset.left + delta[0] + 'px'); 140 y !== null && (el.style.top = y - offset.top + delta[1] + 'px'); 141 142 } 143 144 function Bonzo(elements) { 145 this.length = 0; 146 if (elements) { 147 elements = typeof elements !== 'string' && 148 !elements.nodeType && 149 typeof elements.length !== 'undefined' ? 150 elements : 151 [elements]; 152 this.length = elements.length; 153 for (var i = 0; i < elements.length; i++) { 154 this[i] = elements[i]; 155 } 156 } 157 } 158 159 Bonzo.prototype = { 160 161 each: function (fn, scope) { 162 return each(this, fn, scope); 163 }, 164 165 map: function (fn, reject) { 166 var m = [], n, i; 167 for (i = 0; i < this.length; i++) { 168 n = fn.call(this, this[i]); 169 reject ? (reject(n) && m.push(n)) : m.push(n); 170 } 171 return m; 172 }, 173 174 first: function () { 175 return bonzo(this[0]); 176 }, 177 178 last: function () { 179 return bonzo(this[this.length - 1]); 180 }, 181 182 html: function (h, text) { 183 var method = text ? 184 html.textContent == null ? 185 'innerText' : 186 'textContent' : 187 'innerHTML', m; 188 function append(el) { 189 while (el.firstChild) { 190 el.removeChild(el.firstChild); 191 } 192 each(normalize(h), function (node) { 193 el.appendChild(node); 194 }); 195 } 196 return typeof h !== 'undefined' ? 197 this.each(function (el) { 198 (m = el.tagName.match(specialTags)) ? 199 append(el, m[0]) : 200 (el[method] = h); 201 }) : 202 this[0] ? this[0][method] : ''; 203 }, 204 205 text: function (text) { 206 return this.html(text, 1); 207 }, 208 209 addClass: function (c) { 210 return this.each(function (el) { 211 this.hasClass(el, c) || (el.className = trim(el.className + ' ' + c)); 212 }, this); 213 }, 214 215 removeClass: function (c) { 216 return this.each(function (el) { 217 this.hasClass(el, c) && (el.className = trim(el.className.replace(classReg(c), ' '))); 218 }, this); 219 }, 220 221 hasClass: function (el, c) { 222 return typeof c == 'undefined' ? 223 some(this, function (i) { 224 return classReg(el).test(i.className); 225 }) : 226 classReg(c).test(el.className); 227 }, 228 229 toggleClass: function (c, condition) { 230 if (typeof condition !== 'undefined' && !condition) { 231 return this; 232 } 233 return this.each(function (el) { 234 this.hasClass(el, c) ? 235 (el.className = trim(el.className.replace(classReg(c), ' '))) : 236 (el.className = trim(el.className + ' ' + c)); 237 }, this); 238 }, 239 240 show: function (type) { 241 return this.each(function (el) { 242 el.style.display = type || ''; 243 }); 244 }, 245 246 hide: function (elements) { 247 return this.each(function (el) { 248 el.style.display = 'none'; 249 }); 250 }, 251 252 append: function (node) { 253 return this.each(function (el) { 254 each(normalize(node), function (i) { 255 el.appendChild(i); 256 }); 257 }); 258 }, 259 260 prepend: function (node) { 261 return this.each(function (el) { 262 var first = el.firstChild; 263 each(normalize(node), function (i) { 264 el.insertBefore(i, first); 265 }); 266 }); 267 }, 268 269 appendTo: function (target, host) { 270 return insert.call(this, target, host, function (t, el) { 271 t.appendChild(el); 272 }); 273 }, 274 275 prependTo: function (target, host) { 276 return insert.call(this, target, host, function (t, el) { 277 t.insertBefore(el, t.firstChild); 278 }); 279 }, 280 281 next: function () { 282 return this.related('nextSibling'); 283 }, 284 285 previous: function () { 286 return this.related('previousSibling'); 287 }, 288 289 related: function (method) { 290 return this.map( 291 function (el) { 292 el = el[method]; 293 while (el && el.nodeType !== 1) { 294 el = el[method]; 295 } 296 return el || 0; 297 }, 298 function (el) { 299 return el; 300 } 301 ); 302 }, 303 304 before: function (node) { 305 return this.each(function (el) { 306 each(bonzo.create(node), function (i) { 307 el[parentNode].insertBefore(i, el); 308 }); 309 }); 310 }, 311 312 after: function (node) { 313 return this.each(function (el) { 314 each(bonzo.create(node), function (i) { 315 el[parentNode].insertBefore(i, el.nextSibling); 316 }); 317 }); 318 }, 319 320 insertBefore: function (target, host) { 321 return insert.call(this, target, host, function (t, el) { 322 t[parentNode].insertBefore(el, t); 323 }); 324 }, 325 326 insertAfter: function (target, host) { 327 return insert.call(this, target, host, function (t, el) { 328 var sibling = t.nextSibling; 329 if (sibling) { 330 t[parentNode].insertBefore(el, sibling); 331 } 332 else { 333 t[parentNode].appendChild(el); 334 } 335 }); 336 }, 337 338 css: function (o, v, p) { 339 // is this a request for just getting a style? 340 if (v === undefined && typeof o == 'string') { 341 // repurpose 'v' 342 v = this[0]; 343 if (!v) { 344 return null; 345 } 346 if (v == doc || v == win) { 347 p = (v == doc) ? bonzo.doc() : bonzo.viewport(); 348 return o == 'width' ? p.width : 349 o == 'height' ? p.height : ''; 350 } 351 return getStyle(v, o); 352 } 353 var iter = o; 354 if (typeof o == 'string') { 355 iter = {}; 356 iter[o] = v; 357 } 358 359 if (ie && iter.opacity) { 360 // oh this 'ol gamut 361 iter.filter = 'alpha(opacity=' + (iter.opacity * 100) + ')'; 362 // give it layout 363 iter.zoom = o.zoom || 1; 364 delete iter.opacity; 365 } 366 367 if (v = iter['float']) { 368 // float is a reserved style word. w3 uses cssFloat, ie uses styleFloat 369 ie ? (iter.styleFloat = v) : (iter.cssFloat = v); 370 delete iter['float']; 371 } 372 373 var fn = function (el, p, v) { 374 for (var k in iter) { 375 if (iter.hasOwnProperty(k)) { 376 v = iter[k]; 377 // change "5" to "5px" - unless you're line-height, which is allowed 378 (p = camelize(k)) && digit.test(v) && !(p in unitless) && (v += px); 379 el.style[p] = v; 380 } 381 } 382 }; 383 return this.each(fn); 384 }, 385 386 offset: function (x, y) { 387 if (x || y) { 388 return this.each(function (el) { 389 xy(el, x, y); 390 }); 391 } 392 var el = this[0]; 393 var width = el.offsetWidth; 394 var height = el.offsetHeight; 395 var top = el.offsetTop; 396 var left = el.offsetLeft; 397 while (el = el.offsetParent) { 398 top = top + el.offsetTop; 399 left = left + el.offsetLeft; 400 } 401 402 return { 403 top: top, 404 left: left, 405 height: height, 406 width: width 407 }; 408 }, 409 410 attr: function (k, v) { 411 var el = this[0]; 412 return typeof v == 'undefined' ? 413 specialAttributes.test(k) ? 414 stateAttributes.test(k) && typeof el[k] == 'string' ? 415 true : el[k] : el[getAttribute](k) : 416 this.each(function (el) { 417 k == 'value' ? (el.value = v) : el[setAttribute](k, v); 418 }); 419 }, 420 421 val: function (s) { 422 return (typeof s == 'string') ? this.attr('value', s) : this[0].value; 423 }, 424 425 removeAttr: function (k) { 426 return this.each(function (el) { 427 el.removeAttribute(k); 428 }); 429 }, 430 431 data: function (k, v) { 432 var el = this[0]; 433 if (typeof v === 'undefined') { 434 el[getAttribute]('data-node-uid') || el[setAttribute]('data-node-uid', ++uuids); 435 var uid = el[getAttribute]('data-node-uid'); 436 uidList[uid] || (uidList[uid] = {}); 437 return uidList[uid][k]; 438 } else { 439 return this.each(function (el) { 440 el[getAttribute]('data-node-uid') || el[setAttribute]('data-node-uid', ++uuids); 441 var uid = el[getAttribute]('data-node-uid'); 442 var o = {}; 443 o[k] = v; 444 uidList[uid] = o; 445 }); 446 } 447 }, 448 449 remove: function () { 450 return this.each(function (el) { 451 el[parentNode] && el[parentNode].removeChild(el); 452 }); 453 }, 454 455 empty: function () { 456 return this.each(function (el) { 457 while (el.firstChild) { 458 el.removeChild(el.firstChild); 459 } 460 }); 461 }, 462 463 detach: function () { 464 return this.map(function (el) { 465 return el[parentNode].removeChild(el); 466 }); 467 }, 468 469 scrollTop: function (y) { 470 return scroll.call(this, null, y, 'y'); 471 }, 472 473 scrollLeft: function (x) { 474 return scroll.call(this, x, null, 'x'); 475 } 476 }; 477 478 function normalize(node) { 479 return typeof node == 'string' ? bonzo.create(node) : is(node) ? [node] : node; // assume [nodes] 480 } 481 482 function scroll(x, y, type) { 483 var el = this[0]; 484 if (x == null && y == null) { 485 return (isBody(el) ? getWindowScroll() : { x: el.scrollLeft, y: el.scrollTop })[type]; 486 } 487 if (isBody(el)) { 488 win.scrollTo(x, y); 489 } else { 490 x != null && (el.scrollLeft = x); 491 y != null && (el.scrollTop = y); 492 } 493 return this; 494 } 495 496 function isBody(element) { 497 return element === win || (/^(?:body|html)$/i).test(element.tagName); 498 } 499 500 function getWindowScroll() { 501 return { x: win.pageXOffset || html.scrollLeft, y: win.pageYOffset || html.scrollTop }; 502 } 503 504 function bonzo(els, host) { 505 return new Bonzo(els, host); 506 } 507 508 bonzo.setQueryEngine = function (q) { 509 query = q; 510 delete bonzo.setQueryEngine; 511 }; 512 513 bonzo.aug = function (o, target) { 514 for (var k in o) { 515 o.hasOwnProperty(k) && ((target || Bonzo.prototype)[k] = o[k]); 516 } 517 }; 518 519 bonzo.create = function (node) { 520 return typeof node == 'string' ? 521 function () { 522 var tag = /^<([^\s>]+)/.exec(node); 523 var el = doc.createElement(tag && tagMap[tag[1].toLowerCase()] || 'div'), els = []; 524 el.innerHTML = node; 525 var nodes = el.childNodes; 526 el = el.firstChild; 527 els.push(el); 528 while (el = el.nextSibling) { 529 (el.nodeType == 1) && els.push(el); 530 } 531 return els; 532 533 }() : is(node) ? [node.cloneNode(true)] : []; 534 }; 535 536 bonzo.doc = function () { 537 var w = html.scrollWidth, 538 h = html.scrollHeight, 539 vp = this.viewport(); 540 return { 541 width: Math.max(w, vp.width), 542 height: Math.max(h, vp.height) 543 }; 544 }; 545 546 bonzo.firstChild = function (el) { 547 for (var c = el.childNodes, i = 0, j = (c && c.length) || 0, e; i < j; i++) { 548 if (c[i].nodeType === 1) { 549 e = c[j = i]; 550 } 551 } 552 return e; 553 }; 554 555 bonzo.viewport = function () { 556 var h = self.innerHeight, 557 w = self.innerWidth; 558 if (ie) { 559 h = html.clientHeight; 560 w = html.clientWidth; 561 } 562 return { 563 width: w, 564 height: h 565 }; 566 }; 567 568 bonzo.isAncestor = 'compareDocumentPosition' in html ? 569 function (container, element) { 570 return (container.compareDocumentPosition(element) & 16) == 16; 571 } : 'contains' in html ? 572 function (container, element) { 573 return container !== element && container.contains(element); 574 } : 575 function (container, element) { 576 while (element = element[parentNode]) { 577 if (element === container) { 578 return true; 579 } 580 } 581 return false; 582 }; 583 584 var old = context.bonzo; 585 bonzo.noConflict = function () { 586 context.bonzo = old; 587 return this; 588 }; 589 context['bonzo'] = bonzo; 590 591}(this, window);