r80170 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80169‎ | r80170 | r80171 >
Date:16:03, 13 January 2011
Author:yaron
Status:deferred
Tags:
Comment:
Improved handling of "add another" button for multiple-instance templates: connecting each button to its task is no longer done via a global variable, but instead using jQuery and class names; i.e., the "right" way. Also, added a brief "fade out" animation when instances are removed.
Modified paths:
  • /trunk/extensions/SemanticForms/libs/SemanticForms.js (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/libs/SemanticForms.js
@@ -257,8 +257,8 @@
258258 // Register an initialization method
259259 //
260260 // More than one method may be registered for one input by subsequent calls to
261 -// SemanticForms_registerInputInit. This method also executes the initFunction if the
262 -// element referenced by /this/ is not part of a multipleTemplateStarter.
 261+// SemanticForms_registerInputInit. This method also executes the initFunction
 262+// if the element referenced by /this/ is not part of a multipleTemplateStarter.
263263 //
264264 // @param initFunction The initialization function. Must take a string (the input's id) and an object as parameters
265265 // @param param The parameter object given to the initialization function
@@ -589,23 +589,21 @@
590590 return (num_errors == 0);
591591 }
592592
593 -function addInstanceEventHandler(templateName, fieldNum) {
594 - return function() {
595 - addInstance('starter_' + templateName, 'main_' + templateName, fieldNum);
596 - }
597 -}
598 -
599593 /**
600594 * Functions for multiple-instance templates.
601595 */
602596
603 -function addInstance(starter_div_id, main_div_id, tab_index) {
 597+jQuery.fn.addInstance = function() {
 598+ // Global variable.
604599 num_elements++;
605600
606601 // Create the new instance
607 - var new_div = jQuery('#' + starter_div_id).clone()
 602+ var new_div = this.closest(".multipleTemplateWrapper")
 603+ .find(".multipleTemplateStarter")
 604+ .clone()
608605 .removeClass('multipleTemplateStarter')
609 - .addClass('multipleTemplate')
 606+ .addClass('multipleTemplateInstance')
 607+ .addClass('multipleTemplate') // backwards compatibility
610608 .removeAttr("id")
611609 .css("display", "block");
612610
@@ -661,18 +659,20 @@
662660 return this.id.replace(/span_/g, 'span_' + num_elements + '_');
663661 });
664662
665 - // Create remove button
 663+ // Create remove button, and add it to the new instance.
666664 var removeButton = jQuery("<input>").attr({
667665 type: 'button',
668666 value: sfgRemoveText,
669 - tabIndex: tab_index
 667+ tabIndex: this.attr("tabIndex")
670668 }).addClass("remover");
671669 // (class can't be set as an attr() parameter, because it causes
672670 // an error in IE.)
673671 new_div.append(removeButton);
674672
675673 // Add the new instance
676 - jQuery('#' + main_div_id).append(new_div);
 674+ this.closest(".multipleTemplateWrapper")
 675+ .find(".multipleTemplatePlaceholder")
 676+ .append(new_div);
677677
678678 // Enable the new remover
679679 new_div.find('.remover').click( function() {
@@ -689,7 +689,8 @@
690690 );
691691
692692 // Remove the encompassing div for this instance.
693 - jQuery(this).parents(".multipleTemplate").remove();
 693+ jQuery(this).closest(".multipleTemplateInstance")
 694+ .fadeOut('fast', function() { jQuery(this).remove(); });
694695 });
695696
696697 // Enable autocompletion
@@ -767,7 +768,8 @@
768769
769770 jQuery(".remover").click( function() {
770771 // Remove the encompassing div for this instance.
771 - jQuery(this).parents(".multipleTemplate").remove();
 772+ jQuery(this).closest(".multipleTemplateInstance")
 773+ .fadeOut('fast', function() { jQuery(this).remove(); });
772774 });
773775 jQuery(".autocompleteInput").attachAutocomplete();
774776 jQuery(".sfComboBox").combobox();
@@ -783,15 +785,7 @@
784786 'overlayOpacity' : '0.8'
785787 });
786788
787 - // Could this be done via classes and attributes, instead of a
788 - // global variable?
789 - for (var i in sfgAdderButtons) {
790 - var components = sfgAdderButtons[i].split(',');
791 - adderID = components[0];
792 - templateName = components[1];
793 - fieldNum = components[2];
794 - jQuery('#' + adderID).click( addInstanceEventHandler(templateName, fieldNum) );
795 - }
 789+ jQuery('.multipleTemplateAdder').click( function() { jQuery(this).addInstance(); } );
796790
797791 // If the form is submitted, validate everything!
798792 jQuery('#sfForm').submit( function() { return validateAll(); } );

Status & tagging log