~sirn/fanboi2

ref: 5b482cfc1bfb0bb1e89715cb57c7bac864391768 fanboi2/assets/app/javascripts/components/base.ts -rw-r--r-- 1.6 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
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
import {NotImplementedError} from '../utils/errors';


export interface IComponent {
    targetSelector: string;
}


export class DelegationComponent implements IComponent {
    public targetSelector: string;

    constructor() {
        setTimeout((): void => {
            this.bindGlobal();
        }, 1);
    }

    protected bindGlobal(): void {
        throw new NotImplementedError;
    }
}


export class SingletonComponent implements IComponent {
    public targetSelector: string;

    constructor($context?: Element | Document) {
        if (!$context) {
            $context = document;
        }

        setTimeout((): void => {
            if ($context) {
                let $element = $context.querySelector(this.targetSelector);
                if ($element) {
                    this.bindOne($element);
                }
            }
        }, 1);
    }

    protected bindOne(element: Element): void {
        throw new NotImplementedError;
    }
}


export class CollectionComponent implements IComponent {
    public targetSelector: string;

    constructor($context?: Element | Document) {
        if (!$context) {
            $context = document;
        }

        setTimeout((): void => {
            if ($context) {
                let $elements = $context.querySelectorAll(this.targetSelector);
                this.bindAll($elements);
            }
        }, 1);
    }

    protected bindAll($targets: NodeListOf<Element>): void {
        for (let i = 0, len = $targets.length; i < len; i++) {
            this.bindOne($targets[i]);
        }
    }

    protected bindOne($target: Element): void {
        throw new NotImplementedError;
    }
}