forked from nolanlawson/hello-electron-with-pouchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
188 lines (155 loc) · 5.37 KB
/
script.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
(function () {
'use strict';
var $ = document.querySelector.bind(document);
var ENTER_KEY = 13;
var newTodoDom = document.getElementById('new-todo');
var reportRunnerDom = document.getElementById('btnRunReport');
var manifestReportRunnerDom = document.getElementById('btnRunManifestReport');
var syncDom = document.getElementById('sync-wrapper');
// LevelDB
var NodePouchDB = require('pouchdb');
var serverConfig = require('./serverconfig.js').serverConfig;
var reportRunner = require('./reportrunner.js').generateReport;
var manifestReportRunner = require('./manifestReports/manifestReportRunner.js').generateReport;
var manifestMock = require('./manifestReports/manifestMock.js').mockManifest;
var leveldbDB = new NodePouchDB('mydb-leveldb');
var remoteCouch = new NodePouchDB(serverConfig.couchbaseSyncAddress);
leveldbDB.info(function (err, info) {
leveldbDB.changes({
since: info.update_seq,
live: true
}).on('change', showTodos);
});
leveldbDB.info().then(function (info) {
$('#leveldb').innerHTML = '✔ We can use PouchDB with LevelDB!';
}).catch(function (err) {
$('#leveldb').innerHTML = 'Error for LevelDB';
});
function addToDo(text) {
var todo = {
_id: new Date().toISOString(),
title: text,
completed: false
};
leveldbDB.put(todo).then(function (response) {
console.log("Success: ", response);
}).catch(function (err) {
console.log("Failure: ", err);
});
}
function showTodos() {
leveldbDB.allDocs({ include_docs: true, descending: true }).then(function (doc) {
redrawTOdosUI(doc.rows);
}).catch(function (err) {
console.log("failure: ", err);
});
}
function checkboxChanged(todo, event) {
todo.completed = event.target.checked;
console.log(todo);
leveldbDB.put(todo);
}
function deleteButtonPressed(todo) {
leveldbDB.remove(todo);
}
function todoBlurred(todo, event) {
var trimmedText = event.target.value.trim();
if (!trimmedText) {
leveldbDB.remove(todo);
} else {
todo.title = trimmedText;
leveldbDB.put(todo);
}
}
function sync() {
syncDom.setAttribute('data-sync-state', 'syncing');
var opts = { live: true, retry: true };
leveldbDB.sync(remoteCouch, opts, syncError).on('change', function (change) {
showTodos();
}).on('error', function (err) {
console.log(err);
});
}
function createTodoListItem(todo) {
var checkbox = document.createElement('input');
checkbox.className = 'toggle';
checkbox.type = 'checkbox';
checkbox.addEventListener('change', checkboxChanged.bind(this, todo));
var label = document.createElement('label');
label.appendChild(document.createTextNode(todo.title));
label.addEventListener('dblclick', todoDblClicked.bind(this, todo));
var deleteLink = document.createElement('button');
deleteLink.className = 'destroy';
deleteLink.addEventListener('click', deleteButtonPressed.bind(this, todo));
var divDisplay = document.createElement('div');
divDisplay.className = 'view';
divDisplay.appendChild(checkbox);
divDisplay.appendChild(label);
divDisplay.appendChild(deleteLink);
var inputEditTodo = document.createElement('input');
inputEditTodo.id = 'input_' + todo._id;
inputEditTodo.className = 'edit';
inputEditTodo.value = todo.title;
inputEditTodo.addEventListener('keypress', todoKeyPressed.bind(this, todo));
inputEditTodo.addEventListener('blur', todoBlurred.bind(this, todo));
var li = document.createElement('li');
li.id = 'li_' + todo._id;
li.appendChild(divDisplay);
li.appendChild(inputEditTodo);
if (todo.completed) {
li.classname += 'complete'; checkbox.checked = true;
}
return li;
}
function todoDblClicked(todo) {
var div = document.getElementById('li_' + todo._id);
var inputEditTodo = document.getElementById('input_' + todo._id);
div.className = 'editing';
inputEditTodo.focus();
}
function todoKeyPressed(todo, event) {
if (event.keyCode === ENTER_KEY) {
var inputEditTodo = document.getElementById('input_' + todo._id); inputEditTodo.blur();
}
}
function redrawTOdosUI(todos) {
var ul = document.getElementById('todo-list');
ul.innerHTML = '';
todos.forEach(function (todo) {
ul.appendChild(createTodoListItem(todo.doc));
});
}
function syncError() {
syncDom.setAttribute('data-sync-state', 'error');
}
function newTodoKeyPressHandler(event) {
if (event.keyCode === ENTER_KEY) {
addToDo(newTodoDom.value);
newTodoDom.value = '';
}
}
function runReportBtnHandler(event){
leveldbDB.allDocs({ include_docs: true, descending: true }).then(function(todos){
console.log(todos);
let todoReportData = [];
todos.rows.forEach(function(todo){
todoReportData.push(todo.doc);
})
// reportRunner(manifestMock.header);
reportRunner(todoReportData);
})
}
function runManifestReportBtnHandler(event){
manifestReportRunner();
}
function addEventListeners() {
newTodoDom.addEventListener('keypress', newTodoKeyPressHandler, false);
reportRunnerDom.addEventListener('click', runReportBtnHandler, false);
manifestReportRunnerDom.addEventListener('click', runManifestReportBtnHandler, false);
}
addEventListeners();
showTodos();
if (remoteCouch) {
sync();
}
})();