-
Notifications
You must be signed in to change notification settings - Fork 38
/
helper.js
120 lines (116 loc) · 3.21 KB
/
helper.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
module.exports = {
hasParentSelector(e, selector) {
var children = [];
while (e && "matches" in e && !e.matches(selector)) {
children.push(e);
e = e.parentNode;
}
children.push(e);
return (e && "matches" in e && e.matches(selector)) ? children : false;
},
getTransDur(e) {
return parseFloat(window.getComputedStyle(e, null).getPropertyValue("transition-duration").replace(/[a-z]/g, "").replace(/,/g, "."))
},
parseCSSProperty(value, type) {
const typeParsers = {
number(value) {
return parseFloat(value.replace(/[^0-9\.,]/g, ""));
}
};
return typeParsers[type](value);
},
objFillDefaults(obj, defaults) {
for (let key in defaults) {
if (defaults.hasOwnProperty(key)) {
if (!(key in obj)) {
obj[key] = defaults[key];
}
else if (typeof defaults[key] === "object" && defaults[key] != null) {
obj[key] = this.objFillDefaults(obj[key], defaults[key]);
}
}
}
return obj;
},
setSVGAttributes(e, attributes = {}) {
for (let name in attributes) {
if (attributes.hasOwnProperty(name)) {
e.setAttributeNS(null, name, attributes[name]);
}
}
},
arrayMerge(arrays) {
return [].concat.apply([], arrays);
},
argumentsSort(argumentsList = [], order = {}) {
const result = {};
for (let arg of argumentsList) {
let type = typeof arg;
let key = order.keyFromValue(type);
result[key] = arg;
delete order[key];
}
return result;
},
getTemplate(templateScript) {
return new Promise(function(resolve, reject) {
if (templateScript.__templateChache) {
resolve(templateScript.__templateChache);
}
else if (templateScript.src) {
const templateRequest = new XMLHttpRequest();
templateRequest.open("GET", templateScript.src, true);
templateRequest.addEventListener("load", function(event) {
templateScript.__templateChache = this.responseText;
resolve(this.responseText);
});
templateRequest.addEventListener("error", function(event) {
reject(event);
});
templateRequest.send();
}
else {
resolve(templateScript.innerHTML);
}
});
}
};
Array.merge = module.exports.arrayMerge;
Object.defineProperty(Array.prototype, "last", {
get() {
return this.lastFrom(0);
}
});
Object.defineProperty(Object.prototype, 'keyFromValue', {
enumerable: false,
value: function (value) {
for (var key in this) {
if (this.hasOwnProperty(key)) {
if (this[key] === value) {
return key;
}
}
}
}
});
Array.prototype.indexOfKey = function(value, key, start = 0) {
for (var i = start; i < this.length; i++) {
if (this[i][key] === value) {
return i;
}
}
return -1;
}
Array.prototype.lastFrom = function(pos = 0) {
return this[this.length - 1 - pos];
}
Math.roundDeep = function(number, deepness = 0) {
const multi = Math.pow(10, deepness);
return Math.round(number * multi) / multi;
};
Object.defineProperty(Object.prototype, 'fillDefaults', {
enumerable: false,
value: function (defaults) {
return module.exports.objFillDefaults(this, defaults);
}
});