-
Notifications
You must be signed in to change notification settings - Fork 0
/
template-history.htm
170 lines (148 loc) · 4.44 KB
/
template-history.htm
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
<!DOCTYPE html>
<html lang="{{language}}">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" >
<title>History</title>
<link rel="icon" href="{{icon}}" >
<style>
body {
font: normal 14px Verdana, Arial, sans-serif;
display: none;
}
td {
padding-left: 15px;
padding-right: 15px;
text-align: right;
}
td.dates {
padding-left: 15px;
padding-right: 15px;
text-align: left;
}
div.h2like {
line-height: 18px;
border-top: 2px solid lightgrey;
border-bottom: 2px solid lightgrey;
margin-top:17px;
padding-top:5px;
padding-bottom:5px;
font: normal 16px Verdana, Arial, sans-serif;
}
</style>
{% include 'menu.css' %}
<script src="history.js"></script>
<script>
function query_history_answer(params) {
// mode = store|retrieve
let mode = params.get('mode');
let back = params.get('back').replace(/\\'/g, "'");
let movie = params.get('movie');
let dates = localStorage.getItem(movie);
if (mode == 'store') {
let today = new Date().toISOString().split('T')[0];
dates = (dates === null) ? today : (dates + ',' + today);
localStorage.setItem(movie, dates);
set_history_status('modified');
} else if (mode == 'retrieve') {
// pass
} else
alert('Wrong mode');
window.open(back + `?mode=back&history=${dates}`, "_self");
}
function onload() {
const params = new URLSearchParams(window.location.search);
if (params.get('mode') !== null) {
query_history_answer(params);
} else {
document.getElementsByTagName("body")[0].style.display = "block";
show_history();
if (get_history_status() === 'saved')
document.getElementById("statusmsg").innerHTML = '';
else
document.getElementById("statusmsg").innerHTML = '⚠ History not saved.';
}
}
function sort_by_movie() {
localStorage.setItem('show_by', 'movie');
show_history();
}
function sort_by_date() {
localStorage.setItem('show_by', 'date');
show_history();
}
function show_history() {
if (localStorage.getItem('show_by') == 'movie')
s = history_sorted_by_movie();
else
s = history_sorted_by_date();
document.getElementById("content").innerHTML = s;
}
function onClickRestore() {
const [file] = document.querySelector("input[type=file]").files;
const reader = new FileReader();
reader.addEventListener("load", () => {restore_history(reader.result);}, false,);
if (file) {
reader.readAsText(file);
}
}
function restore_history(s) {
// remove all movie cookies
localStorage.clear();
// get new movie cookies
movies_to_dates = {};
let movie;
for (const line of s.split(/\r?\n/)) {
if (line.search(/\d\d\d\d-\d\d-\d\d/) < 0)
// line is a movie title
movie = line.trim();
else
// line is a date (or comma separated list of dates)
movies_to_dates[movie] = line;
}
// set new movie cookies
for (const movie in movies_to_dates) {
localStorage.setItem(movie, movies_to_dates[movie]);
}
location.reload();
set_history_status('saved');
}
function download(content, mimeType, filename){
let a = document.createElement('a');
let blob = new Blob([content], {type: mimeType});
let url = URL.createObjectURL(blob);
a.setAttribute('href', url);
a.setAttribute('download', filename);
a.click();
}
function save_history_with_anchor_download() {
// no feedback on user action, assume user confirms download
s = formatted_history_for_saving();
set_history_status('saved');
location.reload();
download(s, 'text/plain', 'history.txt');
}
function save_history() {
save_history_with_anchor_download();
}
</script>
</head>
<body onload="onload();">
<div style="width: 95%; margin-left: auto; margin-right: auto">
<div style="width: 94%; height:0px; top: 0; position: fixed; ">
{% include 'menu.htm' %}
</div>
<div class="h2like">
<input type="file" id="load_input" onchange="onClickRestore()" style="display:none;">
<input type="button"
onclick="document.getElementById('load_input').click()"
value="Restore history" >
<input type="button" onclick="save_history();" value="Save history" >
<input type="button" onclick="sort_by_movie();" value="Sort by movie" >
<input type="button" onclick="sort_by_date();" value="Sort by date" >
<span id='statusmsg' style="margin-left:5px"></span>
</div>
<table id="content">
</table>
</div>
</body>
</html>