~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to lib/yuilib/3.9.1/build/view-node-map/view-node-map.js

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

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