-
Notifications
You must be signed in to change notification settings - Fork 1
/
offline.js
195 lines (167 loc) · 4.22 KB
/
offline.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
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
function offline()
{
var offline=false;
return offline;
}
function isChromeApp()
{
// we assume here that chrome.app.runtime is only available in chrome app or extensions
if (typeof chrome == "object")
if (chrome.app && chrome.app.runtime)
return true;
return false;
}
function offline_storage_key()
{
return "fwprofile";
}
function offline_storage_saverow(stor,saveData)
{
var maxID=99; // start at ID 100
var saveIX=0;
var saveID=0;
saveData.lastUpdate=Math.floor((new Date()).getTime()/1000);
saveData.curtime=0;
for (var i=0; i<stor.diagrams.length; i++)
{
maxID=Math.max(maxID,stor.diagrams[i].id);
if ($.trim(stor.diagrams[i].name)==$.trim(saveData.name)) { saveID=stor.diagrams[i].id; saveIX=i; }
}
if (saveID>0) // existing file
{
saveData.id=saveID;
stor.diagrams[saveIX]=saveData;
}
else
{
saveID=maxID+1;
saveData.id=saveID;
stor.diagrams.push(saveData);
}
return {"saveID": saveID, "storageData": stor};
}
function offline_storage_renamerow(stor,id,newname,data)
{
for (var i=0; i<stor.diagrams.length; i++)
if (stor.diagrams[i].id==id)
{
stor.diagrams[i].name=newname;
stor.diagrams[i].fwprop=data;
}
return stor;
}
function offline_storage_deleterow(stor,id)
{
for (var i=0; i<stor.diagrams.length; i++)
if (stor.diagrams[i].id==id)
stor.diagrams.splice(i,1);
return stor;
}
function offline_load()
{
var initVal={"version":g.currentVersion,"userID":-1,"username":"Offline", "idMap":"{}", "diagrams":[]};
var key=offline_storage_key();
var setval={};
if (isChromeApp())
{
chrome.storage.local.get(key,function(result)
{
result=result[key];
if (!result)
{
setval[key]=initVal;
chrome.storage.local.set(setval);
result=initVal;
}
processFilelist(JSON_toString(result));
});
}
else
{
if (!localStorage[key]) localStorage.setItem(key,JSON_toString(initVal));
processFilelist(localStorage[key]);
}
}
function offline_save(saveData)
{
var key=offline_storage_key();
var setval={};
if (isChromeApp())
{
chrome.storage.local.get(key,function(stor)
{
stor=stor[key];
var ret=offline_storage_saverow(stor,saveData);
setval[key]=ret.storageData;
chrome.storage.local.set(setval, function(){saveFinish(ret.saveID);});
});
}
else
{
var stor=JSON_fromString(localStorage[key]);
var ret=offline_storage_saverow(stor,saveData);
localStorage.setItem(key,JSON_toString(ret.storageData));
saveFinish(ret.saveID);
}
}
function offline_save_map(map)
{
var key=offline_storage_key();
var setval={};
if (isChromeApp())
{
chrome.storage.local.get(key,function(stor)
{
stor=stor[key];
stor.idMap=map;
setval[key]=stor;
chrome.storage.local.set(setval);
});
}
else
{
var stor=JSON_fromString(localStorage[key]);
stor.idMap=map;
localStorage.setItem(key,JSON_toString(stor));
}
}
function offline_rename(id,newtags,newname,data)
{
var key=offline_storage_key();
var setval={};
if (isChromeApp())
{
chrome.storage.local.get(key,function(stor)
{
stor=stor[key];
setval[key]=offline_storage_renamerow(stor,id,newname,data);
chrome.storage.local.set(setval, function(){ renameDone(id,newtags,newname); });
});
}
else
{
var stor=JSON_fromString(localStorage[key]);
localStorage.setItem(key,JSON_toString(offline_storage_renamerow(stor,id,newname,data)));
renameDone(id,newtags,newname);
}
}
function offline_delete(id)
{
var key=offline_storage_key();
var setval={};
if (isChromeApp())
{
chrome.storage.local.get(key,function(stor)
{
stor=stor[key];
setval[key]=offline_storage_deleterow(stor,id);
chrome.storage.local.set(setval, function(){ deleteDone(); });
});
}
else
{
var stor=JSON_fromString(localStorage[key]);
localStorage.setItem(key,JSON_toString(offline_storage_deleterow(stor,id)));
deleteDone();
}
}