/
data.js
133 lines (126 loc) · 3.79 KB
/
data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
var domDataState = require("can-dom-data-state");
var mutationDocument = require("../mutation-observer/document/document");
var deleteNode = function() {
return domDataState.delete.call(this);
};
// count of distinct elements that have domData set
var elementSetCount = 0;
var cleanupDomData = function(node) {
// decrement count if node was deleted
elementSetCount -= deleteNode.call(node) ? 1 : 0;
// remove handler once all domData has been cleaned up
if (elementSetCount === 0) {
mutationDocument.offAfterRemovedNodes(cleanupDomData);
}
};
/**
* @module {{}} can-util/dom/data/data data
* @parent can-util/dom
* @description Allows associating data as a key/value pair for a particular
* DOM Node.
*
* ```js
* var domData = require("can-util/dom/data/data");
* ```
*/
module.exports = {
/**
* @function can-util/dom/data/data.getCid domData.getCid
* @signature `domData.getCid.call(el)`
* @return {Number} The value of the element's unique CID
*
* Return the previously set unique identifier for the dom node.
*/
getCid: domDataState.getCid,
/**
* @function can-util/dom/data/data.cid domData.cid
* @signature `domData.cid.call(el)`
* @return {Number} The value of the element's unique CID
*
* Set a unique identifier for the dom node, using the
* [can-util/dom/data/data.expando expando] property.
*
* @body
*
* If a unique cid value has not yet been set for this element, set it
* using the [can-util/dom/data/data.expando expando] property. Return the
* unique cid whether or not it is newly set
*/
cid: domDataState.cid,
/**
* @property can-util/dom/data/data.expando domData.expando
* @type {String}
*
* The key in which elements' cids are stored
*/
expando: domDataState.expando,
/**
* @function can-util/dom/data/data.clean domData.clean
* @param {String} prop the property to remove from the element's data
* @signature `domData.clean.call(el, key)`
*
* Remove data from an element previously added by [can-util/dom/data/data.set set]
*
* ```js
* var domData = require("can-util/dom/data/data");
*
* domData.clean.call(el, "metadata");
* ```
*/
clean: domDataState.clean,
/**
* @function can-util/dom/data/data.get domData.get
* @signature `domData.get.call(el, key)`
*
* Get data that was stored in a DOM Node using the specified `key`.
*
* ```js
* var domData = require("can-util/dom/data/data");
*
* var metadata = domData.get.call(el, "metadata");
* ```
*
* @param {String} key A string used as a unique key for storing data associated with this DOM Node.
*/
get: domDataState.get,
/**
* @function can-util/dom/data/data.set domData.set
* @signature `domData.set.call(el, name, value)`
*
* @param {String} name the key to store the value under
* @param {*} value the value to store under the key
*
* Set data to be associated with a DOM Node using the specified `key`. If data already exists for this key, it will be overwritten.
*
* ```js
* var domData = require("can-util/dom/data/data");
*
* domData.set.call(el, "metadata", {
* foo: "bar"
* });
* ```
*/
set: function(name, value) {
// set up handler to clean up domData when elements are removed
// handler only needs to be set up the first time set is called
if (elementSetCount === 0) {
mutationDocument.onAfterRemovedNodes(cleanupDomData);
}
// increment elementSetCount if set returns true
elementSetCount += domDataState.set.call(this, name, value) ? 1 : 0;
},
/**
* @function can-util/dom/data/data.delete domData.delete
* @signature `domData.delete.call(el)`
*
* Remove all data for an element previously added by [can-util/dom/data/data.set set]
*
* ```js
* var domData = require("can-util/dom/data/data");
*
* domData.delete.call(el);
* ```
*/
delete: deleteNode
};