summaryrefslogtreecommitdiff
path: root/assets/js/page-init.js
blob: 207393a6441fb35d3eeb12725059c3a8b6af0dc2 (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
class Init {
    constructor() {
        this.object = {}
        this.settings = {
            scroll: 0
        }
        this.html = {}
    }

    GetSettings() {return this.settings}
    async Start() {
        this.fetch = `${window.location.pathname}?page=index`

        await fetch(this.fetch, {
            method: 'GET',
            headers: new Headers({
                'Content-Type': 'application/json'
            })
        })
            .then(res => res.json())
            .then(output => {
                this.output = output
                this.object.head = document.getElementsByTagName('head')[0]
                this.object.body = document.getElementsByTagName('body')[0]
                this.object.header = document.getElementsByTagName('header')[0]
                this.object.main = document.getElementsByTagName('main')[0]
                this.object.footer = document.getElementsByTagName('footer')[0]
                
                this.object.header.innerHTML += output.html.header
                this.object.main.innerHTML = output.html.main
                this.object.footer.innerHTML += output.html.footer
                this.object.nav = document.getElementById('header')
                this.object.nav.style.opacity = 0

                this.#LoadCSS(output.css).then(() => {
                    this.output.js.map(file => {
                        const script = document.createElement('script')
                        script.src = `${window.location.origin}/js/${file}`
                        this.object.body.appendChild(script)
                    })
                })
                this.#Scroll()
            })
    }

    #LoadElements() {
        setTimeout(() => {
            this.object.nav.style.opacity = 1
        }, 750)
    }

    async Startup(data) {
        const promise = new Promise((res, rej) => {
            const map = new Map()

            try {
                data.Stop()
                map.set('status', 'success')
                map.set('msg', 'success')
                this.#LoadElements()
                res(map)
            } catch (err) {
                map.set('status', 'error')
                map.set('msg', err)
                // ! Dev mode
                rej(map)
            }
        })
        return await promise
    }



    #Scroll() {window.addEventListener('scroll', () => this.settings.scroll = window.scrollY)}
    async #Error() {
        this.fetch = `${window.location.pathname}?page=index`;
        await fetch(this.fetch, {
            method: 'POST',
            headers: new Headers({
                'Content-Type': 'application/json'
            })
        })
            .then(res => console.log(res))
    }
    async #LoadCSS(files) {
        const promises = files.map(file => {
            return new Promise(res => {
                const link = document.createElement('link')
                link.rel = 'stylesheet'
                link.type = 'text/css'
                link.href = `${window.location.origin}/css/${file}`
                this.object.head.appendChild(link)
                link.onload = res
            })
        })
        return Promise.all(promises)
    }
}

const app = new Init()
app.Start()