node-base-debug.js revision 6469c0fd34f5d65f53cc4c8097c46e732a0819ae
c544ac2705cf516901c8455356d56b52c8615525mfloryanYUI.add('node-base', function(Y) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan/**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * The Node Utility provides a DOM-like interface for interacting with DOM nodes.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @module node
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @submodule node-base
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan/**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * The Node class provides a wrapper for manipulating DOM Nodes.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Node properties can be accessed via the set/get methods.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Use Y.get() to retrieve Node instances.
c544ac2705cf516901c8455356d56b52c8615525mfloryan *
c544ac2705cf516901c8455356d56b52c8615525mfloryan * <strong>NOTE:</strong> Node properties are accessed using
c544ac2705cf516901c8455356d56b52c8615525mfloryan * the <code>set</code> and <code>get</code> methods.
c544ac2705cf516901c8455356d56b52c8615525mfloryan *
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @class Node
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @constructor
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @for Node
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan// "globals"
c544ac2705cf516901c8455356d56b52c8615525mfloryanvar DOT = '.',
c544ac2705cf516901c8455356d56b52c8615525mfloryan NODE_NAME = 'nodeName',
c544ac2705cf516901c8455356d56b52c8615525mfloryan NODE_TYPE = 'nodeType',
c544ac2705cf516901c8455356d56b52c8615525mfloryan OWNER_DOCUMENT = 'ownerDocument',
c544ac2705cf516901c8455356d56b52c8615525mfloryan TAG_NAME = 'tagName',
c544ac2705cf516901c8455356d56b52c8615525mfloryan UID = '_yuid',
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan Node = function(node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var uid = node[UID];
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (uid && Node._instances[uid] && Node._instances[uid]._node !== node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan node[UID] = null; // unset existing uid to prevent collision (via clone or hack)
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan uid = Y.stamp(node);
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (!uid) { // stamp failed; likely IE non-HTMLElement
c544ac2705cf516901c8455356d56b52c8615525mfloryan uid = Y.guid();
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan this[UID] = uid;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan this._node = node;
c544ac2705cf516901c8455356d56b52c8615525mfloryan Node._instances[uid] = this;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan this._stateProxy = node; // when augmented with Attribute
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (this._initPlugins) { // when augmented with Plugin.Host
c544ac2705cf516901c8455356d56b52c8615525mfloryan this._initPlugins();
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan // used with previous/next/ancestor tests
c544ac2705cf516901c8455356d56b52c8615525mfloryan _wrapFn = function(fn) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var ret = null;
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (fn) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan ret = (typeof fn === 'string') ?
c544ac2705cf516901c8455356d56b52c8615525mfloryan function(n) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Y.Selector.test(n, fn);
c544ac2705cf516901c8455356d56b52c8615525mfloryan } :
c544ac2705cf516901c8455356d56b52c8615525mfloryan function(n) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return fn(Y.one(n));
c544ac2705cf516901c8455356d56b52c8615525mfloryan };
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan return ret;
c544ac2705cf516901c8455356d56b52c8615525mfloryan };
c544ac2705cf516901c8455356d56b52c8615525mfloryan// end "globals"
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.NAME = 'Node';
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.re_aria = /^(?:role$|aria-)/;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.DOM_EVENTS = {
c544ac2705cf516901c8455356d56b52c8615525mfloryan abort: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan beforeunload: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan blur: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan change: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan click: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan close: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan command: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan contextmenu: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan drag: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan dragstart: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan dragenter: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan dragover: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan dragleave: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan dragend: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan drop: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan dblclick: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan error: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan focus: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan keydown: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan keypress: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan keyup: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan load: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan message: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mousedown: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mousemove: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mouseout: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mouseover: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mouseup: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mousemultiwheel: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mousewheel: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan submit: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mouseenter: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan mouseleave: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan scroll: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan reset: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan resize: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan select: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan textInput: true,
c544ac2705cf516901c8455356d56b52c8615525mfloryan unload: true
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan// Add custom event adaptors to this list. This will make it so
c544ac2705cf516901c8455356d56b52c8615525mfloryan// that delegate, key, available, contentready, etc all will
c544ac2705cf516901c8455356d56b52c8615525mfloryan// be available through Node.on
c544ac2705cf516901c8455356d56b52c8615525mfloryanY.mix(Node.DOM_EVENTS, Y.Env.evt.plugins);
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode._instances = {};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan/**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Retrieves the DOM node bound to a Node instance
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @method Node.getDOMNode
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @static
c544ac2705cf516901c8455356d56b52c8615525mfloryan *
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {Y.Node || HTMLNode} node The Node instance or an HTMLNode
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @return {HTMLNode} The DOM node bound to the Node instance. If a DOM node is passed
c544ac2705cf516901c8455356d56b52c8615525mfloryan * as the node argument, it is simply returned.
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.getDOMNode = function(node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return (node.nodeType) ? node : node._node || null;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan return null;
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.scrubVal = function(val, node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (node && val) { // only truthy values are risky
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (typeof val === 'object' || typeof val === 'function') { // safari nodeList === function
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (NODE_TYPE in val || Y.DOM.isWindow(val)) {// node || window
c544ac2705cf516901c8455356d56b52c8615525mfloryan val = Y.one(val);
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else if ((val.item && !val._nodes) || // dom collection or Node instance
c544ac2705cf516901c8455356d56b52c8615525mfloryan (val[0] && val[0][NODE_TYPE])) { // array of DOM Nodes
c544ac2705cf516901c8455356d56b52c8615525mfloryan val = Y.all(val);
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else if (val === undefined) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan val = node; // for chaining
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan return val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.addMethod = function(name, fn, context) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (name && fn && typeof fn === 'function') {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Node.prototype[name] = function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan context = context || this;
c544ac2705cf516901c8455356d56b52c8615525mfloryan var args = Y.Array(arguments, 0, true),
c544ac2705cf516901c8455356d56b52c8615525mfloryan ret;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (args[0] && args[0] instanceof Node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan args[0] = args[0]._node;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (args[1] && args[1] instanceof Node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan args[1] = args[1]._node;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan args.unshift(this._node);
c544ac2705cf516901c8455356d56b52c8615525mfloryan ret = Node.scrubVal(fn.apply(context, args), this);
c544ac2705cf516901c8455356d56b52c8615525mfloryan return ret;
c544ac2705cf516901c8455356d56b52c8615525mfloryan };
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Y.log('unable to add method: ' + name, 'warn', 'Node');
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.importMethod = function(host, name, altName) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (typeof name === 'string') {
c544ac2705cf516901c8455356d56b52c8615525mfloryan altName = altName || name;
c544ac2705cf516901c8455356d56b52c8615525mfloryan Node.addMethod(altName, host[name], host);
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Y.Array.each(name, function(n) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Node.importMethod(host, n);
c544ac2705cf516901c8455356d56b52c8615525mfloryan });
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan/**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Returns a single Node instance bound to the node or the
c544ac2705cf516901c8455356d56b52c8615525mfloryan * first element matching the given selector.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @method Y.one
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @static
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {String | HTMLElement} node a node or Selector
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {Y.Node || HTMLElement} doc an optional document to scan. Defaults to Y.config.doc.
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.one = function(node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var instance = null,
c544ac2705cf516901c8455356d56b52c8615525mfloryan cachedNode,
c544ac2705cf516901c8455356d56b52c8615525mfloryan uid;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (typeof node === 'string') {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (node.indexOf('doc') === 0) { // doc OR document
c544ac2705cf516901c8455356d56b52c8615525mfloryan node = Y.config.doc;
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else if (node.indexOf('win') === 0) { // win OR window
c544ac2705cf516901c8455356d56b52c8615525mfloryan node = Y.config.win;
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else {
c544ac2705cf516901c8455356d56b52c8615525mfloryan node = Y.Selector.query(node, null, true);
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (!node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return null;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else if (node instanceof Node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return node; // NOTE: return
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan uid = node._yuid;
c544ac2705cf516901c8455356d56b52c8615525mfloryan instance = Node._instances[uid]; // reuse exising instances
c544ac2705cf516901c8455356d56b52c8615525mfloryan cachedNode = instance ? instance._node : null;
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (!instance || (cachedNode && node !== cachedNode)) { // new Node when nodes don't match
c544ac2705cf516901c8455356d56b52c8615525mfloryan instance = new Node(node);
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan return instance;
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan/**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Returns a single Node instance bound to the node or the
c544ac2705cf516901c8455356d56b52c8615525mfloryan * first element matching the given selector.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @method Y.get
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @deprecated Use Y.one
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @static
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {String | HTMLElement} node a node or Selector
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {Y.Node || HTMLElement} doc an optional document to scan. Defaults to Y.config.doc.
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.get = function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Y.log('Y.get is deprecated, use Y.one', 'warn', 'deprecated');
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Node.one.apply(Node, arguments);
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan/**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Creates a new dom node using the provided markup string.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @method create
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @static
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {String} html The markup used to create the element
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @param {HTMLDocument} doc An optional document context
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @return {Node} A Node instance bound to a DOM node or fragment
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.create = function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Y.one(Y.DOM.create.apply(Y.DOM, arguments));
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.ATTRS = {
c544ac2705cf516901c8455356d56b52c8615525mfloryan /**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Allows for getting and setting the text of an element.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Formatting is preserved and special characters are treated literally.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @config text
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @type String
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryan text: {
c544ac2705cf516901c8455356d56b52c8615525mfloryan getter: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Y.DOM.getText(this._node);
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan setter: function(content) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Y.DOM.setText(this._node, content);
c544ac2705cf516901c8455356d56b52c8615525mfloryan return content;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan 'options': {
c544ac2705cf516901c8455356d56b52c8615525mfloryan getter: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return this._node.getElementsByTagName('option');
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan // IE: elements collection is also FORM node which trips up scrubVal.
c544ac2705cf516901c8455356d56b52c8615525mfloryan // preconverting to NodeList
c544ac2705cf516901c8455356d56b52c8615525mfloryan // TODO: break out for IE only
c544ac2705cf516901c8455356d56b52c8615525mfloryan 'elements': {
c544ac2705cf516901c8455356d56b52c8615525mfloryan getter: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Y.all(this._node.elements);
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan /**
c544ac2705cf516901c8455356d56b52c8615525mfloryan * Returns a NodeList instance of all HTMLElement children.
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @readOnly
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @config children
c544ac2705cf516901c8455356d56b52c8615525mfloryan * @type NodeList
c544ac2705cf516901c8455356d56b52c8615525mfloryan */
c544ac2705cf516901c8455356d56b52c8615525mfloryan 'children': {
c544ac2705cf516901c8455356d56b52c8615525mfloryan getter: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var node = this._node,
c544ac2705cf516901c8455356d56b52c8615525mfloryan children = node.children,
c544ac2705cf516901c8455356d56b52c8615525mfloryan childNodes, i, len;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (!children) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan childNodes = node.childNodes;
c544ac2705cf516901c8455356d56b52c8615525mfloryan children = [];
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan for (i = 0, len = childNodes.length; i < len; ++i) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (childNodes[i][TAG_NAME]) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan children[children.length] = childNodes[i];
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Y.all(children);
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan value: {
c544ac2705cf516901c8455356d56b52c8615525mfloryan getter: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return Y.DOM.getValue(this._node);
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan setter: function(val) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan Y.DOM.setValue(this._node, val);
c544ac2705cf516901c8455356d56b52c8615525mfloryan return val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan data: {
c544ac2705cf516901c8455356d56b52c8615525mfloryan getter: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan return this._data;
c544ac2705cf516901c8455356d56b52c8615525mfloryan },
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan setter: function(val) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan this._data = val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan return val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan// call with instance context
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.DEFAULT_SETTER = function(name, val) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var node = this._stateProxy,
c544ac2705cf516901c8455356d56b52c8615525mfloryan strPath;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (name.indexOf(DOT) > -1) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan strPath = name;
c544ac2705cf516901c8455356d56b52c8615525mfloryan name = name.split(DOT);
c544ac2705cf516901c8455356d56b52c8615525mfloryan // only allow when defined on node
c544ac2705cf516901c8455356d56b52c8615525mfloryan Y.Object.setValue(node, name, val);
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else if (node[name] !== undefined) { // pass thru DOM properties
c544ac2705cf516901c8455356d56b52c8615525mfloryan node[name] = val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan return val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan// call with instance context
c544ac2705cf516901c8455356d56b52c8615525mfloryanNode.DEFAULT_GETTER = function(name) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var node = this._stateProxy,
c544ac2705cf516901c8455356d56b52c8615525mfloryan val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (name.indexOf && name.indexOf(DOT) > -1) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan val = Y.Object.getValue(node, name.split(DOT));
c544ac2705cf516901c8455356d56b52c8615525mfloryan } else if (node[name] !== undefined) { // pass thru from DOM
c544ac2705cf516901c8455356d56b52c8615525mfloryan val = node[name];
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan return val;
c544ac2705cf516901c8455356d56b52c8615525mfloryan};
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanY.augment(Node, Y.Event.Target);
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryanY.mix(Node.prototype, {
c544ac2705cf516901c8455356d56b52c8615525mfloryan toString: function() {
c544ac2705cf516901c8455356d56b52c8615525mfloryan var str = '',
c544ac2705cf516901c8455356d56b52c8615525mfloryan errorMsg = this[UID] + ': not bound to a node',
c544ac2705cf516901c8455356d56b52c8615525mfloryan node = this._node;
c544ac2705cf516901c8455356d56b52c8615525mfloryan
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (node) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan str += node[NODE_NAME];
c544ac2705cf516901c8455356d56b52c8615525mfloryan if (node.id) {
c544ac2705cf516901c8455356d56b52c8615525mfloryan str += '#' + node.id;
c544ac2705cf516901c8455356d56b52c8615525mfloryan }
c544ac2705cf516901c8455356d56b52c8615525mfloryan
if (node.className) {
str += '.' + node.className.replace(' ', '.');
}
// TODO: add yuid?
str += ' ' + this[UID];
}
return str || errorMsg;
},
/**
* Returns an attribute value on the Node instance
* @method get
* @param {String} attr The attribute
* @return {any} The current value of the attribute
*/
get: function(attr) {
var val;
if (this._getAttr) { // use Attribute imple
val = this._getAttr(attr);
} else {
val = this._get(attr);
}
if (val) {
val = Y.Node.scrubVal(val, this);
}
return val;
},
_get: function(attr) {
var attrConfig = Node.ATTRS[attr],
val;
if (attrConfig && attrConfig.getter) {
val = attrConfig.getter.call(this);
} else if (Node.re_aria.test(attr)) {
val = this._node.getAttribute(attr, 2);
} else {
val = Node.DEFAULT_GETTER.apply(this, arguments);
}
return val;
},
/**
* Sets an attribute on the Node instance.
* @method set
* @param {String} attr The attribute to be set.
* @param {any} val The value to set the attribute to.
* @chainable
*/
set: function(attr, val) {
var attrConfig = Node.ATTRS[attr];
if (this._setAttr) { // use Attribute imple
this._setAttr.apply(this, arguments);
} else { // use setters inline
if (attrConfig && attrConfig.setter) {
attrConfig.setter.call(this, val);
} else if (Node.re_aria.test(attr)) { // special case Aria
this._node.setAttribute(attr, val);
} else {
Node.DEFAULT_SETTER.apply(this, arguments);
}
}
return this;
},
/**
* Sets multiple attributes.
* @method setAttrs
* @param {Object} attrMap an object of name/value pairs to set
* @chainable
*/
setAttrs: function(attrMap) {
if (this._setAttrs) { // use Attribute imple
this._setAttrs(attrMap);
} else { // use setters inline
Y.Object.each(attrMap, function(v, n) {
this.set(n, v);
}, this);
}
return this;
},
/**
* Returns an object containing the values for the requested attributes.
* @method getAttrs
* @param {Array} attrs an array of attributes to get values
* @return {Object} An object with attribute name/value pairs.
*/
getAttrs: function(attrs) {
var ret = {};
if (this._getAttrs) { // use Attribute imple
this._getAttrs(attrs);
} else { // use setters inline
Y.Array.each(attrs, function(v, n) {
ret[v] = this.get(v);
}, this);
}
return ret;
},
/**
* Creates a new Node using the provided markup string.
* @method create
* @param {String} html The markup used to create the element
* @param {HTMLDocument} doc An optional document context
* @return {Node} A Node instance bound to a DOM node or fragment
*/
create: Node.create,
/**
* Compares nodes to determine if they match.
* Node instances can be compared to each other and/or HTMLElements.
* @method compareTo
* @param {HTMLElement | Node} refNode The reference node to compare to the node.
* @return {Boolean} True if the nodes match, false if they do not.
*/
compareTo: function(refNode) {
var node = this._node;
if (refNode instanceof Y.Node) {
refNode = refNode._node;
}
return node === refNode;
},
/**
* Determines whether the node is appended to the document.
* @method inDoc
* @param {Node|HTMLElement} doc optional An optional document to check against.
* Defaults to current document.
* @return {Boolean} Whether or not this node is appended to the document.
*/
inDoc: function(doc) {
var node = this._node;
doc = (doc) ? doc._node || doc : node[OWNER_DOCUMENT];
if (doc.documentElement) {
return Y.DOM.contains(doc.documentElement, node);
}
},
getById: function(id) {
var node = this._node,
ret = Y.DOM.byId(id, node[OWNER_DOCUMENT]);
if (ret && Y.DOM.contains(node, ret)) {
ret = Y.one(ret);
} else {
ret = null;
}
return ret;
},
/**
* Returns the nearest ancestor that passes the test applied by supplied boolean method.
* @method ancestor
* @param {String | Function} fn A selector string or boolean method for testing elements.
* @param {Boolean} testSelf optional Whether or not to include the element in the scan
* If a function is used, it receives the current node being tested as the only argument.
* @return {Node} The matching Node instance or null if not found
*/
ancestor: function(fn, testSelf) {
return Y.one(Y.DOM.ancestor(this._node, _wrapFn(fn), testSelf));
},
/**
* Returns the previous matching sibling.
* Returns the nearest element node sibling if no method provided.
* @method previous
* @param {String | Function} fn A selector or boolean method for testing elements.
* If a function is used, it receives the current node being tested as the only argument.
* @return {Node} Node instance or null if not found
*/
previous: function(fn, all) {
return Y.one(Y.DOM.elementByAxis(this._node, 'previousSibling', _wrapFn(fn), all));
},
/**
* Returns the next matching sibling.
* Returns the nearest element node sibling if no method provided.
* @method next
* @param {String | Function} fn A selector or boolean method for testing elements.
* If a function is used, it receives the current node being tested as the only argument.
* @return {Node} Node instance or null if not found
*/
next: function(fn, all) {
return Y.one(Y.DOM.elementByAxis(this._node, 'nextSibling', _wrapFn(fn), all));
},
/**
* Returns all matching siblings.
* Returns all siblings if no method provided.
* @method siblings
* @param {String | Function} fn A selector or boolean method for testing elements.
* If a function is used, it receives the current node being tested as the only argument.
* @return {NodeList} NodeList instance bound to found siblings
*/
siblings: function(fn) {
return Y.all(Y.DOM.siblings(this._node, _wrapFn(fn)));
},
/**
* Retrieves a Node instance of nodes based on the given CSS selector.
* @method one
*
* @param {string} selector The CSS selector to test against.
* @return {Node} A Node instance for the matching HTMLElement.
*/
one: function(selector) {
return Y.one(Y.Selector.query(selector, this._node, true));
},
/**
* Retrieves a Node instance of nodes based on the given CSS selector.
* @method query
* @deprecated Use one()
* @param {string} selector The CSS selector to test against.
* @return {Node} A Node instance for the matching HTMLElement.
*/
query: function(selector) {
Y.log('query() is deprecated, use one()', 'warn', 'deprecated');
return this.one(selector);
},
/**
* Retrieves a nodeList based on the given CSS selector.
* @method all
*
* @param {string} selector The CSS selector to test against.
* @return {NodeList} A NodeList instance for the matching HTMLCollection/Array.
*/
all: function(selector) {
var nodelist = Y.all(Y.Selector.query(selector, this._node));
nodelist._query = selector;
nodelist._queryRoot = this;
return nodelist;
},
/**
* Retrieves a nodeList based on the given CSS selector.
* @method queryAll
* @deprecated Use all()
* @param {string} selector The CSS selector to test against.
* @return {NodeList} A NodeList instance for the matching HTMLCollection/Array.
*/
queryAll: function(selector) {
Y.log('queryAll() is deprecated, use all()', 'warn', 'deprecated');
return this.all(selector);
},
// TODO: allow fn test
/**
* Test if the supplied node matches the supplied selector.
* @method test
*
* @param {string} selector The CSS selector to test against.
* @return {boolean} Whether or not the node matches the selector.
*/
test: function(selector) {
return Y.Selector.test(this._node, selector);
},
/**
* Removes the node from its parent.
* Shortcut for myNode.get('parentNode').removeChild(myNode);
* @method remove
* @chainable
*
*/
remove: function(destroy) {
var node = this._node,
parentNode = node.parentNode;
if (parentNode) {
parentNode.removeChild(node);
}
if (destroy) {
this.destroy(true);
}
return this;
},
/**
* Replace the node with the other node. This is a DOM update only
* and does not change the node bound to the Node instance.
* Shortcut for myNode.get('parentNode').replaceChild(newNode, myNode);
* @method replace
* @param {Y.Node || HTMLNode} newNode Node to be inserted
* @chainable
*
*/
replace: function(newNode) {
var node = this._node;
node.parentNode.replaceChild(Y.Node.getDOMNode(newNode), node);
return this;
},
/**
* Removes event listeners from the node and (optionally) its subtree
* @method purge
* @param {Boolean} recurse (optional) Whether or not to remove listeners from the
* node's subtree
* @param {String} type (optional) Only remove listeners of the specified type
* @chainable
*
*/
purge: function(recurse, type) {
Y.Event.purgeElement(this._node, recurse, type);
return this;
},
/**
* Nulls internal node references, removes any plugins and event listeners
* @method destroy
* @param {Boolean} recursivePurge (optional) Whether or not to remove listeners from the
* node's subtree (default is false)
*
*/
destroy: function(recursivePurge) {
delete Node._instances[this[UID]];
this.purge(recursivePurge);
if (this.unplug) { // may not be a PluginHost
this.unplug();
}
this._node._yuid = null;
this._node = null;
this._stateProxy = null;
},
/**
* Invokes a method on the Node instance
* @method invoke
* @param {String} method The name of the method to invoke
* @param {Any} a, b, c, etc. Arguments to invoke the method with.
* @return Whatever the underly method returns.
* DOM Nodes and Collections return values
* are converted to Node/NodeList instances.
*
*/
invoke: function(method, a, b, c, d, e) {
var node = this._node,
ret;
if (a && a instanceof Y.Node) {
a = a._node;
}
if (b && b instanceof Y.Node) {
b = b._node;
}
ret = node[method](a, b, c, d, e);
return Y.Node.scrubVal(ret, this);
},
/**
* Applies the given function to each Node in the NodeList.
* @method each
* @deprecated Use NodeList
* @param {Function} fn The function to apply
* @param {Object} context optional An optional context to apply the function with
* Default context is the NodeList instance
* @chainable
*/
each: function(fn, context) {
context = context || this;
Y.log('each is deprecated on Node', 'warn', 'deprecated');
return fn.call(context, this);
},
/**
* Retrieves the Node instance at the given index.
* @method item
* @deprecated Use NodeList
*
* @param {Number} index The index of the target Node.
* @return {Node} The Node instance at the given index.
*/
item: function(index) {
Y.log('item is deprecated on Node', 'warn', 'deprecated');
return this;
},
/**
* Returns the current number of items in the Node.
* @method size
* @deprecated Use NodeList
* @return {Int} The number of items in the Node.
*/
size: function() {
Y.log('size is deprecated on Node', 'warn', 'deprecated');
return this._node ? 1 : 0;
},
/**
* Inserts the content before the reference node.
* @method insert
* @param {String | Y.Node | HTMLElement} content The content to insert
* @param {Int | Y.Node | HTMLElement | String} where The position to insert at.
* @chainable
*/
insert: function(content, where) {
var node = this._node;
if (content) {
if (typeof where === 'number') { // allow index
where = this._node.childNodes[where];
} else if (where && where._node) { // Node
where = where._node;
}
if (typeof content !== 'string') { // allow Node or NodeList/Array instances
if (content._node) { // Node
content = content._node;
} else if (content._nodes || (!content.nodeType && content.length)) { // NodeList or Array
content = Y.all(content);
Y.each(content._nodes, function(n) {
Y.DOM.addHTML(node, n, where);
});
return this; // NOTE: early return
}
}
Y.DOM.addHTML(node, content, where);
} else {
Y.log('unable to insert content ' + content, 'warn', 'node');
}
return this;
},
/**
* Inserts the content as the firstChild of the node.
* @method prepend
* @param {String | Y.Node | HTMLElement} content The content to insert
* @chainable
*/
prepend: function(content) {
return this.insert(content, 0);
},
/**
* Inserts the content as the lastChild of the node.
* @method append
* @param {String | Y.Node | HTMLElement} content The content to insert
* @chainable
*/
append: function(content) {
return this.insert(content, null);
},
/**
* Replaces the node's current content with the content.
* @method setContent
* @param {String | Y.Node | HTMLElement} content The content to insert
* @chainable
*/
setContent: function(content) {
if (content) {
if (content._node) { // map to DOMNode
content = content._node;
} else if (content._nodes) { // convert DOMNodeList to documentFragment
content = Y.DOM._nl2Frag(content._nodes);
}
}
Y.DOM.addHTML(this._node, content, 'replace');
return this;
},
/**
* @method swap
* @description Swap DOM locations with the given node.
* This does not change which DOM node each Node instance refers to.
* @param {Node} otherNode The node to swap with
* @chainable
*/
swap: document.documentElement.swapNode ?
function(otherNode) {
this._node.swapNode(Y.Node.getDOMNode(otherNode));
} :
function(otherNode) {
otherNode = Y.Node.getDOMNode(otherNode);
var node = this._node,
parent = otherNode.parentNode,
nextSibling = otherNode.nextSibling;
if (nextSibling === node) {
parent.insertBefore(node, otherNode);
} else if (otherNode === node.nextSibling) {
parent.insertBefore(otherNode, node);
} else {
node.parentNode.replaceChild(otherNode, node);
Y.DOM.addHTML(parent, node, nextSibling);
}
return this;
},
hasMethod: function(method) {
var node = this._node;
return (node && node[method] && (typeof node[method] === 'function'));
}
}, true);
Y.Node = Node;
Y.get = Y.Node.get;
Y.one = Y.Node.one;
/**
* The NodeList module provides support for managing collections of Nodes.
* @module node
* @submodule nodelist
*/
/**
* The NodeList class provides a wrapper for manipulating DOM NodeLists.
* NodeList properties can be accessed via the set/get methods.
* Use Y.all() to retrieve NodeList instances.
*
* @class NodeList
* @constructor
*/
var NodeList = function(nodes) {
if (typeof nodes === 'string') {
this._query = nodes;
nodes = Y.Selector.query(nodes);
} else if (nodes.nodeType) {
nodes = [nodes];
} else {
nodes = Y.Array(nodes, 0, true);
}
NodeList._instances[Y.stamp(this)] = this;
this._nodes = nodes;
};
// end "globals"
NodeList.NAME = 'NodeList';
/**
* Retrieves the DOM nodes bound to a NodeList instance
* @method NodeList.getDOMNodes
* @static
*
* @param {Y.NodeList} node The NodeList instance
* @return {Array} The array of DOM nodes bound to the NodeList
*/
NodeList.getDOMNodes = function(nodeList) {
return nodeList._nodes;
};
NodeList._instances = [];
NodeList.each = function(instance, fn, context) {
var nodes = instance._nodes;
if (nodes && nodes.length) {
Y.Array.each(nodes, fn, context || instance);
} else {
Y.log('no nodes bound to ' + this, 'warn', 'NodeList');
}
};
NodeList.addMethod = function(name, fn, context) {
if (name && fn) {
NodeList.prototype[name] = function() {
var ret = [],
args = arguments;
Y.Array.each(this._nodes, function(node) {
var UID = '_yuid',
instance = Y.Node._instances[node[UID]],
ctx,
result;
if (!instance) {
instance = NodeList._getTempNode(node);
}
ctx = context || instance;
result = fn.apply(ctx, args);
if (result !== undefined && result !== instance) {
ret[ret.length] = result;
}
});
// TODO: remove tmp pointer
return ret.length ? ret : this;
};
} else {
Y.log('unable to add method: ' + name, 'warn', 'Node');
}
};
NodeList.importMethod = function(host, name, altName) {
if (typeof name === 'string') {
altName = altName || name;
NodeList.addMethod(name, host[name]);
} else {
Y.Array.each(name, function(n) {
NodeList.importMethod(host, n);
});
}
};
NodeList._getTempNode = function(node) {
var tmp = NodeList._tempNode;
if (!tmp) {
tmp = Y.Node.create('<div></div>');
NodeList._tempNode = tmp;
}
tmp._node = node;
tmp._stateProxy = node;
return tmp;
};
Y.mix(NodeList.prototype, {
/**
* Retrieves the Node instance at the given index.
* @method item
*
* @param {Number} index The index of the target Node.
* @return {Node} The Node instance at the given index.
*/
item: function(index) {
return Y.one((this._nodes || [])[index]);
},
/**
* Applies the given function to each Node in the NodeList.
* @method each
* @param {Function} fn The function to apply. It receives 3 arguments:
* the current node instance, the node's index, and the NodeList instance
* @param {Object} context optional An optional context to apply the function with
* Default context is the current Node instance
* @chainable
*/
each: function(fn, context) {
var instance = this;
Y.Array.each(this._nodes, function(node, index) {
node = Y.one(node);
return fn.call(context || node, node, index, instance);
});
return instance;
},
batch: function(fn, context) {
var nodelist = this;
Y.Array.each(this._nodes, function(node, index) {
var instance = Y.Node._instances[node[UID]];
if (!instance) {
instance = NodeList._getTempNode(node);
}
return fn.call(context || instance, instance, index, nodelist);
});
return nodelist;
},
/**
* Executes the function once for each node until a true value is returned.
* @method some
* @param {Function} fn The function to apply. It receives 3 arguments:
* the current node instance, the node's index, and the NodeList instance
* @param {Object} context optional An optional context to execute the function from.
* Default context is the current Node instance
* @return {Boolean} Whether or not the function returned true for any node.
*/
some: function(fn, context) {
var instance = this;
return Y.Array.some(this._nodes, function(node, index) {
node = Y.one(node);
context = context || node;
return fn.call(context, node, index, instance);
});
},
/**
* Creates a documenFragment from the nodes bound to the NodeList instance
* @method toFrag
* @return Node a Node instance bound to the documentFragment
*/
toFrag: function() {
return Y.one(Y.DOM._nl2frag(this._nodes));
},
/**
* Returns the index of the node in the NodeList instance
* or -1 if the node isn't found.
* @method indexOf
* @param {Y.Node || DOMNode} node the node to search for
* @return {Int} the index of the node value or -1 if not found
*/
indexOf: function(node) {
return Y.Array.indexOf(this._nodes, Y.Node.getDOMNode(node));
},
/**
* Filters the NodeList instance down to only nodes matching the given selector.
* @method filter
* @param {String} selector The selector to filter against
* @return {NodeList} NodeList containing the updated collection
* @see Selector
*/
filter: function(selector) {
return Y.all(Y.Selector.filter(this._nodes, selector));
},
/**
* Creates a new NodeList containing all nodes at every n indices, where
* remainder n % index equals r.
* (zero-based index).
* @method modulus
* @param {Int} n The offset to use (return every nth node)
* @param {Int} r An optional remainder to use with the modulus operation (defaults to zero)
* @return {NodeList} NodeList containing the updated collection
*/
modulus: function(n, r) {
r = r || 0;
var nodes = [];
NodeList.each(this, function(node, i) {
if (i % n === r) {
nodes.push(node);
}
});
return Y.all(nodes);
},
/**
* Creates a new NodeList containing all nodes at odd indices
* (zero-based index).
* @method odd
* @return {NodeList} NodeList containing the updated collection
*/
odd: function() {
return this.modulus(2, 1);
},
/**
* Creates a new NodeList containing all nodes at even indices
* (zero-based index), including zero.
* @method even
* @return {NodeList} NodeList containing the updated collection
*/
even: function() {
return this.modulus(2);
},
destructor: function() {
delete NodeList._instances[this[UID]];
},
/**
* Reruns the initial query, when created using a selector query
* @method refresh
* @chainable
*/
refresh: function() {
var doc,
nodes = this._nodes,
query = this._query,
root = this._queryRoot;
if (query) {
if (!root) {
if (nodes && nodes[0] && nodes[0].ownerDocument) {
root = nodes[0].ownerDocument;
}
}
this._nodes = Y.Selector.query(query, root);
}
return this;
},
/**
* Applies an event listener to each Node bound to the NodeList.
* @method on
* @param {String} type The event being listened for
* @param {Function} fn The handler to call when the event fires
* @param {Object} context The context to call the handler with.
* Default is the NodeList instance.
* @return {Object} Returns an event handle that can later be use to detach().
* @see Event.on
*/
on: function(type, fn, context) {
var args = Y.Array(arguments, 0, true);
args.splice(2, 0, this._nodes);
args[3] = context || this;
return Y.on.apply(Y, args);
},
/**
* Applies an event listener to each Node bound to the NodeList.
* The handler is called only after all on() handlers are called
* and the event is not prevented.
* @method after
* @param {String} type The event being listened for
* @param {Function} fn The handler to call when the event fires
* @param {Object} context The context to call the handler with.
* Default is the NodeList instance.
* @return {Object} Returns an event handle that can later be use to detach().
* @see Event.on
*/
after: function(type, fn, context) {
var args = Y.Array(arguments, 0, true);
args.splice(2, 0, this._nodes);
args[3] = context || this;
return Y.after.apply(Y, args);
},
/**
* Returns the current number of items in the NodeList.
* @method size
* @return {Int} The number of items in the NodeList.
*/
size: function() {
return this._nodes.length;
},
/**
* Determines if the instance is bound to any nodes
* @method isEmpty
* @return {Boolean} Whether or not the NodeList is bound to any nodes
*/
isEmpty: function() {
return this._nodes.length < 1;
},
toString: function() {
var str = '',
errorMsg = this[UID] + ': not bound to any nodes',
nodes = this._nodes,
node;
if (nodes && nodes[0]) {
node = nodes[0];
str += node[NODE_NAME];
if (node.id) {
str += '#' + node.id;
}
if (node.className) {
str += '.' + node.className.replace(' ', '.');
}
if (nodes.length > 1) {
str += '...[' + nodes.length + ' items]';
}
}
return str || errorMsg;
}
}, true);
NodeList.importMethod(Y.Node.prototype, [
/**
* Called on each Node instance
* @for NodeList
* @method append
* @see Node.append
*/
'append',
/**
* Called on each Node instance
* @method detach
* @see Node.detach
*/
'detach',
/** Called on each Node instance
* @method detachAll
* @see Node.detachAll
*/
'detachAll',
/** Called on each Node instance
* @method insert
* @see NodeInsert
*/
'insert',
/** Called on each Node instance
* @method prepend
* @see Node.prepend
*/
'prepend',
/** Called on each Node instance
* @method remove
* @see Node.remove
*/
'remove',
/** Called on each Node instance
* @method removeAttribute
* @see Node.removeAttribute
*/
'removeAttribute',
/** Called on each Node instance
* @method set
* @see Node.set
*/
'set',
/** Called on each Node instance
* @method setContent
* @see Node.setContent
*/
'setContent'
]);
// one-off implementation to convert array of Nodes to NodeList
// e.g. Y.all('input').get('parentNode');
/** Called on each Node instance
* @method get
* @see Node
*/
NodeList.prototype.get = function(attr) {
var ret = [],
nodes = this._nodes,
isNodeList = false,
getTemp = NodeList._getTempNode,
instance,
val;
if (nodes[0]) {
instance = Y.Node._instances[nodes[0]._yuid] || getTemp(nodes[0]);
val = instance._get(attr);
if (val && val.nodeType) {
isNodeList = true;
}
}
Y.Array.each(nodes, function(node) {
instance = Y.Node._instances[node._yuid];
if (!instance) {
instance = getTemp(node);
}
val = instance._get(attr);
if (!isNodeList) { // convert array of Nodes to NodeList
val = Y.Node.scrubVal(val, instance);
}
ret.push(val);
});
return (isNodeList) ? Y.all(ret) : ret;
};
Y.NodeList = NodeList;
Y.all = function(nodes) {
return new NodeList(nodes);
};
Y.Node.all = Y.all;
Y.Array.each([
/**
* Passes through to DOM method.
* @method replaceChild
* @for Node
* @param {HTMLElement | Node} node Node to be inserted
* @param {HTMLElement | Node} refNode Node to be replaced
* @return {Node} The replaced node
*/
'replaceChild',
/**
* Passes through to DOM method.
* @method appendChild
* @param {HTMLElement | Node} node Node to be appended
* @return {Node} The appended node
*/
'appendChild',
/**
* Passes through to DOM method.
* @method insertBefore
* @param {HTMLElement | Node} newNode Node to be appended
* @param {HTMLElement | Node} refNode Node to be inserted before
* @return {Node} The inserted node
*/
'insertBefore',
/**
* Passes through to DOM method.
* @method removeChild
* @param {HTMLElement | Node} node Node to be removed
* @return {Node} The removed node
*/
'removeChild',
/**
* Passes through to DOM method.
* @method hasChildNodes
* @return {Boolean} Whether or not the node has any childNodes
*/
'hasChildNodes',
/**
* Passes through to DOM method.
* @method cloneNode
* @param {Boolean} deep Whether or not to perform a deep clone, which includes
* subtree and attributes
* @return {Node} The clone
*/
'cloneNode',
/**
* Passes through to DOM method.
* @method hasAttribute
* @param {String} attribute The attribute to test for
* @return {Boolean} Whether or not the attribute is present
*/
'hasAttribute',
/**
* Passes through to DOM method.
* @method removeAttribute
* @param {String} attribute The attribute to be removed
* @chainable
*/
'removeAttribute',
/**
* Passes through to DOM method.
* @method scrollIntoView
* @chainable
*/
'scrollIntoView',
/**
* Passes through to DOM method.
* @method getElementsByTagName
* @param {String} tagName The tagName to collect
* @return {NodeList} A NodeList representing the HTMLCollection
*/
'getElementsByTagName',
/**
* Passes through to DOM method.
* @method focus
* @chainable
*/
'focus',
/**
* Passes through to DOM method.
* @method blur
* @chainable
*/
'blur',
/**
* Passes through to DOM method.
* Only valid on FORM elements
* @method submit
* @chainable
*/
'submit',
/**
* Passes through to DOM method.
* Only valid on FORM elements
* @method reset
* @chainable
*/
'reset',
/**
* Passes through to DOM method.
* @method select
* @chainable
*/
'select'
], function(method) {
Y.Node.prototype[method] = function(arg1, arg2, arg3) {
var ret = this.invoke(method, arg1, arg2, arg3);
return ret;
};
});
Node.importMethod(Y.DOM, [
/**
* Determines whether the ndoe is an ancestor of another HTML element in the DOM hierarchy.
* @method contains
* @param {Node | HTMLElement} needle The possible node or descendent
* @return {Boolean} Whether or not this node is the needle its ancestor
*/
'contains',
/**
* Allows setting attributes on DOM nodes, normalizing in some cases.
* This passes through to the DOM node, allowing for custom attributes.
* @method setAttribute
* @for Node
* @for NodeList
* @chainable
* @param {string} name The attribute name
* @param {string} value The value to set
*/
'setAttribute',
/**
* Allows getting attributes on DOM nodes, normalizing in some cases.
* This passes through to the DOM node, allowing for custom attributes.
* @method getAttribute
* @for Node
* @for NodeList
* @param {string} name The attribute name
* @return {string} The attribute value
*/
'getAttribute'
]);
/**
* Allows setting attributes on DOM nodes, normalizing in some cases.
* This passes through to the DOM node, allowing for custom attributes.
* @method setAttribute
* @see Node
* @for NodeList
* @chainable
* @param {string} name The attribute name
* @param {string} value The value to set
*/
/**
* Allows getting attributes on DOM nodes, normalizing in some cases.
* This passes through to the DOM node, allowing for custom attributes.
* @method getAttribute
* @see Node
* @for NodeList
* @param {string} name The attribute name
* @return {string} The attribute value
*/
Y.NodeList.importMethod(Y.Node.prototype, ['getAttribute', 'setAttribute']);
(function(Y) {
var methods = [
/**
* Determines whether each node has the given className.
* @method hasClass
* @for Node
* @param {String} className the class name to search for
* @return {Array} An array of booleans for each node bound to the NodeList.
*/
'hasClass',
/**
* Adds a class name to each node.
* @method addClass
* @param {String} className the class name to add to the node's class attribute
* @chainable
*/
'addClass',
/**
* Removes a class name from each node.
* @method removeClass
* @param {String} className the class name to remove from the node's class attribute
* @chainable
*/
'removeClass',
/**
* Replace a class with another class for each node.
* If no oldClassName is present, the newClassName is simply added.
* @method replaceClass
* @param {String} oldClassName the class name to be replaced
* @param {String} newClassName the class name that will be replacing the old class name
* @chainable
*/
'replaceClass',
/**
* If the className exists on the node it is removed, if it doesn't exist it is added.
* @method toggleClass
* @param {String} className the class name to be toggled
* @chainable
*/
'toggleClass'
];
Y.Node.importMethod(Y.DOM, methods);
/**
* Determines whether each node has the given className.
* @method hasClass
* @see Node.hasClass
* @for NodeList
* @param {String} className the class name to search for
* @return {Array} An array of booleans for each node bound to the NodeList.
*/
/**
* Adds a class name to each node.
* @method addClass
* @see Node.addClass
* @param {String} className the class name to add to the node's class attribute
* @chainable
*/
/**
* Removes a class name from each node.
* @method removeClass
* @see Node.removeClass
* @param {String} className the class name to remove from the node's class attribute
* @chainable
*/
/**
* Replace a class with another class for each node.
* If no oldClassName is present, the newClassName is simply added.
* @method replaceClass
* @see Node.replaceClass
* @param {String} oldClassName the class name to be replaced
* @param {String} newClassName the class name that will be replacing the old class name
* @chainable
*/
/**
* If the className exists on the node it is removed, if it doesn't exist it is added.
* @method toggleClass
* @see Node.toggleClass
* @param {String} className the class name to be toggled
* @chainable
*/
Y.NodeList.importMethod(Y.Node.prototype, methods);
})(Y);
if (!document.documentElement.hasAttribute) { // IE < 8
Y.Node.prototype.hasAttribute = function(attr) {
return !!(this._node.attributes[attr] &&
this._node.attributes[attr].specified);
};
}
// IE throws error when setting input.type = 'hidden',
// input.setAttribute('type', 'hidden') and input.attributes.type.value = 'hidden'
Y.Node.ATTRS.type = {
setter: function(val) {
if (val === 'hidden') {
try {
this._node.type = 'hidden';
} catch(e) {
this.setStyle('display', 'none');
this._inputType = 'hidden';
}
} else {
try { // IE errors when changing the type from "hidden'
this._node.type = val;
} catch (e) {
Y.log('error setting type: ' + val, 'info', 'node');
}
}
return val;
},
getter: function() {
return this._inputType || this._node.type;
},
_bypassProxy: true // don't update DOM when using with Attribute
};
}, '@VERSION@' ,{requires:['dom-base', 'selector-css2', 'event-base']});