-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.html
534 lines (478 loc) · 16.9 KB
/
index.html
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>100%晴天女孩</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0">
<link rel="icon" href="favicon.ico">
<link rel="manifest" href="manifest.json">
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
.out-box {
display: grid;
gap: 16px;
grid-template-columns: 12fr 10fr;
grid-template-areas: "head head" "left main";
margin: 0 auto;
padding: 16px;
width: 100%;
max-width: 980px;
}
.header-pic, .left-pic {
display: block;
width: 100%
}
.header-pic {
grid-area: head;
}
.left-pic {
grid-area: left;
}
.top-pic {
grid-area: head;
display: none;
}
.container {
grid-area: main;
display: flex;
flex-direction: column;
gap: 8px;
background-color: bisque;
border-radius: 10px;
padding: 16px 20px;
box-shadow: rgb(85, 85, 85) 0 0 5px;
}
.form-two-column {
display: flex;
gap: 8px;
width: 100%;
}
@media screen and (max-width: 750px) {
.out-box {
grid-template-columns: 1fr;
grid-template-areas: "head" "main";
}
.header-pic, .left-pic {
display: none;
}
.top-pic {
width: 100%;
display: block;
}
.form-two-column {
flex-direction: column;
}
}
.price {
font-size: 24px;
color: red;
font-weight: bold;
line-height: 1;
}
.intro {
font-size: 20px;
font-weight: bold;
}
.add-cart {
display: flex;
align-items: center;
width: 230px;
background: red;
border: none;
border-radius: 8px;
color: white;
outline: none;
cursor: pointer;
margin: 4px 0 8px;
}
.add-cart:hover, .add-cart:active {
background: #ff4444;
}
.add-cart-icon {
padding: 4px 8px 4px 6px;
font-size: 20px;
line-height: 1;
}
.add-cart-text {
flex: 1;
border-left: 1px solid white;
font-weight: bold;
padding: 4px;
font-size: 16px;
}
.text-box {
width: 100%;
line-height: 12px;
font-size: 12px;
padding: 6px;
border: chocolate 1px solid;
border-radius: 2px;
outline: none;
resize: vertical;
}
.text-box:focus, .text-box:active {
border: #ffa155 1px solid;
}
.form-main {
display: flex;
align-items: center;
flex-direction: column;
gap: 8px;
}
.form-item {
width: 100%;
line-height: 1;
}
.input-label {
font-size: 13px;
font-weight: bold;
margin: 0 0 2px 0;
padding: 0;
}
.input-label:after {
margin-left: 4px;
content: attr(data-need);
color: red;
font-size: 12px;
}
.avatar {
width: 32px;
height: 32px;
border-radius: 50%;
margin-right: 8px;
}
.card-list {
--list-column: 5;
display: grid;
grid-template-columns: repeat(var(--list-column), 1fr);
margin: 16px auto;
padding-inline-start: 0;
max-width: 1300px;
overflow: hidden;
padding: 1.5em;
--data-lang-text-place: '';
--data-lang-text-time: '';
--data-lang-text-reason: '';
}
@media screen and (min-width: 354px) {
.card-list {
--list-column: 1;
}
}
@media screen and (min-width: 647px) {
.card-list {
--list-column: 2;
}
}
@media screen and (min-width: 934px) {
.card-list {
--list-column: 3;
}
}
@media screen and (min-width: 1225px) {
.card-list {
--list-column: 4;
}
}
@media screen and (min-width: 1517px) {
.card-list {
--list-column: 5;
}
}
.card-list-item {
--item-top: 0;
--item-back: #ffc;
--item-border: #f0f0c0;
--item-rotate: -6deg;
position: relative;
text-decoration: none;
color: #000;
display: block;
height: 14em;
padding: 1em;
box-shadow: 5px 5px 7px #2121213d;
transition: transform .15s linear;
top: var(--item-top);
background: var(--item-back);
border: 1px solid var(--item-border);
transform: rotate(var(--item-rotate));
margin: 1em;
cursor: grab;
}
.card-list-item:active {
cursor: grabbing;
}
.card-list-item:nth-child(even) {
--item-rotate: 4deg;
--item-top: 5px;
--item-back: #cfc;
--item-border: #c0f0c0;
}
.card-list-item:nth-child(3n) {
--item-rotate: -3deg;
--item-top: -5px;
--item-back: #ccf;
--item-border: #c0c0f0;
}
.card-list-item:nth-child(4n) {
--item-rotate: 4deg;
--item-top: 5px;
--item-back: #fcf;
--item-border: #f0c0f0;
}
.card-list-item:nth-child(5n) {
--item-rotate: 5deg;
--item-top: -10px;
}
.card-list-item:nth-child(6n) {
--item-rotate: 4deg;
--item-top: 5px;
--item-back: #cff;
--item-border: #c0f0f0;
}
.card-list-item:nth-child(7n) {
--item-rotate: -5deg;
--item-top: -6px;
--item-back: #fcc;
--item-border: #f0c0c0;
}
.card-list-item:nth-child(8n) {
--item-rotate: -3deg;
--item-top: -10px;
}
.card-list-item:hover, .card-list-item:focus {
box-shadow: 7px 7px 10px #00000047;
transform: scale(1.04);
position: relative;
z-index: 5;
}
.card-list-item:active {
box-shadow: 2px 2px 10px #00000047;
}
.item-field {
color: #333;
font-weight: bold;
}
.item-field:before {
color: #666;
font-weight: lighter;
font-size: 14px;
}
.item-contact {
display: flex;
align-items: center;
margin-bottom: 16px;
}
.item-contact-text {
max-width: 180px;
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
.item-place:before {
content: var(--data-lang-text-place) ': ';
}
.item-time:before {
content: var(--data-lang-text-time) ': ';
}
.item-reason:before {
content: var(--data-lang-text-reason) ': ';
}
.item-datestamp {
text-align: right;
color: #999;
font-weight: lighter;
margin-top: 16px;
}
</style>
</head>
<body>
<div>
<div class="out-box">
<img class="top-pic" src="img/top.jpg" alt="TOP">
<img class="header-pic" src="img/header.jpg" alt="HEADER">
<img class="left-pic" src="img/left.jpg" alt="LEFT">
<div class="container">
<div class="price" data-lang="price"></div>
<div class="intro" data-lang="intro"></div>
<form class="form-main">
<button id="add_cart" type="button" class="add-cart">
<span class="add-cart-icon">
<svg viewBox="0 0 1024 1024" width="1em" height="1em" fill="currentColor">
<path d="M289.92 768c2.56 0 632.32 0 639.36 0 14.72 0 26.24-12.8 28.16-28.8 0 0 43.52-244.48 64-413.44C1027.2 279.04 1004.8 256 961.92 256L264.96 256 253.44 156.16C251.52 140.8 240.64 128 226.56 128 224.64 128 97.92 128 97.92 128c-17.92 0-32 14.08-32 32 0 17.92 14.08 32 32 32l99.84 0 64.64 547.2C263.68 755.2 275.84 768 289.92 768zM897.92 832c-35.2 0-64 28.8-64 64 0 35.2 28.8 64 64 64 35.2 0 64-28.8 64-64C961.92 860.8 933.12 832 897.92 832zM321.92 832c-35.2 0-64 28.8-64 64 0 35.2 28.8 64 64 64 35.2 0 64-28.8 64-64C385.92 860.8 357.12 832 321.92 832z"></path>
</svg>
</span>
<span class="add-cart-text" data-lang="add_cart"></span>
</button>
<div class="form-two-column">
<label class="form-item">
<span class="input-label" data-lang="time"></span>
<input type="text" class="text-box" data-name="date">
</label>
<label class="form-item">
<span class="input-label" data-lang="place"></span>
<input type="text" class="text-box" data-name="place">
</label>
</div>
<label class="form-item">
<span class="input-label" data-lang="contact" data-need></span>
<input type="text" class="text-box" required data-name="contact">
</label>
<label class="form-item">
<span class="input-label" data-lang="reason" data-need></span>
<textarea rows="3" cols="20" class="text-box" data-lang-attrs="placeholder"
data-lang-placeholder="prompt" required data-name="reason"></textarea>
</label>
</form>
</div>
</div>
<div id="post_list" class="card-list" data-lang-styles="place,time,reason"></div>
<bunny-dialog id="dialog" data-lang-attrs="ok,cancel" data-lang-ok="ok" data-lang-cancel="cancel">
<p id="tips"></p>
</bunny-dialog>
</div>
<script src="dialog.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gun/gun.js"></script>
<script>
'use strict';
(function () {
const APP_NAME = 'OtenkiGirl';
const LANGUAGES = ['zh-cn', 'zh-tw', 'ja', 'en'];
const DEFAULT_LANG = 'zh-cn';
const DEFAULT_LANGUAGE = {
"title": "100%晴天女孩",
"price": "¥220(含税)",
"intro": "100%晴女为被雨困扰的你带来阳光\uD83D\uDE06无论什么地点…",
"add_cart": "加入购物车",
"time": "希望的时间",
"select": "请选择吧",
"place": "希望放晴的地点",
"need": "※必填",
"contact": "联系方式",
"reason": "想要晴天的理由",
"prompt": "请尽量写得详细一些",
"empty_contact": "请填写联系方式,联系方式不用写真的哦~",
"empty_reason": "请填写理由",
"confirm": "确认提交嘛?联系方式不用写真的哦~",
"ok": "确定",
"cancel": "取消",
"network_error": "网络错误,请检查网络连接",
};
let post_list = document.getElementById('post_list');
let dialog = document.getElementById('dialog');
let dialogTip = document.getElementById('tips');
let curLang = DEFAULT_LANGUAGE;
let gun = Gun('https://otenkigirl.herokuapp.com/gun');
let wishes = null;
if ('serviceWorker' in navigator) {
const isRoot = location.href.indexOf(`/${APP_NAME}/`) < 0;
let sw = isRoot ? '/sw.js' : `/${APP_NAME}/sw.js`;
let scope = isRoot ? '/' : `/${APP_NAME}/`;
navigator.serviceWorker.register(sw, {scope}).then(() => {
console.log('ServiceWorker Init');
}).catch(() => {
console.log('ServiceWorker Failed')
})
}
const $q = (s) => document.querySelectorAll(s);
const escapeHtml = (unsafe) => (unsafe || '').replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
const replaceLanguage = (lang, t) => {
curLang = t;
document.querySelector('html').lang = lang;
document.title = t.title;
[...$q('[data-lang]')].forEach((el) => el.innerHTML = t[el.dataset.lang]);
[...$q('[data-need]')].forEach((el) => el.setAttribute("data-need", t.need));
[...$q('[data-lang-attrs]')].forEach((el) => {
const names = el.dataset.langAttrs || "";
if (!names) return;
(names).split(",").forEach((n) => el.setAttribute(n, t[el.getAttribute(`data-lang-${n}`)]));
});
[...$q('[data-lang-styles]')].forEach((el) => {
const names = el.dataset.langStyles || "";
if (!names) return;
(names).split(",").forEach((n) => el.style.setProperty(`--data-lang-text-${n}`, `'${t[n]}'`));
});
}
const appLang = () => {
const full = (localStorage['og_language'] || navigator.language || DEFAULT_LANG).toLowerCase().substring(0, 5);
const short = full.substring(0, 2);
if (LANGUAGES.includes(short)) return short;
else if (LANGUAGES.includes(full)) return full;
else return DEFAULT_LANG;
}
const initLanguage = () => new Promise(resolve => {
const lang = appLang();
if (lang === DEFAULT_LANG) {
replaceLanguage(lang, DEFAULT_LANGUAGE);
resolve();
} else {
fetch(`lang/${lang}.json`).then(r => r.json()).then(r => {
replaceLanguage(lang, r);
resolve();
}).catch(() => {
replaceLanguage(DEFAULT_LANG, DEFAULT_LANGUAGE);
resolve();
})
}
});
const ESCAPE_FIELDS = ["contact", "place", "reason", "date"];
const SHOW_FIELDS = ["place", "time", "reason", "datestamp"];
const createItem = (it, id) => {
let d = document.getElementById(id);
if (d) return;
d = document.createElement('div');
d.id = id;
d.classList.add("card-list-item");
ESCAPE_FIELDS.forEach(f => it[f] = escapeHtml(it[f]));
it.time = it.date;
it.datestamp = (new Date(it.timestamp)).toLocaleDateString();
let html = `<div class="item-contact"><img alt="avatar" class="avatar" src="img/avatar.jpg"/><div class="item-contact-text">${it.contact}</div></div>`;
SHOW_FIELDS.forEach(f => html += `<div class="item-field item-${f}">${it[f] || '--'}</div>`);
d.innerHTML = html;
post_list.prepend(d);
};
function loadContent() {
wishes = gun.get('wishes');
wishes.map().on((wish, id) => createItem(wish, id));
}
const submitCheck = (value) => {
if (!wishes) return curLang.network_error;
if (!value.contact) return curLang.empty_contact;
if (!value.reason) return curLang.empty_reason;
}
const val = () => {
const v = {};
[...$q('[data-name]')].forEach(x => v[x.dataset.name] = x.value);
return v;
}
const reset = () => [...$q('[data-name]')].forEach(x => x.value = '');
document.getElementById('add_cart').addEventListener('click', _ => {
const post = val();
let tip = submitCheck(post);
if (tip) {
dialogTip.innerHTML = tip;
dialog.cancel = "";
dialog.visible = true;
return;
}
post.timestamp = new Date().getTime();
dialog.cancel = curLang.cancel;
dialogTip.innerText = curLang.confirm;
dialog.show(() => {
reset();
wishes.get(Gun.text.random()).put(post);
});
});
window.addEventListener("storage", (e) => {
if (e.key === "og_language") initLanguage().then(() => loadContent());
});
initLanguage().then(() => loadContent());
})();
</script>
</body>
</html>