-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.prefixData.js
136 lines (114 loc) · 4.05 KB
/
jquery.prefixData.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
/**
* Prefix Data
*
* Return the value at the prefixed data store for the first element in the set of matched elements. Returned value can
* be an object based on the attribute values and attributes name structure.
*
* @version 0.0.2
* @link https://github.com/cichy380/prefixData
* @author Marcin Dobroszek <marcin.dobroszek@gmail.pl>
* @license The MIT License (MIT)
*
* @todo: method need to be getter and setter (now only getter)
* @todo: chaining (as setter)
* to possible: $elem = $(‘.selector’).prefixData(‘mojprefix’, {„prop1”:”val1”, „prop2”:”val2”});
*/
;(function($) {
'use strict';
/**
* All available methods.
* @private
*/
var methods = {
/**
* Set value to specific property of object
* based on https://stackoverflow.com/a/44271732
*/
_setValue: function(object, path, value) {
var way = path.split('-'),
last = way.pop();
way.reduce(function (o, k) {
o[k] = o[k] || {};
return o[k];
}, object)[last] = value;
},
/**
* Convert "flat object" with 1-level deep to immersion object
* based on https://stackoverflow.com/a/44271732
*/
immersion: function(input) {
var output = $.extend({}, input); // working on copy of object
Object.keys(output).forEach(function (key) {
try {
output[key] = JSON.parse(output[key]); // convert JSON-string to object
}
catch(err) {}
if (key.indexOf('-') !== -1) {
methods._setValue(output, key, output[key]);
delete output[key];
}
});
return output;
},
};
/**
* .prefixData()
* @param {string} prefix - prefix text next to data-* attributes
* @return {object} data from prefixed data-* attributes
*/
$.fn.prefixData = function(prefix) {
var data,
mainData, // from data-prefixname=".."
deeperData, // from data-prefixname-propery=".."
attrs;
if ($.type(prefix) === 'undefined') {
$.error('Prefix param is required.');
}
if ($.type(prefix) !== 'string') {
$.error('Prefix param has wrong type. It has to be string format.');
}
// try to read data from storaged data
data = this.data(prefix + '--storage');
if (typeof data === 'undefined') {
// try to read data from single attribute
mainData = this.data(prefix);
// get all data-* attributes with our prefix
attrs = this.getAttributes('data-' + prefix + '-', true);
// convert list of attributes datas to object
deeperData = methods.immersion(attrs);
data = $.extend(true, {}, mainData, deeperData);
// save created object with data to "data-prefixname" attribute
// to speed up reading this data from this element next time
this.data(prefix + '--storage', data);
}
return data;
};
})(jQuery);
/**
* Get Attributes
*
* Return the list of HTML attributes for the first element in the set of matched elements.
*
* @link https://stackoverflow.com/a/5282801
*/
;(function($) {
'use strict';
$.fn.getAttributes = function(prefix, removePrefix) {
var attributes = {};
if (this.length) {
$.each(this[0].attributes, function(index, attr) {
var property = attr.name,
value = attr.value;
if (prefix) {
if (attr.name.indexOf(prefix) === 0) {
property = removePrefix ? property.substring(prefix.length) : property;
attributes[ property ] = value;
}
} else {
attributes[ property ] = value;
}
});
}
return attributes;
};
})(jQuery);