summaryrefslogtreecommitdiffstats
path: root/asset/js/notjQuery.js
blob: d24cd90a96db3ce40d89dad40c0f4e3a008855e9 (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
161
define(function () {

    "use strict";

    class notjQuery {
        /**
         * Create a new notjQuery object
         *
         * @param {Element} element
         */
        constructor(element) {
            if (! element) {
                throw new Error("Can't create a notjQuery object for `" + element + "`");
            }

            this.element = element;
        }

        /**
         * Add an event listener to the element
         *
         * @param {string} type
         * @param {string} selector
         * @param {function} handler
         * @param {object} context
         */
        on(type, selector, handler, context = null) {
            if (typeof selector === 'function') {
                context = handler;
                handler = selector;
                selector = null;
            }

            if (selector === null) {
                this.element.addEventListener(type, e => {
                    if (type === 'focusin' && e.target.receivesCustomFocus) {
                        // Ignore native focus event if a custom one follows
                        if (e instanceof FocusEvent) {
                            delete e.target.receivesCustomFocus;
                            e.stopImmediatePropagation();
                            return;
                        }
                    }

                    if (context === null) {
                        handler.apply(e.currentTarget, [e]);
                    } else {
                        handler.apply(context, [e]);
                    }
                });
            } else {
                this.element.addEventListener(type, e => {
                    if (type === 'focusin' && e.target.receivesCustomFocus) {
                        // Ignore native focus event if a custom one follows
                        if (e instanceof FocusEvent) {
                            delete e.target.receivesCustomFocus;
                            e.stopImmediatePropagation();
                            return;
                        }
                    }

                    Object.defineProperty(e, 'currentTarget', { value: e.currentTarget, writable: true });

                    let currentParent = e.currentTarget.parentNode;
                    for (let target = e.target; target && target !== currentParent; target = target.parentNode) {
                        if (target.matches(selector)) {
                            e.currentTarget = target;
                            if (context === null) {
                                handler.apply(target, [e]);
                            } else {
                                handler.apply(context, [e]);
                            }

                            break;
                        }
                    }
                }, false);
            }
        }

        /**
         * Trigger a custom event on the element, asynchronously
         *
         * The event will bubble and is not cancelable.
         *
         * @param {string} type
         * @param {{}} detail
         */
        trigger(type, detail = null) {
            setTimeout(() => {
                this.element.dispatchEvent(new CustomEvent(type, {
                    cancelable: true, // TODO: this should depend on whether it's a native or custom event
                    bubbles: true,
                    detail: detail
                }));
            }, 0);
        }

        /**
         * Focus the element
         *
         * Any other option than `preventScroll` is used as `event.detail`.
         *
         * @param {{}} options
         */
        focus(options = {}) {
            let { preventScroll = false, ...data } = options;

            const hasData = Object.keys(data).length > 0;
            if (hasData) {
                this.element.receivesCustomFocus = true;
            }

            // Put separately on the event loop because focus() forces layout.
            setTimeout(() => this.element.focus({ preventScroll: preventScroll }), 0);

            if (hasData) {
                this.trigger('focusin', data);
            }
        }

        /**
         * Render the element string as DOM Element
         *
         * @param {string} html
         * @return {Element}
         */
        static render(html) {
            if (typeof html !== 'string') {
                throw new Error("Can\'t render `" + html + "`");
            }

            let template = document.createElement('template');
            template.innerHTML = html;
            return template.content.firstChild;
        }
    }

    /**
     * Return a notjQuery object for the given element
     *
     * @param {Element} element
     * @return {notjQuery}
     */
    let factory = function (element) {
        return new notjQuery(element);
    }

    // Define the static methods on the factory
    for (let name of Object.getOwnPropertyNames(notjQuery)) {
        if (['length', 'prototype', 'name'].includes(name)) {
            continue;
        }

        Object.defineProperty(factory, name, {
            value: notjQuery[name]
        });
    }

    return factory;
});