apps/library/templates/admin.html 13.5 K raw
1
<!doctype html>
2
<html lang="en">
3
  <head>
4
    <meta charset="UTF-8" />
5
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
    <meta name="theme-color" content="#121113" />
7
    <link rel="stylesheet" href="/assets/darkmatter.css" />
8
    <link rel="stylesheet" href="/static/styles.css" />
9
    <link rel="apple-touch-icon" sizes="180x180" href="/static/apple-touch-icon.png" />
10
    <link rel="icon" type="image/png" sizes="32x32" href="/static/favicon-32x32.png" />
11
    <link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png" />
12
    <link rel="manifest" href="/static/site.webmanifest" />
13
    <title>Library | Admin</title>
14
  </head>
15
  <body>
16
    <div class="header">
17
      <a href="/" class="logo"><h1>LIBRARY</h1></a>
18
      <nav class="links">
19
        <a href="/admin/quotes">quotes</a>
20
        <a href="/admin/logout">logout</a>
21
      </nav>
22
    </div>
23
24
    {{if .Success}}<p class="success">{{.Success}}</p>{{end}}
25
    {{if .Error}}<p class="error">{{.Error}}</p>{{end}}
26
27
    <section class="admin-form">
28
      <h3>Category Labels</h3>
29
      <form method="POST" action="/admin/categories/labels" class="labels-form">
30
        <label>
31
          <span>Reading</span>
32
          <input type="text" name="reading" value="{{.Labels.Reading}}" required />
33
        </label>
34
        <label>
35
          <span>Read</span>
36
          <input type="text" name="read" value="{{.Labels.Read}}" required />
37
        </label>
38
        <label>
39
          <span>Want to Read</span>
40
          <input type="text" name="want" value="{{.Labels.Want}}" required />
41
        </label>
42
        <button type="submit">Save labels</button>
43
      </form>
44
    </section>
45
46
    <section class="admin-form">
47
      <h3>Search Books (Open Library)</h3>
48
      <div class="search-row">
49
        <input type="text" id="book-query" placeholder="title, author, isbn" />
50
        <button type="button" id="search-btn" onclick="searchBooks()">Search</button>
51
        <button type="button" id="scan-btn" onclick="openScanner()" hidden>Scan</button>
52
      </div>
53
      <div id="search-status" class="search-status" style="display:none;"></div>
54
      <div id="search-results" class="search-results"></div>
55
    </section>
56
57
    <section class="admin-form">
58
      <h3>Search Library</h3>
59
      <form method="GET" action="/admin" class="search-row">
60
        <input type="text" name="q" placeholder="title, author, isbn" value="{{.LibraryQuery}}" />
61
        <button type="submit">Search</button>
62
        {{if .LibrarySearched}}
63
        <a href="/admin" class="hint">clear</a>
64
        {{end}}
65
      </form>
66
      {{if .LibrarySearched}}
67
        {{if not .LibraryResults}}
68
        <p class="hint">No matches.</p>
69
        {{else}}
70
        <div class="books-list">
71
          {{$labels := .Labels}}
72
          {{range .LibraryResults}}
73
          <div class="book-card admin">
74
            {{if .CoverURL}}
75
            <img class="book-cover" src="{{.CoverURL}}" alt="" loading="lazy" />
76
            {{else}}
77
            <div class="book-cover placeholder"></div>
78
            {{end}}
79
            <div class="book-info">
80
            <h3 class="book-title"><a href="/admin/books/{{.ID}}">{{.Title}}</a></h3>
81
              <p class="book-authors">{{.Authors}}</p>
82
              {{if .ISBN}}<p class="book-meta">ISBN: {{.ISBN}}</p>{{end}}
83
              <form method="POST" action="/admin/books/{{.ID}}/status" class="inline">
84
                <select name="status" onchange="this.form.submit()">
85
                  <option value="read"{{if eq .Status "read"}} selected{{end}}>{{$labels.Read}}</option>
86
                  <option value="reading"{{if eq .Status "reading"}} selected{{end}}>{{$labels.Reading}}</option>
87
                  <option value="want"{{if eq .Status "want"}} selected{{end}}>{{$labels.Want}}</option>
88
                </select>
89
                <noscript><button type="submit">Save</button></noscript>
90
              </form>
91
              <form method="POST" action="/admin/books/{{.ID}}/notes" class="inline notes-form">
92
                <textarea name="notes" rows="5" placeholder="notes">{{.Notes}}</textarea>
93
                <button type="submit">Save notes</button>
94
              </form>
95
              <form method="POST" action="/admin/books/{{.ID}}/delete" class="inline">
96
                <button type="submit" class="danger">Delete</button>
97
              </form>
98
            </div>
99
          </div>
100
          {{end}}
101
        </div>
102
        {{end}}
