summaryrefslogtreecommitdiff
path: root/assets/js/core/dom.js
blob: 330392856b7026a58283d55c05fdae0fa8dbf52b (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
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
KB.dom = function (tag) {

    function DomManipulation(tag) {
        var element = typeof tag === 'string' ? document.createElement(tag) : tag;

        this.attr = function (attribute, value) {
            if (value !== null) {
                element.setAttribute(attribute, value);
            }
            return this;
        };

        this.hide = function () {
            element.style.display = 'none';
            return this;
        };

        this.show = function () {
            element.style.display = 'block';
            return this;
        };

        this.toggle = function () {
            if (element.style.display === 'none') {
                this.show();
            } else{
                this.hide();
            }

            return this;
        };

        this.on = function (eventName, callback) {
            element.addEventListener(eventName, function (e) {
                e.preventDefault();
                callback(e.target);
            });

            return this;
        };

        this.click = function (callback) {
            return this.on('click', callback);
        };

        this.change = function (callback) {
            return this.on('change', callback);
        };

        this.add = function (node) {
            element.appendChild(node);
            return this;
        };

        this.replace = function (node) {
            element.parentNode.replaceChild(node, element);
            return this;
        };

        this.html = function (html) {
            element.innerHTML = html;
            return this;
        };

        this.text = function (text) {
            element.appendChild(document.createTextNode(text));
            return this;
        };

        this.addClass = function (className) {
            element.classList.add(className);
            return this;
        };

        this.removeClass = function (className) {
            element.classList.remove(className);
            return this;
        };

        this.toggleClass = function (className) {
            element.classList.toggle(className);
            return this;
        };

        this.hasClass = function (className) {
            return element.classList.contains(className);
        };

        this.disable = function () {
            element.disabled = true;
            return this;
        };

        this.enable = function () {
            element.disabled = false;
            return this;
        };

        this.parent = function (selector) {
            for (; element && element !== document; element = element.parentNode) {
                if (element.matches(selector)) {
                    return element;
                }
            }

            return null;
        };

        this.find = function (selector) {
            return element.querySelector(selector);
        };

        this.for = function (tag, list) {
            for (var i = 0; i < list.length; i++) {
                var dict = list[i];

                if (typeof dict !== 'object') {
                    element.appendChild(KB.dom(tag).text(dict).build());
                } else {
                    var node = KB.dom(tag);

                    for (var attribute in dict) {
                        if (dict.hasOwnProperty(attribute) && attribute in this && typeof this[attribute] === 'function') {
                            node[attribute](dict[attribute]);
                        } else {
                            node.attr(attribute, dict[attribute]);
                        }
                    }

                    element.appendChild(node.build());
                }
            }

            return this;
        };

        this.build = function () {
            return element;
        };
    }

    return new DomManipulation(tag);
};

KB.find = function (selector) {
    var element = document.querySelector(selector);

    if (element) {
        return KB.dom(element);
    }

    return null;
};