-
Notifications
You must be signed in to change notification settings - Fork 0
/
diary.html
1404 lines (1213 loc) · 37.6 KB
/
diary.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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>Diary</title>
<style>
body {
margin: 0;
text-align: center;
width: 100vw;
min-height: 100vh;
overflow-x: hidden;
background-size: cover;
background-repeat: no-repeat;
}
#w_today {
max-width: 700px;
width: calc(100vw - 40px);
margin-top: 20px;
margin: auto;
padding-bottom: 10px;
}
#moodsel {
margin-top: 80px !important;
backdrop-filter: blur(10px);
width: max-content;
margin: auto;
padding: 2px 4px 2px 4px;
background-color: #6663;
border-radius: 6px;
transition: .3s;
}
#moodsel>button {
width: 40px;
height: 40px;
font-size: 20px;
border-radius: 5px;
border: 0;
background-color: #fff5;
margin: 2px;
box-shadow: none;
transition: .3s;
}
#moodsel>button.focus {
box-shadow: 0 2px 2px 0 rgba(26, 24, 29, .16), 0 1px 4px 0 rgba(26, 24, 29, .12);
}
.loca {
width: max-content;
max-width: min(700px, 80vw);
text-overflow: ellipsis;
margin: auto;
padding: 5px 10px 5px 10px;
background-color: #7773;
border-radius: 5px;
margin-top: 10px;
white-space: nowrap;
overflow: hidden;
opacity: 75%;
}
.loca::before {
content: ' ';
font-family: 'SMDL';
color: orange;
}
.mood {
width: max-content;
margin: auto;
padding: 5px 10px 5px 10px;
background-color: #7772;
border-radius: 5px;
margin-top: 10px;
opacity: 75%;
}
.card {
margin: 20px;
text-align: left;
border: solid #cfac74 2px;
border-radius: 10px;
padding: 10px;
background-color: #cfac7455;
min-height: 5em;
opacity: 75%;
}
.floatingBtn {
width: 50px;
height: 50px;
line-height: 50px;
text-align: center;
border-radius: 100%;
font-size: 50px;
background-color: #f0f9fb;
position: fixed;
bottom: 50px;
right: 50px;
user-select: none;
transition: .3s;
}
.floatingBtn:hover {
box-shadow: 3px 1px 5px 0 rgba(26, 24, 29, .12), 0 4px 4px 0 rgba(26, 24, 29, .16);
}
#datetab {
user-select: none;
height: 52px;
width: 100vw;
position: fixed;
top: 0;
left: 0;
padding: 5px;
display: block;
backdrop-filter: blur(10px);
background-color: #cfac7444;
z-index: 1000;
}
#datetab>button {
width: 50px;
height: 50px;
font-size: 30px;
display: inline-block;
border: 0;
border-radius: 5px;
background-color: #fff5;
box-shadow: none;
}
#datetab>button:not(.smdl) {
position: relative;
top: -5px;
}
button:hover {
cursor: pointer;
}
#dateweek {
display: inline-block;
height: 50px;
}
#dateweek>button {
margin: 0;
position: relative;
top: -30px;
width: 50px;
height: 60px;
margin-left: 5px;
margin-right: 5px;
border: 0;
background-color: transparent;
transition: .1s;
border: 0;
border-radius: 5px;
box-shadow: none;
}
#dateweek>button:hover {
box-shadow: 0 2px 2px 0 rgba(26, 24, 29, .16), 0 1px 4px 0 rgba(26, 24, 29, .12);
}
#dateweek>button.focus {
background-color: #fff5;
box-shadow: 0 2px 2px 0 rgba(26, 24, 29, .16), 0 1px 4px 0 rgba(26, 24, 29, .12);
}
#dateweek>button>h2 {
margin: 0;
}
span.mon {
color: grey;
}
#datetab>h2 {
margin: 0;
position: fixed;
left: 10px;
top: calc(25px - 0.5em);
}
@font-face {
font-family: 'SMDL';
src: url('SMDL.woff');
}
.smdl {
font-family: 'SMDL';
}
.timestamp {
font-size: 0.8em;
position: relative;
color: grey;
}
.timestamp.card_ts {
bottom: -20px;
}
.timestamp.mood_ts {
bottom: -10px;
}
.timestamp.loca_ts {
bottom: -10px;
}
.card+.timestamp.loca_ts {
display: none;
}
.card+.timestamp+.loca {
width: calc(min(640px, calc(100% - 40px - 2ch)) - 4px) !important;
position: relative;
top: -20px;
border-top-right-radius: 0;
border-top-left-radius: 0;
border: solid #cfac74 2px;
border-top: 0;
max-width: 100vw !important;
}
</style>
<style>
.card:has(+.timestamp+.loca) {
border-bottom: none;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
</style>
<script>
function $(q) {
return document.querySelector(q);
}
function $$(q) {
return document.querySelectorAll(q);
}
var HttpClient = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
anHttpRequest.onreadystatechange = function() {
if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
aCallback(anHttpRequest.responseText);
}
anHttpRequest.open("GET", aUrl, true);
anHttpRequest.send(null);
}
}
</script>
</head>
<body>
<script>
var sec_key = "\x01";
function sec_encode(content) {
var secret = "";
for (var i = 0; i < content.length; i++) {
secret += String.fromCharCode(content[i].charCodeAt() ^ sec_key[i % sec_key.length].charCodeAt());
}
return secret;
}
var sec_decode = sec_encode;
</script>
<script>
var diary;
var date = new Date();
var dateselector_d = new Date();
const TODAY = {
year: date.getFullYear(),
mon: date.getMonth() + 1,
date: date.getDate()
};
date = TODAY;
const TIMELINE_CARD = 1;
const TIMELINE_MOOD = 2;
const TIMELINE_LOCA = 3;
function load(arg1, flag_mode) {
w_today.innerHTML = "";
diary = JSON.parse(localStorage.getItem('diary') || "{}");
var diary_year = diary[date.year] = diary[date.year] || {};
var diary_mon = diary_year[date.mon] = diary_year[date.mon] || {};
var today = diary_mon[date.date] || sec_encode(`\0${TIMELINE_CARD}\0`);
today.split('\0').forEach(content => {
let item = sec_decode(content).split('\0');
switch (Number(item[1])) {
case TIMELINE_CARD:
create_card(item[2], item[0]);
break;
case TIMELINE_MOOD:
create_mood(item[2], item[0]);
break;
case TIMELINE_LOCA:
create_location(item[2], item[0]);
break;
default:
console.log("Unknown class:", item[1]);
// create_card(item[0]);
console.log(item);
window.globalerror = item[1]
}
})
if (equ(TODAY, date)) {
/* mood */
if (!$('.mood'))
moodsel.style.opacity = "100%";
else {
if (new Date() - new Date($('.mood').dataset.time) < 10 * 60 * 1000)
moodsel.style.opacity = "40%";
else
moodsel.style.opacity = "100%";
}
/* card */
// create_card();
} else {
moodsel.style.opacity = "0%";
}
if (arg1 != 1) {
var today_date = new Date(`${date.year}-${date.mon}-${date.date}`);
switch_week(today_date);
}
dateselector_d = new Date(`${date.year}-${date.mon}-${date.date}`);
// check LOCA
$$('.loca').forEach(ele => {
if (ele.innerText.indexOf("Still") != -1) {
// have 'still' in innertext
if (ele.previousElementSibling.previousElementSibling == undefined ||
!ele.previousElementSibling.previousElementSibling.classList.contains('card')) {
// prev-prev item is not a card, the this is a invalid LOCA.
ele.previousElementSibling.remove(); // remove the time.
ele.remove(); // remove it
}
}
})
process_flag(flag_mode || 0);
}
function randomChoice(all_choices) {
return all_choices[Math.floor(Math.random() * all_choices.length)];
}
function process_flag(mode) {
/*
- mode=0: load mode (exec each time the shown date changed)
- mode=1: wload mode (exec only when the app boot.)
*/
var flags = diary.flags;
flag_box.innerHTML = "";
// <blur-content> : blur the cards which isn't focused.
if (flags['blur-content']) {
flag_box.innerHTML +=
"<style>.card:not(:focus,:hover) {text-shadow: 2px 2px 10px #000000;color: transparent;}</style>";
}
// <random-review> : randomly show a diary to user.
if (flags['random-review'] && mode == 1) {
if (confirm('Would like a random review?')) {
var all_choices = [];
var today;
for (year in diary)
if (year != 'flags')
for (month in diary[year])
for (day in diary[year][month])
if (diary[year][month][day]) {
var lst = diary[year][month][day].split('\0');
var latest_mood = 'Become unknown😑';
for (var i = 0; i < lst.length; i++) {
item = sec_decode(lst[i]);
switch (Number(item.split('\0')[1])) {
case TIMELINE_CARD:
all_choices.push([item, latest_mood]);
break;
case TIMELINE_MOOD:
latest_mood = item.split('\0')[2];
break;
}
}
}
var choice = randomChoice(all_choices);
var cont = choice[0].split('\0')[2];
var latest_mood = choice[1].split(' ')[1];
var time = new Date(choice[0]);
time = `${time.getFullYear()}.${time.getMonth()+1}.${time.getDate()}`;
init_window_rr(time, cont, latest_mood.toLowerCase());
}
}
}
const days = ['Sun', 'Mon', 'Tue', 'Wen', 'Tur', 'Fri', 'Sat'];
const mons = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', "Aug", 'Sep', 'Oct', 'Nov', 'Dec'];
const mons_full = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October',
'November', 'December'
];
function switch_week(today_date) {
var day_of_the_week = today_date.getDay();
var hldate = new Date(`${date.year}-${date.mon}-${date.date}`);
dateweek.innerHTML = "";
ds_year.innerText = today_date.getFullYear();
for (var i = 0; i < 7; i++) {
var day = days[i];
var btn = document.createElement('button');
let this_date = new Date(today_date);
this_date.setDate(today_date.getDate() - (day_of_the_week - i))
btn.innerHTML =
`<span class=mon>${mons[this_date.getMonth()]}.</span><h2>${this_date.getDate()}</h2><span>${day}</span>`;
if (equ2(this_date, hldate)) {
btn.classList = 'focus';
} else {
btn.onclick = function() {
switch_date(this_date.getFullYear(), this_date.getMonth() + 1, this_date.getDate());
dateselector_d = new Date(this_date);
}
}
dateweek.appendChild(btn);
}
}
function ds_month(cnt) {
dateselector_d.setMonth(dateselector_d.getMonth() + cnt);
open_window_sel();
}
function ds_week(cnt) {
dateselector_d.setDate(dateselector_d.getDate() + 7 * cnt);
switch_week(dateselector_d);
}
function ds_day(cnt) {
dateselector_d.setDate(dateselector_d.getDate() + cnt);
switch_date(dateselector_d.getFullYear(), dateselector_d.getMonth() + 1, dateselector_d.getDate())
}
function gen_timeline(filter) {
var timeline = [];
for (var i = w_today.children.length - 1; i >= 0; i--) {
x = w_today.children[i];
if (x.dataset.timeline == 'true') {
var item = [x.dataset.time, x.dataset.class, x.innerText.trim().replaceAll(/\n{3,10}/g, '\n\n')];
var match_filter = true;
if (filter) {
filter.forEach(f => {
if (item[f[0]].indexOf(f[1]) == -1)
match_filter = false;
})
}
if (match_filter)
timeline.push(item);
}
}
timeline.sort((a, b) => {
return new Date(a[0]) > new Date(b[0]) ? 1 : -1;
})
return timeline;
}
function store() {
if (equ(TODAY, date)) {
var today = [];
var timeline = gen_timeline();
timeline.forEach(item => {
if (item[2].trim() != '') {
today.push(sec_encode(item.join('\0')));
}
})
if (today.length == 0) {
delete diary[date.year][date.mon][date.date];
} else {
today = today.join('\0');
diary[date.year][date.mon][date.date] = today;
}
localStorage.diary = JSON.stringify(diary);
}
}
function extract_text(card) {
return card.innerText;
}
function equ(a, b) {
return a.year == b.year && a.mon == b.mon && a.date == b.date;
}
function equ2(a, b) {
return a.getFullYear() == b.getFullYear() && a.getMonth() == b.getMonth() && a.getDate() == b.getDate();
}
function timestamp(time, mode) {
var time = new Date(time);
var hour = String(time.getHours());
var minute = String(time.getMinutes());
return `<span class='timestamp ${mode}_ts'>${hour.padStart(2,'0')}:${minute.padStart(2,'0')}</span>`;
}
function create_card(text, force) {
text = text || '';
if (force || !$('.card') || $('.card').innerText.trim() != '') {
var item = [force || new Date().toString(), TIMELINE_CARD];
if ($('.loca') && !force) {
var coords = $('.loca').innerText.match(/\([0-9].+?\)/g)[0].replace('(', '').replace(')', '').split(',');
var addr = $('.loca').innerText.match(/ at .+?\([0-9]/g)[0];
process_location({
coord: {
latitude: Number(coords[0]), //纬度
longitude: Number(coords[1]) //经度
},
address: addr.substring(4, addr.length - 2)
}, true);
}
w_today.innerHTML =
`${timestamp(item[0], 'card')}<div data-timeline=true data-time='${item[0]}' data-class='${item[1]}' class='card' ${equ(TODAY, date)?'contenteditable':''}></div> ${w_today.innerHTML}`;
$('.card').innerText = text;
store();
}
}
function switch_date(new_year, new_mon, new_date) {
store();
date = {
year: new_year,
mon: new_mon,
date: new_date
}
load();
}
function update_location() {
navigator.geolocation.getCurrentPosition(process_location, console.log, {
provider: "system"
});
return;
}
document.addEventListener('plusready', function() {
update_location();
})
window.onload = function() {
if (navigator.userAgent.indexOf('Html5Plus/1.0') != -1) {
// mobile app
window.has_plus = true;
document.addEventListener('keyboardchange', update_store);
document.addEventListener('resume', update_location);
} else {
update_location();
}
if (window.innerWidth < 900) {
ds_year.style.display = "none";
}
if (window.innerWidth < 650) {
ds_week = ds_day;
document.body.innerHTML += "<style>#dateweek>button:not(.focus){display: none;}</style>"
}
if (window.innerWidth < 470) {
window_sel_h.style.zoom = 0.7;
window_sel_t.style.zoom = 0.7;
}
if (window.innerHeight < 750 * Number(window_sel_h.style.zoom || 1)) {
window_sel_h.style.zoom = window.innerHeight / 850;
window_sel_t.style.zoom = window.innerHeight / 850;
}
load(undefined, 1);
}
function update_store(e) {
if (window_import.style.display == 'none') {
// not importing.
store();
}
}
document.body.onclick = document.body.onkeyup = update_store;
function plus_io_writeFlie(path, data) {
return new Promise(resolve => { //这里封装了个是一个promise异步请求
// plus.io.requestFileSystem是请求本地文件系统对象
plus.io.requestFileSystem(
plus.io.PRIVATE_DOC, // 文件系统中的根目录下的DOCUMENTS目录
fs => {
// 创建或打开文件, fs.root是根目录操作对象,直接fs表示当前操作对象
fs.root.getFile(path, {
create: true // 文件不存在则创建
}, fileEntry => {
// 文件在手机中的路径
// console.log(fileEntry.fullPath)
fileEntry.createWriter(writer => {
// 写入文件成功完成的回调函数
writer.onwrite = e => {
console.log("写入本地文件成功");
resolve("写入本地文件")
};
// 写入数据
writer.write(data);
})
}, e => {
console.log("getFile failed: " + e.message);
});
},
e => {
console.log(e.message);
}
);
})
}
function plus_io_readFile(path) {
return new Promise(resolve => { //文件读写是一个异步请求 用promise包起来方便使用时的async+await
plus.io.requestFileSystem(
plus.io.PRIVATE_DOC,
fs => {
fs.root.getFile(path, {
create: false
}, fileEntry => {
fileEntry.file((file) => {
console.log("文件大小:" + file.size + '-- 文件名:' + file.name);
//创建读取文件对象
let fileReader = new plus.io.FileReader();
//以文本格式读取文件数据内容
fileReader.readAsText(file, 'utf-8');
//文件读取操作完成时的回调函数
fileReader.onloadend = (evt) => {
resolve(evt.target.result)
}
});
}, e => {
console.log("getFile failed: " + e.message);
});
},
e => {
console.log(e.message);
}
);
})
}
async function cmd() {
var cmd = prompt("Command:");
if (!cmd)
return false;
cmd = cmd.trim().toLowerCase().split(' ');
switch (cmd[0]) {
case 'export':
document.write(`<textarea>${localStorage.diary}</textarea>`);
break;
case 'import':
init_window_import();
window_import.style.display = "block";
break;
case 'debug':
var sc = document.createElement('script');
sc.src = "https://cdn.bootcss.com/vConsole/3.3.4/vconsole.min.js";
$("#hidden-div").appendChild(sc);
setTimeout(() => {
var vConsole = new VConsole();
}, 500);
break;
case 'backup':
await plus_io_writeFlie("diary.txt", localStorage.diary);
alert("Backup Complete!");
break;
case 'restore':
if (confirm('Confirm restore? You will lost all your current data.')) {
var res = await plus_io_readFile("diary.txt");
localStorage.diary = res;
window.location.reload();
}
break;
case 'print':
w_today.innerHTML = print_last_month();
flag_box.innerHTML = "";
print();
break;
case 'flag':
var option = cmd[1];
var flag = cmd[2];
if (flag && option) {
diary['flags'] = diary.flags || {};
diary.flags[flag] = (option == 'enable');
store();
alert(`Flag Set to '${diary.flags[flag]}'.`);
} else {
alert("Invalid Parameters.");
}
break;
}
}
function select_mood(mood, emoji) {
if (change_mood(mood, emoji) == 'del') {
mood = 'all need to be unfocused.';
}
$$("#moodsel>button").forEach(child => {
if (child.title == mood) {
child.classList.add("focus");
} else {
child.classList.remove("focus");
}
})
}
function change_mood(mood, emoji) {
var emoji_regex = /(\ud83c[\udf00-\udfff])|(\ud83d[\udc00-\ude4f\ude80-\udeff])|[\u2600-\u2B55]/g;
var timeline = gen_timeline([
[1, TIMELINE_MOOD]
]);
var last_mood = timeline[timeline.length - 1] || [0, TIMELINE_MOOD, ''];
var lastlmood = timeline[timeline.length - 2] || [0, TIMELINE_MOOD, ''];
var last_mood_time = new Date(last_mood[0]);
var last_mood_same = (last_mood[2].match(emoji) == emoji);
if (new Date() - last_mood_time > 10 * 60 * 1000) { // 10 min
// new mood.
var content = "";
if (last_mood_same) {
content = "Still " + mood + emoji;
} else {
content = "Become " + mood + emoji;
}
var item = [new Date().toString(), TIMELINE_MOOD];
create_mood(content, item[0]);
} else {
// change mood.
if (last_mood_same) {
// press the same button, revoke the prev mood.
$('.mood_ts').remove();
$('.mood').remove();
moodsel.style.opacity = "100%";
return 'del';
}
var content = "";
last_mood = lastlmood;
last_mood_same = (last_mood[2].match(emoji_regex) == emoji);
if (last_mood_same) {
content = "Still " + mood + emoji;
} else {
content = "Become " + mood + emoji;
}
var item = [new Date().toString(), TIMELINE_MOOD];
$('.mood').dataset.time = item[0];
$('.mood').innerHTML = content;
}
}
function create_mood(content, time) {
moodsel.style.opacity = "40%";
w_today.innerHTML =
`${timestamp(time, 'mood')}<div data-timeline=true data-time='${time || new Date().toString()}' data-class='${TIMELINE_MOOD}' class='mood'>${content}</div> ${w_today.innerHTML}`;
store();
}
function amap_js_api(long, lati, cback) {
// amap js api
const AMAP_KEY = "0000731dfe185f57e0e23223b14db3de";
var http = new HttpClient();
function six(inti) {
var s = String(inti);
return s.match(/[0-9]{1,3}\.[0-9]{1,6}/g)[0]
}
var url =
`https://restapi.amap.com/v3/geocode/regeo?key=${AMAP_KEY}&location=${six(long)},${six(lati)}&extensions=base`;
http.get(url, res => {
res = JSON.parse(res);
window.regeo_name = "";
if (res.infocode != "10000") {
cback();
console.log(res)
return console.log("Amap reGeo js api error. Error Code:", res.infocode);
}
window.regeo_name = res.regeocode.formatted_address;
cback();
window.regeo_name = null;
});
}
function process_location(position, force_same) {
if (!equ(TODAY, date))
return 'not this day!';
var c = position.coords || position.coord;
if (c.speed)
return 'moving';
var acc = c.accuracy || 150; // 精度
let lati = c.latitude; // 纬度
let long = c.longitude; // 经度
console.log("定位精度:", acc);
if (acc > 400 ||
String(lati).length - String(lati).indexOf('.') < 4 || String(long).length - String(long).indexOf('.') < 4) {
return 'unprecise';
}
var min_delta = 750 + acc;
var name = position.address || ""; // 位置名称
if (window.has_plus && position.address) {
var addr = position.address;
name = addr.street || addr.poiName || addr.district || addr;
}
if (!$('.loca')) {
if (!name) {
amap_js_api(long, lati, function() {
create_location(`Arrive at ${window.regeo_name}(${lati}, ${long})`);
})
} else {
create_location(`Arrive at ${name}(${lati}, ${long})`);
}
return;
}
var last_pos = $('.loca').innerText.match(/\([0-9].+?[0-9]\)/g)[0].replace('(', '').replace(')', '').split(',');
var last_lati = Number(last_pos[0]);
var last_long = Number(last_pos[1]);
var delta_lati = (last_lati - lati) * 4e5; // meter
var delta_long = (last_long - long) * 4e5; // meter
var same_pos = (Math.pow(delta_lati, 2) + Math.pow(delta_long, 2) < Math.pow(min_delta, 2));
if (same_pos) {
console.log($('.loca').dataset.time);
if (force_same && new Date() - new Date($('.loca').dataset.time) > 60 * 60 * 1000) {
// 1 hour
if (!name) {
amap_js_api(long, lati, function() {
create_location(`Still at ${window.regeo_name}(${lati}, ${long})`);
})
} else {
create_location(`Still at ${name}(${lati}, ${long})`);
}
}
} else {
if (new Date() - new Date($('.loca').dataset.time) > 20 * 60 * 1000) {
// 20 min
if (!name) {
amap_js_api(long, lati, function() {
create_location(`Arrive at ${window.regeo_name}(${lati}, ${long})`);
})
} else {
create_location(`Arrive at ${name}(${lati}, ${long})`);
}
}
}
}
function create_location(content, time) {
time = time || new Date().toString();
if (equ(TODAY, date) || time) {
// forbid new location
w_today.innerHTML =
`${timestamp(time, 'loca')} <div data-timeline=true data-time='${time}' data-class='${TIMELINE_LOCA}' class='loca'>${content}</div>${w_today.innerHTML}`;
store();
}
}
function print_a_day(d) {
moodsel.display = "none";
switch_date(d.year, d.mon, d.date);
w_today.innerHTML = "";
diary = JSON.parse(localStorage.getItem('diary') || "{}");
var diary_year = diary[date.year] = diary[date.year] || {};
var diary_mon = diary_year[date.mon] = diary_year[date.mon] || {};
var today = diary_mon[date.date] || sec_encode(`\0${TIMELINE_CARD}\0`);
if (today == sec_encode(`\0${TIMELINE_CARD}\0`))
return false;
today.split('\0').reverse().forEach(content => {
let item = sec_decode(content).split('\0');
switch (Number(item[1])) {
case TIMELINE_CARD:
create_card(item[2], item[0]);
break;
case TIMELINE_MOOD:
create_mood(item[2], item[0]);
break;
case TIMELINE_LOCA:
create_location(item[2], item[0]);
break;
default:
console.log("Unknown class:", item[1]);
// create_card(item[0]);
console.log(item);
window.globalerror = item[1]
}
})
var day = new Date(`${date.year}-${date.mon}-${date.date}`).getDay();
w_today.innerHTML =
`<h1>${mons[d.mon-1]} ${d.date}</h1><br><h3>${d.year}, ${days[day]}</h3>` + w_today.innerHTML;
// check LOCA
$$('.loca').forEach(ele => {
if (ele.innerText.indexOf("Still") != -1) {
// have 'still' in innertext
if (ele.previousElementSibling.previousElementSibling == undefined ||
!ele.previousElementSibling.previousElementSibling.classList.contains('card')) {
// prev-prev item is not a card, the this is a invalid LOCA.
ele.previousElementSibling.remove(); // remove the time.
ele.remove(); // remove it
}
}
})
return w_today.innerHTML;
}
function print_a_month(d) {
var day_of_month = getCountDays(d.year, d.mon);
dateselector_d = new Date(`${date.year}-${date.mon}-${date.date}`);
window_ds_open(d.year, d.mon, true);
res =
`<div class='page-end'><h1 class='f2'>${d.year}</h1><h0>${mons_full[d.mon-1]}</h0><div class='calender'>${$('#window_sel_t').outerHTML}</div></div>`;
for (var i = 1; i <= day_of_month; i++) {
d.date = i;
var day = print_a_day(d);
if (day)
res += `<div class="page-end">${day}</div>`;
}
return res;
}
function print_last_month() {
var d = TODAY;
d = new Date(`${d.year}-${d.mon}-${d.date}`);
d.setMonth(d.getMonth() - 1);
d = {
year: d.getFullYear(),
mon: d.getMonth() + 1,
date: 1
};
return print_a_month(d);
}
</script>
<div id="datetab">
<h2 id="ds_year"></h2>
<button class=smdl
onclick="date=TODAY;load();switch_week(new Date(`${TODAY.year}-${TODAY.mon}-${TODAY.date}`))"
ondblclick="cmd()"></button>
<button onclick="ds_week(-1)" class=smdl></button>
<div id='dateweek'></div>
<button onclick="ds_week(1)" class=smdl></button>
<button class=smdl onclick="open_window_sel()"></button>