Skip to content
This repository
Browse code

Fixed preview link ordering in CMS

jQuery seems to order by DOM rather than occurrence of
selectors, which means it always takes the last populated
field (in our case the "live" link over the "draft" link).
  • Loading branch information...
commit 6162ae5e3f6a126cc4689357c6fb0e78f197346b 1 parent b53790e
Ingo Schommer authored August 28, 2012

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

  1. 31  admin/javascript/LeftAndMain.Preview.js
31  admin/javascript/LeftAndMain.Preview.js
@@ -67,10 +67,7 @@
67 67
 				if(this.is('.is-collapsed')) return;
68 68
 
69 69
 				// var url = ui.xmlhttp.getResponseHeader('x-frontend-url');
70  
-				var url = $('.cms-edit-form')
71  
-					.find(':input[name=PreviewURL],:input[name=StageLink],:input[name=LiveLink]')
72  
-					.filter(function() {return $(this).val() !== '';})
73  
-					.val();
  70
+				var url = $('.cms-edit-form').choosePreviewLink();
74 71
 				if(url) {
75 72
 					this.loadUrl(url);
76 73
 					this.unblock();
@@ -297,11 +294,9 @@
297 294
 			onclick: function(e) {
298 295
 				e.preventDefault();
299 296
 				
300  
-				var preview = $('.cms-preview'), 
301  
-					url = $('.cms-edit-form')
302  
-						.find(':input[name=PreviewURL],:input[name=StageLink],:input[name=LiveLink]')
303  
-						.filter(function() {return $(this).val() !== '';})
304  
-						.val();
  297
+				var preview = $('.cms-preview'),
  298
+					url = $('.cms-edit-form').choosePreviewLink();
  299
+					
305 300
 				if(url) {
306 301
 						preview.loadUrl(url);
307 302
 						preview.unblock();
@@ -309,5 +304,23 @@
309 304
 				}
310 305
 			}
311 306
 		});
  307
+
  308
+		$('.cms-edit-form').entwine({
  309
+			/**
  310
+			 * Choose applicable preview link based on form data,
  311
+			 * in a fixed order of priority: The PreviewURL field is used as an override,
  312
+			 * which falls back to stage or live URLs.
  313
+			 *
  314
+			 * @return String Absolute URL
  315
+			 */
  316
+			choosePreviewLink: function() {
  317
+				var self = this, urls = $.map(['PreviewURL', 'StageLink', 'LiveLink'], function(name) {
  318
+					var val = self.find(':input[name=' + name + ']').val();
  319
+					return val ? val : null;
  320
+				});
  321
+				return urls ? urls[0] : false;
  322
+			}
  323
+		});
  324
+
312 325
 	});
313 326
 }(jQuery));

0 notes on commit 6162ae5

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