browser/components/tabview/iq.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/browser/components/tabview/iq.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,763 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// **********
     1.9 +// Title: iq.js
    1.10 +// Various helper functions, in the vein of jQuery.
    1.11 +
    1.12 +// ----------
    1.13 +// Function: iQ
    1.14 +// Returns an iQClass object which represents an individual element or a group
    1.15 +// of elements. It works pretty much like jQuery(), with a few exceptions,
    1.16 +// most notably that you can't use strings with complex html,
    1.17 +// just simple tags like '<div>'.
    1.18 +function iQ(selector, context) {
    1.19 +  // The iQ object is actually just the init constructor 'enhanced'
    1.20 +  return new iQClass(selector, context);
    1.21 +};
    1.22 +
    1.23 +// A simple way to check for HTML strings or ID strings
    1.24 +// (both of which we optimize for)
    1.25 +let quickExpr = /^[^<]*(<[\w\W]+>)[^>]*$|^#([\w-]+)$/;
    1.26 +
    1.27 +// Match a standalone tag
    1.28 +let rsingleTag = /^<(\w+)\s*\/?>(?:<\/\1>)?$/;
    1.29 +
    1.30 +// ##########
    1.31 +// Class: iQClass
    1.32 +// The actual class of iQ result objects, representing an individual element
    1.33 +// or a group of elements.
    1.34 +//
    1.35 +// ----------
    1.36 +// Function: iQClass
    1.37 +// You don't call this directly; this is what's called by iQ().
    1.38 +function iQClass(selector, context) {
    1.39 +
    1.40 +  // Handle $(""), $(null), or $(undefined)
    1.41 +  if (!selector) {
    1.42 +    return this;
    1.43 +  }
    1.44 +
    1.45 +  // Handle $(DOMElement)
    1.46 +  if (selector.nodeType) {
    1.47 +    this.context = selector;
    1.48 +    this[0] = selector;
    1.49 +    this.length = 1;
    1.50 +    return this;
    1.51 +  }
    1.52 +
    1.53 +  // The body element only exists once, optimize finding it
    1.54 +  if (selector === "body" && !context) {
    1.55 +    this.context = document;
    1.56 +    this[0] = document.body;
    1.57 +    this.selector = "body";
    1.58 +    this.length = 1;
    1.59 +    return this;
    1.60 +  }
    1.61 +
    1.62 +  // Handle HTML strings
    1.63 +  if (typeof selector === "string") {
    1.64 +    // Are we dealing with HTML string or an ID?
    1.65 +
    1.66 +    let match = quickExpr.exec(selector);
    1.67 +
    1.68 +    // Verify a match, and that no context was specified for #id
    1.69 +    if (match && (match[1] || !context)) {
    1.70 +
    1.71 +      // HANDLE $(html) -> $(array)
    1.72 +      if (match[1]) {
    1.73 +        let doc = (context ? context.ownerDocument || context : document);
    1.74 +
    1.75 +        // If a single string is passed in and it's a single tag
    1.76 +        // just do a createElement and skip the rest
    1.77 +        let ret = rsingleTag.exec(selector);
    1.78 +
    1.79 +        if (ret) {
    1.80 +          if (Utils.isPlainObject(context)) {
    1.81 +            Utils.assert(false, 'does not support HTML creation with context');
    1.82 +          } else {
    1.83 +            selector = [doc.createElement(ret[1])];
    1.84 +          }
    1.85 +
    1.86 +        } else {
    1.87 +          Utils.assert(false, 'does not support complex HTML creation');
    1.88 +        }
    1.89 +
    1.90 +        return Utils.merge(this, selector);
    1.91 +
    1.92 +      // HANDLE $("#id")
    1.93 +      } else {
    1.94 +        let elem = document.getElementById(match[2]);
    1.95 +
    1.96 +        if (elem) {
    1.97 +          this.length = 1;
    1.98 +          this[0] = elem;
    1.99 +        }
   1.100 +
   1.101 +        this.context = document;
   1.102 +        this.selector = selector;
   1.103 +        return this;
   1.104 +      }
   1.105 +
   1.106 +    // HANDLE $("TAG")
   1.107 +    } else if (!context && /^\w+$/.test(selector)) {
   1.108 +      this.selector = selector;
   1.109 +      this.context = document;
   1.110 +      selector = document.getElementsByTagName(selector);
   1.111 +      return Utils.merge(this, selector);
   1.112 +
   1.113 +    // HANDLE $(expr, $(...))
   1.114 +    } else if (!context || context.iq) {
   1.115 +      return (context || iQ(document)).find(selector);
   1.116 +
   1.117 +    // HANDLE $(expr, context)
   1.118 +    // (which is just equivalent to: $(context).find(expr)
   1.119 +    } else {
   1.120 +      return iQ(context).find(selector);
   1.121 +    }
   1.122 +
   1.123 +  // HANDLE $(function)
   1.124 +  // Shortcut for document ready
   1.125 +  } else if (typeof selector == "function") {
   1.126 +    Utils.log('iQ does not support ready functions');
   1.127 +    return null;
   1.128 +  }
   1.129 +
   1.130 +  if ("selector" in selector) {
   1.131 +    this.selector = selector.selector;
   1.132 +    this.context = selector.context;
   1.133 +  }
   1.134 +
   1.135 +  let ret = this || [];
   1.136 +  if (selector != null) {
   1.137 +    // The window, strings (and functions) also have 'length'
   1.138 +    if (selector.length == null || typeof selector == "string" || selector.setInterval) {
   1.139 +      Array.push(ret, selector);
   1.140 +    } else {
   1.141 +      Utils.merge(ret, selector);
   1.142 +    }
   1.143 +  }
   1.144 +  return ret;
   1.145 +};
   1.146 +  
   1.147 +iQClass.prototype = {
   1.148 +
   1.149 +  // ----------
   1.150 +  // Function: toString
   1.151 +  // Prints [iQ...] for debug use
   1.152 +  toString: function iQClass_toString() {
   1.153 +    if (this.length > 1) {
   1.154 +      if (this.selector)
   1.155 +        return "[iQ (" + this.selector + ")]";
   1.156 +      else
   1.157 +        return "[iQ multi-object]";
   1.158 +    }
   1.159 +
   1.160 +    if (this.length == 1)
   1.161 +      return "[iQ (" + this[0].toString() + ")]";
   1.162 +
   1.163 +    return "[iQ non-object]";
   1.164 +  },
   1.165 +
   1.166 +  // Start with an empty selector
   1.167 +  selector: "",
   1.168 +
   1.169 +  // The default length of a iQ object is 0
   1.170 +  length: 0,
   1.171 +
   1.172 +  // ----------
   1.173 +  // Function: each
   1.174 +  // Execute a callback for every element in the matched set.
   1.175 +  each: function iQClass_each(callback) {
   1.176 +    if (typeof callback != "function") {
   1.177 +      Utils.assert(false, "each's argument must be a function");
   1.178 +      return null;
   1.179 +    }
   1.180 +    for (let i = 0; this[i] != null && callback(this[i]) !== false; i++) {}
   1.181 +    return this;
   1.182 +  },
   1.183 +
   1.184 +  // ----------
   1.185 +  // Function: addClass
   1.186 +  // Adds the given class(es) to the receiver.
   1.187 +  addClass: function iQClass_addClass(value) {
   1.188 +    Utils.assertThrow(typeof value == "string" && value,
   1.189 +                      'requires a valid string argument');
   1.190 +
   1.191 +    let length = this.length;
   1.192 +    for (let i = 0; i < length; i++) {
   1.193 +      let elem = this[i];
   1.194 +      if (elem.nodeType === 1) {
   1.195 +        value.split(/\s+/).forEach(function(className) {
   1.196 +          elem.classList.add(className);
   1.197 +        });
   1.198 +      }
   1.199 +    }
   1.200 +
   1.201 +    return this;
   1.202 +  },
   1.203 +
   1.204 +  // ----------
   1.205 +  // Function: removeClass
   1.206 +  // Removes the given class(es) from the receiver.
   1.207 +  removeClass: function iQClass_removeClass(value) {
   1.208 +    if (typeof value != "string" || !value) {
   1.209 +      Utils.assert(false, 'does not support function argument');
   1.210 +      return null;
   1.211 +    }
   1.212 +
   1.213 +    let length = this.length;
   1.214 +    for (let i = 0; i < length; i++) {
   1.215 +      let elem = this[i];
   1.216 +      if (elem.nodeType === 1 && elem.className) {
   1.217 +        value.split(/\s+/).forEach(function(className) {
   1.218 +          elem.classList.remove(className);
   1.219 +        });
   1.220 +      }
   1.221 +    }
   1.222 +
   1.223 +    return this;
   1.224 +  },
   1.225 +
   1.226 +  // ----------
   1.227 +  // Function: hasClass
   1.228 +  // Returns true is the receiver has the given css class.
   1.229 +  hasClass: function iQClass_hasClass(singleClassName) {
   1.230 +    let length = this.length;
   1.231 +    for (let i = 0; i < length; i++) {
   1.232 +      if (this[i].classList.contains(singleClassName)) {
   1.233 +        return true;
   1.234 +      }
   1.235 +    }
   1.236 +    return false;
   1.237 +  },
   1.238 +
   1.239 +  // ----------
   1.240 +  // Function: find
   1.241 +  // Searches the receiver and its children, returning a new iQ object with
   1.242 +  // elements that match the given selector.
   1.243 +  find: function iQClass_find(selector) {
   1.244 +    let ret = [];
   1.245 +    let length = 0;
   1.246 +
   1.247 +    let l = this.length;
   1.248 +    for (let i = 0; i < l; i++) {
   1.249 +      length = ret.length;
   1.250 +      try {
   1.251 +        Utils.merge(ret, this[i].querySelectorAll(selector));
   1.252 +      } catch(e) {
   1.253 +        Utils.log('iQ.find error (bad selector)', e);
   1.254 +      }
   1.255 +
   1.256 +      if (i > 0) {
   1.257 +        // Make sure that the results are unique
   1.258 +        for (let n = length; n < ret.length; n++) {
   1.259 +          for (let r = 0; r < length; r++) {
   1.260 +            if (ret[r] === ret[n]) {
   1.261 +              ret.splice(n--, 1);
   1.262 +              break;
   1.263 +            }
   1.264 +          }
   1.265 +        }
   1.266 +      }
   1.267 +    }
   1.268 +
   1.269 +    return iQ(ret);
   1.270 +  },
   1.271 +
   1.272 +  // ----------
   1.273 +  // Function: contains
   1.274 +  // Check to see if a given DOM node descends from the receiver.
   1.275 +  contains: function iQClass_contains(selector) {
   1.276 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.277 +
   1.278 +    // fast path when querySelector() can be used
   1.279 +    if ('string' == typeof selector)
   1.280 +      return null != this[0].querySelector(selector);
   1.281 +
   1.282 +    let object = iQ(selector);
   1.283 +    Utils.assert(object.length <= 1, 'does not yet support multi-objects');
   1.284 +
   1.285 +    let elem = object[0];
   1.286 +    if (!elem || !elem.parentNode)
   1.287 +      return false;
   1.288 +
   1.289 +    do {
   1.290 +      elem = elem.parentNode;
   1.291 +    } while (elem && this[0] != elem);
   1.292 +
   1.293 +    return this[0] == elem;
   1.294 +  },
   1.295 +
   1.296 +  // ----------
   1.297 +  // Function: remove
   1.298 +  // Removes the receiver from the DOM.
   1.299 +  remove: function iQClass_remove(options) {
   1.300 +    if (!options || !options.preserveEventHandlers)
   1.301 +      this.unbindAll();
   1.302 +    for (let i = 0; this[i] != null; i++) {
   1.303 +      let elem = this[i];
   1.304 +      if (elem.parentNode) {
   1.305 +        elem.parentNode.removeChild(elem);
   1.306 +      }
   1.307 +    }
   1.308 +    return this;
   1.309 +  },
   1.310 +
   1.311 +  // ----------
   1.312 +  // Function: empty
   1.313 +  // Removes all of the reciever's children and HTML content from the DOM.
   1.314 +  empty: function iQClass_empty() {
   1.315 +    for (let i = 0; this[i] != null; i++) {
   1.316 +      let elem = this[i];
   1.317 +      while (elem.firstChild) {
   1.318 +        iQ(elem.firstChild).unbindAll();
   1.319 +        elem.removeChild(elem.firstChild);
   1.320 +      }
   1.321 +    }
   1.322 +    return this;
   1.323 +  },
   1.324 +
   1.325 +  // ----------
   1.326 +  // Function: width
   1.327 +  // Returns the width of the receiver, including padding and border.
   1.328 +  width: function iQClass_width() {
   1.329 +    return Math.floor(this[0].offsetWidth);
   1.330 +  },
   1.331 +
   1.332 +  // ----------
   1.333 +  // Function: height
   1.334 +  // Returns the height of the receiver, including padding and border.
   1.335 +  height: function iQClass_height() {
   1.336 +    return Math.floor(this[0].offsetHeight);
   1.337 +  },
   1.338 +
   1.339 +  // ----------
   1.340 +  // Function: position
   1.341 +  // Returns an object with the receiver's position in left and top
   1.342 +  // properties.
   1.343 +  position: function iQClass_position() {
   1.344 +    let bounds = this.bounds();
   1.345 +    return new Point(bounds.left, bounds.top);
   1.346 +  },
   1.347 +
   1.348 +  // ----------
   1.349 +  // Function: bounds
   1.350 +  // Returns a <Rect> with the receiver's bounds.
   1.351 +  bounds: function iQClass_bounds() {
   1.352 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.353 +    let rect = this[0].getBoundingClientRect();
   1.354 +    return new Rect(Math.floor(rect.left), Math.floor(rect.top),
   1.355 +                    Math.floor(rect.width), Math.floor(rect.height));
   1.356 +  },
   1.357 +
   1.358 +  // ----------
   1.359 +  // Function: data
   1.360 +  // Pass in both key and value to attach some data to the receiver;
   1.361 +  // pass in just key to retrieve it.
   1.362 +  data: function iQClass_data(key, value) {
   1.363 +    let data = null;
   1.364 +    if (value === undefined) {
   1.365 +      Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.366 +      data = this[0].iQData;
   1.367 +      if (data)
   1.368 +        return data[key];
   1.369 +      else
   1.370 +        return null;
   1.371 +    }
   1.372 +
   1.373 +    for (let i = 0; this[i] != null; i++) {
   1.374 +      let elem = this[i];
   1.375 +      data = elem.iQData;
   1.376 +
   1.377 +      if (!data)
   1.378 +        data = elem.iQData = {};
   1.379 +
   1.380 +      data[key] = value;
   1.381 +    }
   1.382 +
   1.383 +    return this;
   1.384 +  },
   1.385 +
   1.386 +  // ----------
   1.387 +  // Function: html
   1.388 +  // Given a value, sets the receiver's innerHTML to it; otherwise returns
   1.389 +  // what's already there.
   1.390 +  html: function iQClass_html(value) {
   1.391 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.392 +    if (value === undefined)
   1.393 +      return this[0].innerHTML;
   1.394 +
   1.395 +    this[0].innerHTML = value;
   1.396 +    return this;
   1.397 +  },
   1.398 +
   1.399 +  // ----------
   1.400 +  // Function: text
   1.401 +  // Given a value, sets the receiver's textContent to it; otherwise returns
   1.402 +  // what's already there.
   1.403 +  text: function iQClass_text(value) {
   1.404 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.405 +    if (value === undefined) {
   1.406 +      return this[0].textContent;
   1.407 +    }
   1.408 +
   1.409 +    return this.empty().append((this[0] && this[0].ownerDocument || document).createTextNode(value));
   1.410 +  },
   1.411 +
   1.412 +  // ----------
   1.413 +  // Function: val
   1.414 +  // Given a value, sets the receiver's value to it; otherwise returns what's already there.
   1.415 +  val: function iQClass_val(value) {
   1.416 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.417 +    if (value === undefined) {
   1.418 +      return this[0].value;
   1.419 +    }
   1.420 +
   1.421 +    this[0].value = value;
   1.422 +    return this;
   1.423 +  },
   1.424 +
   1.425 +  // ----------
   1.426 +  // Function: appendTo
   1.427 +  // Appends the receiver to the result of iQ(selector).
   1.428 +  appendTo: function iQClass_appendTo(selector) {
   1.429 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.430 +    iQ(selector).append(this);
   1.431 +    return this;
   1.432 +  },
   1.433 +
   1.434 +  // ----------
   1.435 +  // Function: append
   1.436 +  // Appends the result of iQ(selector) to the receiver.
   1.437 +  append: function iQClass_append(selector) {
   1.438 +    let object = iQ(selector);
   1.439 +    Utils.assert(object.length == 1 && this.length == 1, 
   1.440 +        'does not yet support multi-objects (or null objects)');
   1.441 +    this[0].appendChild(object[0]);
   1.442 +    return this;
   1.443 +  },
   1.444 +
   1.445 +  // ----------
   1.446 +  // Function: attr
   1.447 +  // Sets or gets an attribute on the element(s).
   1.448 +  attr: function iQClass_attr(key, value) {
   1.449 +    Utils.assert(typeof key === 'string', 'string key');
   1.450 +    if (value === undefined) {
   1.451 +      Utils.assert(this.length == 1, 'retrieval does not support multi-objects (or null objects)');
   1.452 +      return this[0].getAttribute(key);
   1.453 +    }
   1.454 +
   1.455 +    for (let i = 0; this[i] != null; i++)
   1.456 +      this[i].setAttribute(key, value);
   1.457 +
   1.458 +    return this;
   1.459 +  },
   1.460 +
   1.461 +  // ----------
   1.462 +  // Function: css
   1.463 +  // Sets or gets CSS properties on the receiver. When setting certain numerical properties,
   1.464 +  // will automatically add "px". A property can be removed by setting it to null.
   1.465 +  //
   1.466 +  // Possible call patterns:
   1.467 +  //   a: object, b: undefined - sets with properties from a
   1.468 +  //   a: string, b: undefined - gets property specified by a
   1.469 +  //   a: string, b: string/number - sets property specified by a to b
   1.470 +  css: function iQClass_css(a, b) {
   1.471 +    let properties = null;
   1.472 +
   1.473 +    if (typeof a === 'string') {
   1.474 +      let key = a;
   1.475 +      if (b === undefined) {
   1.476 +        Utils.assert(this.length == 1, 'retrieval does not support multi-objects (or null objects)');
   1.477 +
   1.478 +        return window.getComputedStyle(this[0], null).getPropertyValue(key);
   1.479 +      }
   1.480 +      properties = {};
   1.481 +      properties[key] = b;
   1.482 +    } else if (a instanceof Rect) {
   1.483 +      properties = {
   1.484 +        left: a.left,
   1.485 +        top: a.top,
   1.486 +        width: a.width,
   1.487 +        height: a.height
   1.488 +      };
   1.489 +    } else {
   1.490 +      properties = a;
   1.491 +    }
   1.492 +
   1.493 +    let pixels = {
   1.494 +      'left': true,
   1.495 +      'top': true,
   1.496 +      'right': true,
   1.497 +      'bottom': true,
   1.498 +      'width': true,
   1.499 +      'height': true
   1.500 +    };
   1.501 +
   1.502 +    for (let i = 0; this[i] != null; i++) {
   1.503 +      let elem = this[i];
   1.504 +      for (let key in properties) {
   1.505 +        let value = properties[key];
   1.506 +
   1.507 +        if (pixels[key] && typeof value != 'string')
   1.508 +          value += 'px';
   1.509 +
   1.510 +        if (value == null) {
   1.511 +          elem.style.removeProperty(key);
   1.512 +        } else if (key.indexOf('-') != -1)
   1.513 +          elem.style.setProperty(key, value, '');
   1.514 +        else
   1.515 +          elem.style[key] = value;
   1.516 +      }
   1.517 +    }
   1.518 +
   1.519 +    return this;
   1.520 +  },
   1.521 +
   1.522 +  // ----------
   1.523 +  // Function: animate
   1.524 +  // Uses CSS transitions to animate the element.
   1.525 +  //
   1.526 +  // Parameters:
   1.527 +  //   css - an object map of the CSS properties to change
   1.528 +  //   options - an object with various properites (see below)
   1.529 +  //
   1.530 +  // Possible "options" properties:
   1.531 +  //   duration - how long to animate, in milliseconds
   1.532 +  //   easing - easing function to use. Possibilities include
   1.533 +  //     "tabviewBounce", "easeInQuad". Default is "ease".
   1.534 +  //   complete - function to call once the animation is done, takes nothing
   1.535 +  //     in, but "this" is set to the element that was animated.
   1.536 +  animate: function iQClass_animate(css, options) {
   1.537 +    Utils.assert(this.length == 1, 'does not yet support multi-objects (or null objects)');
   1.538 +
   1.539 +    if (!options)
   1.540 +      options = {};
   1.541 +
   1.542 +    let easings = {
   1.543 +      tabviewBounce: "cubic-bezier(0.0, 0.63, .6, 1.29)", 
   1.544 +      easeInQuad: 'ease-in', // TODO: make it a real easeInQuad, or decide we don't care
   1.545 +      fast: 'cubic-bezier(0.7,0,1,1)'
   1.546 +    };
   1.547 +
   1.548 +    let duration = (options.duration || 400);
   1.549 +    let easing = (easings[options.easing] || 'ease');
   1.550 +
   1.551 +    if (css instanceof Rect) {
   1.552 +      css = {
   1.553 +        left: css.left,
   1.554 +        top: css.top,
   1.555 +        width: css.width,
   1.556 +        height: css.height
   1.557 +      };
   1.558 +    }
   1.559 +
   1.560 +
   1.561 +    // The latest versions of Firefox do not animate from a non-explicitly
   1.562 +    // set css properties. So for each element to be animated, go through
   1.563 +    // and explicitly define 'em.
   1.564 +    let rupper = /([A-Z])/g;
   1.565 +    this.each(function(elem) {
   1.566 +      let cStyle = window.getComputedStyle(elem, null);
   1.567 +      for (let prop in css) {
   1.568 +        prop = prop.replace(rupper, "-$1").toLowerCase();
   1.569 +        iQ(elem).css(prop, cStyle.getPropertyValue(prop));
   1.570 +      }
   1.571 +    });
   1.572 +
   1.573 +    this.css({
   1.574 +      'transition-property': Object.keys(css).join(", "),
   1.575 +      'transition-duration': (duration / 1000) + 's',
   1.576 +      'transition-timing-function': easing
   1.577 +    });
   1.578 +
   1.579 +    this.css(css);
   1.580 +
   1.581 +    let self = this;
   1.582 +    setTimeout(function() {
   1.583 +      self.css({
   1.584 +        'transition-property': 'none',
   1.585 +        'transition-duration': '',
   1.586 +        'transition-timing-function': ''
   1.587 +      });
   1.588 +
   1.589 +      if (typeof options.complete == "function")
   1.590 +        options.complete.apply(self);
   1.591 +    }, duration);
   1.592 +
   1.593 +    return this;
   1.594 +  },
   1.595 +
   1.596 +  // ----------
   1.597 +  // Function: fadeOut
   1.598 +  // Animates the receiver to full transparency. Calls callback on completion.
   1.599 +  fadeOut: function iQClass_fadeOut(callback) {
   1.600 +    Utils.assert(typeof callback == "function" || callback === undefined, 
   1.601 +        'does not yet support duration');
   1.602 +
   1.603 +    this.animate({
   1.604 +      opacity: 0
   1.605 +    }, {
   1.606 +      duration: 400,
   1.607 +      complete: function() {
   1.608 +        iQ(this).css({display: 'none'});
   1.609 +        if (typeof callback == "function")
   1.610 +          callback.apply(this);
   1.611 +      }
   1.612 +    });
   1.613 +
   1.614 +    return this;
   1.615 +  },
   1.616 +
   1.617 +  // ----------
   1.618 +  // Function: fadeIn
   1.619 +  // Animates the receiver to full opacity.
   1.620 +  fadeIn: function iQClass_fadeIn() {
   1.621 +    this.css({display: ''});
   1.622 +    this.animate({
   1.623 +      opacity: 1
   1.624 +    }, {
   1.625 +      duration: 400
   1.626 +    });
   1.627 +
   1.628 +    return this;
   1.629 +  },
   1.630 +
   1.631 +  // ----------
   1.632 +  // Function: hide
   1.633 +  // Hides the receiver.
   1.634 +  hide: function iQClass_hide() {
   1.635 +    this.css({display: 'none', opacity: 0});
   1.636 +    return this;
   1.637 +  },
   1.638 +
   1.639 +  // ----------
   1.640 +  // Function: show
   1.641 +  // Shows the receiver.
   1.642 +  show: function iQClass_show() {
   1.643 +    this.css({display: '', opacity: 1});
   1.644 +    return this;
   1.645 +  },
   1.646 +
   1.647 +  // ----------
   1.648 +  // Function: bind
   1.649 +  // Binds the given function to the given event type. Also wraps the function
   1.650 +  // in a try/catch block that does a Utils.log on any errors.
   1.651 +  bind: function iQClass_bind(type, func) {
   1.652 +    let handler = function(event) func.apply(this, [event]);
   1.653 +
   1.654 +    for (let i = 0; this[i] != null; i++) {
   1.655 +      let elem = this[i];
   1.656 +      if (!elem.iQEventData)
   1.657 +        elem.iQEventData = {};
   1.658 +
   1.659 +      if (!elem.iQEventData[type])
   1.660 +        elem.iQEventData[type] = [];
   1.661 +
   1.662 +      elem.iQEventData[type].push({
   1.663 +        original: func,
   1.664 +        modified: handler
   1.665 +      });
   1.666 +
   1.667 +      elem.addEventListener(type, handler, false);
   1.668 +    }
   1.669 +
   1.670 +    return this;
   1.671 +  },
   1.672 +
   1.673 +  // ----------
   1.674 +  // Function: one
   1.675 +  // Binds the given function to the given event type, but only for one call;
   1.676 +  // automatically unbinds after the event fires once.
   1.677 +  one: function iQClass_one(type, func) {
   1.678 +    Utils.assert(typeof func == "function", 'does not support eventData argument');
   1.679 +
   1.680 +    let handler = function(e) {
   1.681 +      iQ(this).unbind(type, handler);
   1.682 +      return func.apply(this, [e]);
   1.683 +    };
   1.684 +
   1.685 +    return this.bind(type, handler);
   1.686 +  },
   1.687 +
   1.688 +  // ----------
   1.689 +  // Function: unbind
   1.690 +  // Unbinds the given function from the given event type.
   1.691 +  unbind: function iQClass_unbind(type, func) {
   1.692 +    Utils.assert(typeof func == "function", 'Must provide a function');
   1.693 +
   1.694 +    for (let i = 0; this[i] != null; i++) {
   1.695 +      let elem = this[i];
   1.696 +      let handler = func;
   1.697 +      if (elem.iQEventData && elem.iQEventData[type]) {
   1.698 +        let count = elem.iQEventData[type].length;
   1.699 +        for (let a = 0; a < count; a++) {
   1.700 +          let pair = elem.iQEventData[type][a];
   1.701 +          if (pair.original == func) {
   1.702 +            handler = pair.modified;
   1.703 +            elem.iQEventData[type].splice(a, 1);
   1.704 +            if (!elem.iQEventData[type].length) {
   1.705 +              delete elem.iQEventData[type];
   1.706 +              if (!Object.keys(elem.iQEventData).length)
   1.707 +                delete elem.iQEventData;
   1.708 +            }
   1.709 +            break;
   1.710 +          }
   1.711 +        }
   1.712 +      }
   1.713 +
   1.714 +      elem.removeEventListener(type, handler, false);
   1.715 +    }
   1.716 +
   1.717 +    return this;
   1.718 +  },
   1.719 +
   1.720 +  // ----------
   1.721 +  // Function: unbindAll
   1.722 +  // Unbinds all event handlers.
   1.723 +  unbindAll: function iQClass_unbindAll() {
   1.724 +    for (let i = 0; this[i] != null; i++) {
   1.725 +      let elem = this[i];
   1.726 +
   1.727 +      for (let j = 0; j < elem.childElementCount; j++)
   1.728 +        iQ(elem.children[j]).unbindAll();
   1.729 +
   1.730 +      if (!elem.iQEventData)
   1.731 +        continue;
   1.732 +
   1.733 +      Object.keys(elem.iQEventData).forEach(function (type) {
   1.734 +        while (elem.iQEventData && elem.iQEventData[type])
   1.735 +          this.unbind(type, elem.iQEventData[type][0].original);
   1.736 +      }, this);
   1.737 +    }
   1.738 +
   1.739 +    return this;
   1.740 +  }
   1.741 +};
   1.742 +
   1.743 +// ----------
   1.744 +// Create various event aliases
   1.745 +let events = [
   1.746 +  'keyup',
   1.747 +  'keydown',
   1.748 +  'keypress',
   1.749 +  'mouseup',
   1.750 +  'mousedown',
   1.751 +  'mouseover',
   1.752 +  'mouseout',
   1.753 +  'mousemove',
   1.754 +  'click',
   1.755 +  'dblclick',
   1.756 +  'resize',
   1.757 +  'change',
   1.758 +  'blur',
   1.759 +  'focus'
   1.760 +];
   1.761 +
   1.762 +events.forEach(function(event) {
   1.763 +  iQClass.prototype[event] = function(func) {
   1.764 +    return this.bind(event, func);
   1.765 +  };
   1.766 +});

mercurial