~sirn/fanboi2

ref: 5b482cfc1bfb0bb1e89715cb57c7bac864391768 fanboi2/assets/app/javascripts/utils/elements.ts -rw-r--r-- 1.1 KiB
5b482cfcKridsada Thanabulpong 0.30.0 3 years ago
                                                                                
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
export const addClass = (
    element: Element,
    newClasses: string[]
): void => {
    let classNames = element.className.split(' ');

    for (let i = 0, len = newClasses.length; i < len; i++) {
        let newClass = newClasses[i];

        if (classNames.indexOf(newClass) == -1) {
            classNames.push(newClass);
        }
    }

    element.className = classNames.join(' ');
}


export const removeClass = (
    element: Element,
    removeClasses: string[]
): void => {
    let classNames = element.className.split(' ');

    for (let i = 0, len = removeClasses.length; i < len; i++) {
        let removeClass = removeClasses[i];
        let removeClassIdx = classNames.indexOf(removeClass);

        if (removeClassIdx != -1) {
            classNames.splice(removeClassIdx, 1);
        }
    }

    element.className = classNames.join(' ');
}


export const dispatchCustomEvent = (
    element: Element,
    eventName: string,
    opts: any = {},
): void => {
    element.dispatchEvent(new CustomEvent(eventName, {
        bubbles: true,
        detail: opts
    }));
}