-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
121 lines (109 loc) · 3.93 KB
/
index.html
File metadata and controls
121 lines (109 loc) · 3.93 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Getting Started with Collaborative.js</title>
<style>
textarea {
width: 600px;
height: 200px;
margin-top: 10px;
}
</style>
</head>
<body>
<div>
Use this link to open the document in a separate window or share it with friend:
<br />
<a id="share-link" href="#" target="_blank"></a>
</div>
<textarea id="text-area"></textarea>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="/collaborative.min.js"></script>
<script>
// {{site}} token is replaced by the server with the site data during the page rendering
// For details, see '/document/:id?' endpoint at index.js file
var site = '{{site}}';
var updateDocumentUrl = '/' + site.document.id + '/update';
// These calls are utility ones and are not related to the Collaborative.js functionality
updatePageUrl();
renderSharingLink();
// Creates Collaborative.js string document using initial data from the server
var stringDocument = new clv.string.Document(
site.id,
site.document.execOrder,
site.document.context
);
// Creates Collaborative.js net object
var net = new clv.net.Http();
net.setSendingFn(sendingFunc);
// start sending with default interval 1000ms
net.start();
// Sets initial document value to the textarea control and binds changes handling function
var $textArea = $('#text-area');
$textArea.val(site.document.data);
$textArea.bind('input propertychange', textAreaValueChangeHandler);
/**
* On each change event, generates Collaborative.js operations using the difference between
* the textarea control value and the text stored in the site data object.
* Generated operations are used to update local site data and are sent to the server.
*/
function textAreaValueChangeHandler() {
var ops = clv.string.genOps(site.document.data, $textArea.val());
var tuple = stringDocument.commit(ops);
var value = clv.string.exec(site.document.data, tuple.toExec);
site.document.data = value;
$textArea.val(value);
net.send(stringDocument.getExecOrder(), tuple.toSend);
}
/**
* Renders a sharing link, so you can play around with the demo in two separate windows or
* share it with a friend.
*/
function renderSharingLink() {
var $shareLink = $('#share-link');
$shareLink.attr('href', document.location.href);
$shareLink.html(document.location.href);
}
/**
* Updates page URL with document id, so you may copy page URL from the browser address bar
* to open it in a separate window or share it with a friend.
*/
function updatePageUrl() {
var path = '/' + site.document.id;
if (window.location.pathname !== path) {
if (history) history.replaceState(null, null, path);
else window.location.replace(path);
}
}
/**
* This is a temporary function, it will be replaced with XhrIO implementation ASAP.
*/
function sendingFunc(execOrder, updates, callback) {
var data = {execOrder: execOrder, updates: updates};
$.ajax({
url: updateDocumentUrl,
type: 'POST',
data: JSON.stringify(data),
dataType: 'json',
contentType: 'application/json',
success: function(data) {
var otherSitesUpdates = data.updates;
if (otherSitesUpdates) {
var tuple = stringDocument.update(otherSitesUpdates);
site.document.data = clv.string.exec(site.document.data, tuple.toExec);
$textArea.val(site.document.data);
}
callback(true, stringDocument.getExecOrder(), otherSitesUpdates);
},
error: function(data) {
callback(false);
console.log('Fail to update document\n' + data.status + ', ' + data.responseText);
}
});
}
</script>
</body>
</html>