-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
navigator.js
445 lines (380 loc) · 12.8 KB
/
navigator.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
'use strict';
define('navigator', ['forum/pagination', 'components'], function (pagination, components) {
var navigator = {};
var index = 1;
var count = 0;
var navigatorUpdateTimeoutId;
var touchTooltipEl;
var touchIntervalId;
var touchX;
var touchY;
var touchIndex;
var isNavigating = false;
var firstMove = true;
navigator.scrollActive = false;
var paginationBlockEl = $('.pagination-block');
var paginationTextEl = paginationBlockEl.find('.pagination-text');
var paginationBlockMeterEl = paginationBlockEl.find('meter');
var paginationBlockProgressEl = paginationBlockEl.find('.progress-bar');
$(window).on('action:ajaxify.start', function () {
$(window).off('keydown', onKeyDown);
});
navigator.init = function (selector, count, toTop, toBottom, callback) {
index = 1;
navigator.selector = selector;
navigator.callback = callback;
navigator.toTop = toTop || function () {};
navigator.toBottom = toBottom || function () {};
paginationBlockEl = $('.pagination-block');
paginationTextEl = paginationBlockEl.find('.pagination-text');
paginationBlockMeterEl = paginationBlockEl.find('meter');
paginationBlockProgressEl = paginationBlockEl.find('.progress-bar');
$(window).off('scroll', navigator.delayedUpdate).on('scroll', navigator.delayedUpdate);
paginationBlockEl.find('.dropdown-menu').off('click').on('click', function (e) {
e.stopPropagation();
});
paginationBlockEl.off('shown.bs.dropdown', '.dropdown').on('shown.bs.dropdown', '.dropdown', function () {
setTimeout(function () {
$('.pagination-block input').focus();
}, 100);
});
paginationBlockEl.find('.pageup').off('click').on('click', navigator.scrollUp);
paginationBlockEl.find('.pagedown').off('click').on('click', navigator.scrollDown);
paginationBlockEl.find('.pagetop').off('click').on('click', navigator.toTop);
paginationBlockEl.find('.pagebottom').off('click').on('click', navigator.toBottom);
paginationBlockEl.find('input').on('keydown', function (e) {
if (e.which === 13) {
var input = $(this);
if (!utils.isNumber(input.val())) {
input.val('');
return;
}
var index = parseInt(input.val(), 10);
var url = generateUrl(index);
input.val('');
$('.pagination-block .dropdown-toggle').trigger('click');
ajaxify.go(url);
}
});
$('.pagination-block.visible-xs').on('touchstart', function (e) {
touchTooltipEl = $('.navigator-thumb');
touchX = Math.min($(window).width(), Math.max(0, e.touches[0].clientX));
touchY = Math.min($(window).height(), Math.max(0, e.touches[0].clientY));
firstMove = true;
}).on('touchmove', function (e) {
var windowWidth = $(window).width();
var windowHeight = $(window).height();
var deltaX = Math.abs(touchX - Math.min(windowWidth, Math.max(0, e.touches[0].clientX)));
var deltaY = Math.abs(touchY - Math.min(windowHeight, Math.max(0, e.touches[0].clientY)));
touchX = Math.min(windowWidth, Math.max(0, e.touches[0].clientX));
touchY = Math.min(windowHeight, Math.max(0, e.touches[0].clientY));
if (deltaX >= deltaY && firstMove) {
isNavigating = true;
touchIntervalId = setInterval(updateTooltip, 100);
}
if (isNavigating) {
e.preventDefault();
e.stopPropagation();
var percent = touchX / windowWidth;
index = Math.max(1, Math.ceil(count * percent));
index = index > count ? count : index;
if (firstMove) {
updateTooltip(function () {
touchTooltipEl.removeClass('hidden');
});
}
navigator.updateTextAndProgressBar();
}
firstMove = false;
}).on('touchend', function () {
if (touchIntervalId) {
clearInterval(touchIntervalId);
touchIntervalId = 0;
}
if (isNavigating) {
touchTooltipEl.addClass('hidden');
navigator.scrollToIndex(index - 1, true, 0);
isNavigating = false;
}
});
handleKeys();
navigator.setCount(count);
navigator.update(0);
};
function updateTooltip(callback) {
callback = callback || function () {};
if (touchIndex === index) {
return;
}
touchIndex = index;
touchTooltipEl.css({ left: Math.min($(window).width() - touchTooltipEl.outerWidth(), Math.max(touchX - (touchTooltipEl.outerWidth() / 2), 0)) });
socket.emit('posts.getTimestampByIndex', { tid: ajaxify.data.tid, index: index - 1 }, function (err, timestamp) {
if (err) {
return app.alertError(err.message);
}
var date = new Date(timestamp);
var ds = date.toLocaleString(config.userLang, { month: 'long' });
touchTooltipEl.find('.text').translateText('[[global:pagination.out_of, ' + index + ', ' + count + ']]');
if (timestamp > Date.now() - (30 * 24 * 60 * 60 * 1000)) {
touchTooltipEl.find('.time').text(ds + ' ' + date.getDate());
} else {
touchTooltipEl.find('.time').text(ds + ' ' + date.getFullYear());
}
callback();
});
}
function handleKeys() {
if (!config.usePagination) {
$(window).off('keydown', onKeyDown).on('keydown', onKeyDown);
}
}
function onKeyDown(ev) {
if (ev.target.nodeName === 'BODY') {
if (ev.shiftKey || ev.ctrlKey || ev.altKey) {
return;
}
if (ev.which === 36 && navigator.toTop) { // home key
navigator.toTop();
return false;
} else if (ev.which === 35 && navigator.toBottom) { // end key
navigator.toBottom();
return false;
}
}
}
function generateUrl(index) {
var pathname = window.location.pathname.replace(config.relative_path, '');
var parts = pathname.split('/');
return parts[1] + '/' + parts[2] + '/' + parts[3] + (index ? '/' + index : '');
}
navigator.setCount = function (value) {
count = parseInt(value, 10);
navigator.updateTextAndProgressBar();
};
navigator.show = function () {
toggle(true);
};
navigator.disable = function () {
count = 0;
index = 1;
navigator.callback = null;
navigator.selector = null;
$(window).off('scroll', navigator.delayedUpdate);
toggle(false);
};
function toggle(flag) {
var path = ajaxify.removeRelativePath(window.location.pathname.slice(1));
if (flag && (!path.startsWith('topic') && !path.startsWith('category'))) {
return;
}
paginationBlockEl.toggleClass('ready', flag);
}
navigator.delayedUpdate = function () {
if (!navigatorUpdateTimeoutId) {
navigatorUpdateTimeoutId = setTimeout(function () {
navigator.update();
navigatorUpdateTimeoutId = undefined;
}, 100);
}
};
navigator.update = function (threshold) {
/*
The "threshold" is defined as the distance from the top of the page to
a spot where a user is expecting to begin reading.
*/
threshold = typeof threshold === 'number' ? threshold : undefined;
var els = $(navigator.selector);
if (els.length) {
index = parseInt(els.first().attr('data-index'), 10) + 1;
}
var scrollTop = $(window).scrollTop();
var windowHeight = $(window).height();
var documentHeight = $(document).height();
var middleOfViewport = scrollTop + (windowHeight / 2);
var previousDistance = Number.MAX_VALUE;
els.each(function () {
var elIndex = parseInt($(this).attr('data-index'), 10);
if (elIndex >= 0) {
var distanceToMiddle = Math.abs(middleOfViewport - $(this).offset().top);
if (distanceToMiddle > previousDistance) {
return false;
}
if (distanceToMiddle < previousDistance) {
index = elIndex + 1;
previousDistance = distanceToMiddle;
}
}
});
var atTop = scrollTop === 0 && parseInt(els.first().attr('data-index'), 10) === 0;
var nearBottom = scrollTop + windowHeight > documentHeight - 100 && parseInt(els.last().attr('data-index'), 10) === count - 1;
if (atTop) {
index = 1;
} else if (nearBottom) {
index = count;
}
// If a threshold is undefined, try to determine one based on new index
if (threshold === undefined && ajaxify.data.template.topic === true) {
if (atTop) {
threshold = 0;
} else {
var anchorEl = components.get('post/anchor', index - 1);
if (anchorEl.length) {
var anchorRect = anchorEl.get(0).getBoundingClientRect();
threshold = anchorRect.top;
}
}
}
if (typeof navigator.callback === 'function') {
navigator.callback(index, count, threshold);
}
navigator.updateTextAndProgressBar();
toggle(!!count);
};
navigator.updateTextAndProgressBar = function () {
if (!utils.isNumber(index)) {
return;
}
index = index > count ? count : index;
paginationTextEl.translateHtml('[[global:pagination.out_of, ' + index + ', ' + count + ']]');
var fraction = (index - 1) / (count - 1 || 1);
paginationBlockMeterEl.val(fraction);
paginationBlockProgressEl.width((fraction * 100) + '%');
};
navigator.scrollUp = function () {
var $window = $(window);
if (config.usePagination) {
var atTop = $window.scrollTop() <= 0;
if (atTop) {
return pagination.previousPage(function () {
$('body,html').scrollTop($(document).height() - $window.height());
});
}
}
$('body,html').animate({
scrollTop: $window.scrollTop() - $window.height(),
});
};
navigator.scrollDown = function () {
var $window = $(window);
if (config.usePagination) {
var atBottom = $window.scrollTop() >= $(document).height() - $window.height();
if (atBottom) {
return pagination.nextPage();
}
}
$('body,html').animate({
scrollTop: $window.scrollTop() + $window.height(),
});
};
navigator.scrollTop = function (index) {
if ($(navigator.selector + '[data-index="' + index + '"]').length) {
navigator.scrollToIndex(index, true);
} else {
ajaxify.go(generateUrl());
}
};
navigator.scrollBottom = function (index) {
if (parseInt(index, 10) < 0) {
return;
}
if ($(navigator.selector + '[data-index="' + index + '"]').length) {
navigator.scrollToIndex(index, true);
} else {
index = parseInt(index, 10) + 1;
ajaxify.go(generateUrl(index));
}
};
navigator.scrollToIndex = function (index, highlight, duration) {
var inTopic = !!components.get('topic').length;
var inCategory = !!components.get('category').length;
if (!utils.isNumber(index) || (!inTopic && !inCategory)) {
return;
}
duration = duration !== undefined ? duration : 400;
navigator.scrollActive = true;
// if in topic and item already on page
if (inTopic && components.get('post/anchor', index).length) {
return navigator.scrollToPostIndex(index, highlight, duration);
}
// if in category and item alreay on page
if (inCategory && $('[component="category/topic"][data-index="' + index + '"]').length) {
return navigator.scrollToTopicIndex(index, highlight, duration);
}
if (!config.usePagination) {
navigator.scrollActive = false;
index = parseInt(index, 10) + 1;
ajaxify.go(generateUrl(index));
return;
}
var scrollMethod = inTopic ? navigator.scrollToPostIndex : navigator.scrollToTopicIndex;
var page = 1 + Math.floor(index / config.postsPerPage);
if (parseInt(page, 10) !== ajaxify.data.pagination.currentPage) {
pagination.loadPage(page, function () {
scrollMethod(index, highlight, duration);
});
} else {
scrollMethod(index, highlight, duration);
}
};
navigator.scrollToPostIndex = function (postIndex, highlight, duration) {
var scrollTo = components.get('post', 'index', postIndex);
navigator.scrollToElement(scrollTo, highlight, duration);
};
navigator.scrollToTopicIndex = function (topicIndex, highlight, duration) {
var scrollTo = $('[component="category/topic"][data-index="' + topicIndex + '"]');
navigator.scrollToElement(scrollTo, highlight, duration);
};
navigator.scrollToElement = function (scrollTo, highlight, duration) {
if (!scrollTo.length) {
navigator.scrollActive = false;
return;
}
var postHeight = scrollTo.height();
var viewportHeight = $(window).height();
var navbarHeight = components.get('navbar').height();
// Temporarily disable navigator update on scroll
$(window).off('scroll', navigator.update);
duration = duration !== undefined ? duration : 400;
navigator.scrollActive = true;
var done = false;
function animateScroll() {
function onAnimateComplete() {
if (done) {
// Re-enable onScroll behaviour
$(window).on('scroll', navigator.update);
var scrollToRect = scrollTo.get(0).getBoundingClientRect();
navigator.update(scrollToRect.top);
return;
}
done = true;
navigator.scrollActive = false;
highlightPost();
$('body').scrollTop($('body').scrollTop() - 1);
$('html').scrollTop($('html').scrollTop() - 1);
}
var scrollTop = 0;
if (postHeight < viewportHeight) {
scrollTop = (scrollTo.offset().top - (viewportHeight / 2) + (postHeight / 2));
} else {
scrollTop = scrollTo.offset().top - navbarHeight;
}
if (duration === 0) {
$(window).scrollTop(scrollTop);
return onAnimateComplete();
}
$('html, body').animate({
scrollTop: scrollTop + 'px',
}, duration, onAnimateComplete);
}
function highlightPost() {
if (highlight) {
scrollTo.addClass('highlight');
setTimeout(function () {
scrollTo.removeClass('highlight');
}, 10000);
}
}
animateScroll();
};
return navigator;
});