summaryrefslogtreecommitdiff
path: root/client/js/views/home_view.js
blob: c91363b21a4c51cc4a5f674bebacf0127bcabbeb (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
"use strict";

const router = require("../router.js");
const uri = require("../util/uri.js");
const misc = require("../util/misc.js");
const views = require("../util/views.js");
const PostContentControl = require("../controls/post_content_control.js");
const PostNotesOverlayControl = require("../controls/post_notes_overlay_control.js");
const TagAutoCompleteControl = require("../controls/tag_auto_complete_control.js");

const template = views.getTemplate("home");
const footerTemplate = views.getTemplate("home-footer");
const featuredPostTemplate = views.getTemplate("home-featured-post");

class HomeView {
    constructor(ctx) {
        this._hostNode = document.getElementById("content-holder");
        this._ctx = ctx;

        const sourceNode = template(ctx);
        views.replaceContent(this._hostNode, sourceNode);
        views.syncScrollPosition();

        if (this._formNode) {
            this._autoCompleteControl = new TagAutoCompleteControl(
                this._searchInputNode,
                {
                    confirm: (tag) =>
                        this._autoCompleteControl.replaceSelectedText(
                            misc.escapeSearchTerm(tag.names[0]),
                            true
                        ),
                }
            );
            this._formNode.addEventListener("submit", (e) =>
                this._evtFormSubmit(e)
            );
        }
    }

    showSuccess(text) {
        views.showSuccess(this._hostNode, text);
    }

    showError(text) {
        views.showError(this._hostNode, text);
    }

    setStats(stats) {
        views.replaceContent(
            this._footerContainerNode,
            footerTemplate(Object.assign({}, stats, this._ctx))
        );
    }

    setFeaturedPost(postInfo) {
        views.replaceContent(
            this._postInfoContainerNode,
            featuredPostTemplate(postInfo)
        );
        if (this._postContainerNode && postInfo.featuredPost) {
            this._postContentControl = new PostContentControl(
                this._postContainerNode,
                postInfo.featuredPost,
                () => {
                    return [window.innerWidth * 0.8, window.innerHeight * 0.7];
                },
                "fit-both"
            );

            this._postNotesOverlay = new PostNotesOverlayControl(
                this._postContainerNode.querySelector(".post-overlay"),
                postInfo.featuredPost
            );

            if (
                postInfo.featuredPost.type === "video" ||
                postInfo.featuredPost.type === "flash"
            ) {
                this._postContentControl.disableOverlay();
            }
        }
    }

    get _footerContainerNode() {
        return this._hostNode.querySelector(".footer-container");
    }

    get _postInfoContainerNode() {
        return this._hostNode.querySelector(".post-info-container");
    }

    get _postContainerNode() {
        return this._hostNode.querySelector(".post-container");
    }

    get _formNode() {
        return this._hostNode.querySelector("form");
    }

    get _searchInputNode() {
        return this._formNode.querySelector("input[name=search-text]");
    }

    _evtFormSubmit(e) {
        e.preventDefault();
        this._searchInputNode.blur();
        router.show(
            uri.formatClientLink("posts", {
                query: this._searchInputNode.value,
            })
        );
    }
}

module.exports = HomeView;