/
index.html
484 lines (428 loc) · 13.7 KB
/
index.html
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
<!DOCTYPE html>
<html manifest="cache.manifest">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1, maximum-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<title>All My Etherpads</title>
<script src="jquery.min.js"></script>
<script src="jquery.mobile-1.0a4.1.min.js"></script>
<script src="event-emitter.js"></script>
<script src="pretty-date.js"></script>
<script>
(function installApplicationCacheDebugMonitors() {
if (!window.applicationCache)
return;
var EVENTS = ['cached', 'checking', 'downloading', 'error', 'noupdate',
'obsolete', 'progress', 'updateready'];
var STATUS_NAMES = {
0: "UNCACHED",
1: "IDLE",
2: "CHECKING",
3: "DOWNLOADING",
4: "UPDATEREADY",
5: "OBSOLETE"
};
var console = window.console || {};
if (!console.log)
console.log = function() {};
function status() {
return STATUS_NAMES[applicationCache.status];
}
var listener = {
handleEvent: function(event) {
console.log("window.applicationCache fired event '" +
event.type + "', status is " + status());
$(".appcache-status").text(status());
$(".last-appcache-event").text(event.type);
}
};
$(window).ready(function() {
console.log("window.applicationCache.status is " + status());
$(".appcache-status").text(status());
var manifest = $("html").attr("manifest");
if (manifest)
jQuery.ajax({
url: manifest,
dataType: "text",
success: function(text) {
var match = text.match(/# VERSION: (\d+)/);
var msg = match ? match[1] : "not found in manifest";
$(".manifest-version").text(msg);
},
error: function(jqXHR, textStatus, errorThrown) {
var msg = "ERROR, jqXHR.status=" + jqXHR.status + ", " +
"textStatus=" + textStatus + ", " +
"errorThrown=" + errorThrown;
$(".manifest-version").text(msg);
}
});
});
EVENTS.forEach(function(name) {
applicationCache.addEventListener(name, listener, false);
});
})();
function makeHash(url) {
url = url.replace(/[:\/]/g, '_');
return url.replace(/\./g, "dot");
}
jQuery.extend({
isOffline: function() {
return !navigator.onLine;
},
showErrorMessage: function(msg) {
// Taken from jQuery Mobile's source code.
$("<div class='ui-loader ui-overlay-shadow ui-body-e ui-corner-all'>" +
"<h1></h1></div>")
.css({ "display": "block", "opacity": 0.96, "top": $(window).scrollTop() + 100 })
.appendTo( $.mobile.pageContainer )
.delay( 1800 )
.fadeOut( 400, function(){
$(this).remove();
}).find("h1").text(msg);
}
});
// Taken from http://stackoverflow.com/questions/2090551/parse-query-string-in-javascript
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) {
return decodeURIComponent(pair[1]);
}
}
return null;
}
setInterval(function() {
$(".last-sync").prettyDate();
}, 10000);
function EtherpadPage(options) {
var url = options.url;
var server = options.server;
var id = makeHash(url);
var page = $('#templates div.detail').clone();
var listItem = null;
page.attr('id', id);
$('div#detail-pages').append(page);
// This is a weird thing we need to do in order for the new page
// to exist and be linkable.
page.attr('data-url', id).page();
page.find('h1').text(url);
page.find('.open').attr('href', url);
function updateExcerpt() {
var excerpt = [];
var contents = page.find('.pad-content').contents();
for (var i = 0; i < contents.length; i++) {
var node = contents.get(i);
if (node.nodeName == "BR")
break;
excerpt.push($(node).text());
}
listItem.find(".excerpt").text(jQuery.trim(excerpt.join('')));
}
function update(etherpad) {
if (!listItem) {
var loc = getLocation(url);
listItem = $('#templates .list-item').clone();
listItem.attr("id", "item-" + id);
listItem.find("a").attr('href', "#" + id);
listItem.find(".name").text(loc.pathname.slice(1));
listItem.find(".server").text(loc.hostname);
$("#etherpads").append(listItem);
$("#etherpads").data('listview').refresh();
}
var lastSyncs = listItem.find(".last-sync")
.add(page.find(".last-sync"));
lastSyncs.attr("title", etherpad.lastSync.toString())
.prettyDate();
page.find('.pad-content').html(etherpad.html)
.find("title").prevAll().andSelf().remove();
page.find('.pad-content a').attr("target", "_blank");
updateExcerpt();
}
var self = {
load: function load(forceReload) {
jQuery.mobile.pageLoading();
var response = server.load(url, forceReload);
response.on('error', function(reason) {
jQuery.mobile.pageLoading(true);
jQuery.showErrorMessage("Error: " + reason);
});
response.on('load', function(etherpad) {
jQuery.mobile.pageLoading(true);
update(etherpad);
});
return response;
},
remove: function remove() {
jQuery.mobile.pageLoading();
var response = server.remove(url);
response.on('done', function() {
jQuery.mobile.pageLoading(true);
$("li#item-" + id).remove();
$("#etherpads").data('listview').refresh();
// TODO: What if the user is already at the home page?
jQuery.mobile.changePage("home");
});
}
};
page.find('.resync').click(function() {
self.load(true);
});
page.find('.remove').click(self.remove);
return self;
}
// Taken from http://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l
}
function BasicCache(options) {
var cache = options.cache || window.localStorage;
var prefix = options.prefix || "BASIC_CACHE_";
var self = {
list: function list() {
var results = [];
for (var name in cache) {
if (name.indexOf(prefix) == 0)
results.push(name.slice(prefix.length));
}
return results;
},
has: function has(url) {
return ((prefix+url) in cache);
},
get: function get(url, cb) {
cb(JSON.parse(cache[prefix+url]));
},
set: function set(url, blob) {
cache[prefix+url] = JSON.stringify(blob);
},
remove: function remove(url, cb) {
delete cache[prefix+url];
cb();
},
removeAll: function removeAll() {
self.list().forEach(function(url) {
self.remove(url, function() {});
});
}
};
return self;
}
function BasicServer(options) {
var cache = options.cache;
var network = options.network || jQuery;
var gateway = options.gateway || "http://etherpad-export.appspot.com/";
var self = {
list: function() {
return cache.list();
},
load: function(url, forceReload) {
var response = jQuery.eventEmitter({});
function loadCachedResponse() {
cache.get(url, function(etherpad) {
setTimeout(function() {
etherpad.lastSync = new Date(etherpad.lastSync);
response.emit('load', etherpad);
}, 0);
});
}
if (cache.has(url) && !forceReload) {
loadCachedResponse();
return response;
}
var loc = getLocation(url);
var params = {
server: loc.hostname,
port: loc.port,
pad: loc.pathname.slice(1),
format: "html"
};
if (params.port == "0" || params.port == "")
params.port = "80";
network.ajax({
type: 'GET',
crossDomain: true,
url: gateway,
data: params,
dataType: "text",
error: function(jqXHR, textStatus, errorThrown) {
var msg = "Fetching the pad failed.";
if (jqXHR.status == 404) {
msg = "No pad exists at that url.";
} else if (network.isOffline()) {
msg = "You are offline.";
}
response.emit('error', msg);
},
success: function(data) {
cache.set(url, {
lastSync: (new Date()).toString(),
html: data
});
loadCachedResponse();
},
timeout: 10000
});
return response;
},
remove: function(url) {
var response = jQuery.eventEmitter({});
cache.remove(url, function() {
setTimeout(function() {
response.emit('done');
}, 0);
});
return response;
}
};
return self;
}
var cache = BasicCache({prefix: "ETHERPADS_"});
var server = BasicServer({cache: cache});
function maybeInstallPages(existingUrls) {
var install = getQueryVariable("install");
if (install) {
var urlsToInstall = install.split(",");
var total = urlsToInstall.length;
var failed = 0;
function installNextURL() {
if (urlsToInstall.length) {
var url = urlsToInstall.pop();
if (existingUrls.indexOf(url) == -1) {
var page = EtherpadPage({
url: url,
server: server
});
var response = page.load();
response.on('error', function() {
failed++;
installNextURL();
});
response.on('load', installNextURL);
} else
installNextURL();
} else {
if (failed)
alert(failed + " of " + total + " URLs could not be installed.");
window.location.search = "";
}
}
installNextURL();
}
}
$(window).ready(function() {
function clearForm() {
$("#url").val('');
}
$("form#add-pad").submit(function() {
var url = $("#url").val();
if (url.length) {
if (server.list().indexOf(url) == -1) {
var page = EtherpadPage({
url: url,
server: server
});
var response = page.load(true);
response.on('load', clearForm);
} else {
jQuery.showErrorMessage("That pad is already in your list.");
}
} else {
jQuery.showErrorMessage("Please enter a URL.");
}
return false;
});
$("input#export-list").click(function() {
var urls = server.list();
var loc = window.location;
var params = {install: urls.join(',')};
var url = loc.protocol + "//" + loc.host +
loc.pathname + "?" + jQuery.param(params);
window.prompt("Load this URL in your other browser.", url);
});
var urls = server.list();
urls.forEach(function(url) {
var page = EtherpadPage({
url: url,
server: server
});
page.load();
});
// Not sure how to display a dynamically-generated
// page on page load, so we'll reset the hash to the
// home page until we figure out how to do that.
if (window.location.hash.length &&
!window.location.hash.match(/^(#|#home)$/)) {
window.location.hash = "#home";
// We need to reload the page so that jQuery mobile's
// history management doesn't get broken.
window.location.reload();
}
maybeInstallPages(urls);
});
</script>
<style>
@import url('jquery.mobile-1.0a4.1.min.css');
</style>
</head>
<body>
<div data-role="page" id="home">
<div data-role="header">
<h1>All My Etherpads</h1>
</div>
<div data-role="content">
<ul id="etherpads" data-role="listview" data-filter="true" data-inset="true" data-theme="c">
</ul>
<form id="add-pad">
<h2>Add a Pad</h2>
<p>You can use this form to add an existing Etherpad to your list, allowing you to easily access it offline on this device.</p>
<div data-role="fieldcontain">
<label for="url">Etherpad URL:</label>
<input type="text" name="url" id="url" value="" />
<input type="submit" value="Add" />
</div>
</form>
<h2>Export</h2>
<p>Click the button below to generate a URL that can be used to install your list on another device.</p>
<input type="button" value="Export List" id="export-list">
<h2>About</h2>
<p><em>All My Etherpads</em> was created by <a href="http://twitter.com/toolness" target="_blank">@toolness</a>. You can learn more about it at the <a href="https://github.com/toolness/all-my-etherpads#readme" target="_blank">github project page</a>.</p>
<div data-role="collapsible" data-collapsed="true">
<h3>Under The Hood</h3>
<p>Application Cache Status:
<code class="appcache-status"></code>
</p>
<p>Last Application Cache Event:
<code class="last-appcache-event"></code>
</p>
<p>Current Manifest Version:
<code class="manifest-version"></code>
</p>
</div>
</div>
</div>
<div id="detail-pages"></div>
<div id="templates" style="display: none">
<li class="list-item"><a><h3 class="name"></h3><p><strong class="server"></strong></p><p class="excerpt"></p><p class="ui-li-aside">Last synced <span class="last-sync"></span></p></a></li>
<div data-role="page" class="detail">
<div data-role="header">
<h1></h1>
<a href="#home" data-icon="home" data-iconpos="notext" data-direction="reverse" class="ui-btn-right jqm-home">Home</a>
</div>
<div data-role="content">
<div data-role="controlgroup">
<a class="open" href="http://google.com/" target="_blank" data-role="button">Open In Etherpad</a>
<input type="button" value="Re-Sync" class="resync">
<input type="button" value="Remove" class="remove">
</div>
<p>This pad was last synced <span class="last-sync"></span>.</p>
<div class="pad-content"></div>
</div>
</div>
</div>
</body>
</html>