Skip to content
This repository
Browse code

Refactor the linking functionality for the TinyMCE popup.

Functionality that affects the values in the form better fits in
updateFromEditor function, where we expect the form to be modified.
Redraw should only affect visibility parameters.

Also added a more robust reset code, so we can always expect to get at
least a clean form, and re-added missing "target" checkbox.
  • Loading branch information...
commit 29a039929b0505bd560a75048dee1aea550c74df 1 parent eef67b6
Mateusz U authored July 10, 2012

Showing 1 changed file with 17 additions and 19 deletions. Show diff stats Hide diff stats

  1. 36  javascript/HtmlEditorField.js
36  javascript/HtmlEditorField.js
@@ -468,7 +468,6 @@ ss.editorWrappers['default'] = ss.editorWrappers.tinyMCE;
468 468
 		 * which are toggled through a type dropdown. Variations share fields, so there's only one "title" field in the form.
469 469
 		 */
470 470
 		$('form.htmleditorfield-linkform').entwine({
471  
-
472 471
 			// TODO Entwine doesn't respect submits triggered by ENTER key
473 472
 			onsubmit: function(e) {
474 473
 				this.insertLink();
@@ -477,36 +476,27 @@ ss.editorWrappers['default'] = ss.editorWrappers.tinyMCE;
477 476
 			},
478 477
 			resetFields: function() {
479 478
 				this._super();
480  
-				this.find('fieldset :input:not(:radio)').val('').change();
  479
+
  480
+				// Reset the form using a native call. This will also correctly reset checkboxes and radio buttons.
  481
+				this[0].reset();
481 482
 			},
482  
-			redraw: function(setDefaults) {
  483
+			redraw: function() {
483 484
 				this._super();
484 485
 
485 486
 				var linkType = this.find(':input[name=LinkType]:checked').val(), list = ['internal', 'external', 'file', 'email'];
486 487
 
487  
-				// If we haven't selected an existing link, then just make sure we default to "internal" for the link type.
488  
-				if(!linkType) {
489  
-					this.find(':input[name=LinkType]').val(['internal']);
490  
-					linkType = 'internal';
491  
-				}
492  
-
493 488
 				this.addAnchorSelector();
494 489
 
495  
-				// Toggle field visibility and state based on type selection
  490
+				// Toggle field visibility depending on the link type.
496 491
 				this.find('div.content .field').hide();
497 492
 				this.find('.field#LinkType').show();
498 493
 				this.find('.field#' + linkType).show();
499 494
 				if(linkType == 'internal' || linkType == 'anchor') this.find('.field#Anchor').show();
  495
+				if(linkType !== 'email') this.find('.field#TargetBlank').show();
500 496
 				if(linkType == 'anchor') {
501 497
 					this.find('.field#AnchorSelector').show();
502 498
 					this.find('.field#AnchorRefresh').show();
503 499
 				}
504  
-
505  
-				this.find(':input[name=TargetBlank]').attr('disabled', (linkType == 'email'));
506  
-
507  
-				if(typeof setDefaults == 'undefined' || setDefaults) {
508  
-					this.find(':input[name=TargetBlank]').attr('checked', (linkType == 'file'));
509  
-				}
510 500
 			},
511 501
 			insertLink: function() {
512 502
 				var href, target = null, anchor = this.find(':input[name=Anchor]').val();
@@ -614,16 +604,24 @@ ss.editorWrappers['default'] = ss.editorWrappers.tinyMCE;
614 604
 					selector.append($('<option value="'+anchors[j]+'">'+anchors[j]+'</option>'));
615 605
 				}
616 606
 			},
  607
+			/**
  608
+			 * Updates the state of the dialog inputs to match the editor selection.
  609
+			 * If selection does not contain a link, resets the fields.
  610
+			 */
617 611
 			updateFromEditor: function() {
618 612
 				var htmlTagPattern = /<\S[^><]*>/g, fieldName, data = this.getCurrentLink();
619  
-				
  613
+
620 614
 				if(data) {
621 615
 					for(fieldName in data) {
622 616
 						var el = this.find(':input[name=' + fieldName + ']'), selected = data[fieldName];
623 617
 						// Remove html tags in the selected text that occurs on IE browsers
624 618
 						if(typeof(selected) == 'string') selected = selected.replace(htmlTagPattern, ''); 
625  
-						if(el.is(':radio')) {
626  
-							el.val([selected]).change(); // setting as an arry due to jQuery quirks
  619
+
  620
+						// Set values and invoke the triggers (e.g. for TreeDropdownField).
  621
+						if(el.is(':checkbox')) {
  622
+							el.prop('checked', selected).change();
  623
+						} else if(el.is(':radio')) {
  624
+							el.val([selected]).change();
627 625
 						} else {
628 626
 							el.val(selected).change();
629 627
 						}

0 notes on commit 29a0399

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