Index: trunk/extensions/SemanticForms/libs/SemanticForms.js |
— | — | @@ -257,8 +257,8 @@ |
258 | 258 | // Register an initialization method |
259 | 259 | // |
260 | 260 | // 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. |
263 | 263 | // |
264 | 264 | // @param initFunction The initialization function. Must take a string (the input's id) and an object as parameters |
265 | 265 | // @param param The parameter object given to the initialization function |
— | — | @@ -589,23 +589,21 @@ |
590 | 590 | return (num_errors == 0); |
591 | 591 | } |
592 | 592 | |
593 | | -function addInstanceEventHandler(templateName, fieldNum) { |
594 | | - return function() { |
595 | | - addInstance('starter_' + templateName, 'main_' + templateName, fieldNum); |
596 | | - } |
597 | | -} |
598 | | - |
599 | 593 | /** |
600 | 594 | * Functions for multiple-instance templates. |
601 | 595 | */ |
602 | 596 | |
603 | | -function addInstance(starter_div_id, main_div_id, tab_index) { |
| 597 | +jQuery.fn.addInstance = function() { |
| 598 | + // Global variable. |
604 | 599 | num_elements++; |
605 | 600 | |
606 | 601 | // 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() |
608 | 605 | .removeClass('multipleTemplateStarter') |
609 | | - .addClass('multipleTemplate') |
| 606 | + .addClass('multipleTemplateInstance') |
| 607 | + .addClass('multipleTemplate') // backwards compatibility |
610 | 608 | .removeAttr("id") |
611 | 609 | .css("display", "block"); |
612 | 610 | |
— | — | @@ -661,18 +659,20 @@ |
662 | 660 | return this.id.replace(/span_/g, 'span_' + num_elements + '_'); |
663 | 661 | }); |
664 | 662 | |
665 | | - // Create remove button |
| 663 | + // Create remove button, and add it to the new instance. |
666 | 664 | var removeButton = jQuery("<input>").attr({ |
667 | 665 | type: 'button', |
668 | 666 | value: sfgRemoveText, |
669 | | - tabIndex: tab_index |
| 667 | + tabIndex: this.attr("tabIndex") |
670 | 668 | }).addClass("remover"); |
671 | 669 | // (class can't be set as an attr() parameter, because it causes |
672 | 670 | // an error in IE.) |
673 | 671 | new_div.append(removeButton); |
674 | 672 | |
675 | 673 | // Add the new instance |
676 | | - jQuery('#' + main_div_id).append(new_div); |
| 674 | + this.closest(".multipleTemplateWrapper") |
| 675 | + .find(".multipleTemplatePlaceholder") |
| 676 | + .append(new_div); |
677 | 677 | |
678 | 678 | // Enable the new remover |
679 | 679 | new_div.find('.remover').click( function() { |
— | — | @@ -689,7 +689,8 @@ |
690 | 690 | ); |
691 | 691 | |
692 | 692 | // 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(); }); |
694 | 695 | }); |
695 | 696 | |
696 | 697 | // Enable autocompletion |
— | — | @@ -767,7 +768,8 @@ |
768 | 769 | |
769 | 770 | jQuery(".remover").click( function() { |
770 | 771 | // 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(); }); |
772 | 774 | }); |
773 | 775 | jQuery(".autocompleteInput").attachAutocomplete(); |
774 | 776 | jQuery(".sfComboBox").combobox(); |
— | — | @@ -783,15 +785,7 @@ |
784 | 786 | 'overlayOpacity' : '0.8' |
785 | 787 | }); |
786 | 788 | |
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(); } ); |
796 | 790 | |
797 | 791 | // If the form is submitted, validate everything! |
798 | 792 | jQuery('#sfForm').submit( function() { return validateAll(); } ); |