-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.html
243 lines (211 loc) · 7.99 KB
/
list.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
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<!-- style -->
<style type='text/css'>
html, body{
height: 100%;
}
.place-list {
position: fixed;
height: calc(100% - 48px);
left: 0;
right: 0;
bottom: 48px;
overflow: auto;
}
.place-list .welcome {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
}
.place-list .row {
padding: .75rem 1.25rem;
border-bottom: 1px solid rgba(0,0,0,.125);
margin: 0px;
}
.place-list .row:first-child {
border-top: 1px solid rgba(0,0,0,.125);
}
.bottom {
position: fixed;
height: 48px;
left: 0;
right: 0;
bottom: 0px;
}
.bottom .btn {
border-radius: 0px;
}
</style>
<title>บันทึกไทยชนะ</title>
</head>
<body>
<!-- list -->
<div class='place-list'>
<div class='welcome'>
<h3>กำลังโหลดข้อมูล...</h3>
</div>
</div>
<!-- bottom -->
<div class='bottom row'>
<a class='col btn btn-log btn-lg btn-info' href='#/'>ดูประวัติ</a>
<a class='col btn btn-scan btn-lg btn-danger' href='#/'>สแกน</a>
</div>
<!-- bottom scripts -->
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<!-- app scripts -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.26.0/moment.min.js"></script>
<script src="https://diewland.github.io/disk.js/dist/disk.js"></script>
<script src="./ignore-back.js"></script>
<script src="./thaichana.js"></script>
<script src="./app.js"></script>
<script>
// query data from disk
let data = Db.list();
// common
function update_disk () {
Disk.set(APP_TITLE, data);
}
function get_row_data (target) {
let $root = $(target).parents('div.row');
let title = $root.attr('data-title');
let url = $root.attr('data-url');
return [ $root, title, url ];
}
// prepare place list
if (data.length > 0) {
$('.place-list').html(`
<div class="input-group px-3">
<input type="text" id="filter-place" class="form-control" placeholder="ค้นหาสถานที่" aria-label="ค้นหาสถานที่" aria-describedby="ค้นหาสถานที่">
<div class="input-group-append">
<span class="input-group-text" id="filter-clear">🗑️</span>
</div>
</div>
`);
}
// empty list
else {
$('.welcome h3').html('กดปุ่มด้านล่างเพื่อสแกน');
}
// render places
data.sort((a, b) => {
if (a.checked_in !== b.checked_in) {
// sort by check-out, check-in
return b.checked_in - a.checked_in;
}
else {
// sort by timestamp desc
return b.timestamp - a.timestamp;
}
}).forEach(place => {
let last_timestamp = moment(place.timestamp).format(FMT_DATETIME);
let checked_in = place.checked_in || false;
let html_action = checked_in ? "<a class='btn btn-checkin btn-warning w-100' href='#/'>เช็คเอาท์</a>"
: "<a class='btn btn-checkin btn-success w-100' href='#/'>เช็คอิน</a>";
$('.place-list').append(`
<div class='row align-items-center'
data-title="${place.title}" data-url="${place.url}">
<div class='col-9 px-0'>
<span class='place-title'>${place.title}</span>
<div style='margin-top: -5px;'>
<sub><a class='btn-edit text-primary' href='#/'>แก้</a></sub>
<sub><a class='btn-del text-danger' href='#/'>ลบ</a></sub>
<sub class='text-secondary font-italic'>${last_timestamp}</sub>
</div>
</div>
<div class='col-3 px-0 text-right'>${html_action}</div>
</div>
`);
});
// bind edit button
$('body').on('click', '.btn-edit', evt => {
let [ $root, title, url ] = get_row_data(evt.target);
// prompt
let new_title = prompt('แก้ชื่อร้านค้า', title);
if ((new_title) && (new_title != title)) {
// update place title
let row = data.filter(r => r.url == url)[0];
row.title = new_title;
update_disk();
// update new title to screen
$root.find('.place-title').html(new_title);
}
});
// bind delete button
$('body').on('click', '.btn-del', evt => {
let [ $root, title, url ] = get_row_data(evt.target);
// confirm
let confrm = confirm(`ยืนยันการลบ ${title}`);
if (!confrm) return;
// update to disk
data = data.filter(r => r.url != url);
update_disk();
// remove row from screen
$root.remove();
});
// bind check in/out button
$('body').on('click', '.btn-checkin', evt => {
let [ $root, title, url ] = get_row_data(evt.target);
// update place timestamp, checked_in
let row = data.filter(r => r.url == url)[0];
let new_checked_in = !row.checked_in;
row.timestamp = ts();
row.checked_in = new_checked_in;
update_disk();
// add history
Db.add_history(row);
// check-in/check-out in 100 ms
setTimeout(_ => {
Thaichana.check_in(row.app_id, row.shop_id, new_checked_in);
}, 100);
});
// bind log button
$('.btn-log').click(_ => {
location.href = './log.html';
});
// bind scan button
$('.btn-scan').click(_ => {
location.href = get_scan_url();
});
// usage time
function update_usage_time () {
let checkout_btns = $('.btn-checkin.btn-warning').toArray();
if (checkout_btns.length == 0) return;
checkout_btns.forEach(btn => {
let [ $root, title, url ] = get_row_data(btn);
let shop_info = Thaichana.extract_shop_url(url);
let row = data.filter(r => r.shop_id == shop_info.shop_id)[0];
let label = 'เช็คเอาท์';
if (row) {
label = moment.utc(moment().diff(moment(row.timestamp))).format("H:mm:ss");
}
$(btn).html(label);
});
setTimeout(update_usage_time, 1000);
}
update_usage_time();
// filter
$('body').on('keyup', '#filter-place', evt => {
$('.place-list .row.align-items-center').each((i, r) => {
let filter = evt.target.value.toLowerCase();
let title = $(r).attr('data-title').toLowerCase();
let show = title.indexOf(filter) >= 0;
$(r).toggle(show);
});
});
$('body').on('click', '#filter-clear', _ => {
$('#filter-place').val('');
$('.place-list .row.align-items-center').show();
});
</script>
</body>
</html>