103
      {{end}}
104
    </section>
105
106
    <div id="scan-modal" class="scan-modal" hidden>
107
      <div class="scan-inner">
108
        <video id="scan-video" playsinline muted></video>
109
        <p id="scan-status" class="scan-status">Point camera at barcode</p>
110
        <button type="button" onclick="closeScanner()">Cancel</button>
111
      </div>
112
    </div>
113
114
    <section class="admin-subs">
115
      <h3>Library ({{len .Books}})</h3>
116
      {{if not .Books}}
117
      <p class="hint">No books yet. Search above to add one.</p>
118
      {{else}}
119
      <div class="books-list">
120
        {{$labels := .Labels}}
121
        {{range .Books}}
122
        <div class="book-card admin">
123
          {{if .CoverURL}}
124
          <img class="book-cover" src="{{.CoverURL}}" alt="" loading="lazy" />
125
          {{else}}
126
          <div class="book-cover placeholder"></div>
127
          {{end}}
128
          <div class="book-info">
129
            <h3 class="book-title"><a href="/admin/books/{{.ID}}">{{.Title}}</a></h3>
130
            <p class="book-authors">{{.Authors}}</p>
131
            {{if .ISBN}}<p class="book-meta">ISBN: {{.ISBN}}</p>{{end}}
132
            <form method="POST" action="/admin/books/{{.ID}}/status" class="inline">
133
              <select name="status" onchange="this.form.submit()">
134
                <option value="read"{{if eq .Status "read"}} selected{{end}}>{{$labels.Read}}</option>
135
                <option value="reading"{{if eq .Status "reading"}} selected{{end}}>{{$labels.Reading}}</option>
136
                <option value="want"{{if eq .Status "want"}} selected{{end}}>{{$labels.Want}}</option>
137
              </select>
138
              <noscript><button type="submit">Save</button></noscript>
139
            </form>
140
            <form method="POST" action="/admin/books/{{.ID}}/notes" class="inline notes-form">
141
              <textarea name="notes" rows="5" placeholder="notes">{{.Notes}}</textarea>
142
              <button type="submit">Save notes</button>
143
            </form>
144
            <form method="POST" action="/admin/books/{{.ID}}/delete" class="inline">
145
              <button type="submit" class="danger">Delete</button>
146
            </form>
147
          </div>
148
        </div>
149
        {{end}}
150
      </div>
151
      {{end}}
152
    </section>
153
154
    <div id="category-labels-data"
155
         data-want="{{.Labels.Want}}"
156
         data-reading="{{.Labels.Reading}}"
157
         data-read="{{.Labels.Read}}"
158
         hidden></div>
159
160
    <script src="https://unpkg.com/@zxing/browser@0.1.5/umd/zxing-browser.min.js"></script>
161
    <script>
162
      (function() {
163
        const el = document.getElementById('category-labels-data');
164
        window.__categoryLabels = {
165
          want: el.dataset.want,
166
          reading: el.dataset.reading,
167
          read: el.dataset.read,
168
        };
169
      })();
170
171
      async function searchBooks() {
172
        const q = document.getElementById('book-query').value.trim();
173
        if (!q) return;
174
        const btn = document.getElementById('search-btn');
175
        const status = document.getElementById('search-status');
176
        const results = document.getElementById('search-results');
177
        btn.disabled = true;
178
        btn.textContent = 'Searching...';
179
        status.style.display = 'none';
180
        results.innerHTML = '';
181
        try {
182
          const resp = await fetch('/admin/search?q=' + encodeURIComponent(q));
183
          const data = await resp.json();
184
          if (!resp.ok) {
185
            status.textContent = data.error || 'Search failed';
186
            status.className = 'search-status error';
187
            status.style.display = 'block';
188
            return;
189
          }
190
          if (!data.length) {
191
            status.textContent = 'No results';
192
            status.className = 'search-status';
193
            status.style.display = 'block';
194
            return;
195
          }
196
          data.forEach(function(hit) {
197
            results.appendChild(renderHit(hit));
198
          });
199
        } catch (e) {
200
          status.textContent = 'Request failed';
201
          status.className = 'search-status error';
202
          status.style.display = 'block';
203
        } finally {
204
          btn.disabled = false;
205
          btn.textContent = 'Search';
206
        }
207
      }
