~sirn/fanboi2

ref: 5b482cfc1bfb0bb1e89715cb57c7bac864391768 fanboi2/assets/app/javascripts/components/anchor_popover.ts -rw-r--r-- 6.3 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
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
162
163
164
165
166
167
168
169
170
171
172
173
import {VNode, create, diff, patch, h} from 'virtual-dom';

import {DelegationComponent} from './base';
import {TopicManager} from './topic_manager';
import {Board} from '../models/board';
import {Post} from '../models/post';
import {Topic} from '../models/topic';
import {BoardView} from '../views/board_view';
import {PopoverView} from '../views/popover_view';
import {PostCollectionView} from '../views/post_collection_view';
import {TopicView} from '../views/topic_view';
import {CancellableToken, CancelToken} from '../utils/cancellable';


export class AnchorPopover extends DelegationComponent {
    public targetSelector = '[data-anchor]';

    protected bindGlobal(): void {
        document.body.addEventListener('mouseover', (e: Event): void => {
            if (e.target instanceof Element) {
                if (e.target.matches(this.targetSelector)) {
                    e.preventDefault();
                    AnchorPopover.attach(e.target);
                }
            }
        });
    }

    private static attach($target: Element): void {
        let $parent = $target.closest('.js-popover');

        if (!$parent) {
            $parent = $target.closest('[data-topic]');
        }

        if ($parent) {
            let cancelToken = new CancelToken();
            let boardSlug = $target.getAttribute('data-anchor-board') || '';
            let postNumber = $target.getAttribute('data-anchor') || '';
            let anchorAttr = $target.getAttribute('data-anchor-topic');
            let topicId: number = 0;

            if (anchorAttr) {
                topicId = parseInt(anchorAttr, 10);
            }

            let _render = () => {
                if (topicId && postNumber) {
                    return Post.queryAll(topicId, postNumber, cancelToken).then(
                        (posts: Post[]): VNode => {
                            return new PostCollectionView(posts).render();
                        }
                    );
                } else if (topicId && !postNumber) {
                    return Topic.queryId(topicId, cancelToken).then(
                        (topic: Topic): VNode => {
                            return new TopicView(topic).render();
                        }
                    );
                } else {
                    return Board.querySlug(boardSlug, cancelToken).then(
                        (board: Board): VNode => {
                            return new BoardView(board).render();
                        }
                    );
                }
            }

            let _finalizeRequest = () => {
                cancelToken.cancel();
                $target.removeEventListener('mouseout', _finalizeRequest);
            }

            $target.addEventListener('mouseout', _finalizeRequest);

            _render().then((node) => {
                let $popover: Element;
                let popoverView = new PopoverView(node);
                let popoverNode = popoverView.render($target);
                let rebindTopic: boolean = false;
                let dismissTimer: number = 0;

                let _detach = (): void => {
                    if ($parent && $popover && $popover.parentNode) {
                        $parent.removeChild($popover);
                        $target.removeEventListener('mouseout', _detachOnTarget);
                        document.body.removeEventListener(
                            'mouseover',
                             _detachOnPopover
                        );
                    }
                }

                let _switchDetachMode = (e: Event): void => {
                    e.preventDefault();
                    clearTimeout(dismissTimer);

                    $popover.removeEventListener('mouseover', _switchDetachMode);
                    document.body.addEventListener(
                        'mouseover',
                        _detachOnPopover
                    );
                }

                let _detachOnPopover = (e: Event): void => {
                    e.preventDefault();

                    let _n = e.target;
                    while (_n && _n instanceof Node && _n != $popover) {
                        _n = _n.parentNode;
                    }

                    if (_n != $popover) {
                        _detach();
                    }
                }

                let _detachOnTarget = (e: Event): void => {
                    e.preventDefault();

                    // If $popover is already rendered then wait and
                    // see if user will move mouse into the quote; otherwise
                    // immediately detach.
                    if ($popover.parentNode) {
                        dismissTimer = setTimeout((): void => {
                            _detach();
                        }, 100);
                    } else {
                        _detach();
                    }
                }

                _finalizeRequest();

                // Rebind [data-topic] when anchor is pointing to post from
                // another topic so that quick reply and etc. binds to the
                // correct context.
                if (topicId && postNumber) {
                    let $localTopic = $target.closest('[data-topic]');
                    let localTopicId: number = 0;

                    if ($localTopic) {
                        let topicAttr = $localTopic.getAttribute('data-topic');
                        if (topicAttr) {
                            localTopicId = parseInt(topicAttr, 10);
                        }
                    }

                    if (topicId != localTopicId) {
                        rebindTopic = true;
                        popoverNode = popoverView.render($target, {
                            dataset: {
                                topic: topicId,
                            }
                        });
                    }
                }

                if ($parent) {
                    $popover = create(popoverNode);
                    $parent.appendChild($popover);

                    if (rebindTopic) {
                        new TopicManager($parent);
                    }

                    $target.addEventListener('mouseout', _detachOnTarget);
                    $popover.addEventListener('mouseover', _switchDetachMode);
                }
            });
        }
    }
}