Skip to content

Commit

Permalink
BUG More robust url comparison in CMS
Browse files Browse the repository at this point in the history
Avoid ajax reloads when URLs differ in irrelevant ways,
e.g. admin/?locale=de vs. admin?locale=de.
This caused problems with the translatable module
reloading page content where it didn't need to,
because some previous robustness patches to the same problem
didn't apply (they only removed trailing slashes, ignoring
query parameter strings). The visible problem for this was
a broken tree panel, because it was ajax-loaded in parallel
with its container. Depending on ajax response order,
this would break the inner panel (in this case the tree),
since its original container was replaced with a new DOM.
  • Loading branch information
chillu committed Aug 15, 2012
1 parent 701da8b commit f79d2df
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions admin/javascript/LeftAndMain.js
Expand Up @@ -32,17 +32,35 @@ jQuery.noConflict();
setTimeout(function() {applyChosen(el);},500);
}
};

/**
* Compare URLs, but normalize trailing slashes in
* URL to work around routing weirdnesses in SS_HTTPRequest.
* Also normalizes relative URLs by prefixing them with the <base>.
*/
var isSameUrl = function(url1, url2) {
var baseUrl = $('base').attr('href');
url1 = $.path.isAbsoluteUrl(url1) ? url1 : $.path.makeUrlAbsolute(url1, baseUrl),
url2 = $.path.isAbsoluteUrl(url2) ? url2 : $.path.makeUrlAbsolute(url2, baseUrl);
var url1parts = $.path.parseUrl(url1), url2parts = $.path.parseUrl(url2);
return (
url1parts.pathname.replace(/\/*$/, '') == url2parts.pathname.replace(/\/*$/, '') &&
url1parts.search == url2parts.search
);
};

$(window).bind('resize', positionLoadingSpinner).trigger('resize');

// global ajax handlers
$(document).ajaxComplete(function(e, xhr, settings) {
// Simulates a redirect on an ajax response.
if(window.History.enabled) {
var url = xhr.getResponseHeader('X-ControllerURL'), opts, requestHeaders = settings.headers;
// Normalize trailing slashes in URL to work around routing weirdnesses in SS_HTTPRequest.
var isSame = (url && History.getPageUrl().replace(/\/+$/, '') == url.replace(/\/+$/, ''));
if(url && !isSame) {
var url = xhr.getResponseHeader('X-ControllerURL'),
// TODO Replaces trailing slashes added by History after locale (e.g. admin/?locale=en/)
origUrl = History.getPageUrl().replace(/\/$/, ''),
opts, requestHeaders = settings.headers;

if(url !== null && !isSameUrl(origUrl, url)) {
opts = {pjax: xhr.getResponseHeader('X-Pjax') ? xhr.getResponseHeader('X-Pjax') : settings.headers['X-Pjax']};
window.History.pushState(opts, '', url);
}
Expand Down Expand Up @@ -887,11 +905,9 @@ jQuery.noConflict();
ajaxOptions: {
// Overwrite ajax loading to use CMS logic instead
beforeSend: function(xhr, settings) {
var makeAbs = $.path.makeUrlAbsolute,
baseUrl = $('base').attr('href'),
isSame = (makeAbs(settings.url, baseUrl) == makeAbs(document.location.href));

if(!isSame) $('.cms-container').loadPanel(settings.url);
if(!isSameUrl(document.location.href, settings.url)) {
$('.cms-container').loadPanel(settings.url);
}
return false;
}
},
Expand Down

0 comments on commit f79d2df

Please sign in to comment.