Simplify aborting AJAX request

This commit is contained in:
Jakub Vrana 2012-02-29 14:31:35 -08:00
parent 83113cbe67
commit 99a75c3c44

View file

@ -191,7 +191,7 @@ function selectAddRow(field) {
* @uses ajaxRequest * @uses ajaxRequest
*/ */
function ajaxAbort() { function ajaxAbort() {
ajaxRequest.aborted = true; ajaxRequest.onreadystatechange = null;
if (ajaxRequest.abort) { if (ajaxRequest.abort) {
ajaxRequest.abort(); ajaxRequest.abort();
} }
@ -203,7 +203,6 @@ function ajaxAbort() {
* @param KeyboardEvent * @param KeyboardEvent
* @param [string] * @param [string]
* @return boolean * @return boolean
* @uses ajaxRequest
*/ */
function bodyKeydown(event, button) { function bodyKeydown(event, button) {
var target = event.target || event.srcElement; var target = event.target || event.srcElement;
@ -321,7 +320,6 @@ function replaceFavicon(href) {
} }
} }
var ajaxState = 0;
var ajaxRequest = {}; var ajaxRequest = {};
/** Safely load content to #content /** Safely load content to #content
@ -330,14 +328,13 @@ var ajaxRequest = {};
* @param [boolean] * @param [boolean]
* @param [boolean] * @param [boolean]
* @return XMLHttpRequest or false in case of an error * @return XMLHttpRequest or false in case of an error
* @uses ajaxState, ajaxRequest * @uses ajaxRequest
*/ */
function ajaxSend(url, data, popState, noscroll) { function ajaxSend(url, data, popState, noscroll) {
if (!history.pushState) { if (!history.pushState) {
return false; return false;
} }
ajaxAbort(); ajaxAbort();
var currentState = ++ajaxState;
onblur = function () { onblur = function () {
if (!originalFavicon) { if (!originalFavicon) {
originalFavicon = (document.getElementById('favicon') || {}).href; originalFavicon = (document.getElementById('favicon') || {}).href;
@ -346,53 +343,51 @@ function ajaxSend(url, data, popState, noscroll) {
}; };
document.body.className += ' loading'; document.body.className += ' loading';
ajaxRequest = ajax(url, function (request) { ajaxRequest = ajax(url, function (request) {
if (!request.aborted && currentState == ajaxState) { var title = request.getResponseHeader('X-AJAX-Title');
var title = request.getResponseHeader('X-AJAX-Title'); if (title) {
if (title) { document.title = decodeURIComponent(title);
document.title = decodeURIComponent(title); }
} var redirect = request.getResponseHeader('X-AJAX-Redirect');
var redirect = request.getResponseHeader('X-AJAX-Redirect'); if (redirect) {
if (redirect) { return ajaxSend(redirect, '', popState);
return ajaxSend(redirect, '', popState); }
} onblur = function () { };
onblur = function () { }; if (originalFavicon) {
if (originalFavicon) { replaceFavicon(originalFavicon);
replaceFavicon(originalFavicon); }
} if (!popState) {
if (!popState) { if (data || url != location.href) {
if (data || url != location.href) { history.pushState(data, '', url); //! remember window position
history.pushState(data, '', url); //! remember window position
}
}
if (!noscroll && !/&order/.test(url)) {
scrollTo(0, 0);
}
setHtml('content', (request.status ? request.responseText : '<p class="error">' + noResponse));
document.body.className = document.body.className.replace(/ loading/g, '');
var content = document.getElementById('content');
var scripts = content.getElementsByTagName('script');
var length = scripts.length; // required to avoid infinite loop
for (var i=0; i < length; i++) {
var script = document.createElement('script');
script.text = scripts[i].text;
content.appendChild(script);
} }
}
if (!noscroll && !/&order/.test(url)) {
scrollTo(0, 0);
}
setHtml('content', (request.status ? request.responseText : '<p class="error">' + noResponse));
document.body.className = document.body.className.replace(/ loading/g, '');
var content = document.getElementById('content');
var scripts = content.getElementsByTagName('script');
var length = scripts.length; // required to avoid infinite loop
for (var i=0; i < length; i++) {
var script = document.createElement('script');
script.text = scripts[i].text;
content.appendChild(script);
}
var as = document.getElementById('menu').getElementsByTagName('a'); var as = document.getElementById('menu').getElementsByTagName('a');
var href = location.href.replace(/(&(sql=|dump=|(select|table)=[^&]*)).*/, '$1'); var href = location.href.replace(/(&(sql=|dump=|(select|table)=[^&]*)).*/, '$1');
for (var i=0; i < as.length; i++) { for (var i=0; i < as.length; i++) {
as[i].className = (href == as[i].href ? 'active' : ''); as[i].className = (href == as[i].href ? 'active' : '');
} }
var dump = document.getElementById('dump'); var dump = document.getElementById('dump');
if (dump) { if (dump) {
var match = /&(select|table)=([^&]+)/.exec(href); var match = /&(select|table)=([^&]+)/.exec(href);
dump.href = dump.href.replace(/[^=]+$/, '') + (match ? match[2] : ''); dump.href = dump.href.replace(/[^=]+$/, '') + (match ? match[2] : '');
} }
//! modify Change database hidden fields //! modify Change database hidden fields
if (window.jush) { if (window.jush) {
jush.highlight_tag('code', 0); jush.highlight_tag('code', 0);
}
} }
}, data); }, data);
return ajaxRequest; return ajaxRequest;
@ -400,13 +395,13 @@ function ajaxSend(url, data, popState, noscroll) {
/** Revive page from history /** Revive page from history
* @param PopStateEvent|history * @param PopStateEvent|history
* @uses ajaxState * @uses ajaxRequest
*/ */
onpopstate = function (event) { onpopstate = function (event) {
if ((ajaxState || event.state) && !/#/.test(location.href)) { if ((ajaxRequest.send || event.state) && !/#/.test(location.href)) {
ajaxSend(location.href, (event.state && confirm(areYouSure) ? event.state : ''), 1); // 1 - disable pushState ajaxSend(location.href, (event.state && confirm(areYouSure) ? event.state : ''), 1); // 1 - disable pushState
} else { } else {
ajaxState++; ajaxRequest.send = true; // to enable AJAX for next call of this function
} }
}; };