summaryrefslogtreecommitdiff
path: root/assets/js/blog.js
blob: d04ef1cd51827c4d8c8a91deec3203be797b2f75 (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
class Blog {
    constructor() {
        this.object = {
            body: document.getElementsByTagName('body')[0],
            section: document.getElementById('main'),
            blogButtonList: document.getElementsByClassName('bloglistbutton')
        }
    }

    Init() {this.#EventListener()}
    #EventListener() {
        for (let i = 0; i < this.object.blogButtonList.length; i++) {
            this.object.blogButtonList[i].addEventListener('click', () => {
                new Loading().Start()
                new Menu().HideIcon()
                this.#GetBlog(this.object.blogButtonList[i].value)
            })
        }
    }
    #GetUrl() {
        if (window.location.href.endsWith('/')) {
            return `${window.location.href}item`
        } else {
            return `${window.location.href}/item`
        }
    }
    async #GetBlog(x) {
        const object = {
            url: this.#GetUrl(),
            value: x
        }

        await fetch(object.url, {
            method: 'post',
            body: JSON.stringify(object),
            headers: new Headers({
                'Content-Type': 'application/json'
            })
        })
            .then(res => res.json())
            .then(output => {
                const object = output.html
                new Promise(res => {
                    const script = document.createElement('script')
                    script.src = `${window.location.origin}/js/blogitem.js`
                    this.object.body.appendChild(script)
                    script.onload = res
                }).then(output => {
                    new BlogItem().Init(object)
                })
            })
    }
}

new Blog().Init()