Skip to content
This repository
Browse code

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...
commit f79d2df1048ad086a8796b9fbe27d23d54345d29 1 parent 701da8b
Ingo Schommer authored August 15, 2012

Showing 1 changed file with 25 additions and 9 deletions. Show diff stats Hide diff stats

  1. 34  admin/javascript/LeftAndMain.js
34  admin/javascript/LeftAndMain.js
@@ -32,6 +32,22 @@ jQuery.noConflict();
32 32
 				setTimeout(function() {applyChosen(el);},500);
33 33
 			}
34 34
 		};
  35
+
  36
+		/**
  37
+		 * Compare URLs, but normalize trailing slashes in 
  38
+		 * URL to work around routing weirdnesses in SS_HTTPRequest.
  39
+		 * Also normalizes relative URLs by prefixing them with the <base>.
  40
+		 */
  41
+		var isSameUrl = function(url1, url2) {
  42
+			var baseUrl = $('base').attr('href');
  43
+			url1 = $.path.isAbsoluteUrl(url1) ? url1 : $.path.makeUrlAbsolute(url1, baseUrl),
  44
+			url2 = $.path.isAbsoluteUrl(url2) ? url2 : $.path.makeUrlAbsolute(url2, baseUrl);
  45
+			var url1parts = $.path.parseUrl(url1), url2parts = $.path.parseUrl(url2);
  46
+			return (
  47
+				url1parts.pathname.replace(/\/*$/, '') == url2parts.pathname.replace(/\/*$/, '') && 
  48
+				url1parts.search == url2parts.search
  49
+			);
  50
+		};
35 51
 		
36 52
 		$(window).bind('resize', positionLoadingSpinner).trigger('resize');
37 53
 
@@ -39,10 +55,12 @@ jQuery.noConflict();
39 55
 		$(document).ajaxComplete(function(e, xhr, settings) {
40 56
 			// Simulates a redirect on an ajax response.
41 57
 			if(window.History.enabled) {
42  
-				var url = xhr.getResponseHeader('X-ControllerURL'), opts, requestHeaders = settings.headers;
43  
-				// Normalize trailing slashes in URL to work around routing weirdnesses in SS_HTTPRequest.
44  
-				var isSame = (url && History.getPageUrl().replace(/\/+$/, '') == url.replace(/\/+$/, ''));
45  
-				if(url && !isSame) {
  58
+				var url = xhr.getResponseHeader('X-ControllerURL'), 
  59
+					// TODO Replaces trailing slashes added by History after locale (e.g. admin/?locale=en/)
  60
+					origUrl = History.getPageUrl().replace(/\/$/, ''),
  61
+					opts, requestHeaders = settings.headers;
  62
+
  63
+				if(url !== null && !isSameUrl(origUrl, url)) {
46 64
 					opts = {pjax: xhr.getResponseHeader('X-Pjax') ? xhr.getResponseHeader('X-Pjax') : settings.headers['X-Pjax']};
47 65
 					window.History.pushState(opts, '', url);
48 66
 				}
@@ -887,11 +905,9 @@ jQuery.noConflict();
887 905
 					ajaxOptions: {
888 906
 						// Overwrite ajax loading to use CMS logic instead
889 907
 						beforeSend: function(xhr, settings) {
890  
-							var makeAbs = $.path.makeUrlAbsolute,
891  
-								baseUrl = $('base').attr('href'),
892  
-								isSame = (makeAbs(settings.url, baseUrl) == makeAbs(document.location.href));
893  
-								
894  
-							if(!isSame) $('.cms-container').loadPanel(settings.url);
  908
+							if(!isSameUrl(document.location.href, settings.url)) {
  909
+								$('.cms-container').loadPanel(settings.url);
  910
+							}
895 911
 							return false;
896 912
 						}
897 913
 					},

0 notes on commit f79d2df

Please sign in to comment.
Something went wrong with that request. Please try again.