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