forked from jbillimoria/JavaScriptButtons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastore.js
More file actions
84 lines (56 loc) · 1.66 KB
/
Copy pathdatastore.js
File metadata and controls
84 lines (56 loc) · 1.66 KB
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
'use strict';
var constants = require('../constants');
function DataStore() {
this.items = {};
}
DataStore.prototype.add = function addData(key, val) {
// Remap nice values
key = constants.PRETTY_PARAMS[key] || key;
// Wrap strings in the value object
if (typeof val === 'string') {
val = {
value: val
};
}
this.items[key] = {
label: val.label || '',
value: val.value || '',
editable: !!val.editable
};
};
DataStore.prototype.get = function getData(key) {
var item = this.items[key];
return item && item.value;
};
DataStore.prototype.remove = function removeData(key) {
delete this.items[key];
};
DataStore.prototype.pluck = function pluckData(key) {
var val = this.get(key);
this.remove(key);
return val;
};
DataStore.prototype.parse = function parseData(el) {
var attrs, attr, matches, key, label, value, editable, len, i;
if ((attrs = el.attributes)) {
for (i = 0, len = attrs.length; i < len; i++) {
attr = attrs[i];
if ((matches = attr.name.match(/^data-([a-z0-9_]+)(-editable)?/i))) {
key = matches[1];
editable = !!matches[2];
value = attr.value;
if (key.indexOf('option') === 0) {
value = value.split('=');
label = value[0];
value = value[1].split(',');
}
this.add(key, {
label: label,
value: value,
editable: editable
});
}
}
}
};
module.exports = DataStore;