forked from gritzko/todomvc-swarm
-
Notifications
You must be signed in to change notification settings - Fork 62
/
LocalTodoApp.js
116 lines (107 loc) · 3.96 KB
/
LocalTodoApp.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
var Swarm = require('swarm');
var Spec = Swarm.Spec;
var TodoList = require('./model/TodoList');
var TodoItem = require('./model/TodoItem');
var TodoApp = require('./TodoApp');
module.exports = window.TodoApp = (function(superclass){
var defaultModels = [];
// TODO: default english version
defaultModels.push({text:'распределенное приложение как локальное', completed: location.hash !== "#focused"});
defaultModels.push({text:'всё очень быстро', completed: true});
defaultModels.push({text:'теперь доступно каждому!', completed: true});
var prototype = extend$((import$(S, superclass), S), superclass).prototype, constructor = S;
function S(ssnid, itemId){
this.path = [];
this.ssnid = ssnid;
this.moving = false;
this.initSwarm();
this.installListeners();
this.parseUri();
if (location.hash === '#focused') {
this.selectItem(0);
}
}
prototype.initSwarm = function () {
this.storage = new Swarm.SharedWebStorage();
this.storage.authoritative = true;
this.host = Swarm.env.localhost =
new Swarm.Host(this.ssnid,'',this.storage);
};
prototype.installListeners = function () {
var self = this;
document.addEventListener('keydown', function (ev) {
switch (ev.keyCode) {
// case 9: self.forward();break; // tab
// case 27: self.back(); break; // esc
case 40: self.down(); break; // down arrow
case 38: self.up(); break; // up arrow
case 45: self.toggle(); break; // insert
case 13: self.create(); break; // enter
//case 46: self.delete(); break; // delete
default: return true;
}
ev.preventDefault();
return false;
});
};
prototype.parseUri = function () {
var hash = window.localStorage.getItem(".itemId");
var path = window.localStorage.getItem(".listId");
var idre = Spec.reQTokExt;
idre.lastIndex = 0;
var ml = idre.exec(path), listId = ml&&ml[2];
idre.lastIndex = 0;
var mi = idre.exec(hash), itemId = mi&&mi[2];
if (!listId) {
var list = new TodoList();
var item = new TodoItem();
list.addObject(item);
listId = list._id;
itemId = item._id;
}
this.forward(listId,itemId);
};
prototype.forward = function (listId, itemId) {
var self = this;
var fwdList;
if (!listId) {
var item = this.getItem();
listId = item.childList;
}
if (!listId) {
fwdList = new TodoList();
listId = fwdList._id;
item.set({childList: listId});
} else {
fwdList = this.host.get('/TodoList#'+listId); // TODO fn+id sig
}
// we may need to fetch the data from the server so we use a callback, yes
fwdList.once('.init',function(){
if (!fwdList.length()) {
defaultModels.forEach(function(i){
fwdList.addObject(new TodoItem(i));
})
}
itemId = itemId || fwdList.objectAt(0)._id;
window.localStorage.setItem(".itemId", "#" + itemId);
window.localStorage.setItem(".listId", "/" + listId);
self.path.push({
listId: listId,
itemId: itemId
});
self.refresh();
});
};
return S;
}(TodoApp));
function extend$(sub, sup){
function fun(){} fun.prototype = (sub.superclass = sup).prototype;
(sub.prototype = new fun).constructor = sub;
if (typeof sup.extended == 'function') sup.extended(sub);
return sub;
}
function import$(obj, src){
var own = {}.hasOwnProperty;
for (var key in src) if (own.call(src, key)) obj[key] = src[key];
return obj;
}