summaryrefslogtreecommitdiff
path: root/assets/js/core/base.js
blob: 165e74acd6622944e02029a5a9b4179aa20cf77a (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
154
155
156
157
158
159
160
var KB = {
    components: {},
    utils: {},
    html: {},
    http: {},
    listeners: {
        clicks: {},
        changes: {},
        keys: [],
        internals: {}
    }
};

KB.on = function (eventType, callback) {
    if (! this.listeners.internals.hasOwnProperty(eventType)) {
        this.listeners.internals[eventType] = [];
    }

    this.listeners.internals[eventType].push(callback);
};

KB.trigger = function (eventType, eventData) {
    if (this.listeners.internals.hasOwnProperty(eventType)) {
        for (var i = 0; i < this.listeners.internals[eventType].length; i++) {
            this.listeners.internals[eventType][i](eventData);
        }
    }
};

KB.removeListener = function (eventType, callback) {
    if (this.listeners.internals.hasOwnProperty(eventType)) {
        for (var i = 0; i < this.listeners.internals[eventType].length; i++) {
            if (this.listeners.internals[eventType][i] === callback) {
                this.listeners.internals[eventType].splice(i, 1);
            }
        }
    }
};

KB.onClick = function (selector, callback, noPreventDefault) {
    this.listeners.clicks[selector] = {
        callback: callback,
        noPreventDefault: noPreventDefault === true
    };
};

KB.onChange = function (selector, callback) {
    this.listeners.changes[selector] = callback;
};

KB.onKey = function (combination, callback, ignoreInputField, ctrlKey, metaKey) {
    this.listeners.keys.push({
        'combination': combination,
        'callback': callback,
        'ignoreInputField': ignoreInputField || false,
        'ctrlKey': ctrlKey || false,
        'metaKey': metaKey || false
    });
};

KB.listen = function () {
    var self = this;
    var keysQueue = [];

    function onClick(e) {
        for (var selector in self.listeners.clicks) {
            if (self.listeners.clicks.hasOwnProperty(selector) && e.target.matches(selector)) {
                if (! self.listeners.clicks[selector].noPreventDefault) {
                    e.preventDefault();
                }

                self.listeners.clicks[selector].callback(e);
            }
        }
    }

    function onChange(e) {
        for (var selector in self.listeners.changes) {
            if (self.listeners.changes.hasOwnProperty(selector) && e.target.matches(selector)) {
                self.listeners.changes[selector](e.target);
            }
        }
    }

    function onKeyPressed(e) {
        var key = KB.utils.getKey(e);
        var isInputField = KB.utils.isInputField(e);

        if (! isInputField || ['Escape', 'Enter'].indexOf(key) !== -1) {
            keysQueue.push(key);
        }

        if (keysQueue.length > 0) {
            var reset = true;

            for (var i = 0; i < self.listeners.keys.length; i++) {
                var params = self.listeners.keys[i];
                var combination = params.combination;
                var sequence = combination.split('+');

                if (KB.utils.arraysIdentical(keysQueue, sequence) &&
                    e.ctrlKey === params.ctrlKey && e.metaKey === params.metaKey) {
                    if (isInputField && !params.ignoreInputField) {
                        keysQueue = [];
                        return;
                    }

                    e.preventDefault();
                    e.stopPropagation();
                    keysQueue = [];
                    params.callback(e);
                    break;
                } else if (KB.utils.arraysStartsWith(keysQueue, sequence) && keysQueue.length < sequence.length) {
                    reset = false;
                }
            }

            if (reset) {
                keysQueue = [];
            }
        }
    }

    document.addEventListener('click', onClick, false);
    document.addEventListener('change', onChange, false);
    window.addEventListener('keydown', onKeyPressed, false);
};

KB.component = function (name, object) {
    this.components[name] = object;
};

KB.getComponent = function (name, containerElement, options) {
    var object = this.components[name];
    return new object(containerElement, options);
};

KB.render = function () {
    for (var name in this.components) {
        var elementList = document.querySelectorAll('.js-' + name);

        for (var i = 0; i < elementList.length; i++) {
            if (this.components.hasOwnProperty(name)) {
                var options;

                if (elementList[i].dataset.params) {
                    options = JSON.parse(elementList[i].dataset.params);
                }

                var component = KB.getComponent(name, elementList[i], options);
                component.render();
                elementList[i].className = elementList[i].className + '-rendered';
            }
        }
    }
};

KB.interval = function (seconds, callback) {
    setInterval(callback, seconds * 1000);
};