-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
137 lines (111 loc) · 3.71 KB
/
main.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
"strict";
function NoteListItem(note, viewModel) {
var self = this;
this.note = note;
this.title = ko.observable(note.title);
this.modified = ko.observable(note.modified);
note.subscribe('changed', function() {
self.title(note.title);
self.modified(note.modified);
viewModel.notes.sort(NoteListItem.prototype.compare);
});
}
NoteListItem.prototype.compare = function(a, b) {
if (a.note.modified < b.note.modified) {
return 1;
} else if (a.note.modified > b.note.modified) {
return -1;
} else {
return 0;
}
};
function ViewModel() {
var self = this;
this.notes = ko.observableArray([]);
this.currentNote = ko.observable(null);
this.currentView = ko.observable("list");
this.updateTitle = function(note, event) {
console.log("Update title");
note.title = event.target.innerHTML;
};
this.updateText = function(note, event) {
console.log("Update text");
note.text = event.target.innerHTML;
};
this.addNote = function(note) {
self.notes.push(new NoteListItem(note, this));
self.notes.sort(NoteListItem.prototype.compare);
};
this.openNote = function(item) {
self.currentNote(item.note);
self.currentView("note");
};
this.closeNote = function(event) {
self.currentView("list");
self.currentNote(null);
};
}
function App() {
this.version = 2;
this.idb;
this.db;
this.notesProvider;
this.viewModel;
this.idb = window.indexedDB || window.webkitIndexedDB;
this.notesProvider = new NotesProvider(this);
this.viewModel = new ViewModel(this.notesProvider);
}
App.prototype.init = function() {
var self = this;
var request;
request = this.idb.open("Notes", this.version);
request.addEventListener("error", function(event) {
alert("Got error.");
}, false);
request.addEventListener("upgradeneeded", function(event) {
console.log("IDB onupgradeneeded");
self.db = request.result;
self.notesProvider.initStorage(event.currentTarget.transaction);
}, false);
request.addEventListener("success", function(event) {
console.log("IDB onsuccess");
self.db = event.target.result;
ko.applyBindings(self.viewModel);
self.notesProvider.init(function(err) {
if (!err) {
self.ready();
} else {
alert("Error while initialization.");
}
});
}, false);
};
App.prototype.ready = function() {
var self = this;
document.querySelector('#note_list > input.add-new-btn').addEventListener('click', function(event) {
var note = new Note(self.notesProvider);
self.viewModel.addNote(note);
self.viewModel.currentNote(note);
self.viewModel.currentView("note");
self.notesProvider.saveNote(note);
});
document.querySelector('#note_list > input').disabled = false;
};
document.addEventListener("DOMContentLoaded", function(event) {
console.log("Document loaded.");
window.applicationCache.addEventListener('updateready', function(e) {
console.log("Appcache update ready.");
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
// Browser downloaded a new app cache.
// Swap it in and reload the page to get the new hotness.
window.applicationCache.swapCache();
if (confirm('A new version of this site is available. Load it?')) {
window.location.reload();
}
} else {
// Manifest didn't changed. Nothing new to server.
}
}, false);
var app = new App();
app.init();
}, true);