forked from webodf/WebODF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitscreeneditor.js
517 lines (468 loc) · 17.8 KB
/
splitscreeneditor.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/**
* Copyright (C) 2014 KO GmbH <[email protected]>
*
* @licstart
* This file is part of WebODF.
*
* WebODF is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License (GNU AGPL)
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* WebODF is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with WebODF. If not, see <http://www.gnu.org/licenses/>.
* @licend
*
* @source: http://www.webodf.org/
* @source: https://github.com/kogmbh/WebODF/
*/
/*global require, define, document, window, runtime, alert, Wodo*/
/**
* @param {!{sessionId: string, username: string, backend: string, sessionListElementId: string, editorContainerElementId: string}} args
*/
function createEditor(args) {
"use strict";
var editor = null,
serverFactory = null,
server = null,
editorOptions = {
allFeaturesEnabled: true,
collabEditingEnabled: true
},
currentPage = null,
savingOverlay, disconnectedOverlay, hasLocalUnsyncedOpsOverlay,
sessionList,
userId, token,
currentSessionId, currentMemberId,
backend = args.backend;
// TODO: just some quick hack done for testing, make nice (e.g. the position calculation is fragile)
function addStatusOverlay(parentElement, symbolFileName, position) {
var htmlns = document.documentElement.namespaceURI,
imageElement, overlay;
runtime.assert(parentElement, "heya, no such parentelement");
overlay = document.createElementNS(htmlns, "div");
imageElement = document.createElementNS(htmlns, "img");
imageElement.src = symbolFileName;
overlay.appendChild(imageElement);
overlay.style.position = "absolute";
overlay.style.top = 24*position + "px";
overlay.style.opacity = "0.8";
overlay.style.display = "none";
parentElement.appendChild(overlay);
return overlay;
}
/**
* Switches the pages by hiding the old one and unhiding the new
* @param {!string} pageId
* @return {undefined}
*/
function switchToPage(pageId, useParent) {
if (currentPage) {
currentPage.style.display = "none";
}
currentPage = document.getElementById(pageId);
if (useParent) {
currentPage = currentPage.parentNode;
}
currentPage.style.display = "";
}
/**
* @return {undefined}
*/
function showSessions() {
switchToPage(args.sessionListElementId, true);
sessionList.setUpdatesEnabled(true);
}
/*jslint emptyblock: true*/
/**
* @return {undefined}
*/
function onEditingStarted() {
// nothing to do right now
}
/*jslint emptyblock: false*/
/**
* @param {!boolean=} ignoreError
* @return {undefined}
*/
function closeEditing(ignoreError) {
editor.leaveSession(function() {
server.leaveSession(currentSessionId, currentMemberId, function() {
showSessions();
}, function() {
if (ignoreError) {
showSessions();
}
// TODO: else report problem
});
});
}
/**
* @return {undefined}
*/
function handleEditingError(error) {
alert("Something went wrong:\n"+error);
closeEditing(true);
}
/**
* @param {!string} sessionId
* @return {undefined}
*/
function joinSession(sessionId) {
switchToPage(args.editorContainerElementId);
sessionList.setUpdatesEnabled(false);
currentSessionId = sessionId;
server.joinSession(userId, sessionId, function(memberId) {
currentMemberId = memberId;
if (!editor) {
editorOptions.networkSecurityToken = token;
editorOptions.closeCallback = closeEditing;
Wodo.createCollabTextEditor(args.editorContainerElementId, editorOptions, function(err, e) {
if (err) {
runtime.log(err);
return;
}
var canvasContainerElement;
editor = e;
canvasContainerElement = editor.getCanvasContainerElement();
savingOverlay = addStatusOverlay(canvasContainerElement, "document-save.png", 0);
hasLocalUnsyncedOpsOverlay = addStatusOverlay(canvasContainerElement, "vcs-locally-modified.png", 0);
disconnectedOverlay = addStatusOverlay(canvasContainerElement, "network-disconnect.png", 1);
editor.addEventListener(Wodo.EVENT_BEFORESAVETOFILE, function() {
savingOverlay.style.display = "";
});
editor.addEventListener(Wodo.EVENT_SAVEDTOFILE, function() {
savingOverlay.style.display = "none";
});
editor.addEventListener(Wodo.EVENT_HASLOCALUNSYNCEDOPERATIONSCHANGED, function(has) {
hasLocalUnsyncedOpsOverlay.style.display = has ? "" : "none";
});
editor.addEventListener(Wodo.EVENT_HASSESSIONHOSTCONNECTIONCHANGED, function(has) {
disconnectedOverlay.style.display = has ? "none" : "";
});
editor.addEventListener(Wodo.EVENT_UNKNOWNERROR, handleEditingError);
// load the document and get called back when it's live
editor.joinSession(serverFactory.createSessionBackend(sessionId, memberId, server), onEditingStarted);
});
} else {
editor.joinSession(serverFactory.createSessionBackend(sessionId, memberId, server), onEditingStarted);
}
}, function(err) {
// TODO: handle error
runtime.log(err);
});
}
function loginSuccess(userData) {
require(["webodf/editor/SessionListView"],
function (SessionListView) {
var sessionListDiv,
sessionListView;
runtime.log("connected:" + userData.full_name);
userId = userData.uid;
token = userData.securityToken || null;
// add session listing
if (args.sessionListElementId) {
sessionListDiv = document.getElementById(args.sessionListElementId);
sessionList = new serverFactory.createSessionList(server);
sessionListView = new SessionListView(sessionList, sessionListDiv, joinSession);
}
if (args.sessionId) {
joinSession(args.sessionId);
} else {
switchToPage(args.sessionListElementId, true);
}
// only done to make jslint see the var used
return sessionListView;
}
);
}
// start the editor with network
runtime.log("starting collaborative editor for ["+backend+"].");
require(["webodf/editor/backend/"+backend+"/ServerFactory"], function (ServerFactory) {
serverFactory = new ServerFactory();
server = serverFactory.createServer();
// wait for a network connection to establish.
// call the callback when done, when finally failed, or
// when a timeout reached.
// the parameter to the callback is a string with the possible
// values: "unavailable", "timeout", "ready"
server.connect(8000, function (state) {
if (state === "ready") {
server.login(args.username, loginSuccess);
}
});
});
}
window.JsGlobalServer = (function() {
"use strict";
function createServiceError(code, message) {
var error = new Error(message);
error.code = code;
return error;
}
function JsGlobalSession(title) {
var operations = [],
lastMemberSyncPoint = {},
stateSequence = 0;
function getSequenceId() {
return "jsg" + stateSequence;
}
/**
* Push the specified specs into the current session. Will throw a 'SEQ_OUTOFDATE' error if the supplied
* lastSequenceId does not match the current sequence id.
*
* @param {!string} opMemberId
* @param {!string} lastSequenceId
* @param {!Array.<!Object>} opSpecs
* @return {!{sequenceId: !string}}
*/
this.push = function(opMemberId, lastSequenceId, opSpecs) {
if (!lastMemberSyncPoint.hasOwnProperty(opMemberId)) {
throw createServiceError("MEMBER_NOT_IN_SESSION", "Member with id " + opMemberId + " is not part of session");
}
if (opSpecs.length > 0) {
if (lastSequenceId !== getSequenceId()) {
throw createServiceError("SEQ_OUTOFDATE", "State sequence out of date");
}
stateSequence += 1;
operations = operations.concat(opSpecs);
lastMemberSyncPoint[opMemberId] = operations.length;
}
return {
sequenceId: getSequenceId()
};
};
/**
* Add a member to the current session
*
* @param {!string} memberId
* @return {undefined}
*/
this.addMember = function (memberId) {
var addMember = {
optype: "AddMember",
memberid: memberId,
timestamp: Date.now(),
setProperties: {
fullName: memberId,
color: '#'+('000000' + Math.floor(Math.random()*16777215).toString(16)).slice(-6),
imageUrl: "avatar-" + memberId + ".png"
}
};
operations.push(addMember);
lastMemberSyncPoint[memberId] = 0;
};
/**
* Remove the member from the current session
*
* @param {!string} memberId
* @return {undefined}
*/
this.removeMember = function (memberId) {
if (!lastMemberSyncPoint.hasOwnProperty(memberId)) {
throw createServiceError("MEMBER_NOT_IN_SESSION", "Member with id " + memberId + " is not part of session");
}
var removeMember = {
optype: "RemoveMember",
memberid: memberId,
timestamp: Date.now()
};
operations.push(removeMember);
delete lastMemberSyncPoint[memberId];
};
/**
* Fetch unsynchronized remote changes for the given member
*
* @param {!string} memberId
* @return {!{specs: !Array.<!Object>, sequenceId: !string}}
*/
this.getRemoteChanges = function (memberId) {
if (!lastMemberSyncPoint.hasOwnProperty(memberId)) {
throw createServiceError("MEMBER_NOT_IN_SESSION", "Member with id " + memberId + " is not part of session");
}
var unsyncedOps = operations.slice(lastMemberSyncPoint[memberId]);
lastMemberSyncPoint[memberId] = operations.length;
// Make a copy to ensure consumers can't modify internal state
return JSON.parse(JSON.stringify({
specs: unsyncedOps,
sequenceId: getSequenceId()
}));
};
/**
* Get an array of the currently active memberids in the current session
* @return {!Array.<!string>}
*/
this.getMemberIds = function () {
return Object.keys(lastMemberSyncPoint);
};
/**
* Get the title of this session
* @return {!string}
*/
this.getTitle = function() {
return title;
};
}
/**
* Central server used for collaborative editing. A single real JS instance is expected to be shared between
* all connected editors.
*
* @constructor
*/
function JsGlobalServer() {
var sessionsCount = 1,
users = {},
sessions = {},
inhibited = false,
latencyMs = 100;
function processCallback(callback, arg) {
window.setTimeout(function () {
callback(arg);
}, latencyMs); // Simulate some latency (e.g., via a HTTP req)
}
/**
* Log into the server with the supplied credentials.
* NB: this currently performs no checks of any sort, and is just meant for demonstration.
*
* @param {!string} login
* @param {!function(!string):undefined} successCb
* @return {undefined}
*/
this.login = function (login, successCb) {
users[login] = true;
processCallback(successCb, {
full_name: login,
uid: login,
securityToken: login
});
};
/**
* Join an existing session
*
* @param {!string} sessionId
* @param {!string} memberId
* @param {!function(!string):undefined} successCb
* @return {undefined}
*/
this.joinSession = function (memberId, sessionId, successCb) {
var session = sessions[sessionId];
if (!session) {
session = sessions[sessionId] = new JsGlobalSession("Split screen session #" + sessionsCount);
sessionsCount += 1;
}
session.addMember(memberId);
processCallback(successCb, memberId);
};
/**
* Leave an existing session
*
* @param {!string} sessionId
* @param {!string} memberId
* @param {!function(!string):undefined} successCb
* @param {!function(!string):undefined} failCb
* @return {undefined}
*/
this.leaveSession = function (sessionId, memberId, successCb, failCb) {
var session = sessions[sessionId];
if (session) {
session.removeMember(memberId);
processCallback(successCb, memberId);
} else {
processCallback(failCb, createServiceError("SESSION_NOT_FOUND"));
}
};
/**
* Return the current sessions.
*
* @return {!Array.<!{id: string, title: string, cursors: Array}>}
*/
this.getSessions = function () {
return Object.keys(sessions).sort().map(function(sessionId) {
var session = sessions[sessionId];
return {
id: sessionId,
title: session.getTitle(),
cursors: session.getMemberIds().sort()
};
});
};
/**
* Fetches all unsynchronized updates for the specified user
*
* @param {!string} sessionId
* @param {!string} memberId
* @param {!function(!{sequenceId: !string, specs: !Array.<!Object>}):undefined} successCb
* @param {!function(!string):undefined} failCb
* @return {undefined}
*/
this.getRemoteChanges = function (sessionId, memberId, successCb, failCb) {
try {
if (inhibited) {
throw createServiceError("SERVICE_UNAVAILABLE", "Service is unavailable");
}
var remoteChanges = sessions[sessionId].getRemoteChanges(memberId);
processCallback(successCb, remoteChanges);
} catch (e) {
processCallback(failCb, e);
}
};
/**
* Push operations to the specified session. If the supplied sequenceId doesn't match
* the server's current sequenceId, an error will be returned instead.
*
* @param {!string} sessionId
* @param {!string} memberId
* @param {!string} sequenceId
* @param {!Array.<!Object>} opSpecs
* @param {!function(!{sequenceId: !string}):undefined} successCb
* @param {!function(!string):undefined} failCb
* @return {undefined}
*/
this.push = function (sessionId, memberId, sequenceId, opSpecs, successCb, failCb) {
try {
if (inhibited) {
throw createServiceError("SERVICE_UNAVAILABLE", "Service is unavailable");
}
var pushResult = sessions[sessionId].push(memberId, sequenceId, opSpecs);
processCallback(successCb, pushResult);
} catch (e) {
processCallback(failCb, e);
}
};
/**
* Pauses all incoming and outgoing messages, causing a 'SERVICE_UNAVAILABLE' error to be reported instead
* @return {undefined}
*/
this.pause = function () {
inhibited = true;
};
/**
* Resumes processing of incoming and outgoing messages
* @return {undefined}
*/
this.resume = function () {
inhibited = false;
};
/**
* Gets the current simulated latency in milliseconds
* @return {!number}
*/
this.getLatency = function () {
return latencyMs;
};
/**
* Sets the current simulated latency in milliseconds
* @param {!number} newLatencyMs
* @return {undefined}
*/
this.setLatency = function (newLatencyMs) {
latencyMs = newLatencyMs;
};
}
return JsGlobalServer;
}());