From 736db4745755736f059bcbc22dd7d29c546b059a Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Thu, 3 Feb 2022 07:46:12 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A1=B5=E9=9D=A2=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/src/ajaxify.js | 48 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/public/src/ajaxify.js b/public/src/ajaxify.js index 26576dabbd..ec8b3fc1c6 100644 --- a/public/src/ajaxify.js +++ b/public/src/ajaxify.js @@ -10,6 +10,7 @@ ajaxify = window.ajaxify || {}; var retry = true; var previousBodyClass = ''; + ajaxify.loading = false; ajaxify.count = 0; ajaxify.currentPage = null; @@ -18,7 +19,26 @@ ajaxify = window.ajaxify || {}; 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.loading = true; + // Automatically reconnect to socket and re-ajaxify on success if (!socket.connected) { app.reconnect(); @@ -35,11 +55,13 @@ ajaxify = window.ajaxify || {}; // Abort subsequent requests if clicked multiple times within a short window of time if (ajaxifyTimer && (Date.now() - ajaxifyTimer) < 500) { + startThrottle(url, callback, quiet); return true; } ajaxifyTimer = Date.now(); if (ajaxify.handleRedirects(url)) { + ajaxify.loading = false; 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 (url === null) { hooks.fire('action:ajaxify.end', { url: url, tpl_url: ajaxify.data.template.name, title: ajaxify.data.title }); + ajaxify.loading = false; return false; } @@ -73,6 +96,7 @@ ajaxify = window.ajaxify || {}; (parseInt(err.data.status, 10) !== 302 && parseInt(err.data.status, 10) !== 308) )) { ajaxify.updateHistory(url, quiet); + ajaxify.loading = false; } if (err) { @@ -138,6 +162,8 @@ ajaxify = window.ajaxify || {}; var data = err.data; var textStatus = err.textStatus; + ajaxify.loading = false; + if (data) { var status = parseInt(data.status, 10); 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 }); 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 () { @@ -439,7 +477,7 @@ ajaxify = window.ajaxify || {}; }); }()); -$(document).ready(function () { +$(function () { var hooks; require(['hooks'], function (_hooks) { hooks = _hooks; @@ -490,8 +528,12 @@ $(document).ready(function () { // Special handling for urls with hashes if (window.location.pathname === this.pathname && this.hash.length) { window.location.hash = this.hash; - } else if (ajaxify.go(pathname)) { - e.preventDefault(); + } else { + 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') { if (config.openOutgoingLinksInNewTab && $.contains(contentEl, this)) {