-
Notifications
You must be signed in to change notification settings - Fork 113
/
script.js
144 lines (128 loc) · 3.39 KB
/
script.js
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
;"use strict";
(function(){
const state = {
currClips: []
};
function showHistory(){
searchAction();
}
function renderClips(clips){
state.currClips = clips;
const list = T.queryElem('.list');
const hint = T.queryElem('.no-record');
list.style.display = 'none';
hint.style.display = 'none';
if(clips.length > 0) {
const trowTpl = T.findElem('trow-tpl').innerHTML;
const items = [];
clips.forEach(function(clip){
const jsonUrl = calcRelativePath(clip.path);
let filename = clip.filename;
if(!filename) {
filename = ['index', clip.format].join('.');
}
const url = jsonUrl.replace('index.json', filename);
items.push(T.renderTemplate(trowTpl, {
clipId: clip.clipId,
time: renderTime(clip),
category: clip.category,
tags: clip.tags.join(", "),
title: clip.title,
url: url
}));
});
T.setHtml('.clippings > tbody', items.join(''));
list.style.display = 'block';
} else {
hint.style.display = 'block';
}
}
function calcRelativePath(clippingPath){
const root = 'mx-wc';
const path = window.location.href;
const ridx = path.lastIndexOf('/');
const dir = path.substring(0, ridx);
const currDir = [root, dir.split(root)[1]].join('');
return T.calcPath(currDir, clippingPath);
}
function renderTime(clip) {
try{
const d = new Date(clip.created_at)
const t = T.wrapDate(d).str;
return [t.month, t.day].join('/')
}catch(e) {
return '-/-'
}
}
function initSearch(){
const input = T.findElem('search');
const btn = T.findElem('search-btn');
input.value = getKeyword();
T.bindOnce(input, 'keypress', searchInputListener);
T.bindOnce(btn, 'click', searchAction);
}
function searchInputListener(e){
if(e.keyCode === 13){
searchAction();
}
}
function getKeyword(){
return window.localStorage.getItem('search.keyword');
}
function storeKeyword(value){
window.localStorage.setItem('search.keyword', value);
}
function searchAction(){
const input = T.findElem('search');
const keyword = input.value.trim();
storeKeyword(keyword);
if(keyword == ""){
renderClips(clippings);
}else{
const regExp = new RegExp(keyword, 'i');
const r = []
clippings.forEach(function(clip){
let keep = true;
if(keep && clip.title.match(regExp)){
keep = false;
r.push(clip)
}
if(keep && clip.category.match(regExp)){
keep = false;
r.push(clip)
}
if(keep){
const tag = clip.tags.some(function(tag){
if(tag.match(regExp)){
return true
}else{
return false
}
})
if(tag){ r.push(clip); }
}
});
renderClips(r);
}
}
function recallScroll(){
window.addEventListener('unload', function(e){
window.localStorage.setItem('scrollY', window.scrollY);
})
setTimeout(function(){
let scrollY = window.localStorage.getItem('scrollY')
if(scrollY){
scrollY = parseInt(scrollY);
if(scrollY > 150){
window.scrollTo(0, parseInt(scrollY));
}
}
}, 0);
}
function init(){
initSearch();
showHistory();
recallScroll();
}
init();
})();