forked from remotestorage/myfavoritedrinks
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
144 lines (105 loc) · 3.88 KB
/
app.js
File metadata and controls
144 lines (105 loc) · 3.88 KB
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
// remoteStorage module
const remoteStorage = new RemoteStorage({
modules: [todos],
logging: true,
changeEvents: { local: true, window: true, remote: true, conflict: true },
});
remoteStorage.access.claim('todos', 'rw');
remoteStorage.todos.cacheTodos();
// remoteStorage events
remoteStorage.todos.handle('change', (event) => {
if (event.newValue && !event.oldValue) {
console.log(`Change from ${ event.origin } (add)`, event);
return mod.displayItem(event.relativePath, event.newValue);
}
if (!event.newValue && event.oldValue) {
console.log(`Change from ${ event.origin } (remove)`, event);
return mod.undisplayItem(event.relativePath);
}
if (event.newValue && event.oldValue) {
console.log(`Change from ${ event.origin } (change)`, event);
if (event.origin !== 'conflict') {
return mod.renderItems();
}
return mod.updateItem(event.relativePath, Object.assign(event.newValue, {
description: `${event.oldValue.description} / ${event.newValue.description} (was ${event.lastCommonValue.description})`,
})).then(mod.renderItems);
}
console.log(`Change from ${ event.origin }`, event);
});
// app interface
const mod = {
addItem: (description) => remoteStorage.todos.addTodo({
description,
}),
updateItem: (id, object) => remoteStorage.todos.updateTodo(id, object),
removeItem: (id) => remoteStorage.todos.removeTodo(id),
renderItems: () => remoteStorage.todos.getAllTodos().then(mod.displayItems),
displayItems (items) {
document.querySelector('#item-list').innerHTML = '';
for (const id in items) {
mod.displayItem(id, items[id]);
}
},
displayItem (id, object) {
let li = mod.liForID(id);
if (!li) {
li = document.createElement('li');
li.dataset.id = id;
document.querySelector('#item-list').appendChild(li);
}
li.innerHTML += `<form>
<input type="text" value="${ object.description }" placeholder="description">
<button class="save">Save</button>
<a class="delete button" title="Delete" href="#">×</a>
</form>`;
const save = li.querySelector('button.save');
const input = li.querySelector('input');
input.addEventListener('focus', () => save.style.visibility = 'visible');
input.addEventListener('blur', () => {
setTimeout(() => save.style.visibility = 'hidden', 100)
});
input.addEventListener('keydown', (event) => {
if (event.key === 'Enter') {
event.preventDefault();
mod.updateItem(id, Object.assign(object, {
description: input.value,
}));
}
});
save.addEventListener('click', () => {
mod.updateItem(id, Object.assign(object, {
description: input.value,
}));
});
li.querySelector('a.delete').addEventListener('click', (event) => {
event.preventDefault();
mod.removeItem(li.dataset.id);
});
},
undisplayItem: (id) => document.querySelector('#item-list').removeChild(mod.liForID(id)),
emptyItems () {
document.querySelector('#item-list').innerHTML = '';
document.querySelector('#add-item input').value = '';
},
liForID: (id) => document.querySelector(`#item-list li[data-id="${ id }"]`),
};
// setup after page loads
document.addEventListener('DOMContentLoaded', () => {
(new Widget(remoteStorage)).attach(document.querySelector('widget-wrapper'));
remoteStorage.on('ready', () => {
document.getElementById('add-item').addEventListener('submit', (event) => {
event.preventDefault();
const text = document.querySelector('#add-item input').value.trim();
if (text) {
mod.addItem(text);
}
document.querySelector('#add-item input').value = '';
});
// hide intro if inside frame
if (remoteStorage.remote.token && window.self !== window.top) {
document.body.classList.add('embedded');
}
});
remoteStorage.on('disconnected', mod.emptyItems);
});