-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUtils.js
342 lines (321 loc) · 12.6 KB
/
Utils.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/**
* @author Alban Xhaferllari
* XScripts
* Used in Real Ajax Uploader
* Common utilities function
*/
define(['i18n', 'Constants'], function(_, Constants) {
var Utils = {
/**
* Run a function in background using WebWorkers
* @param job the function to run
* @returns {Worker}
*/
runInBackground: function (job) {
// Build a worker from an anonymous function body
var blobURL = URL.createObjectURL(new Blob(['(',
function (job) {
function startJob(passeddata) {
job(passeddata);
}
self.addEventListener("message", function (e) {
startJob(e.data);
}, !1);
}.toString(),
')(', job.toString(), ')'], {type: 'application/javascript'}));
var worker = new Worker(blobURL);
return worker;
},
/**
* Standard function to splice a file
* @param blob Blob or File
* @param start start index
* @param end end index
* @returns blob
*/
sliceFile: function (blob, start, end) {
try {
blob.slice(); // deprecated version will throw WRONG_ARGUMENTS_ERR exception
var slice = window.File.prototype.slice || window.File.prototype.webkitSlice || window.File.prototype.mozSlice;
return blob.slice(start, end);
} catch (e) {
return blob.slice(start, end - start);// deprecated old slice method
}
return null;
},
/**
* Short hand method for creating a dom element
* @param type {String} type of element
* @param cls {String} class
* @param style {Object} style to apply
* @returns {HTMLElement}
*/
doEl: function (type, cls, attrs, style) {
var el = document.createElement(type);
//add class to element
this.addCls(el, cls);
if (attrs) {
for (var prop in attrs) {
el.setAttribute(prop, attrs[prop]);
}
}
this.setStyle(el, style);
return el;
},
/**
* Set the style of dom element
* @param {HTMLElement} el
* @param {Object} style
* @returns {HTMLElement}
*/
setStyle: function (el, style) {
if (style) {
for (var prop in style) {
el.style[prop] = style[prop];
}
}
return el;
},
/**
* Add a class to a dom element
* @param {HTMLElement} el
* @param {String} cls
* @returns {*}
*/
addCls: function (el, cls) {
if (cls && "classList" in el) {
el.classList.add(cls);
} else if (cls) { //this is only for supporting old browser IE9 (?!)
el.className + ' ' + cls;
}
return el;
},
/**
* Short name helper function for finding an element by class name
* @param parent parent element
* @param cls class to find
* @param first if true returns only the first matched element
* @returns {NodeList} list of found elements
*/
byCls: function (cls, parent, first) {
if (!parent) parent = document;
var els = parent.getElementsByClassName(cls)
return parent.getElementsByClassName(cls);
},
/**
* Get the first element match the selector
* @param el dynamic element if defined is the parent container, if not define is the document
* @param selector if defined as second parameter is the selector,
* @returns {HTMLElement}
*/
getEl: function() {
if(arguments.length == 1) {
return document.querySelector(arguments[0]);
}
try{
if(arguments[0] && arguments[0].querySelector)
return arguments[0].querySelector(arguments[1]);
}catch(e){
return null;
}
return null;
},
/**
* Get all the elements matching the selector
* @param el dynamic element if defined is the parent container, if not define is the document
* @param selector if defined as second parameter is the selector
* @returns {NodeList}
*/
getEls: function() {
if(arguments.length == 1) {
return document.querySelectorAll(arguments[0]);
}
return arguments[0].querySelectorAll(arguments[1]);
},
/**
* Helper function for formatting a size from number to human readable
* @param {number} size
* @returns {string} the formatted string
*/
formatSize: function (size) {
var suffix = [_('b'), _('KB'), _('MB'), _('GB')];
var i = 0;
while (size >= 1024 && (i < (suffix.length - 1))) {
size /= 1024;
i++;
}
var intVal = Math.round(size);
var multiFactor = Math.pow(10, 2); //set a default precision decimal of 2
var floor = Math.round((size * multiFactor ) % multiFactor);
return intVal + '.' + floor + ' ' + suffix[i];
},
/**
* Fast extend deep function. Faster than jQuery and other methods
* @param target The target object to override the new properties
* @param source Source object with new values
* @param shallow internal parameter use for the recursive
* @returns Object {*}
*/
extend: function (target, source, shallow) {
var array = '[object Array]', object = '[object Object]', targetMeta, sourceMeta;
var setMeta = function (value) {
if (value === undefined) return 0;
if (typeof value !== 'object') return false;
var jClass = {}.toString.call(value);
if (jClass === array) return 1;
if (jClass === object) return 2;
};
for (var key in source) {
if (source.hasOwnProperty(key)) {
targetMeta = setMeta(target[key]), sourceMeta = setMeta(source[key]);
if (!shallow && sourceMeta && targetMeta && targetMeta === sourceMeta) {
target[key] = this.extend(target[key], source[key], true);
} else if (sourceMeta !== 0) {
target[key] = source[key];
}
} // ownProperties are always first
}
return target;
},
/**
* Show the current image in a lightBox preview
* Related to light-box.css
* @param image Image to show or file to preview
* @param title Name of the image or a title
* @param info Other info to show near the name
* @param cssClass Classes to add to the image container
* @return {DOM} returns the main dom object
*/
lightBoxPreview: function (image, title, info, cssClass) {
//create the box for the preview
var mainBox = this.doEl('div', 'ax-lightbox-target', {});
var imageBox = this.doEl('div', 'ax-image-box', {});
var imageEl = this.doEl('img', 'ax-image-prev', {src: image.src});
var infoBox = this.doEl('div', 'ax-info-box', {});
var fnBox = this.doEl('div', 'ax-name-box', {});
var sizeBox = this.doEl('div', 'ax-size-box', {});
var closeBox = this.doEl('a', 'ax-lightbox-close', {});
//append elements to dom
mainBox.appendChild(imageBox);
mainBox.appendChild(closeBox);
mainBox.appendChild(infoBox);
imageBox.appendChild(imageEl);
infoBox.appendChild(fnBox);
infoBox.appendChild(sizeBox);
document.body.appendChild(mainBox);
//set the information
fnBox.innerHTML = title;
sizeBox.innerHTML = info;
if(cssClass) {
imageEl.className +=' ' + cssClass;
}
//apply a small timeout to allow dom render
setTimeout(function () {
mainBox.classList.add('show');
}, 100);
//the close box function
//remove some references for memory performance use
var killBox = function (e) {
if(mainBox)
document.body.removeChild(mainBox);
mainBox = null;
imageBox = null;
infoBox = null;
fnBox = null;
sizeBox = null;
closeBox = null;
imageEl = null;
killBox = null;
window.removeEventListener('keyup', closeEsc);
};
var closeEsc = function (e) {
if (e.keyCode === 27) {
killBox();
}
};
closeBox.addEventListener('click', killBox);
window.addEventListener('keyup', closeEsc);
//mainBox.addEventListener('touchstart', killBox);
//mainBox.addEventListener('click', killBox);
return mainBox;
},
/**
* Test if the value is a integer number
* @param value value to test
* @returns {boolean}
*/
isInt: function (value) {
var x;
if (isNaN(value)) {
return false;
}
x = parseFloat(value);
return (x | 0) === x;
},
/**
* A small function for parsing file unit size of the form 10M, 2G, 100K
* @param {String} size the string size
* @returns {number}
*/
parseSize: function (size) {
if (!Utils.isInt(size)) {
var unit = size.slice(-1);
if (isNaN(unit)) {
size = parseInt(size.replace(unit, ''));//remove the last char
switch (unit) {
case 'P':
size = size * 1024;//1024 or 1000??
case 'T':
size = size * 1024;
case 'G':
size = size * 1024;
case 'M':
size = size * 1024;
case 'K':
size = size * 1024;
}
}
}
return size;
},
/**
* Simple Ajax Post function, will automatically parse JSON if the return string is a valid JSON
* @param {URL} url post URL
* @param {FormData} data parameters to post
* @param {function} cb function to call on request done
*/
ajaxPost: function (url, data, cb) {
var xhr = new XMLHttpRequest();
if (typeof cb == 'function') {
this.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var results = this.responseText;
try {
results = JSON.parse(this.responseText);
} catch (e) {
return false;
}
cb(results);
xhr = null;//remove reference
}
};
}
xhr.open('POST', url);
//xhr.setRequestHeader('Cache-Control', 'no-cache');
//xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');//header
//xhr.setRequestHeader('Content-Type', 'application/octet-stream');//generic stream header
xhr.send(data);
},
//better log function
log: function() {
if(Constants.ENV === 'DEV') {
var args = Array.prototype.slice.call(arguments, 0);
var date = new Date();
var now = ((date.getHours() < 10) ? "0" : "") + date.getHours() + ":" + ((date.getMinutes() < 10) ? "0" : "") + date.getMinutes() + ":" + ((date.getSeconds() < 10) ? "0" : "") + date.getSeconds();
args.unshift(now);
console.log.apply(console, args);
}
}
};
return Utils;
});