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