amWiki.storage.js
8.65 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/**
* amWiki Web端 - 浏览器数据缓存模块
* @author Tevin
*/
;
(function (win) {
'use strict';
var tools = win.tools;
var wikiPath = tools.simString(win.location.pathname.replace('/', '').replace(/\//g, '_')).toUpperCase();
var LOCAL_STORAGE_NAME = 'AMWikiDataBase@' + wikiPath; //本地数据localStorage键名
var LOCAL_STATES = 'AMWikiStates@' + wikiPath; //本地状态集键名
/**
* 本地存储管理
* @constructor
*/
var Storage = function () {
this._db = null; //内存中的文库缓存
this._states = null; //内存中的状态集
this.$e = {
win: $(win),
//更新全部缓存按钮
searchUpdate: $('#searchUpdate'),
//缓存状态
cacheState: $('#cacheState'),
//文档总数
cacheDocTotal: $('#cacheDocTotal'),
//上次全部缓存更新时间
cacheLasttime: $('#cacheLasttime')
};
this._bridgeLocalStorage('read');
this._bindCtrl();
};
/**
* 存取本地存储
* @param {String} type - read / save
* @private
*/
Storage.prototype._bridgeLocalStorage = function (type) {
if (type == 'read') {
var defaultStr = '{"documents":{},"lastBuild":0}';
this._db = JSON.parse(win.localStorage[LOCAL_STORAGE_NAME] || defaultStr);
//转换旧名称
if (typeof this._db.libraries != 'undefined') {
this._db.documents = this._db.libraries;
delete this._db.libraries;
}
} else if (type == 'save') {
win.localStorage[LOCAL_STORAGE_NAME] = JSON.stringify(this._db);
}
};
/**
* 绑定操作
* @private
*/
Storage.prototype._bindCtrl = function () {
var that = this;
this.$e.win.on('beforeunload', function () {
that._bridgeLocalStorage('save');
});
};
/**
* 更新一篇文档,如果相同则不操作(对应内容不用重新渲染)
* @param {String} uri
* @param {String} content
* @returns {Boolean}
* @public
*/
Storage.prototype.update = function (uri, content) {
var id = tools.simString(uri, 'short');
if (this._db.documents[id]) {
if (this._db.documents[id].content == content) {
return false;
} else {
this.saveDoc(uri, content, id);
return true;
}
} else {
this.saveDoc(uri, content, id);
return true;
}
};
/**
* 保存一篇文档
* @param {String} uri - 文档资源地址
* @param {String} content - 文档内容
* @param {String} [id] - 已经编码的文档地址,可选
* @public
*/
Storage.prototype.saveDoc = function (uri, content, id) {
this.saveDocToDB(uri, content, id);
this._bridgeLocalStorage('save');
this._changeSummary('sateOnly');
};
/**
* 将文档存储到内存
* @param {String} uri
* @param {String} content
* @param {String} id
* @public
*/
Storage.prototype.saveDocToDB = function (uri, content, id) {
if (typeof uri != 'string' && uri == '') {
throw new Error('Error, uri must be a string!');
}
if (typeof id == 'undefined') {
id = tools.simString(uri, 'short');
}
this._db.documents[id] = {
id: id,
uri: uri,
content: content || '',
timestamp: Date.now()
};
this._changeSummary('sateOnly', 'prepare');
};
/**
* 读取一篇文档
* @param {String} uri - 文档资源地址
* @returns {String} 文档内容
* @public
*/
Storage.prototype.read = function (uri) {
var id = tools.simString(uri, 'short');
var article = '';
if (this._db.documents[id]) {
article = this._db.documents[id].content;
}
return article;
};
/**
* 读取一篇文档的时间
* @param {String} uri - 文档资源地址
* @returns {String} 文档内容
* @public
*/
Storage.prototype.readTime = function (uri) {
var id = tools.simString(uri, 'short');
if (this._db.documents[id]) {
return this._db.documents[id].timestamp;
} else {
return 0;
}
};
/**
* 删除一篇文档
* @param {String} uri - 文档资源地址
* @public
*/
Storage.prototype.remove = function (uri) {
var id = tools.simString(uri, 'short');
delete this._db.documents[id];
this._bridgeLocalStorage('save');
this._changeSummary('sateOnly');
};
/**
* 增涨文档打开数记录
* @param {String} uri
* @public
*/
Storage.prototype.increaseOpenedCount = function (uri) {
var id = tools.simString(uri, 'short');
//TODO: 待续...打开次数将一定程度影响排行
};
/**
* 校对列表,清除失效文档
* @param {Array} list - 由导航树偏平化生成的文档列表
* @public
*/
Storage.prototype.checkLibChange = function (list) {
this._indexing = list;
var documents = {};
var id = '';
for (var i = 0; i < list.length; i++) {
id = tools.simString(list[i], 'short');
if (typeof this._db.documents[id] != 'undefined') {
documents[id] = this._db.documents[id];
}
}
this._db.documents = documents;
this._bridgeLocalStorage('save');
this._changeSummary();
};
/**
* 更新缓存摘要(位于搜素面板)
* @param {String} stateOnly - 是否为只读 stateOnly / ...
* @param {String} prepare - 是否为预先 prepare / ...
* @private
*/
Storage.prototype._changeSummary = function (stateOnly, prepare) {
var libraryiesLong = 0;
for (var p in this._db.documents) {
if (this._db.documents.hasOwnProperty(p)) {
libraryiesLong++;
}
}
//如果是预先,百分数减1
if (prepare == 'prepare') {
this.$e.cacheState.text(parseInt(libraryiesLong / this._indexing.length * 100 - 1) + '%');
}
//非预先则正常
else {
this.$e.cacheState.text(parseInt(libraryiesLong / this._indexing.length * 100) + '%');
}
//如果不只是状态
if (stateOnly != 'stateOnly') {
this.$e.cacheDocTotal.text(this._indexing.length);
if (this._db.lastBuild) {
this.$e.cacheLasttime.text(win.tools.formatTime(this._db.lastBuild));
} else {
this.$e.cacheLasttime.text('0000-00-00 00:00:00');
}
}
};
/**
* 清除内存中的库列表
* @public
*/
Storage.prototype.clearLibraries = function () {
this._db.documents = {};
this._changeSummary('sateOnly');
};
/**
* 完成本次缓存重建
* @public
*/
Storage.prototype.saveRebuild = function () {
this._db.lastBuild = Date.now();
this._bridgeLocalStorage('save');
this._changeSummary();
};
/**
* 返回导航列表
* @returns {Array}
* @public
*/
Storage.prototype.getIndexList = function () {
return this._indexing;
};
/**
* 获取当前缓存的所有文档
* @returns {{Object}}
* @public
*/
Storage.prototype.getAllDocs = function () {
return this._db.documents;
};
/**
* 获取缓存最后重建时间
* @returns {Number}
* @public
*/
Storage.prototype.getLastBuildTs = function () {
return this._db.lastBuild;
};
/**
* 获取本地存储中指定名称的值
* @param {String} name
* @returns {*}
* @public
*/
Storage.prototype.getStates = function (name) {
if (!this._states) {
this._states = JSON.parse(win.localStorage[LOCAL_STATES] || '{}');
}
return this._states[name];
};
/**
* 保持键值对到本地存储
* @param {String} name
* @param {*} value
* @public
*/
Storage.prototype.setStates = function (name, value) {
if (!this._states) {
this._states = JSON.parse(win.localStorage[LOCAL_STATES] || '{}');
}
if (typeof value == 'undefined') {
delete this._states[name];
} else {
this._states[name] = value;
}
win.localStorage[LOCAL_STATES] = JSON.stringify(this._states);
};
return win.AWStorage = Storage;
})(window);