208
209
      function renderHit(hit) {
210
        const card = document.createElement('form');
211
        card.method = 'POST';
212
        card.action = '/admin/add';
213
        card.className = 'book-card hit';
214
215
        const cover = document.createElement('div');
216
        if (hit.cover_url) {
217
          const img = document.createElement('img');
218
          img.src = hit.cover_url;
219
          img.className = 'book-cover';
220
          img.loading = 'lazy';
221
          card.appendChild(img);
222
        } else {
223
          cover.className = 'book-cover placeholder';
224
          card.appendChild(cover);
225
        }
226
227
        const info = document.createElement('div');
228
        info.className = 'book-info';
229
        info.innerHTML =
230
          '<h3 class="book-title"></h3>' +
231
          '<p class="book-authors"></p>' +
232
          (hit.isbn ? '<p class="book-meta">ISBN: ' + escapeHtml(hit.isbn) + '</p>' : '');
233
        info.querySelector('.book-title').textContent = hit.title;
234
        info.querySelector('.book-authors').textContent = hit.author_name;
235
236
        const hidden = function(name, value) {
237
          const el = document.createElement('input');
238
          el.type = 'hidden';
239
          el.name = name;
240
          el.value = value || '';
241
          return el;
242
        };
243
        info.appendChild(hidden('key', hit.key));
244
        info.appendChild(hidden('title', hit.title));
245
        info.appendChild(hidden('author_name', hit.author_name));
246
        info.appendChild(hidden('isbn', hit.isbn));
247
        info.appendChild(hidden('cover_url', hit.cover_url));
248
249
        const select = document.createElement('select');
250
        select.name = 'status';
251
        const labels = window.__categoryLabels || { want: 'Want to Read', reading: 'Reading', read: 'Read' };
252
        ['want', 'reading', 'read'].forEach(function(s) {
253
          const o = document.createElement('option');
254
          o.value = s;
255
          o.textContent = labels[s];
256
          select.appendChild(o);
257
        });
258
        info.appendChild(select);
259
260
        const btn = document.createElement('button');
261
        btn.type = 'submit';
262
        btn.textContent = 'Add';
263
        info.appendChild(btn);
264
265
        card.appendChild(info);
266
        return card;
267
      }
268
269
      function escapeHtml(s) {
270
        return String(s || '').replace(/[&<>"']/g, function(c) {
271
          return ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":"&#39;"})[c];
272
        });
273
      }
274
275
      let scanStream = null;
276
      let scanRaf = null;
277
      let zxingControls = null;
278
279
      const hasNativeBarcode = 'BarcodeDetector' in window;
280
      const hasZxing = typeof ZXingBrowser !== 'undefined';
281
282
      (function initScan() {
283
        if ((hasNativeBarcode || hasZxing) && navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
284
          document.getElementById('scan-btn').hidden = false;
285
        }
286
      })();
287
288
      async function openScanner() {
289
        const modal = document.getElementById('scan-modal');
290
        const video = document.getElementById('scan-video');
291
        const status = document.getElementById('scan-status');
292
        status.textContent = 'Point camera at barcode';
293
        modal.hidden = false;
294
295
        const onHit = (isbn) => {
296
          closeScanner();
297
          document.getElementById('book-query').value = isbn;
298
          searchBooks();
299
        };
300
301
        try {
302
          let detector = null;
303
          if (hasNativeBarcode) {
304
            try {
305
              detector = new BarcodeDetector({ formats: ['ean_13', 'ean_8', 'upc_a'] });
306
            } catch (_) {
307
              detector = null;
308
            }
309
          }
310
311
          if (detector) {
312
            scanStream = await navigator.mediaDevices.getUserMedia({
313
              video: { facingMode: 'environment' }
314
            });
315
            video.srcObject = scanStream;
316
            await video.play();
317
            const tick = async () => {
318
              if (!scanStream) return;
319
              try {
320
                const codes = await detector.detect(video);
321
                if (codes.length) return onHit(codes[0].rawValue);
322
              } catch (_) {}
323
              scanRaf = requestAnimationFrame(tick);
324
            };
325
            tick();
326
            return;
327
          }
328
329
          if (hasZxing) {
330
            const reader = new ZXingBrowser.BrowserMultiFormatReader();
331
            zxingControls = await reader.decodeFromVideoDevice(undefined, video, (result, err, controls) => {
332
              if (result) {
333
                controls.stop();
334
                zxingControls = null;
335
                onHit(result.getText());
336
              }
337
            });
338
            return;
339
          }
340
341
          status.textContent = 'Scanner not supported';
342
        } catch (e) {
343
          status.textContent = 'Camera unavailable';
344
        }
345
      }
346
347
      function closeScanner() {
348
        if (scanRaf) cancelAnimationFrame(scanRaf);
349
        scanRaf = null;
350
        if (zxingControls) {
351
          try { zxingControls.stop(); } catch (_) {}
352
          zxingControls = null;
353
        }
354
        if (scanStream) {
355
          scanStream.getTracks().forEach(function(t) { t.stop(); });
356
          scanStream = null;
357
        }
358
        document.getElementById('scan-modal').hidden = true;
359
      }
360
    </script>
361
    <script src="/pwa.js" defer></script>
362
  </body>
363
</html>