Permalink
Browse files

BUG More robust url comparison in CMS

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...
1 parent 701da8b commit f79d2df1048ad086a8796b9fbe27d23d54345d29 @chillu chillu committed Aug 15, 2012
Showing with 25 additions and 9 deletions.
  1. +25 −9 admin/javascript/LeftAndMain.js
View
34 admin/javascript/LeftAndMain.js
@@ -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);
}
@@ -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;
}
},

0 comments on commit f79d2df

Please sign in to comment.