-
Notifications
You must be signed in to change notification settings - Fork 0
/
ng-helpers.js
243 lines (225 loc) · 8.71 KB
/
ng-helpers.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
var angularHelpers = angular.module('angular.helpers', []);
angularHelpers.service('$helpers', function() {
var _this = this;
this.mixin = function (obj, keypath, val) {
var key = keypath.match(/^[^.]+/)[0];
if (key == keypath) {
obj[key] = typeof val == 'function' ? val(obj[key]) : val;
} else {
if (!obj[key]) {
obj[key] = {};
}
return _this.mixin(obj[key], keypath.substr(key.length + 1), val);
}
return obj[key]
};
/**
* Ensure that object is an array
*
* @function
* @param scope
* @param path
* @param length
*/
this.ensureArray = function(scope, path, length) {
var result = _this.mixin(scope, path, function (val) {
if (!val && !(val instanceof Array)) {
return []
} else {
return val
}
});
while (result.length < length) {
result.push({})
}
return result;
};
this.isTouchDevice = 'ontouchstart' in window || navigator.maxTouchPoints;
/**
* Sort external array by rule
*
* @param external_array
* @param sort_keypath sort field
* @param rule 1 (asc, default), -1 (desc), [1,2,3,4,..] - array to compare
*/
this.sortExternalArray = function (external_array, sort_keypath, rule) {
if(typeof rule == 'object' && !rule._sortExternalArray_rule_id) {
Object.defineProperty(rule, '_sortExternalArray_rule_id', { enumerable: false, value: Math.random(), writable: true });
}
if(!rule) {
rule = [];
}
if(external_array) {
if(!external_array['_sortExternalArray' + sort_keypath + rule._sortExternalArray_rule_id]) {
Object.defineProperty(external_array, '_sortExternalArray' + sort_keypath + rule._sortExternalArray_rule_id,
{ enumerable: false, value: [], writable: true });
}
var linkedArray = external_array['_sortExternalArray' + sort_keypath + rule._sortExternalArray_rule_id];
[].concat(external_array).concat(linkedArray).map(function(it) {
if(linkedArray.indexOf(it) == -1) {
linkedArray.push(it);
}
if(external_array.indexOf(it) == -1) {
linkedArray.splice(linkedArray.indexOf(it), 1);
}
});
linkedArray.sort(function (a, b) {
var ap = _this.get_by_path(a, sort_keypath);
var bp = _this.get_by_path(b, sort_keypath);
if (!rule || rule == 1) {
if (ap > bp) return 1;
if (ap < bp) return -1;
return 0
} else if (rule == -1) {
if (ap > bp) return -1;
if (ap < bp) return 1;
return 0
} else if (rule instanceof Array) {
var ax = rule.indexOf(ap) + 1 || rule.length + 1;
var bx = rule.indexOf(bp) + 1 || rule.length + 1;
if (ax > bx) return 1;
if (ax < bx) return -1;
if (ax == bx) {
if(external_array.indexOf(a) > external_array.indexOf(b)) {
return 1;
}
if(external_array.indexOf(a) < external_array.indexOf(b)) {
return -1;
}
}
return 0;
} else {
throw new Error('Undefined external sort rule' + rule);
}
});
//rule.splice(0);
//linkedArray.map(function(it) {
// rule.push(get_by_path(it, sort_keypath));
//});
}
return linkedArray;
};
/**
* Get data by path
* @function
*
* @param obj
* @param keypath
*
* @returns {*}
*/
this.get_by_path = function(obj, keypath) {
var key = keypath.match(/^[^.]+/)[0];
if(key == keypath) {
return obj[key];
} else {
if(!obj[key]) { return obj[key] }
return _this.get_by_path(obj[key], keypath.substr(key.length + 1));
}
};
});
angularHelpers.directive('injectHelpers', function() {
return {
restrict: 'A',
link: function (scope, el) {
scope.console = console;
scope.helpers = {
focus: function(selector) {
$($(selector)[0]).focus();
setTimeout(function() {
$($(selector)[0]).focus();
}, 0);
},
focusAndSelect: function(selector, timeout) {
$($(selector)[0]).focus().select();
setTimeout(function() {
$($(selector)[0]).focus().select();
}, timeout || 0);
},
scrollIntoView: function(selector) {
setTimeout(function() {
$(selector).each(function() {
this.scrollIntoViewIfNeeded();
})
}, 0);
},
scrollEndH: function(selector) {
setTimeout(function() {
$(selector).each(function() {
this.scrollLeft = 10000000;
})
}, 0);
},
scrollPageLeft: function(selector) {
setTimeout(function() {
$(selector).each(function() {
this.scrollLeft -= this.clientWidth;
})
}, 0);
},
scrollPageRight: function(selector) {
setTimeout(function() {
$(selector).each(function() {
this.scrollLeft += this.clientWidth;
})
}, 0);
},
isScrollable: function(selector) {
var node = $(selector)[0];
// optimize getting sizing properties, it is important for performance
if(node && !node.hasOwnProperty('scrollable')) {
if(!node.bypass_apply) {
updateScrollable(node);
}
node.bypass_apply = false
} else {
if(node.scrollable_interval) {
clearTimeout(node.scrollable_interval);
delete node.scrollable_interval;
} else {
if(!node.bypass_apply) {
updateScrollable(node);
}
node.bypass_apply = false
}
node.scrollable_interval = setTimeout(function() {
if(!node.bypass_apply) {
updateScrollable(node);
}
node.bypass_apply = false
}, 250);
}
return node.scrollable;
},
isScrollableToLeft: function(selector) {
var node = $(selector)[0];
return node.scrollLeft > 0;
},
isScrollableToRight: function(selector) {
var node = $(selector)[0];
return node.clientWidth + node.scrollLeft < node.scrollWidth;
},
removeClass: function(classname, selector) {
$(selector).removeClass(classname);
},
timeoutAndEval: function(timeout, fn) {
setTimeout(function() {
scope.$eval(fn);
scope.$$phase || scope.$apply();
}, timeout);
},
isTouchDevice: 'ontouchstart' in window || navigator.maxTouchPoints /* IE10/11 & Surface */ ? 1 : 0
};
function updateScrollable(node) {
var oldscr = node.scrollable;
node.scrollable = node.scrollWidth != node.clientWidth || node.scrollHeight != node.clientHeight;
if(oldscr != node.scrollable) {
if(!scope.$$phase) {
node.bypass_apply = true;
scope.$apply();
}
}
}
}
}
});