修复页面切换问题

v1.18.x
落雨楓 3 years ago
parent 04409632dd
commit 736db47457

@ -10,6 +10,7 @@ ajaxify = window.ajaxify || {};
var retry = true; var retry = true;
var previousBodyClass = ''; var previousBodyClass = '';
ajaxify.loading = false;
ajaxify.count = 0; ajaxify.count = 0;
ajaxify.currentPage = null; ajaxify.currentPage = null;
@ -18,7 +19,26 @@ ajaxify = window.ajaxify || {};
hooks = _hooks; hooks = _hooks;
}); });
var _throttleTimer = null;
var _cachedRequest = null;
function startThrottle(...args) {
if (typeof args[0] === "string") {
_cachedRequest = args;
}
if (_cachedRequest && !_throttleTimer) {
_throttleTimer = setTimeout(() => {
if (_cachedRequest) {
ajaxify.go.apply(ajaxify, _cachedRequest);
_cachedRequest = null;
}
_throttleTimer = null;
}, 500);
}
}
ajaxify.go = function (url, callback, quiet) { ajaxify.go = function (url, callback, quiet) {
ajaxify.loading = true;
// Automatically reconnect to socket and re-ajaxify on success // Automatically reconnect to socket and re-ajaxify on success
if (!socket.connected) { if (!socket.connected) {
app.reconnect(); app.reconnect();
@ -35,11 +55,13 @@ ajaxify = window.ajaxify || {};
// Abort subsequent requests if clicked multiple times within a short window of time // Abort subsequent requests if clicked multiple times within a short window of time
if (ajaxifyTimer && (Date.now() - ajaxifyTimer) < 500) { if (ajaxifyTimer && (Date.now() - ajaxifyTimer) < 500) {
startThrottle(url, callback, quiet);
return true; return true;
} }
ajaxifyTimer = Date.now(); ajaxifyTimer = Date.now();
if (ajaxify.handleRedirects(url)) { if (ajaxify.handleRedirects(url)) {
ajaxify.loading = false;
return true; return true;
} }
@ -60,6 +82,7 @@ ajaxify = window.ajaxify || {};
// If any listeners alter url and set it to an empty string, abort the ajaxification // If any listeners alter url and set it to an empty string, abort the ajaxification
if (url === null) { if (url === null) {
hooks.fire('action:ajaxify.end', { url: url, tpl_url: ajaxify.data.template.name, title: ajaxify.data.title }); hooks.fire('action:ajaxify.end', { url: url, tpl_url: ajaxify.data.template.name, title: ajaxify.data.title });
ajaxify.loading = false;
return false; return false;
} }
@ -73,6 +96,7 @@ ajaxify = window.ajaxify || {};
(parseInt(err.data.status, 10) !== 302 && parseInt(err.data.status, 10) !== 308) (parseInt(err.data.status, 10) !== 302 && parseInt(err.data.status, 10) !== 308)
)) { )) {
ajaxify.updateHistory(url, quiet); ajaxify.updateHistory(url, quiet);
ajaxify.loading = false;
} }
if (err) { if (err) {
@ -138,6 +162,8 @@ ajaxify = window.ajaxify || {};
var data = err.data; var data = err.data;
var textStatus = err.textStatus; var textStatus = err.textStatus;
ajaxify.loading = false;
if (data) { if (data) {
var status = parseInt(data.status, 10); var status = parseInt(data.status, 10);
if (status === 403 || status === 404 || status === 500 || status === 502 || status === 503) { if (status === 403 || status === 404 || status === 500 || status === 502 || status === 503) {
@ -302,6 +328,18 @@ ajaxify = window.ajaxify || {};
hooks.fire('action:ajaxify.contentLoaded', { url: url, tpl: tpl_url }); hooks.fire('action:ajaxify.contentLoaded', { url: url, tpl: tpl_url });
app.processPage(); app.processPage();
ajaxify.loading = false;
if (_cachedRequest) {
if (_throttleTimer) {
clearTimeout(_throttleTimer);
_throttleTimer = null;
}
ajaxifyTimer = 0;
ajaxify.go(_cachedRequest[0], _cachedRequest[1], true);
_cachedRequest = null;
}
}; };
ajaxify.parseData = function () { ajaxify.parseData = function () {
@ -439,7 +477,7 @@ ajaxify = window.ajaxify || {};
}); });
}()); }());
$(document).ready(function () { $(function () {
var hooks; var hooks;
require(['hooks'], function (_hooks) { require(['hooks'], function (_hooks) {
hooks = _hooks; hooks = _hooks;
@ -490,8 +528,12 @@ $(document).ready(function () {
// Special handling for urls with hashes // Special handling for urls with hashes
if (window.location.pathname === this.pathname && this.hash.length) { if (window.location.pathname === this.pathname && this.hash.length) {
window.location.hash = this.hash; window.location.hash = this.hash;
} else if (ajaxify.go(pathname)) { } else {
e.preventDefault(); var isReplaceLink = $this.parent('li').parent('.nav.nav-pills').length > 0 ||
$this.parent('li').parent('.pagination').length > 0
if (ajaxify.go(pathname, null, isReplaceLink)) {
e.preventDefault();
}
} }
} else if (window.location.pathname !== config.relative_path + '/outgoing') { } else if (window.location.pathname !== config.relative_path + '/outgoing') {
if (config.openOutgoingLinksInNewTab && $.contains(contentEl, this)) { if (config.openOutgoingLinksInNewTab && $.contains(contentEl, this)) {

Loading…
Cancel
Save