-
Notifications
You must be signed in to change notification settings - Fork 211
/
local-storage-open-widget.js
74 lines (72 loc) · 2.57 KB
/
local-storage-open-widget.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
/*global $, _ */
$.fn.localStorageOpenWidget = function (offlineMapStorage) {
'use strict';
var modal = this,
template = this.find('[data-mm-role=template]'),
parent = template.parent(),
statusDiv = this.find('[data-mm-role=status]'),
showAlert = function (message, type, prompt, callback) {
type = type || 'block';
var html = '<div class="alert fade-in alert-' + type + '">' +
'<button type="button" class="close" data-dismiss="alert">×</button>' +
'<strong>' + message + '</strong>';
if (callback && prompt) {
html = html + ' <a href="#" data-mm-role="auth">' + prompt + '</a>';
}
html = html + '</div>';
statusDiv.html(html);
$('[data-mm-role=auth]').click(function () {
statusDiv.empty();
callback();
});
},
restoreMap = function (mapId, map, mapInfo) {
offlineMapStorage.restore(mapId, map, mapInfo);
fileRetrieval();
},
deleteMap = function (mapId, mapInfo) {
var map = offlineMapStorage.load(mapId);
offlineMapStorage.remove(mapId);
fileRetrieval();
showAlert('Map "' + map.title + '" removed.', 'info', 'Undo', restoreMap.bind(undefined, mapId, map, mapInfo));
},
loaded = function (fileMap) {
statusDiv.empty();
var sorted = [];
_.each(fileMap, function (value, key) {
sorted.push({id: key, title: value.d, modifiedDate: value.t * 1000, info: value});
});
sorted = _.sortBy(sorted, function (file) {
return file && file.modifiedDate;
}).reverse();
if (sorted && sorted.length > 0) {
_.each(sorted, function (file) {
var added;
if (file) {
added = template.clone().appendTo(parent);
added.find('a[data-mm-role=file-link]').attr('href', '/map/' + file.id).text(file.title);
added.find('[data-mm-role=modification-status]').text(new Date(file.modifiedDate).toLocaleString());
added.find('[data-mm-role=map-delete]').click(deleteMap.bind(undefined, file.id, file.info));
}
});
} else {
$('<tr><td colspan="3">No maps found in Browser storage</td></tr>').appendTo(parent);
}
},
fileRetrieval = function () {
parent.empty();
statusDiv.html('<i class="icon-spinner icon-spin"/> Retrieving files...');
var fileMap;
try {
fileMap = offlineMapStorage.list();
loaded(fileMap);
} catch (e) {
showAlert('Unable to retrieve files from browser storage', 'error');
}
};
template.detach();
modal.on('show', function () {
fileRetrieval();
});
return modal;
};