~sirn/fanboi2

ref: a68873a108d20a6a49a1b6caf374bfb2cf6a94fe fanboi2/assets/app/javascripts/components/topic_manager.ts -rw-r--r-- 1.3 KiB
a68873a1Kridsada Thanabulpong Coding style cleanups and setup pre-commit hooks (#42) 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
import { VNode, create, diff, patch } from "virtual-dom";
import { CollectionComponent } from "./base";
import { ITopicEventConstructor, ITopicEventHandler } from "./topic/base";
import { TopicState } from "./topic/topic_state";
import { TopicLoadPosts } from "./topic/topic_load_posts";
import { TopicNewPost } from "./topic/topic_new_post";

export class TopicManager extends CollectionComponent {
    public targetSelector = "[data-topic]";

    protected bindOne($target: Element): void {
        let topicIdAttr = $target.getAttribute("data-topic");

        if (topicIdAttr) {
            let topicId = parseInt(topicIdAttr, 10);

            this.bindEvent($target, topicId, "updateState", TopicState);
            this.bindEvent($target, topicId, "readState", TopicState);
            this.bindEvent($target, topicId, "loadPosts", TopicLoadPosts);
            this.bindEvent($target, topicId, "newPost", TopicNewPost);
        }
    }

    private bindEvent(
        $target: Element,
        topicId: number,
        eventName: string,
        eventHandler: ITopicEventConstructor,
    ): void {
        let handler = new eventHandler(topicId, $target);

        $target.addEventListener(eventName, (e: CustomEvent) => {
            e.stopPropagation();
            handler.bind(e);
        });
    }
}