PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/dojo/1.8.9/NodeList-data.js.uncompressed.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 192 lines | 53 code | 15 blank | 124 comment | 15 complexity | 6fd128ff11924e618c8508facac2485c MD5 | raw file
  1. define("dojo/NodeList-data", [
  2. "./_base/kernel", "./query", "./_base/lang", "./_base/array", "./dom-attr"
  3. ], function(dojo, query, lang, array, attr){
  4. // module:
  5. // dojo/NodeList-data
  6. /*=====
  7. return function(){
  8. // summary:
  9. // Adds data() and removeData() methods to NodeList, and returns NodeList constructor.
  10. };
  11. =====*/
  12. var NodeList = query.NodeList;
  13. var dataCache = {}, x = 0, dataattr = "data-dojo-dataid",
  14. dopid = function(node){
  15. // summary:
  16. // Return a uniqueish ID for the passed node reference
  17. var pid = attr.get(node, dataattr);
  18. if(!pid){
  19. pid = "pid" + (x++);
  20. attr.set(node, dataattr, pid);
  21. }
  22. return pid;
  23. }
  24. ;
  25. var dodata = dojo._nodeData = function(node, key, value){
  26. // summary:
  27. // Private helper for dojo/NodeList.data for single node data access. Refer to NodeList.data
  28. // documentation for more information.
  29. //
  30. // node: String|DomNode
  31. // The node to associate data with
  32. //
  33. // key: Object|String?
  34. // If an object, act as a setter and iterate over said object setting data items as defined.
  35. // If a string, and `value` present, set the data for defined `key` to `value`
  36. // If a string, and `value` absent, act as a getter, returning the data associated with said `key`
  37. //
  38. // value: Anything?
  39. // The value to set for said `key`, provided `key` is a string (and not an object)
  40. //
  41. var pid = dopid(node), r;
  42. if(!dataCache[pid]){ dataCache[pid] = {}; }
  43. // API discrepency: calling with only a node returns the whole object. $.data throws
  44. if(arguments.length == 1){ r = dataCache[pid]; }
  45. if(typeof key == "string"){
  46. // either getter or setter, based on `value` presence
  47. if(arguments.length > 2){
  48. dataCache[pid][key] = value;
  49. }else{
  50. r = dataCache[pid][key];
  51. }
  52. }else{
  53. // must be a setter, mix `value` into data hash
  54. // API discrepency: using object as setter works here
  55. r = lang.mixin(dataCache[pid], key);
  56. }
  57. return r; // Object|Anything|Nothing
  58. };
  59. var removeData = dojo._removeNodeData = function(node, key){
  60. // summary:
  61. // Remove some data from this node
  62. // node: String|DomNode
  63. // The node reference to remove data from
  64. // key: String?
  65. // If omitted, remove all data in this dataset.
  66. // If passed, remove only the passed `key` in the associated dataset
  67. var pid = dopid(node);
  68. if(dataCache[pid]){
  69. if(key){
  70. delete dataCache[pid][key];
  71. }else{
  72. delete dataCache[pid];
  73. }
  74. }
  75. };
  76. dojo._gcNodeData = function(){
  77. // summary:
  78. // super expensive: GC all data in the data for nodes that no longer exist in the dom.
  79. // description:
  80. // super expensive: GC all data in the data for nodes that no longer exist in the dom.
  81. // MUCH safer to do this yourself, manually, on a per-node basis (via `NodeList.removeData()`)
  82. // provided as a stop-gap for exceptionally large/complex applications with constantly changing
  83. // content regions (eg: a dijit/layout/ContentPane with replacing data)
  84. // There is NO automatic GC going on. If you dojo.destroy() a node, you should _removeNodeData
  85. // prior to destruction.
  86. var livePids = query("[" + dataattr + "]").map(dopid);
  87. for(var i in dataCache){
  88. if(array.indexOf(livePids, i) < 0){ delete dataCache[i]; }
  89. }
  90. };
  91. // make nodeData and removeNodeData public on dojo/NodeList:
  92. lang.extend(NodeList, {
  93. data: NodeList._adaptWithCondition(dodata, function(a){
  94. return a.length === 0 || a.length == 1 && (typeof a[0] == "string");
  95. }),
  96. removeData: NodeList._adaptAsForEach(removeData)
  97. });
  98. /*=====
  99. lang.extend(NodeList, {
  100. data: function(key, value){
  101. // summary:
  102. // stash or get some arbitrary data on/from these nodes.
  103. //
  104. // description:
  105. // Stash or get some arbitrary data on/from these nodes. This private _data function is
  106. // exposed publicly on `dojo/NodeList`, eg: as the result of a `dojo.query` call.
  107. // DIFFERS from jQuery.data in that when used as a getter, the entire list is ALWAYS
  108. // returned. EVEN WHEN THE LIST IS length == 1.
  109. //
  110. // A single-node version of this function is provided as `dojo._nodeData`, which follows
  111. // the same signature, though expects a String ID or DomNode reference in the first
  112. // position, before key/value arguments.
  113. //
  114. // node: String|DomNode
  115. // The node to associate data with
  116. //
  117. // key: Object|String?
  118. // If an object, act as a setter and iterate over said object setting data items as defined.
  119. // If a string, and `value` present, set the data for defined `key` to `value`
  120. // If a string, and `value` absent, act as a getter, returning the data associated with said `key`
  121. //
  122. // value: Anything?
  123. // The value to set for said `key`, provided `key` is a string (and not an object)
  124. //
  125. // example:
  126. // Set a key `bar` to some data, then retrieve it.
  127. // | dojo.query(".foo").data("bar", "touched");
  128. // | var touched = dojo.query(".foo").data("bar");
  129. // | if(touched[0] == "touched"){ alert('win'); }
  130. //
  131. // example:
  132. // Get all the data items for a given node.
  133. // | var list = dojo.query(".foo").data();
  134. // | var first = list[0];
  135. //
  136. // example:
  137. // Set the data to a complex hash. Overwrites existing keys with new value
  138. // | dojo.query(".foo").data({ bar:"baz", foo:"bar" });
  139. // Then get some random key:
  140. // | dojo.query(".foo").data("foo"); // returns [`bar`]
  141. //
  142. // returns: Object|Anything|Nothing
  143. // When used as a setter via `dojo/NodeList`, a NodeList instance is returned
  144. // for further chaining. When used as a getter via `dojo/NodeList` an ARRAY
  145. // of items is returned. The items in the array correspond to the elements
  146. // in the original list. This is true even when the list length is 1, eg:
  147. // when looking up a node by ID (#foo)
  148. },
  149. removeData: function(key){
  150. // summary:
  151. // Remove the data associated with these nodes.
  152. // key: String?
  153. // If omitted, clean all data for this node.
  154. // If passed, remove the data item found at `key`
  155. }
  156. });
  157. =====*/
  158. // TODO: this is the basic implementation of adaptWithCondtionAndWhenMappedConsiderLength, for lack of a better API name
  159. // it conflicts with the the `dojo/NodeList` way: always always return an arrayLike thinger. Consider for 2.0:
  160. //
  161. // NodeList.prototype.data = function(key, value){
  162. // var a = arguments, r;
  163. // if(a.length === 0 || a.length == 1 && (typeof a[0] == "string")){
  164. // r = this.map(function(node){
  165. // return d._data(node, key);
  166. // });
  167. // if(r.length == 1){ r = r[0]; } // the offending line, and the diff on adaptWithCondition
  168. // }else{
  169. // r = this.forEach(function(node){
  170. // d._data(node, key, value);
  171. // });
  172. // }
  173. // return r; // NodeList|Array|SingleItem
  174. // };
  175. return NodeList;
  176. });