]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/NodeList-data.js.uncompressed.js
1 define("dojo/NodeList-data", [
2 "./_base/kernel", "./query", "./_base/lang", "./_base/array", "./dom-attr"
3 ], function(dojo
, query
, lang
, array
, attr
){
11 // Adds data() and removeData() methods to NodeList, and returns NodeList constructor.
15 var NodeList
= query
.NodeList
;
17 var dataCache
= {}, x
= 0, dataattr
= "data-dojo-dataid",
18 dopid = function(node
){
20 // Return a uniqueish ID for the passed node reference
21 var pid
= attr
.get(node
, dataattr
);
24 attr
.set(node
, dataattr
, pid
);
31 var dodata
= dojo
._nodeData = function(node
, key
, value
){
33 // Private helper for dojo/NodeList.data for single node data access. Refer to NodeList.data
34 // documentation for more information.
36 // node: String|DomNode
37 // The node to associate data with
39 // key: Object|String?
40 // If an object, act as a setter and iterate over said object setting data items as defined.
41 // If a string, and `value` present, set the data for defined `key` to `value`
42 // If a string, and `value` absent, act as a getter, returning the data associated with said `key`
45 // The value to set for said `key`, provided `key` is a string (and not an object)
47 var pid
= dopid(node
), r
;
48 if(!dataCache
[pid
]){ dataCache
[pid
] = {}; }
50 // API discrepency: calling with only a node returns the whole object. $.data throws
51 if(arguments
.length
== 1){ r
= dataCache
[pid
]; }
52 if(typeof key
== "string"){
53 // either getter or setter, based on `value` presence
54 if(arguments
.length
> 2){
55 dataCache
[pid
][key
] = value
;
57 r
= dataCache
[pid
][key
];
60 // must be a setter, mix `value` into data hash
61 // API discrepency: using object as setter works here
62 r
= lang
.mixin(dataCache
[pid
], key
);
65 return r
; // Object|Anything|Nothing
68 var removeData
= dojo
._removeNodeData = function(node
, key
){
70 // Remove some data from this node
71 // node: String|DomNode
72 // The node reference to remove data from
74 // If omitted, remove all data in this dataset.
75 // If passed, remove only the passed `key` in the associated dataset
76 var pid
= dopid(node
);
79 delete dataCache
[pid
][key
];
81 delete dataCache
[pid
];
86 dojo
._gcNodeData = function(){
88 // super expensive: GC all data in the data for nodes that no longer exist in the dom.
90 // super expensive: GC all data in the data for nodes that no longer exist in the dom.
91 // MUCH safer to do this yourself, manually, on a per-node basis (via `NodeList.removeData()`)
92 // provided as a stop-gap for exceptionally large/complex applications with constantly changing
93 // content regions (eg: a dijit/layout/ContentPane with replacing data)
94 // There is NO automatic GC going on. If you dojo.destroy() a node, you should _removeNodeData
95 // prior to destruction.
96 var livePids
= query("[" + dataattr
+ "]").map(dopid
);
97 for(var i
in dataCache
){
98 if(array
.indexOf(livePids
, i
) < 0){ delete dataCache
[i
]; }
102 // make nodeData and removeNodeData public on dojo/NodeList:
103 lang
.extend(NodeList
, {
104 data
: NodeList
._adaptWithCondition(dodata
, function(a
){
105 return a
.length
=== 0 || a
.length
== 1 && (typeof a
[0] == "string");
107 removeData
: NodeList
._adaptAsForEach(removeData
)
111 lang.extend(NodeList, {
112 data: function(key, value){
114 // stash or get some arbitrary data on/from these nodes.
117 // Stash or get some arbitrary data on/from these nodes. This private _data function is
118 // exposed publicly on `dojo/NodeList`, eg: as the result of a `dojo.query` call.
119 // DIFFERS from jQuery.data in that when used as a getter, the entire list is ALWAYS
120 // returned. EVEN WHEN THE LIST IS length == 1.
122 // A single-node version of this function is provided as `dojo._nodeData`, which follows
123 // the same signature, though expects a String ID or DomNode reference in the first
124 // position, before key/value arguments.
126 // node: String|DomNode
127 // The node to associate data with
129 // key: Object|String?
130 // If an object, act as a setter and iterate over said object setting data items as defined.
131 // If a string, and `value` present, set the data for defined `key` to `value`
132 // If a string, and `value` absent, act as a getter, returning the data associated with said `key`
135 // The value to set for said `key`, provided `key` is a string (and not an object)
138 // Set a key `bar` to some data, then retrieve it.
139 // | dojo.query(".foo").data("bar", "touched");
140 // | var touched = dojo.query(".foo").data("bar");
141 // | if(touched[0] == "touched"){ alert('win'); }
144 // Get all the data items for a given node.
145 // | var list = dojo.query(".foo").data();
146 // | var first = list[0];
149 // Set the data to a complex hash. Overwrites existing keys with new value
150 // | dojo.query(".foo").data({ bar:"baz", foo:"bar" });
151 // Then get some random key:
152 // | dojo.query(".foo").data("foo"); // returns [`bar`]
154 // returns: Object|Anything|Nothing
155 // When used as a setter via `dojo/NodeList`, a NodeList instance is returned
156 // for further chaining. When used as a getter via `dojo/NodeList` an ARRAY
157 // of items is returned. The items in the array correspond to the elements
158 // in the original list. This is true even when the list length is 1, eg:
159 // when looking up a node by ID (#foo)
162 removeData: function(key){
164 // Remove the data associated with these nodes.
166 // If omitted, clean all data for this node.
167 // If passed, remove the data item found at `key`
172 // TODO: this is the basic implementation of adaptWithCondtionAndWhenMappedConsiderLength, for lack of a better API name
173 // it conflicts with the the `dojo/NodeList` way: always always return an arrayLike thinger. Consider for 2.0:
175 // NodeList.prototype.data = function(key, value){
176 // var a = arguments, r;
177 // if(a.length === 0 || a.length == 1 && (typeof a[0] == "string")){
178 // r = this.map(function(node){
179 // return d._data(node, key);
181 // if(r.length == 1){ r = r[0]; } // the offending line, and the diff on adaptWithCondition
183 // r = this.forEach(function(node){
184 // d._data(node, key, value);
187 // return r; // NodeList|Array|SingleItem