aboutsummaryrefslogtreecommitdiff
path: root/saved/index.php
blob: 5387392b093a3e46c86a60760319cef174180a0c (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
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
174
<?php $title = "Saved"; require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/header.php"; global $userName; global $allowNsfw;

$saved = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/includes/data/saved.json"), true)[$userName];

?>

<?php if (!isset($_GET["id"])): ?>
<div style="margin: 20px 50px 0;" class="main-app">
    <div class="list-group saved-list">
        <?php foreach ($saved as $id => $category): if ($id === "favorites"): $category["items"] = array_filter(array_values($category["items"]), function ($i) { return isset($i); }) ?>
            <a href="/saved/?id=<?= $id ?>" class="list-group-item list-group-item-action" style="display: grid; grid-template-columns: 5vw 1fr; grid-gap: 15px;">
                <div style="background-color: rgba(255, 255, 255, .1); height: 64px; border-radius: 5px;">
                    <div style="opacity: 0; transition: opacity 200ms; background-size: cover; background-position: center; height: 100%; width: 100%; border-radius: 5px;" class="category-img" data-category-img="<?= count($category["items"]) > 0 ? $category["items"][count($category["items"]) - 1] : "" ?>">
                        <div style="display:none;width: 100%;backdrop-filter: blur(5px);height: 100%;border-radius: 5px;"></div>
                    </div>
                </div>
                <div style="display: flex; align-items: center;">
                    <div>
                        <b><?= $category["name"] ?></b><br>
                        <?= count($category["items"]) ?> image<?= count($category["items"]) > 1 ? "s" : "" ?>
                    </div>
                </div>
            </a>
        <?php endif; endforeach; ?>
    </div>

    <div class="list-group saved-list" style="margin-top: 10px;">
        <?php foreach ($saved as $id => $category): if ($id !== "favorites"): ?>
            <a href="/saved/?id=<?= $id ?>" class="list-group-item list-group-item-action" style="display: grid; grid-template-columns: 5vw 1fr; grid-gap: 15px;">
                <div style="background-color: rgba(255, 255, 255, .1); height: 64px; border-radius: 5px;">
                    <div style="opacity: 0; transition: opacity 200ms; background-size: cover; background-position: center; height: 100%; width: 100%; border-radius: 5px;" class="category-img" data-category-img="<?= count($category["items"]) > 0 ? $category["items"][count($category["items"]) - 1] : "" ?>">
                        <div style="display:none;width: 100%;backdrop-filter: blur(5px);height: 100%;border-radius: 5px;"></div>
                    </div>
                </div>
                <div style="display: flex; align-items: center;">
                    <div>
                        <b><?= $category["name"] ?></b><br>
                        <?= count($category["items"]) ?> image<?= count($category["items"]) > 1 ? "s" : "" ?>
                    </div>
                </div>
            </a>
        <?php endif; endforeach; ?>
    </div>
</div>

<script>
    function showBackgroundImage(item, url, blur) {
        if (blur) {
            item.children[0].style.display = "";
        }

        return new Promise((res, rej) => {
            try {
                let tmp = new Image();
                tmp.onload = function() {
                    item.style.backgroundImage = 'url("' + url + '")';
                    item.style.opacity = "1";
                    res();
                }
                tmp.src = url;
            } catch (e) {
                rej(e);
            }
        })
    }

    (async () => {
        for (let item of document.getElementsByClassName("category-img")) {
            await sleep(50);

            let id = item.getAttribute("data-category-img");
            let data = JSON.parse(await (await window.fetch("https://derpibooru.org/api/v1/json/images/" + id)).text());
            let url = data['image']['representations'] ? (data['image']['representations']['small'] ?? data['image']['view_url']) : data['image']['view_url'];

            await showBackgroundImage(item, url, !data['image']['tags'].includes("safe"));
        }
    })();
</script>
<?php else: $category = $saved[$_GET["id"]];

if (!isset($category)) die();

?>
<div style="margin: 20px 50px 0;" class="main-app">
    <h2 <?= $_GET["id"] !== "favorites" ? "contenteditable" : "" ?> id="category-title"><?= $category["name"] ?></h2>

    <p <?php if (!$allowNsfw): ?>style="display:none;"<?php endif; ?>>
        <div class="form-check">
            <input class="form-check-input" type="checkbox" id="show-nsfw" name="show-nsfw" disabled onchange="toggleNSFW();">
            <label class="form-check-label" for="show-nsfw">Display NSFW images</label>
        </div>
    </p>

    <div style="display: grid; grid-template-columns: repeat(6, 1fr); grid-gap: 10px;" id="grid">
        <?php foreach ($category["items"] as $id) { ?>
            <a style="cursor: pointer;" onclick="viewImage(<?= $id ?>)">
                <div class="card" style="height: calc((100vw - 150px) / 6);">
                    <div class="card-body" style="padding: 0; height: 100%; width: 100%; display: flex; align-items: center; justify-content: center;">
                        <div class="display-image" data-display-image="<?= $id ?>" style="background-size: cover; background-position: center; opacity: 0; transition: opacity 200ms; height: 100%; width: 100%; border-radius: 0.375rem;">
                            <div style="display: none;height: 100%; width: 100%; backdrop-filter: blur(10px); border-radius: 0.375rem;"></div>
                        </div>
                    </div>
                </div>
            </a>
        <?php } ?>
    </div>

    <script>
        let lastTitle = document.getElementById("category-title").innerHTML;

        setInterval(async () => {
            if (document.getElementById("category-title").innerHTML !== lastTitle) {
                let out = await (await window.fetch("/categories/rename.php?id=" + encodeURIComponent(`<?= $_GET["id"] ?>`) + "&name=" + encodeURIComponent(document.getElementById("category-title").innerText))).text();
                if (out.trim() !== "ok") throw new Error(out);
                lastTitle = document.getElementById("category-title").innerHTML;
            }
        }, 1000);

        function showBackgroundImage(item, url, blur) {
            if (blur) {
                item.children[0].style.display = "";
                item.classList.add("display-image-nsfw");
                item.parentElement.parentElement.parentElement.style.pointerEvents = "none";
                document.getElementById("show-nsfw").disabled = false;
            }

            return new Promise((res, rej) => {
                try {
                    let tmp = new Image();
                    tmp.onload = function() {
                        item.style.backgroundImage = 'url("' + url + '")';
                        item.style.opacity = "1";
                        res();
                    }
                    tmp.src = url;
                } catch (e) {
                    rej(e);
                }
            })
        }

        function toggleNSFW() {
            if (document.getElementById("show-nsfw").disabled) return;
            let enable = document.getElementById("show-nsfw").checked;

            if (enable) {
                Array.from(document.getElementsByClassName("display-image-nsfw")).forEach((item) => {
                    item.children[0].style.display = "none";
                    item.parentElement.parentElement.parentElement.style.pointerEvents = "";
                });
            } else {
                Array.from(document.getElementsByClassName("display-image-nsfw")).forEach((item) => {
                    item.children[0].style.display = "";
                    item.parentElement.parentElement.parentElement.style.pointerEvents = "none";
                });
            }
        }

        (async () => {
            for (let item of document.getElementsByClassName("display-image")) {
                await sleep(50);

                let id = item.getAttribute("data-display-image");
                let data = JSON.parse(await (await window.fetch("https://derpibooru.org/api/v1/json/images/" + id)).text());
                let url = data['image']['representations'] ? (data['image']['representations']['small'] ?? data['image']['view_url']) : data['image']['view_url'];

                await showBackgroundImage(item, url, !data['image']['tags'].includes("safe"));
            }
        })();
    </script>
</div>
<?php endif; ?>

<?php require_once $_SERVER['DOCUMENT_ROOT'] . "/includes/footer.php"; ?>