This repository has been archived by the owner on Jul 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui-behaviour.js
213 lines (172 loc) · 5.04 KB
/
ui-behaviour.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
const toDataURI = async function(inputData) {
return new Promise(function (res, rej) {
Object.assign(new FileReader(), {
onload (e) {
return res(e.target.result);
}
}).readAsDataURL(new Blob([inputData]));
});
};
const toByteArray = async function(inputData) {
return new Promise(function (res, rej) {
const xhr = Object.assign(new XMLHttpRequest(), {
responseType: 'arraybuffer',
onload () {
if (xhr.status !== 200) {
return rej(new Error('problem!'));
}
res(new Uint8Array(xhr.response))
},
});
xhr.open('GET', inputData);
xhr.send();
});
};
const Automerge = (typeof window === 'undefined' ? {} : window).Automerge || {
init () {
return {};
},
change (doc, callback) {
callback(doc);
return Object.assign({}, doc);
},
getLastLocalChange () {},
};
const mod = {
OLSKControllerRoutes () {
return [{
OLSKRoutePath: '/#name=MainDevice&[email protected]',
OLSKRouteMethod: 'get',
OLSKRouteSignature: 'AppMainRoute',
OLSKRouteFunction (req, res, next) {
return res.render(require('path').join(__dirname, 'index.html'));
},
}, {
OLSKRoutePath: '/#name=PeerDevice&[email protected]',
OLSKRouteMethod: 'get',
OLSKRouteSignature: 'AppPeerRoute',
OLSKRouteFunction (req, res, next) {
return res.render(require('path').join(__dirname, 'index.html'));
},
}];
},
// VALUE
_ValueDocument: Automerge.init(),
_ValueJournaledChanges: [],
// INTERFACE
InterfaceEdit (index) {
const item = mod._ValueDocument.items[index];
const name = document.querySelector('#' + item.guid + ' .AppListItemName');
name.remove();
const form = document.createElement('form');
form.classList.add('AppUpdate');
form.classList.add('AppListItemForm');
form.id = item.guid + '-form'
form.innerHTML = `
<input class="AppListItemUpdateField AppListItemField" type="text" value="${ item.text }" autofocus />
<input class="AppListItemUpdateButton AppListItemButton" type="submit" value="Update" />`;
form.onsubmit = function () {
event.preventDefault();
const response = document.querySelector(`#${ item.guid }-form .AppListItemUpdateField`).value;
form.remove();
if (!response.trim().length) {
return mod.ControlDelete(index);
}
window[item.guid].appendChild(name);
mod.ControlUpdate(index, response);
};
window[item.guid].appendChild(form);
},
// CONTROL
ControlCreate (inputData) {
if (!inputData.trim().length) {
return;
}
const created = Date.now();
mod._StoreChange(Automerge.change(mod._ValueDocument, function (doc) {
if (!doc.items) {
doc.items = [];
}
doc.items.unshift({
guid: created.toString(36),
text: inputData,
done: false,
created,
creator: window.webxdc.selfAddr,
})
}));
window.AppCreateField.value = '';
},
ControlToggle (index) {
mod._StoreChange(Automerge.change(mod._ValueDocument, function (doc) {
doc.items[index].done = !doc.items[index].done;
}));
},
ControlUpdate (index, text) {
mod._StoreChange(Automerge.change(mod._ValueDocument, function (doc) {
doc.items[index].text = text;
}));
},
ControlDelete (index) {
mod._StoreChange(Automerge.change(mod._ValueDocument, function (doc) {
doc.items.splice(index, 1);
}));
},
_ControlJournal (payload) {
info = window.webxdc.selfName + ' updated the list';
window.webxdc.sendUpdate({
payload,
info,
}, info);
},
async _StoreChange (inputData) {
mod._ValueDocument = inputData;
mod.ReactDocument(inputData);
mod._ValueJournaledChanges.push(await toDataURI(Automerge.getLastLocalChange(inputData)));
// throttle
// journal
mod._ControlJournal({
changes: mod._ValueJournaledChanges,
});
mod._ValueJournaledChanges = [];
},
// MESSAGE
async MessageDidArrive (inputData) {
const changes = await Promise.all(inputData.payload.changes.map(toByteArray));
mod.ReactDocument(mod._ValueDocument = Automerge.applyChanges(mod._ValueDocument, changes)[0]);
},
// REACT
async ReactDocument (doc) {
const list = document.querySelector('#AppList');
list.innerHTML = '';
doc.items && doc.items.forEach((item, index) => {
const element = document.createElement('li')
element.id = item.guid;
element.innerHTML = `<input class="AppListItemToggle" type="checkbox" onclick="AppBehaviour.ControlToggle(${ index });" ${ item.done ? 'checked' : '' } /> <span class="AppListItemName" onclick="AppBehaviour.InterfaceEdit(${ index });">${ item.text }</span>`;
element.classList.add('AppListItem');
if (item.done) {
element.classList.add('AppListItemDone');
}
list.appendChild(element);
})
},
// SETUP
_SetupMethods () {
return Object.keys(mod).filter(function (e) {
return e.match(/^Setup/);
});
},
SetupListener () {
window.webxdc.setUpdateListener(mod.MessageDidArrive);
},
// LIFECYCLE
LifecyclePageDidLoad () {
return mod._SetupMethods().forEach(function (e) {
return mod[e]();
});
},
};
if (typeof module !== 'undefined') {
module.exports = mod;
}
AppBehaviour = mod;