-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.js
108 lines (90 loc) · 2.99 KB
/
app.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
(function() {
var inputElement;
var formElement;
var ulElement;
var drinkRowPrefix = 'drinkrow-';
var remoteStorage = new RemoteStorage({
changeEvents: { local: true, window: true, remote: true, conflicts: true },
modules: [MyFavoriteDrinks]
});
function prefixId(id) {
return drinkRowPrefix + id;
}
function unprefixId(prefixedId) {
return prefixedId.replace(drinkRowPrefix, '');
}
function init() {
formElement = document.getElementById('add-drink');
inputElement = formElement.getElementsByTagName('input')[0];
ulElement = document.getElementById('drink-list');
// Claim read/write access for the /myfavoritedrinks category
remoteStorage.access.claim('myfavoritedrinks', 'rw');
// Display the RS connect widget
var widget = new Widget(remoteStorage);
widget.attach();
remoteStorage.myfavoritedrinks.init();
remoteStorage.myfavoritedrinks.on('change', function(event) {
if(event.newValue && (! event.oldValue)) {
console.log('Change from '+event.origin+' (add)', event);
displayDrink(event.relativePath, event.newValue.name);
}
else if((! event.newValue) && event.oldValue) {
console.log('Change from '+event.origin+' (remove)', event);
undisplayDrink(event.relativePath);
}
else if(event.newValue && event.oldValue) {
console.log('Change from '+event.origin+' (change)', event);
// TODO update drink
}
});
remoteStorage.on('ready', function() {
ulElement.addEventListener('click', function(event) {
if(event.target.tagName === 'SPAN') {
removeDrink(unprefixId(event.target.parentNode.id));
}
});
formElement.addEventListener('submit', function(event) {
event.preventDefault();
var trimmedText = inputElement.value.trim();
if(trimmedText) {
addDrink(trimmedText);
}
inputElement.value = '';
});
});
remoteStorage.on('disconnected', function() {
emptyDrinks();
});
}
function addDrink(name) {
remoteStorage.myfavoritedrinks.addDrink(name);
}
function removeDrink(id) {
remoteStorage.myfavoritedrinks.removeDrink(id);
}
function displayDrinks(drinks) {
for(var drinkId in drinks) {
displayDrink(drinkId, drinks[drinkId].name);
}
}
function displayDrink(id, name) {
var domID = prefixId(id);
var liElement = document.getElementById(domID);
if(! liElement) {
liElement = document.createElement('li');
liElement.id = domID;
ulElement.appendChild(liElement);
}
liElement.appendChild(document.createTextNode(name));//this will do some html escaping
liElement.innerHTML += ' <span title="Delete">×</span>';
}
function undisplayDrink(id) {
var elem = document.getElementById(prefixId(id));
ulElement.removeChild(elem);
}
function emptyDrinks() {
ulElement.innerHTML = '';
inputElement.value = '';
}
document.addEventListener('DOMContentLoaded', init);
})();