-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
214 lines (191 loc) · 5.38 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*global MozActivity, URL, File, asyncStorage*/
(function () {
"use strict";
function convertToFile (data) {
//Tfe-Drive (and perhaps other apps) send at least in some cases
//a Blob instead of the expected File, so the name is missing.
//Convert it to a real file, by adding the separate name or
//providing a fallback.
var EXT = {
'application/epub+zip': 'epub',
'application/pdf': 'pdf',
'application/vnd.oasis.opendocument.text': 'odt',
'application/vnd.oasis.opendocument.spreadsheet': 'ods',
'application/vnd.oasis.opendocument.presentation': 'odt'
}, name, type;
if (data.blob.name) {
return data.blob;
}
type = data.blob.type || data.type;
name = data.name || data.filename || data.title || ('noname.' + (EXT[type] || 'txt'));
return new File([data.blob], name, {type: type});
}
function getFile (types, callback) {
var pick;
if (window.MozActivity) {
pick = new MozActivity({
name: 'pick',
data: {
type: types
}
});
pick.onsuccess = function () {
try {
callback(convertToFile(this.result));
} catch (e) {
callback();
}
};
pick.onerror = function () {
callback();
};
} else {
pick = document.createElement('input');
pick.type = 'file';
pick.style.display = 'none';
document.getElementsByTagName('body')[0].appendChild(pick);
pick.addEventListener('change', function () {
var file = pick.files[0];
if (file) {
callback(file);
} else {
callback();
}
document.getElementsByTagName('body')[0].removeChild(pick);
}, false);
pick.click();
}
}
var openBlob = (function () {
var iframe, container;
iframe = document.getElementById('iframe');
container = document.getElementById('container');
iframe.style.display = 'none';
return function openBlob (blob, viewer, buffer) {
var url;
function messageHandler (e) {
if (e.data === 'ready') {
iframe.contentWindow.postMessage({url: url, name: blob.name || '', buffer: !!buffer}, '*');
} else {
URL.revokeObjectURL(url);
window.removeEventListener('message', messageHandler, false);
iframe.src = '';
iframe.style.display = 'none';
container.style.display = 'block';
if (location.search === '?mode=inline') {
try {
window.close();
} catch (e) {
}
}
}
}
url = URL.createObjectURL(blob);
window.addEventListener('message', messageHandler, false);
iframe.src = viewer;
container.style.display = 'none';
iframe.style.display = 'block';
};
/**/})();
function openFile (file) {
var type = file.name.replace(/.*\./, ''), viewer, buffer;
switch (type) {
case 'epub': viewer = 'lib/epub/index.html'; break;
case 'pdf': viewer = 'lib/pdf/viewer.html'; break;
case 'odt': case 'ods': case 'odp': viewer = 'lib/odf/index.html'; break;
default: viewer = 'lib/txt/index.html';
}
openBlob(file, viewer, buffer);
}
function saveFile (file) {
var a = document.createElement('a'),
href = URL.createObjectURL(file);
a.href = href;
if ('download' in a) {
a.download = file.name || '';
} else {
a.target = '_blank';
}
a.style.display = 'none';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
function storeFile (file) {
asyncStorage.setItem(file.name, file, rebuildFileList);
}
function removeFromStore (name) {
asyncStorage.removeItem(name, rebuildFileList);
}
function openFromStore (name) {
asyncStorage.getItem(name, openFile);
}
function saveFromStore (name) {
asyncStorage.getItem(name, saveFile);
}
function buildItem (file) {
var name = file.name.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
document.getElementsByTagName('ul')[0].innerHTML += '<li>' + [
'<a href="#" data-name="' + name + '">' + name.replace(/.*\//, '') + '</a>',
'<a href="#" class="remove" title="Remove from shelf" data-name="' + name + '">x</a>',
'<a href="#" class="save" title="Save from shelf" data-name="' + name + '">↓</a>'
].join(' ') + '</li>';
}
function rebuildFileList () {
document.getElementsByTagName('ul')[0].innerHTML = '';
asyncStorage.length(function (l) {
var i;
function callback (key) {
asyncStorage.getItem(key, buildItem);
}
for (i = 0; i < l; i++) {
asyncStorage.key(i, callback);
}
});
}
document.getElementsByTagName('button')[0].onclick = function () {
var formats = [
'application/epub+zip',
'application/pdf',
'application/vnd.oasis.opendocument.text',
'application/vnd.oasis.opendocument.spreadsheet',
'application/vnd.oasis.opendocument.presentation',
'text/*',
'*/*' //format is ignored more or less anyway, so just allow everything
];
getFile(formats, function (file) {
if (document.getElementsByTagName('input')[0].checked) {
storeFile(file);
}
openFile(file);
});
};
document.getElementsByTagName('ul')[0].onclick = function (e) {
var el = e.target, name;
if (e.button) {
return;
}
while (el && (!el.tagName || (el.tagName !== 'UL' && el.tagName !== 'A'))) {
el = el.parentNode;
}
name = el && el.dataset && el.dataset.name;
if (name) {
e.preventDefault();
if (el.className === 'remove') {
if (window.confirm('Remove "' + name + '" from local shelf?')) {
removeFromStore(name);
}
} else if (el.className === 'save') {
saveFromStore(name);
} else {
openFromStore(name);
}
}
};
if (navigator.mozSetMessageHandler) {
navigator.mozSetMessageHandler('activity', function (request) {
openFile(convertToFile(request.source.data));
});
}
rebuildFileList();
})();