-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotes.js
266 lines (229 loc) · 7.18 KB
/
notes.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
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
function Note(notesProvider, initData) {
var dirty = false;
var saving = false;
var inputFormat = "html";
var outputFormat = "html";
var memento = {
id: null,
title: "Note " + new Date(),
text: "New note.",
created: new Date(),
modified: new Date(),
format: "text"
};
if (initData) {
if (!initData.id) {
memento.id = UUID();
}
for (key in initData) {
memento[key] = initData[key];
}
} else {
memento.id = UUID();
}
this.subscribers = {};
// Use Object.defineProperty to hide actual data.
// When values are changed, notify NotesProvider so that
// note will be updated in the database.
Object.defineProperty(this, '_memento', {
configurable: false,
enumerable: false,
get: function() {
return memento;
}
});
Object.defineProperty(this, 'id', {
configurable: false,
enumerable: true,
get: function() {
return memento.id;
}
});
Object.defineProperty(this, 'created', {
configurable: false,
enumerable: true,
get: function() {
return memento.created;
}
});
Object.defineProperty(this, 'modified', {
configurable: false,
enumerable: true,
get: function() {
return memento.modified;
}
});
Object.defineProperty(this, 'title', {
configurable: false,
enumerable: true,
get: function() {
return memento.title;
},
set: function(val) {
if ( memento.title != val) {
memento.title = val;
memento.modified = new Date();
notesProvider.updated(this);
}
}
});
Object.defineProperty(this, 'text', {
configurable: false,
enumerable: true,
get: function() {
return memento.text;
},
set: function(val) {
if ( memento.text != val) {
memento.text = val;
memento.modified = new Date();
notesProvider.updated(this);
}
}
});
Object.defineProperty(this, '_dirty', {
configurable: false,
enumerable: false,
get: function() {
return dirty;
},
set: function(val) {
dirty = val;
}
});
Object.defineProperty(this, '_saving', {
configurable: false,
enumerable: false,
get: function() {
return saving;
},
set: function(val) {
saving = val;
}
});
}
Note.prototype.format = function(inputFormat, outputFormat, data) {
if (inputFormat == outputFormat) {
return data;
}
};
Note.prototype.subscribe = function(event, callback) {
if (! this.subscribers[event]) {
this.subscribers[event] = [];
}
this.subscribers[event].push(callback);
};
function NotesProvider(app) {
console.log("NotesProvider");
this.app = app;
}
NotesProvider.prototype.initStorage = function(transaction) {
console.log("NotesProvider.initStorage");
var self = this;
var store;
if (!self.app.db.objectStoreNames.contains("notes")) {
store = self.app.db.createObjectStore("notes", { keyPath: "id" });
} else {
store = transaction.objectStore("notes");
}
if (!store.indexNames.contains("modified")) {
store.createIndex("modified", "modified", { unique: false });
}
if (!store.indexNames.contains("created")) {
store.createIndex("created", "created", { unique: false });
}
}
NotesProvider.prototype.init = function(callback) {
console.log("NotesProvider.init");
var self = this;
var store;
var index;
var cursorRequest;
store = self.app.db.transaction(["notes"]).objectStore("notes");
index = store.index("modified");
cursorRequest = index.openCursor();
cursorRequest.addEventListener("success", function(e) {
console.log("NotesProvider.init, cursorRequest success");
var result = e.target.result;
if(!!result == false) {
callback();
} else {
console.log(JSON.stringify(result.value, null, '\t'));
self.app.viewModel.addNote(new Note(self, result.value));
result.continue();
}
}, false);
cursorRequest.addEventListener("error", function(err) {
console.log("NotesProvider.init, cursorRequest error");
callback(err);
}, false);
cursorRequest.addEventListener("abort", function(reason) {
console.log("NotesProvider.init, cursorRequest abort");
callback(reason);
}, false);
};
NotesProvider.prototype.addNote = function(note) {
var self = this;
var store;
var request;
var IDBTransaction = window.IDBTransaction;
var transaction = this.app.db.transaction(["notes"], "readwrite");
transaction.addEventLister("complete", function(event) {
console.log("NotesProvider.addNote, transaction complete.");
}, false);
transaction.addEventListener("error", function(event) {
console.log("NotesProvider.addNote, transaction error");
console.log(event);
alert("Creating new note failed.");
}, false);
store = transaction.objectStore("notes");
request = store.add(note._memento);
request.addEventListener("success", function(event) {
console.log("NotesProvider.addNote, succeed.");
console.log("Add new note to top of the list and make it current.");
self.app.viewModel.notes.splice(0, 0, note);
self.app.viewModel.currentNote(note);
}, false);
};
NotesProvider.prototype.saveNote = function(note) {
var self = this;
var store;
var request;
var IDBTransaction = window.IDBTransaction;
var transaction;
note._saving = true;
transaction = this.app.db.transaction(["notes"], "readwrite");
transaction.addEventListener("complete", function(event) {
console.log("NotesProvider.saveNote, transaction completed.");
note._saving = false;
if (note._dirty) {
console.log("NotesProvider.saveNote, re-save since note was updated during the operation.");
self.saveNote(note);
}
}, false);
transaction.addEventListener("error", function(event) {
console.log("NotesProvider.saveNote, error: ", event);
alert("Failed to save note.");
}, false);
store = transaction.objectStore("notes");
note._dirty = false;
request = store.put(note._memento);
request.addEventListener("success", function(event) {
console.log("NotesProvider.saveNote, saved.");
}, false);
};
NotesProvider.prototype.updated = function(note) {
var i = 0;
if (note.subscribers['changed'] instanceof Array) {
for (i = 0; i < note.subscribers['changed'].length; ++i) {
note.subscribers['changed'][i](note);
}
}
if (!note._dirty && !note._saving) {
console.log("Note updated, save current state.");
note._dirty = true;
this.saveNote(note);
} else if (!note._dirty && note._saving) {
note._dirty = true;
}
};