forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutility.js
More file actions
75 lines (68 loc) · 2.24 KB
/
utility.js
File metadata and controls
75 lines (68 loc) · 2.24 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
/**
* Write loading image to a container for ajax request
*
* @param container: a jQuery object
*/
function showLoadingImage(container) {
container.empty().append('<div class="barspinner dark"><div class="rect1"></div><div class="rect2"></div><div class="rect3"></div><div class="rect4"></div><div class="rect5"></div></div>');
}
/**
* Returns the highest z-index in the page.
* Accepts a jquery style selector to only check those elements,
* uses all container tags by default
* If no element found, returns null.
*
* @param selector: a jquery style selector for target elements
* @return int|null
*/
function get_highest_zindex(selector) {
if (!selector) {
selector = 'div,p,area,nav,section,header,canvas,aside,span';
}
const all = [];
const _store_zindex = function () {
if ($(this).is(':visible')) {
const z = $(this).css('z-index');
if (!isNaN(z)) {
all.push(z);
}
}
};
$(selector).each(_store_zindex);
return all.length ? Math.max(...all) : null;
}
function downloadCSV(csvContents, filename) {
filename = filename || 'data.csv';
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(new Blob([csvContents], { type: 'text/csv;charset=utf-8;' }), filename);
} else { // Other browsers
const csv = 'data:text/csv;charset=utf-8,' + csvContents;
const downloadLink = document.createElement('a');
downloadLink.href = encodeURI(csv);
downloadLink.download = filename;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
function template(string, content) {
return string.replace(/\[_(\d+)\]/g, function(s, index) {
return content[(+index) - 1];
});
}
function objectNotEmpty(obj) {
let isEmpty = true;
if (obj && obj instanceof Object) {
Object.keys(obj).forEach(function(key) {
if (obj.hasOwnProperty(key)) isEmpty = false;
});
}
return !isEmpty;
}
module.exports = {
showLoadingImage : showLoadingImage,
get_highest_zindex: get_highest_zindex,
downloadCSV : downloadCSV,
template : template,
objectNotEmpty : objectNotEmpty,
};