aboutsummaryrefslogtreecommitdiff
path: root/player.js
blob: 8cf2151318573312c121167dd96668ea21aeaa9c (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
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
(function () {
    openDialog();
    let isPlaying = false;
    let currentTracks = [];
    let cursor = 0;
    let howerInstance;
    const retroWaveRu = "https://retrowave.ru";
    getMusic();

    let titleEl = document.getElementById("track-name");
    let coverArtEl = document.getElementsByClassName("music-player")[0];

    coverArtEl.addEventListener("click", () => {
        if (howerInstance) {
            isPlaying = !isPlaying;

            if (isPlaying) {
                howerInstance.play();
            } else {
                howerInstance.pause();
            }
        }
    });

    document.getElementById("initButton").addEventListener("click", () => {
        playMusic();
    });

    document.getElementById("history").addEventListener("click", () => {
        downloadHistory();
    });

    document.addEventListener("keyup", (event) => {
        const { key } = event;
        switch (key) {
            case "Space":
                break;
            case "ArrowLeft":
                break;
            case "ArrowRight":
                break;
        }
    });

    let touchstartX = 0;
    let touchendX = 0;
    const musixPlayerEl = document.getElementsByClassName("music-player")[0];
    if (musixPlayerEl) {
        musixPlayerEl.addEventListener("touchstart", (e) => {
            touchstartX = e.changedTouches[0].screenX;
        });

        musixPlayerEl.addEventListener("touchend", (e) => {
            touchendX = e.changedTouches[0].screenX;
            checkDirection();
        });
    }

    function checkDirection() {
        if (touchendX < touchstartX) {
            // Swipe Left
        }
        if (touchendX > touchstartX) {
            // Swipe Right
        }
    }

    function openDialog() {
        const dialog = document.getElementById("dialog");
        dialog.showModal();
    }

    function getMusic() {
        fetch(`https://retrowave.ru/api/v1/tracks?limit=5&cursor=${cursor}`)
            .then((res) => res.json())
            .then((res) => {
                const {
                    body: { tracks, cursor: currentCursor },
                } = res;
                cursor = currentCursor;
                currentTracks = tracks;
            });
    }

    function initPlayer() { }

    function playMusic() {
        const currentTrack = currentTracks[0];
        const singleTrack = currentTrack.streamUrl;
        const fullTrack = `${retroWaveRu}${singleTrack}`;
        howerInstance = new Howl({
            src: [fullTrack],
            html5: true,
            onend: function () {
                currentTracks.shift();
                if (currentTracks.length <= 3) {
                    getMusic();
                }
                playMusic();
            },
        });
        isPlaying = true;

        updateInfo(currentTrack);

        addToHistory(currentTrack);
        howerInstance.play();
    }

    function updateInfo(trackDetails) {
        titleEl.innerText = trackDetails.title;
        coverArtEl.style.backgroundImage = `url("${retroWaveRu}${trackDetails.artworkUrl}")`;
    }

    function getHistory() {
        let localHistoryStore = localStorage.getItem("retrowave-history") || "[]";
        let historyArray = JSON.parse(localHistoryStore);
        console.log(historyArray);
        return historyArray;
    }

    function addToHistory(trackDetails) {
        let historyArray = getHistory();
        historyArray.push(trackDetails);
        localStorage.setItem("retrowave-history", JSON.stringify(historyArray));
    }

    function downloadHistory() {
        const historyArray = getHistory();
        let element = document.createElement("a");
        element.setAttribute(
            "href",
            "data:application/json;charset=utf-8," +
            encodeURIComponent(JSON.stringify(historyArray))
        );
        element.setAttribute("download", "history.json");

        element.style.display = "none";
        document.body.appendChild(element);

        element.click();

        document.body.removeChild(element);
    }
})();