summaryrefslogtreecommitdiff
path: root/assets/js/core/utils.js
blob: ed38cf17d5f078838255dabaf929552a63b3020d (plain)
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
KB.utils.formatDuration = function (d) {
    if (d >= 86400) {
        return Math.round(d/86400) + "d";
    }
    else if (d >= 3600) {
        return Math.round(d/3600) + "h";
    }
    else if (d >= 60) {
        return Math.round(d/60) + "m";
    }

    return d + "s";
};

KB.utils.getSelectionPosition = function (element) {
    var selectionStart, selectionEnd;

    if (element.value.length < element.selectionStart) {
        selectionStart = element.value.length;
    } else {
        selectionStart = element.selectionStart;
    }

    if (element.selectionStart === element.selectionEnd) {
        selectionEnd = selectionStart;
    } else {
        selectionEnd = element.selectionEnd;
    }

    return {
        selectionStart: selectionStart,
        selectionEnd: selectionEnd
    };
};

KB.utils.arraysIdentical = function (a, b) {
    var i = a.length;

    if (i !== b.length) {
        return false;
    }

    while (i--) {
        if (a[i] !== b[i]) {
            return false;
        }
    }

    return true;
};

KB.utils.arraysStartsWith = function (array1, array2) {
    var length = Math.min(array1.length, array2.length);

    for (var i = 0; i < length; i++) {
        if (array1[i] !== array2[i]) {
            return false;
        }
    }

    return true;
};

KB.utils.isInputField = function (event) {
    var element = event.target;

    return !!(element.tagName === 'INPUT' ||
    element.tagName === 'SELECT' ||
    element.tagName === 'TEXTAREA' ||
    element.isContentEditable);
};

KB.utils.getKey = function (e) {
    var mapping = {
        'Esc': 'Escape',
        'Up': 'ArrowUp',
        'Down': 'ArrowDown',
        'Left': 'ArrowLeft',
        'Right': 'ArrowRight'
    };

    for (var key in mapping) {
        if (mapping.hasOwnProperty(key) && key === e.key) {
            return mapping[key];
        }
    }

    return e.key;
};

KB.utils.getViewportSize = function () {
    return {
        width: Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
        height: Math.max(document.documentElement.clientHeight, window.innerHeight || 0)
    };
};

KB.utils.isVisible = function() {
    var property = '';

    if (typeof document.hidden !== 'undefined') {
        property = 'visibilityState';
    } else if (typeof document.mozHidden !== 'undefined') {
        property = 'mozVisibilityState';
    } else if (typeof document.msHidden !== 'undefined') {
        property = 'msVisibilityState';
    } else if (typeof document.webkitHidden !== 'undefined') {
        property = 'webkitVisibilityState';
    }

    if (property !== '') {
        return document[property] === 'visible';
    }

    return true;
};