src/cm/media/js/lib/yui/yui_3.10.3/build/view-node-map/view-node-map.js
changeset 525 89ef5ed3c48b
equal deleted inserted replaced
524:322d0feea350 525:89ef5ed3c48b
       
     1 /*
       
     2 YUI 3.10.3 (build 2fb5187)
       
     3 Copyright 2013 Yahoo! Inc. All rights reserved.
       
     4 Licensed under the BSD License.
       
     5 http://yuilibrary.com/license/
       
     6 */
       
     7 
       
     8 YUI.add('view-node-map', function (Y, NAME) {
       
     9 
       
    10 /**
       
    11 View extension that adds a static `getByNode()` method that returns the nearest
       
    12 View instance associated with the given Node (similar to Widget's `getByNode()`
       
    13 method).
       
    14 
       
    15 @module app
       
    16 @submodule view-node-map
       
    17 @since 3.5.0
       
    18 **/
       
    19 
       
    20 var buildCfg  = Y.namespace('View._buildCfg'),
       
    21     instances = {};
       
    22 
       
    23 /**
       
    24 View extension that adds a static `getByNode()` method that returns the nearest
       
    25 View instance associated with the given Node (similar to Widget's `getByNode()`
       
    26 method).
       
    27 
       
    28 Note that it's important to call `destroy()` on a View instance using this
       
    29 extension when you plan to stop using it. This ensures that all internal
       
    30 references to that View are cleared to prevent memory leaks.
       
    31 
       
    32 @class View.NodeMap
       
    33 @extensionfor View
       
    34 @since 3.5.0
       
    35 **/
       
    36 function NodeMap() {}
       
    37 
       
    38 // Tells Base.create() to mix the static getByNode method into built classes.
       
    39 // We're cheating and modifying Y.View here, because right now there's no better
       
    40 // way to do it.
       
    41 buildCfg.aggregates || (buildCfg.aggregates = []);
       
    42 buildCfg.aggregates.push('getByNode');
       
    43 
       
    44 /**
       
    45 Returns the nearest View instance associated with the given Node. The Node may
       
    46 be a View container or any child of a View container.
       
    47 
       
    48 Note that only instances of Views that have the Y.View.NodeMap extension mixed
       
    49 in will be returned. The base View class doesn't provide this functionality by
       
    50 default due to the additional memory management overhead involved in maintaining
       
    51 a mapping of Nodes to View instances.
       
    52 
       
    53 @method getByNode
       
    54 @param {Node|HTMLElement|String} node Node instance, selector string, or
       
    55     HTMLElement.
       
    56 @return {View} Closest View instance associated with the given Node, or `null`
       
    57     if no associated View instance was found.
       
    58 @static
       
    59 @since 3.5.0
       
    60 **/
       
    61 NodeMap.getByNode = function (node) {
       
    62     var view;
       
    63 
       
    64     Y.one(node).ancestor(function (ancestor) {
       
    65         return (view = instances[Y.stamp(ancestor, true)]) || false;
       
    66     }, true);
       
    67 
       
    68     return view || null;
       
    69 };
       
    70 
       
    71 // To make this testable.
       
    72 NodeMap._instances = instances;
       
    73 
       
    74 NodeMap.prototype = {
       
    75     initializer: function () {
       
    76         instances[Y.stamp(this.get('container'))] = this;
       
    77     },
       
    78 
       
    79     destructor: function () {
       
    80         var stamp = Y.stamp(this.get('container'), true);
       
    81 
       
    82         if (stamp in instances) {
       
    83             delete instances[stamp];
       
    84         }
       
    85     }
       
    86 };
       
    87 
       
    88 Y.View.NodeMap = NodeMap;
       
    89 
       
    90 
       
    91 }, '3.10.3', {"requires": ["view"]});