This repository has been archived by the owner on Apr 4, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
186 lines (164 loc) · 2.94 KB
/
index.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* expose `data`
*/
module.exports = data;
/**
* global unique id
*/
data.uniq = 0;
/**
* global cache.
*/
data.cache = {};
/**
* api
*/
data.api = {
has: has,
del: del,
set: set,
get: get
};
/**
* Get data `api` for the provided `el`.
*
* example:
*
* var el = document.scripts[0];
*
* data(el)
* .set({ foo: bar })
* .set('hello', 'world')
*
* data(el).has('foo')
* // > true
* data(el).del('foo').has('foo');
* // > false
*
* data(el).get();
* // > { hello: 'world' }
* data(el).get('hello');
* // > world
* data(el).get('foo');
* // > undefined
*
* @param {HTMLElement} el
* @return {Object}
*/
function data (el) {
var id = el.__uniq || ++data.uniq, cache;
cache = (data.cache[id] = data.cache[id] || {});
el.__uniq = id;
data.api.el = el;
data.api.cache = cache;
return data.api;
}
/**
* Set `key` to `val` or provide
* an object that will be merged
* with the element `data`.
*
*
* example:
*
* set({ foo: 'bar' });
* set(console).get();
* // > { foo: 'bar', log: fn ... }
* set('foo', 'hello').get();
* // > { foo: 'hello', log: fn }
* set({}).get();
* // > {}
*
* @param {String|Object} name
* @param {mixed} val
* @return {self}
*/
function set (name, val) {
if ('string' != typeof name) {
for (var k in name) {
this.cache[k] = name[k];
}
} else {
this.cache[name] = val;
}
return this;
}
/**
* Get `value` where `key`.
*
* if `key` argument is omitted
* the method will return all `data`.
*
* note that the method will first
* attempt to get the `key` from the
* cache, if it's not there it will attempt
* to get `data-*` attr.
*
* example:
*
* get('foo');
* // > null
* get();
* // > {}
*
* @param {String} key
* @return {mixed}
*/
function get (k) {
var cache = this.cache, ret;
if (!k) return cache;
if (ret = cache[k]) return ret;
return cache[k] = attr(this.el, k);
}
/**
* Check whether or not `key` exists.
*
* example:
*
* has('foo');
* // > false
* set('foo', 'bar').has('foo');
* // > true
*
* @param {String} key
* @return {bool}
*/
function has (key) {
return !! this.get(key);
}
/**
* delete `key` from cache.
*
* if `key` is omitted the method
* will reset the element cache to
* a new `Object`.
*
* example:
*
* set('foo', 'bar');
* del('foo').get();
* // > {}
* set(console).del();
* // > {}
*
* @param {String} key
* @return {self}
*/
function del (key) {
if (key) {
delete this.cache[key];
} else {
data.cache[this.el.__uniq] =
this.cache = {};
}
return this;
}
/**
* get attribute helper.
*
* @param {HTMLElement} el
* @param {String} k
*/
function attr (el, k) {
return el.getAttribute('data-' + k);
}