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
*/
function ajaxAbort() {
ajaxRequest.aborted = true;
ajaxRequest.onreadystatechange = null;
if (ajaxRequest.abort) {
ajaxRequest.abort();
}
@ -203,7 +203,6 @@ function ajaxAbort() {
* @param KeyboardEvent
* @param [string]
* @return boolean
* @uses ajaxRequest
*/
function bodyKeydown(event, button) {
var target = event.target || event.srcElement;
@ -321,7 +320,6 @@ function replaceFavicon(href) {
}
}
var ajaxState = 0;
var ajaxRequest = {};
/** Safely load content to #content
@ -330,14 +328,13 @@ var ajaxRequest = {};
* @param [boolean]
* @param [boolean]
* @return XMLHttpRequest or false in case of an error
* @uses ajaxState, ajaxRequest
* @uses ajaxRequest
*/
function ajaxSend(url, data, popState, noscroll) {
if (!history.pushState) {
return false;
}
ajaxAbort();
var currentState = ++ajaxState;
onblur = function () {
if (!originalFavicon) {
originalFavicon = (document.getElementById('favicon') || {}).href;
@ -346,7 +343,6 @@ function ajaxSend(url, data, popState, noscroll) {
};
document.body.className += ' loading';
ajaxRequest = ajax(url, function (request) {
if (!request.aborted && currentState == ajaxState) {
var title = request.getResponseHeader('X-AJAX-Title');
if (title) {
document.title = decodeURIComponent(title);
@ -393,20 +389,19 @@ function ajaxSend(url, data, popState, noscroll) {
if (window.jush) {
jush.highlight_tag('code', 0);
}
}
}, data);
return ajaxRequest;
}
/** Revive page from history
* @param PopStateEvent|history
* @uses ajaxState
* @uses ajaxRequest
*/
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
} else {
ajaxState++;
ajaxRequest.send = true; // to enable AJAX for next call of this function
}
};