-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pronode.js
283 lines (178 loc) · 6.02 KB
/
Pronode.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
const Pronode = {};
/**
Modify default anchor-click behavior:
*/
Pronode.anchorClickCallback = function(e) {
var e = window.e || e;
if (e.target.tagName !== 'A')
return;
const a = e.target;
if (a.attributes.pn_xhr && a.attributes.pn_xhr.value == "false") // Normal full-page reload if pn_xhr attribute is set to false:
return;
if (a.origin != window.origin) // Skip external links
return;
e.preventDefault();
Pronode.makeRequest(a.pathname);
if (a.attributes.pn_scrollup) { // Scroll page to top if pn_scrollup attribute is set
document.addEventListener('pn_complete', () => { Pronode.scrollUp() }, { once: true });
}
}
/**
Modify default form submit behavior:
*/
Pronode.formSubmitCallback = function(e) {
var e = window.e || e;
if (e.target.tagName !== 'FORM')
return;
const form = e.target;
// domain check here?
e.preventDefault();
Pronode.makeRequest(form.action, form.method, new FormData(form));
}
/**
Popstate callback fired when back and forward buttons are clicked
*/
Pronode.popstateCallback = function(e) {
document.documentElement.innerHTML = event.state.html;
Pronode.drawProgressBar(0);
}
/**
Add click and sumbit listeners:
*/
try {
// document.addEventListener('pn_complete', () => { hljs.highlightAll() }, false);
document.addEventListener('click', Pronode.anchorClickCallback, false);
document.addEventListener('submit', Pronode.formSubmitCallback, false);
window.addEventListener('popstate', Pronode.popstateCallback);
} catch(e) {
console.log(e);
}
/**
Update state
*/
Pronode.updateState = function(url) {
const state = { html: document.documentElement.innerHTML, pageTitle: document.title};
if (url == window.location.href) {
window.history.replaceState(state, document.title, url);
return;
}
window.history.pushState(state, document.title, url);
}
/**
Perform XHR to server
*/
Pronode.makeRequest = function(url, method = 'GET', formData = null) {
const xhr = new XMLHttpRequest();
xhr.open(method, url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
//xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.withCredentials = true;
xhr.onloadend = function() {
xhr.time = (new Date() - xhr.startTime);
console.log("Pronode: loaded " + url+ " in "+ xhr.time + " ms")
try {
const response = JSON.parse(xhr.responseText);
Pronode.drawProgressBar(75);
Pronode.updateFragments(response);
Pronode.dispatchEvent('pn_complete');
Pronode.updateState(url);
Pronode.drawProgressBar(100);
} catch (e) {
console.log("Pronode ERROR: response must be a valid JSON. " + e, xhr.responseText);
Pronode.drawProgressBar(75, 500);
Pronode.drawErrorBar(25, 500);
}
}
Pronode.drawProgressBar(0, null, 75);
xhr.startTime = new Date();
xhr.send(formData);
return xhr;
};
Pronode.dispatchEvent = function(eventName) {
const event = new Event(eventName);
document.dispatchEvent(event);
}
/**
Mass-update Pronode HTML chunks (pn_fragment) received from server.
*/
Pronode.updateFragments = function(fragments) {
console.log(fragments);
fragments.forEach(function(fragment) {
Pronode.updateFragment(fragment);
});
};
/**
Update a single Pronode HTML chunk (pn_fragment) received from server.
*/
Pronode.updateFragment = function(fragment) {
var query = '[pn_fragment="' + fragment.pn_fragment + '"][pn_origin="' + fragment.pn_origin + '"]';
var element = document.querySelector(query);
if (!element) {
console.log("Pronode ERROR: Couldn't find fragment with " + query);
return false;
}
if (fragment.htmlPlacement == 'inner') {
// not supported yet
element.outerHTML = fragment.html;
} else {
element.outerHTML = fragment.html;
}
return true;
};
Pronode.getInner = function(htmlString) {}; // TODO, get inner html of Fragment sent
/**
Draw Pronode progress bar for given state
*/
Pronode.drawProgressBar = function(progress, autohide, animateTo) {
this.progress = progress;
if (document.getElementById('pn_progress_bar') === null) {
const progressBarNode = document.createElement('div');
progressBarNode.id = 'pn_progress_bar';
progressBarNode.style.position = 'fixed';
progressBarNode.style.top = 0;
progressBarNode.style.left = 0;
progressBarNode.style.height = '1px';
progressBarNode.style.zIndex = 999;
progressBarNode.style.backgroundColor = 'CornflowerBlue';
document.getElementsByTagName('body')[0].appendChild(progressBarNode);
return Pronode.drawProgressBar(progress);
}
const progressBarNode = document.getElementById('pn_progress_bar');
progressBarNode.style.width = progress + '%';
if (animateTo) {
progressBarNode.animate([{ width: progress + '%' }, { width: animateTo + '%' }], 200);
}
if (autohide || progress == 100) {
if (!autohide) autohide = 100;
Pronode.progressTimeout = setTimeout(() => {Pronode.drawProgressBar(0)}, autohide);
}
}
/**
Draw Pronode error bar for given state
*/
Pronode.drawErrorBar = function(progress, autohide) {
if (document.getElementById('pn_error_bar') === null) {
const errorBarNode = document.createElement('div');
errorBarNode.id = 'pn_error_bar';
errorBarNode.style.position = 'fixed';
errorBarNode.style.top = 0;
errorBarNode.style.right = 0;
errorBarNode.style.height = '1px';
errorBarNode.style.zIndex = 999;
errorBarNode.style.backgroundColor = 'Crimson';
document.getElementsByTagName('body')[0].appendChild(errorBarNode);
return Pronode.drawErrorBar(progress, autohide);
}
const progressBarNode = document.getElementById('pn_error_bar');
progressBarNode.style.width = progress + '%';
if (autohide || progress == 100) {
if (!autohide) autohide = 100;
Pronode.errorTimeout = setTimeout(() => {Pronode.drawErrorBar(0)}, autohide);
}
}
/**
Scroll page to the top
*/
Pronode.scrollUp = function() {
return window.scrollTo({ top: 0, behavior: 'smooth' });
}