Index: trunk/extensions/SemanticForms/libs/SemanticForms.js |
— | — | @@ -308,14 +308,24 @@ |
309 | 309 | */ |
310 | 310 | |
311 | 311 | // Display a div that would otherwise be hidden by "show on select". |
312 | | -function showDiv(div_id, instanceWrapperDiv) { |
313 | | - jQuery('[id="' + div_id + '"]', instanceWrapperDiv).find(".hiddenBySF").removeClass('hiddenBySF'); |
314 | | - jQuery('[id="' + div_id + '"]', instanceWrapperDiv).show(); |
| 312 | +function showDiv(div_id, instanceWrapperDiv, speed) { |
| 313 | + var elem = jQuery('[id="' + div_id + '"]', instanceWrapperDiv); |
| 314 | + elem.find(".hiddenBySF").removeClass('hiddenBySF'); |
| 315 | + |
| 316 | + elem.each( function() { |
| 317 | + if ( jQuery(this).css('display') == 'none' ) { |
| 318 | + |
| 319 | + jQuery(this).slideDown(speed, function() { |
| 320 | + jQuery(this).fadeTo(speed,1); |
| 321 | + }); |
| 322 | + |
| 323 | + } |
| 324 | + }); |
315 | 325 | } |
316 | 326 | |
317 | 327 | // Hide a div due to "show on select". The CSS class is there so that SF can |
318 | 328 | // ignore the div's contents when the form is submitted. |
319 | | -function hideDiv(div_id, instanceWrapperDiv) { |
| 329 | +function hideDiv(div_id, instanceWrapperDiv, speed) { |
320 | 330 | // IDs can't contain spaces, and jQuery won't work with such IDs - if |
321 | 331 | // this one has a space, display an alert. |
322 | 332 | if ( div_id.indexOf( ' ' ) > -1 ) { |
— | — | @@ -324,79 +334,114 @@ |
325 | 335 | alert( "Warning: this form has \"show on select\" pointing to an invalid element ID (\"" + div_id + "\") - IDs in HTML cannot contain spaces." ); |
326 | 336 | } |
327 | 337 | |
328 | | - jQuery('[id="' + div_id + '"]', instanceWrapperDiv).find("span, div").addClass('hiddenBySF'); |
329 | | - jQuery('[id="' + div_id + '"]', instanceWrapperDiv).hide(); |
| 338 | + var elem = jQuery('[id="' + div_id + '"]', instanceWrapperDiv); |
| 339 | + elem.find("span, div").addClass('hiddenBySF'); |
| 340 | + |
| 341 | + elem.each( function() { |
| 342 | + if ( jQuery(this).css('display') != 'none' ) { |
| 343 | + |
| 344 | + // if 'display' is not 'hidden', but the element is hidden otherwise |
| 345 | + // (e.g. by having height = 0), just hide it, else animate the hiding |
| 346 | + if ( jQuery(this).is(':hidden') ) { |
| 347 | + jQuery(this).hide(); |
| 348 | + } else { |
| 349 | + jQuery(this).fadeTo(speed, 0, function() { |
| 350 | + jQuery(this).slideUp(speed); |
| 351 | + }); |
| 352 | + } |
| 353 | + } |
| 354 | + }); |
330 | 355 | } |
331 | 356 | |
332 | 357 | // Show this div if the current value is any of the relevant options - |
333 | 358 | // otherwise, hide it. |
334 | | -function showDivIfSelected(options, div_id, inputVal, instanceWrapperDiv) { |
| 359 | +function showDivIfSelected(options, div_id, inputVal, instanceWrapperDiv, initPage) { |
335 | 360 | for ( var i = 0; i < options.length; i++ ) { |
336 | 361 | // If it's a listbox and the user has selected more than one |
337 | 362 | // value, it'll be an array - handle either case. |
338 | 363 | if ((jQuery.isArray(inputVal) && jQuery.inArray(options[i], inputVal) >= 0) || |
339 | 364 | (!jQuery.isArray(inputVal) && (inputVal == options[i]))) { |
340 | | - showDiv(div_id, instanceWrapperDiv); |
| 365 | + showDiv( div_id, instanceWrapperDiv, initPage ? 0 : 'fast' ); |
341 | 366 | return; |
342 | 367 | } |
343 | 368 | } |
344 | | - hideDiv(div_id, instanceWrapperDiv); |
| 369 | + hideDiv(div_id, instanceWrapperDiv, initPage ? 0 : 'fast' ); |
345 | 370 | } |
346 | 371 | |
347 | 372 | // Used for handling 'show on select' for the 'dropdown' and 'listbox' inputs. |
348 | | -jQuery.fn.showIfSelected = function(partOfMultiple) { |
| 373 | +jQuery.fn.showIfSelected = function(initPage) { |
349 | 374 | var inputVal = this.val(); |
350 | 375 | var showOnSelectVals = sfgShowOnSelect[this.attr("id")]; |
351 | | - var instanceWrapperDiv = null; |
| 376 | + |
| 377 | + var instanceWrapperDiv = this.closest('.multipleTemplateInstance'); |
| 378 | + if ( instanceWrapperDiv.length == 0 ) { |
| 379 | + instanceWrapperDiv = null; |
| 380 | + } |
352 | 381 | |
353 | 382 | if ( showOnSelectVals !== undefined ) { |
354 | 383 | for ( var i = 0; i < showOnSelectVals.length; i++ ) { |
355 | 384 | var options = showOnSelectVals[i][0]; |
356 | 385 | var div_id = showOnSelectVals[i][1]; |
357 | | - showDivIfSelected(options, div_id, inputVal, instanceWrapperDiv); |
| 386 | + showDivIfSelected( options, div_id, inputVal, instanceWrapperDiv, initPage ); |
358 | 387 | } |
359 | 388 | } |
| 389 | + |
| 390 | + return this; |
360 | 391 | } |
361 | 392 | |
362 | 393 | // Show this div if any of the relevant selections are checked - |
363 | 394 | // otherwise, hide it. |
364 | | -jQuery.fn.showDivIfChecked = function(options, div_id, instanceWrapperDiv) { |
| 395 | +jQuery.fn.showDivIfChecked = function(options, div_id, instanceWrapperDiv, initPage ) { |
365 | 396 | for ( var i = 0; i < options.length; i++ ) { |
366 | 397 | if (jQuery(this).find('[value="' + options[i] + '"]').is(":checked")) { |
367 | | - showDiv(div_id, instanceWrapperDiv); |
368 | | - return; |
| 398 | + showDiv(div_id, instanceWrapperDiv, initPage ? 0 : 'fast' ); |
| 399 | + return this; |
369 | 400 | } |
370 | 401 | } |
371 | 402 | hideDiv(div_id, instanceWrapperDiv); |
| 403 | + |
| 404 | + return this; |
372 | 405 | } |
373 | 406 | |
374 | 407 | // Used for handling 'show on select' for the 'checkboxes' and 'radiobutton' |
375 | 408 | // inputs. |
376 | | -jQuery.fn.showIfChecked = function(partOfMultiple) { |
| 409 | +jQuery.fn.showIfChecked = function(initPage) { |
377 | 410 | |
378 | 411 | var showOnSelectVals = sfgShowOnSelect[this.attr("id")]; |
379 | | - var instanceWrapperDiv = null; |
380 | 412 | |
| 413 | + var instanceWrapperDiv = this.closest('.multipleTemplateInstance'); |
| 414 | + if ( instanceWrapperDiv.length == 0 ) { |
| 415 | + instanceWrapperDiv = null; |
| 416 | + } |
| 417 | + |
381 | 418 | if ( showOnSelectVals !== undefined ) { |
382 | 419 | for ( var i = 0; i < showOnSelectVals.length; i++ ) { |
383 | 420 | var options = showOnSelectVals[i][0]; |
384 | 421 | var div_id = showOnSelectVals[i][1]; |
385 | | - this.showDivIfChecked(options, div_id, instanceWrapperDiv); |
| 422 | + this.showDivIfChecked(options, div_id, instanceWrapperDiv, initPage ); |
386 | 423 | } |
387 | 424 | } |
| 425 | + |
| 426 | + return this; |
388 | 427 | } |
389 | 428 | |
390 | 429 | // Used for handling 'show on select' for the 'checkbox' input. |
391 | | -jQuery.fn.showIfCheckedCheckbox = function(partOfMultiple) { |
| 430 | +jQuery.fn.showIfCheckedCheckbox = function(initPage) { |
392 | 431 | |
393 | 432 | var div_id = sfgShowOnSelect[this.attr("id")]; |
394 | | - var instanceWrapperDiv = null; |
395 | 433 | |
| 434 | + var instanceWrapperDiv = this.closest('.multipleTemplateInstance'); |
| 435 | + if ( instanceWrapperDiv.length == 0 ) { |
| 436 | + instanceWrapperDiv = null; |
| 437 | + } |
| 438 | + |
396 | 439 | if (jQuery(this).is(":checked")) { |
397 | | - showDiv(div_id, instanceWrapperDiv); |
| 440 | + showDiv(div_id, instanceWrapperDiv, initPage ? 0 : 'fast' ); |
398 | 441 | } else { |
399 | | - hideDiv(div_id, instanceWrapperDiv); |
| 442 | + hideDiv(div_id, instanceWrapperDiv, initPage ? 0 : 'fast' ); |
400 | 443 | } |
| 444 | + |
| 445 | + return this; |
401 | 446 | } |
402 | 447 | |
403 | 448 | /* |
— | — | @@ -615,16 +660,11 @@ |
616 | 661 | .addClass('multipleTemplateInstance') |
617 | 662 | .addClass('multipleTemplate') // backwards compatibility |
618 | 663 | .removeAttr("id") |
619 | | - .css("display", "block"); |
| 664 | + .fadeTo(0,0) |
| 665 | + .slideDown('fast', function() { |
| 666 | + jQuery(this).fadeTo('fast', 1); |
| 667 | + }); |
620 | 668 | |
621 | | - // Add on a new attribute, "origID", representing the ID of all |
622 | | - // HTML elements that had an ID; and delete the actual ID attribute |
623 | | - // of any divs and spans (presumably, these exist only for the |
624 | | - // sake of "show on select"). We do the deletions because no two |
625 | | - // elements on the page are allowed to have the same ID. |
626 | | - new_div.find('[id!=""]').attr('origID', function() { return this.id; }); |
627 | | - new_div.find('div[id!=""], span[id!=""]').removeAttr('id'); |
628 | | - |
629 | 669 | // Make internal ID unique for the relevant form elements, and replace |
630 | 670 | // the [num] index in the element names with an actual unique index |
631 | 671 | new_div.find("input, select, textarea").each( |
— | — | @@ -640,6 +680,12 @@ |
641 | 681 | |
642 | 682 | this.id = this.id.replace(/input_/g, 'input_' + num_elements + '_'); |
643 | 683 | |
| 684 | + // TODO: Data in sfgShowOnSelect should probably be stored in |
| 685 | + // jQuery("#sfForm").data('SemanticForms') |
| 686 | + if ( sfgShowOnSelect[ old_id ] ) { |
| 687 | + sfgShowOnSelect[ this.id ] = sfgShowOnSelect[ old_id ]; |
| 688 | + } |
| 689 | + |
644 | 690 | // register initialization and validation methods for new inputs |
645 | 691 | |
646 | 692 | var sfdata = jQuery("#sfForm").data('SemanticForms'); |
— | — | @@ -703,9 +749,6 @@ |
704 | 750 | } |
705 | 751 | ); |
706 | 752 | |
707 | | - // Remove the encompassing div for this instance. |
708 | | - jQuery(this).closest(".multipleTemplateInstance") |
709 | | - .fadeOut('fast', function() { jQuery(this).remove(); }); |
710 | 753 | }); |
711 | 754 | |
712 | 755 | // Somewhat of a hack - remove the divs that the combobox() call |
— | — | @@ -714,7 +757,7 @@ |
715 | 758 | // that doesn't involve removing and then recreating divs. |
716 | 759 | new_div.find('.sfComboBoxActual').remove(); |
717 | 760 | |
718 | | - new_div.initializeJSElements(true); |
| 761 | + new_div.initializeJSElements(); |
719 | 762 | |
720 | 763 | // Initialize new inputs |
721 | 764 | new_div.find("input, select, textarea").each( |
— | — | @@ -744,33 +787,41 @@ |
745 | 788 | * called for either the entire HTML body, or for a div representing an |
746 | 789 | * instance of a multiple-instance template. |
747 | 790 | */ |
748 | | -jQuery.fn.initializeJSElements = function(partOfMultiple) { |
| 791 | +jQuery.fn.initializeJSElements = function() { |
749 | 792 | this.find(".sfShowIfSelected").each( function() { |
750 | | - jQuery(this).showIfSelected(partOfMultiple); |
751 | | - jQuery(this).change( function() { |
752 | | - jQuery(this).showIfSelected(partOfMultiple); |
| 793 | + jQuery(this) |
| 794 | + .showIfSelected(true) |
| 795 | + .change( function() { |
| 796 | + jQuery(this).showIfSelected(false); |
753 | 797 | }); |
754 | 798 | }); |
755 | 799 | |
756 | 800 | this.find(".sfShowIfChecked").each( function() { |
757 | | - jQuery(this).showIfChecked(partOfMultiple); |
758 | | - jQuery(this).click( function() { |
759 | | - jQuery(this).showIfChecked(partOfMultiple); |
| 801 | + jQuery(this) |
| 802 | + .showIfChecked(true) |
| 803 | + .click( function() { |
| 804 | + jQuery(this).showIfChecked(false); |
760 | 805 | }); |
761 | 806 | }); |
762 | 807 | |
763 | 808 | this.find(".sfShowIfCheckedCheckbox").each( function() { |
764 | | - jQuery(this).showIfCheckedCheckbox(partOfMultiple); |
765 | | - jQuery(this).click( function() { |
766 | | - jQuery(this).showIfCheckedCheckbox(partOfMultiple); |
| 809 | + jQuery(this) |
| 810 | + .showIfCheckedCheckbox(true) |
| 811 | + .click( function() { |
| 812 | + jQuery(this).showIfCheckedCheckbox(false); |
767 | 813 | }); |
768 | 814 | }); |
769 | 815 | |
770 | 816 | this.find(".remover").click( function() { |
771 | 817 | // Remove the encompassing div for this instance. |
772 | 818 | jQuery(this).closest(".multipleTemplateInstance") |
773 | | - .fadeOut('fast', function() { jQuery(this).remove(); }); |
| 819 | + .fadeTo('fast', 0, function() { |
| 820 | + jQuery(this).slideUp('fast', function() { |
| 821 | + jQuery(this).remove(); |
| 822 | + }); |
| 823 | + }); |
774 | 824 | }); |
| 825 | + |
775 | 826 | this.find('.autocompleteInput').attachAutocomplete(); |
776 | 827 | this.find('.sfComboBox').combobox(); |
777 | 828 | this.find('.autoGrow').autoGrow(); |
— | — | @@ -790,10 +841,10 @@ |
791 | 842 | |
792 | 843 | // Once the document has finished loading, set up everything! |
793 | 844 | jQuery(document).ready(function() { |
794 | | - jQuery('body').initializeJSElements(false); |
| 845 | + jQuery('body').initializeJSElements(); |
795 | 846 | |
796 | 847 | jQuery('.multipleTemplateInstance').initializeJSElements(true); |
797 | | - jQuery('.multipleTemplateAdder').click( function() { jQuery(this).addInstance(); } ); |
| 848 | + jQuery('.multipleTemplateAdder').click( function() {jQuery(this).addInstance();} ); |
798 | 849 | jQuery('.multipleTemplateList').sortable({ |
799 | 850 | axis: 'y', |
800 | 851 | handle: '.rearrangerImage' |
— | — | @@ -801,7 +852,7 @@ |
802 | 853 | |
803 | 854 | |
804 | 855 | // If the form is submitted, validate everything! |
805 | | - jQuery('#sfForm').submit( function() { return validateAll(); } ); |
| 856 | + jQuery('#sfForm').submit( function() {return validateAll();} ); |
806 | 857 | }); |
807 | 858 | |
808 | 859 | /* extending jquery functions */ |