forked from andreaskoch/allmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoupdatejs.go
215 lines (165 loc) · 6.38 KB
/
autoupdatejs.go
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
// Copyright 2015 Andreas Koch. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package themefiles
const AutoupdateJs = `var Autoupdate = (function () {
function Autoupdate() { }
var self = this;
/**
* Get the currently opened web route
* @return string The currently opened web route (e.g. "documents/Sample-Document")
*/
var getCurrentRoute = function() {
var url = document.location.pathname;
// remove leading slash
var leadingSlash = /^\//;
url = url.replace(leadingSlash, "");
return url;
};
/**
* Get the URL for the web socket connection
* @return string The url for the web socket connection (e.g. "ws://example.com:8080/documents/Sample-Document.ws")
*/
var getWebSocketURL = function() {
var routeParameter = getCurrentRoute();
var host = document.location.host;
var protocol = "ws";
if (location.protocol === 'https:') {
protocol = "wss";
}
if (routeParameter === "") {
return protocol + "://" + host + "/" + "ws";
}
return protocol + "://" + host + "/" + routeParameter + ".ws";
};
/**
* Execute all on change callbacks
*/
var executeOnChangeCallbacks = function() {
if (typeof(self.changeCallbacks) !== 'object') {
return;
}
for (var callbackName in self.changeCallbacks) {
console.log("Executing on change callback: " + callbackName);
self.changeCallbacks[callbackName]();
}
};
/**
* cleanupSnippetCode removes newline characters from the given snippet code
* @param string snippetCode Code of a snippet that might contain newline characters
* @return string
*/
var cleanupSnippetCode = function(snippetCode) {
return snippetCode.replace('\\n', "");
};
/**
* getCSSSelectorFromSnippetName returns the proper CSS selector for the snippet with the given name.
* @param string snippetName The name of a snippet (e.g. "toplevelnavigation")
* @return string A CSS selector that matches the snippet with the given snippetName.
*/
var getCSSSelectorFromSnippetName = function(snippetName) {
switch(snippetName) {
case "aliases":
return "body > article > section.aliases";
case "tags":
return "body > article > section.tags";
case "publisher":
return "body > article > section.publisher";
case "toplevelnavigation":
return "body>nav.toplevel";
case "breadcrumbnavigation":
return "body>nav.breadcrumb";
case "itemnavigation":
return "aside.sidebar>nav.navigation";
case "children":
return "aside.sidebar>.children";
case "tagcloud":
return "aside.sidebar>.tagcloud";
default:
return "#" + snippetName;
}
};
/**
* Connect to the server
* @param string webSocketURL The url of the web-socket to connect to
*/
var connect = function(webSocketURL) {
var reconnectionTimeInSeconds = 3;
var connection = new WebSocket(webSocketURL);
connection.onclose = function(evt) {
console.log("Connection closed. Trying to reconnect in " + reconnectionTimeInSeconds + " seconds.");
setTimeout(function() {
console.log("Reconnecting");
connect(webSocketURL);
}, (reconnectionTimeInSeconds * 1000));
};
connection.onopen = function() {
console.log("Connection established.")
};
connection.onmessage = function(evt) {
// validate event data
if (typeof(evt) !== 'object' || typeof(evt.data) !== 'string') {
console.log("Invalid data from server.");
return;
}
// unwrap the message
message = JSON.parse(evt.data);
// check if all required fields are present
if (message === null || typeof(message) !== 'object' || typeof(message.route) !== 'string' || message.model === null || typeof(message.model) !== 'object') {
console.log("Invalid response format.", message);
return;
}
// check the model structure
var model = message.model;
if (typeof(model.content) !== 'string' || typeof(model.description) !== 'string' || typeof(model.title) !== 'string') {
console.log("Cannot update the view with the given model object. Missing some required fields.", model);
return;
}
// update the title
$('title').html(model.title);
$('.title').html(model.title);
// update the description
$('.description').html(model.description);
// update the content
$('.content').html(model.content);
// on-change handlers
executeOnChangeCallbacks();
// snippets
if (typeof(model.snippets) !== 'object') {
return;
}
for (var snippetName in model.snippets) {
// the new snippet content
var snippetContent = cleanupSnippetCode(model.snippets[snippetName]);
// get the CSS selector of the snippet
var cssSelector = getCSSSelectorFromSnippetName(snippetName);
// check if the snippet exists
var elementExists = $(cssSelector).length > 0;
if (elementExists == false) {
continue;
}
// replace the existing snippet
$(cssSelector).replaceWith(snippetContent);
}
};
};
Autoupdate.prototype.start = function () {
// check if websockets are supported
if(!window["WebSocket"]) {
console.log("Your browser does not support WebSockets.");
return;
}
// establish the connection
connect(getWebSocketURL());
};
Autoupdate.prototype.onchange = function(name, callback) {
if (typeof(self.changeCallbacks) !== 'object') {
self.changeCallbacks = {};
}
self.changeCallbacks[name] = callback;
};
return Autoupdate;
})();
autoupdate = new Autoupdate();
autoupdate.start();
`