summaryrefslogtreecommitdiff
path: root/htdocs/js/common/03-dom.js
blob: d05bcd0ae8a56e532a8927c7c87c1149dc928af8 (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
117
//
// DOM helpers
//
function ge(id) {
    return document.getElementById(id)
}

function hasClass(el, name) {
    return el && el.nodeType === 1 && (" " + el.className + " ").replace(/[\t\r\n\f]/g, " ").indexOf(" " + name + " ") >= 0
}

function addClass(el, name) {
    if (!el) {
        return console.warn('addClass: el is', el)
    }
    if (!hasClass(el, name)) {
        el.className = (el.className ? el.className + ' ' : '') + name
    }
}

function removeClass(el, name) {
    if (!el) {
        return console.warn('removeClass: el is', el)
    }
    if (isArray(name)) {
        for (var i = 0; i < name.length; i++) {
            removeClass(el, name[i]);
        }
        return;
    }
    el.className = ((el.className || '').replace((new RegExp('(\\s|^)' + name + '(\\s|$)')), ' ')).trim()
}

function addEvent(el, type, f, useCapture) {
    if (!el) {
        return console.warn('addEvent: el is', el, stackTrace())
    }

    if (isArray(type)) {
        for (var i = 0; i < type.length; i++) {
            addEvent(el, type[i], f, useCapture);
        }
        return;
    }

    if (el.addEventListener) {
        el.addEventListener(type, f, useCapture || false);
        return true;
    } else if (el.attachEvent) {
        return el.attachEvent('on' + type, f);
    }

    return false;
}

function removeEvent(el, type, f, useCapture) {
    if (isArray(type)) {
        for (var i = 0; i < type.length; i++) {
            var t = type[i];
            removeEvent(el, type[i], f, useCapture);
        }
        return;
    }

    if (el.removeEventListener) {
        el.removeEventListener(type, f, useCapture || false);
    } else if (el.detachEvent) {
        return el.detachEvent('on' + type, f);
    }

    return false;
}

function cancelEvent(evt) {
    if (!evt) {
        return console.warn('cancelEvent: event is', evt)
    }

    if (evt.preventDefault) evt.preventDefault();
    if (evt.stopPropagation) evt.stopPropagation();

    evt.cancelBubble = true;
    evt.returnValue = false;

    return false;
}


//
// Cookies
//
function setCookie(name, value, days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
    }
    document.cookie = name + "=" + (value || "") + expires + "; domain=" + window.appConfig.cookieHost + "; path=/";
}

function unsetCookie(name) {
    document.cookie = name + '=; Max-Age=-99999999; domain=' + window.appConfig.cookieHost + "; path=/";
}

function getCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ')
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) === 0)
            return c.substring(nameEQ.length, c.length);
    }
    return null;
}