mirror of
https://github.com/nextcloud/server.git
synced 2026-06-08 16:26:59 -04:00
chore: drop duplicated legacy Ajax error handling
The XHR error handling was duplicated, as we already handle this in `core/src/init.js` where we intercept Fetch API and XHR API. So we can drop this jQuery specific duplicate. Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
This commit is contained in:
parent
6b6fdc2bbf
commit
a3bd996e7c
3 changed files with 3 additions and 142 deletions
|
|
@ -73,11 +73,6 @@ import Settings from './settings.js'
|
|||
import { theme } from './theme.js'
|
||||
import Util from './util.js'
|
||||
import webroot from './webroot.js'
|
||||
import {
|
||||
ajaxConnectionLostHandler,
|
||||
processAjaxError,
|
||||
registerXHRForErrorProcessing,
|
||||
} from './xhr-error.js'
|
||||
|
||||
/** @namespace OC */
|
||||
export default {
|
||||
|
|
@ -125,13 +120,11 @@ export default {
|
|||
L10N,
|
||||
|
||||
/**
|
||||
* Ajax error handlers
|
||||
* This is already handled by `interceptRequests` in `core/src/init.js`.
|
||||
*
|
||||
* @todo remove from here and keep internally -> requires new tests
|
||||
* @deprecated 33.0.0 - unused by Nextcloud and only a stub remains. Just remove usage.
|
||||
*/
|
||||
_ajaxConnectionLostHandler: ajaxConnectionLostHandler,
|
||||
_processAjaxError: processAjaxError,
|
||||
registerXHRForErrorProcessing,
|
||||
registerXHRForErrorProcessing: () => {},
|
||||
|
||||
/**
|
||||
* Capabilities
|
||||
|
|
|
|||
|
|
@ -1,102 +0,0 @@
|
|||
/**
|
||||
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
import { getCurrentUser } from '@nextcloud/auth'
|
||||
import { showWarning } from '@nextcloud/dialogs'
|
||||
import $ from 'jquery'
|
||||
import _ from 'underscore'
|
||||
import OC from './index.js'
|
||||
import Notification from './notification.js'
|
||||
|
||||
/**
|
||||
* Warn users that the connection to the server was lost temporarily
|
||||
*
|
||||
* This function is throttled to prevent stacked notifications.
|
||||
* After 7sec the first notification is gone, then we can show another one
|
||||
* if necessary.
|
||||
*/
|
||||
export const ajaxConnectionLostHandler = _.throttle(() => {
|
||||
showWarning(t('core', 'Connection to server lost'))
|
||||
}, 7 * 1000, { trailing: false })
|
||||
|
||||
/**
|
||||
* Process ajax error, redirects to main page
|
||||
* if an error/auth error status was returned.
|
||||
*
|
||||
* @param {XMLHttpRequest} xhr xhr request
|
||||
*/
|
||||
export function processAjaxError(xhr) {
|
||||
// purposefully aborted request ?
|
||||
// OC._userIsNavigatingAway needed to distinguish Ajax calls cancelled by navigating away
|
||||
// from calls cancelled by failed cross-domain Ajax due to SSO redirect
|
||||
if (xhr.status === 0 && (xhr.statusText === 'abort' || xhr.statusText === 'timeout' || OC._reloadCalled)) {
|
||||
return
|
||||
}
|
||||
|
||||
if ([302, 303, 307, 401].includes(xhr.status) && getCurrentUser()) {
|
||||
// sometimes "beforeunload" happens later, so need to defer the reload a bit
|
||||
setTimeout(function() {
|
||||
if (!OC._userIsNavigatingAway && !OC._reloadCalled) {
|
||||
let timer = 0
|
||||
const seconds = 5
|
||||
const interval = setInterval(
|
||||
function() {
|
||||
Notification.showUpdate(n('core', 'Problem loading page, reloading in %n second', 'Problem loading page, reloading in %n seconds', seconds - timer))
|
||||
if (timer >= seconds) {
|
||||
clearInterval(interval)
|
||||
window.location.reload()
|
||||
}
|
||||
timer++
|
||||
},
|
||||
1000, // 1 second interval
|
||||
)
|
||||
|
||||
// only call reload once
|
||||
OC._reloadCalled = true
|
||||
}
|
||||
}, 100)
|
||||
} else if (xhr.status === 0) {
|
||||
// Connection lost (e.g. WiFi disconnected or server is down)
|
||||
setTimeout(function() {
|
||||
if (!OC._userIsNavigatingAway && !OC._reloadCalled) {
|
||||
// TODO: call method above directly
|
||||
OC._ajaxConnectionLostHandler()
|
||||
}
|
||||
}, 100)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers XmlHttpRequest object for global error processing.
|
||||
*
|
||||
* This means that if this XHR object returns 401 or session timeout errors,
|
||||
* the current page will automatically be reloaded.
|
||||
*
|
||||
* @param {XMLHttpRequest} xhr xhr request
|
||||
*/
|
||||
export function registerXHRForErrorProcessing(xhr) {
|
||||
const loadCallback = () => {
|
||||
if (xhr.readyState !== 4) {
|
||||
return
|
||||
}
|
||||
|
||||
if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
|
||||
return
|
||||
}
|
||||
|
||||
// fire jquery global ajax error handler
|
||||
$(document).trigger(new $.Event('ajaxError'), xhr)
|
||||
}
|
||||
|
||||
const errorCallback = () => {
|
||||
// fire jquery global ajax error handler
|
||||
$(document).trigger(new $.Event('ajaxError'), xhr)
|
||||
}
|
||||
|
||||
if (xhr.addEventListener) {
|
||||
xhr.addEventListener('load', loadCallback)
|
||||
xhr.addEventListener('error', errorCallback)
|
||||
}
|
||||
}
|
||||
|
|
@ -67,36 +67,6 @@ export function initCore() {
|
|||
interceptRequests()
|
||||
initFallbackClipboardAPI()
|
||||
|
||||
$(window).on('unload.main', () => {
|
||||
OC._unloadCalled = true
|
||||
})
|
||||
$(window).on('beforeunload.main', () => {
|
||||
// super-trick thanks to http://stackoverflow.com/a/4651049
|
||||
// in case another handler displays a confirmation dialog (ex: navigating away
|
||||
// during an upload), there are two possible outcomes: user clicked "ok" or
|
||||
// "cancel"
|
||||
|
||||
// first timeout handler is called after unload dialog is closed
|
||||
setTimeout(() => {
|
||||
OC._userIsNavigatingAway = true
|
||||
|
||||
// second timeout event is only called if user cancelled (Chrome),
|
||||
// but in other browsers it might still be triggered, so need to
|
||||
// set a higher delay...
|
||||
setTimeout(() => {
|
||||
if (!OC._unloadCalled) {
|
||||
OC._userIsNavigatingAway = false
|
||||
}
|
||||
}, 10000)
|
||||
}, 1)
|
||||
})
|
||||
$(document).on('ajaxError.main', function(event, request, settings) {
|
||||
if (settings && settings.allowAuthErrors) {
|
||||
return
|
||||
}
|
||||
OC._processAjaxError(request)
|
||||
})
|
||||
|
||||
initSessionHeartBeat()
|
||||
|
||||
OC.registerMenu($('#expand'), $('#expanddiv'), false, true)
|
||||
|
|
|
|||
Loading…
Reference in a new issue