-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccessstorage.js
138 lines (130 loc) · 3.73 KB
/
accessstorage.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
if (!window.is_using_electron){
if (typeof(Storage) === "undefined") {
alert("error storage unavailable");
}
function getPoints(etab_id){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null){
data = {};
window.localStorage.setItem('data',JSON.stringify(data));
}
if (etab_id=="NaN" || !data.hasOwnProperty(etab_id.toString())){
return null;
}
return {"name":data[etab_id.toString()]["name"],"points": data[etab_id.toString()]["points"]};
}
function addRank(etab_id, point){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null || !data.hasOwnProperty(etab_id.toString())){
return;
}
let points = data[etab_id.toString()]["points"];
let ndays = point[0];
let rank = point[1];
let i=0;
if (points.length>0){
for (i=0; i<points.length; i++){
if (points[i][0] > ndays){
if (points[i][1] > rank){
return;
}
break;
}else if(points[i][0] == ndays){
if (!((i+1<points.length && points[i+1][1] > rank)
||(i>0 && points[i-1][1] < rank))){
points[i][1] = rank;
data[etab_id.toString()]["points"] = points;
window.localStorage.setItem('data',JSON.stringify(data));
}
return;
}
}
if (i>0 && points[i-1][1] < rank){
return;
}
}
points.splice(i, 0, [ndays,rank]);
data[etab_id.toString()]["points"] = points;
window.localStorage.setItem('data',JSON.stringify(data));
}
function deletePoint(etab_id, ndays){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null || !data.hasOwnProperty(etab_id.toString())){
return;
}
let points = data[etab_id.toString()]["points"];
for (let i=0; i<points.length; i++){
if (points[i][0] == ndays){
points.splice(i, 1);
data[etab_id.toString()]["points"] = points;
window.localStorage.setItem('data',JSON.stringify(data));
return;
}
}
}
function setEtabs(){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null){
data = {};
window.localStorage.setItem('data',JSON.stringify(data));
}
let etabs = [];
let keys = Object.keys(data);
for (let i=0; i<keys.length; i++){
let key = keys[i];
etabs.push([key,data[key]['name']]);
}
if(etabs != null && etabs.length>0){
for (let i=0; i<etabs.length; i++){
let etab = etabs[i];
let etab_id = etab[0];
let etab_name = etab[1];
insertElement("etabList",createElement(etab_id,etab_name));
}
}
}
function addEtab(etab_name){
if (etab_name.replace(/\s/g, '').length > 0){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null){
data = {};
window.localStorage.setItem('data',JSON.stringify(data));
}
let etab_id = 0;
while (data.hasOwnProperty(etab_id.toString())){
etab_id++;
}
data[etab_id.toString()] = {"name": etab_name, "points": []};
window.localStorage.setItem('data',JSON.stringify(data));
return etab_id;
}else{
return -1;
}
}
function delEtab(etab_id){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null){
data = {};
window.localStorage.setItem('data',JSON.stringify(data));
}
if(data.hasOwnProperty(etab_id.toString())){
delete data[etab_id.toString()];
window.localStorage.setItem('data',JSON.stringify(data));
}
}
function changeEtabName(etab_id,etab_name){
let data = JSON.parse(window.localStorage.getItem('data'));
if (data == null){
data = {};
window.localStorage.setItem('data',JSON.stringify(data));
return;
}
if (etab_name.replace(/\s/g, '').length > 0){
data[etab_id.toString()]["name"] = etab_name;
window.localStorage.setItem('data',JSON.stringify(data));
}
}
if (typeof window.etabsUnset !== "undefined" && window.etabsUnset){
setEtabs();
}
}