app/assets/javascripts/materialize/collapsible.js in materialize-sass-0.97.7 vs app/assets/javascripts/materialize/collapsible.js in materialize-sass-0.97.8

- old
+ new

@@ -1,9 +1,11 @@ (function ($) { $.fn.collapsible = function(options) { var defaults = { - accordion: undefined + accordion: undefined, + onOpen: undefined, + onClose: undefined }; options = $.extend(defaults, options); @@ -14,63 +16,93 @@ var $panel_headers = $(this).find('> li > .collapsible-header'); var collapsible_type = $this.data("collapsible"); // Turn off any existing event handlers - $this.off('click.collapse', '> li > .collapsible-header'); - $panel_headers.off('click.collapse'); + $this.off('click.collapse', '> li > .collapsible-header'); + $panel_headers.off('click.collapse'); - /**************** - Helper Functions - ****************/ + /**************** + Helper Functions + ****************/ // Accordion Open function accordionOpen(object) { $panel_headers = $this.find('> li > .collapsible-header'); if (object.hasClass('active')) { - object.parent().addClass('active'); + object.parent().addClass('active'); } else { - object.parent().removeClass('active'); + object.parent().removeClass('active'); } if (object.parent().hasClass('active')){ object.siblings('.collapsible-body').stop(true,false).slideDown({ duration: 350, easing: "easeOutQuart", queue: false, complete: function() {$(this).css('height', '');}}); } else{ object.siblings('.collapsible-body').stop(true,false).slideUp({ duration: 350, easing: "easeOutQuart", queue: false, complete: function() {$(this).css('height', '');}}); } $panel_headers.not(object).removeClass('active').parent().removeClass('active'); - $panel_headers.not(object).parent().children('.collapsible-body').stop(true,false).slideUp( - { - duration: 350, - easing: "easeOutQuart", - queue: false, - complete: - function() { - $(this).css('height', ''); - } - }); + + // Close previously open accordion elements. + $panel_headers.not(object).parent().children('.collapsible-body').stop(true,false).each(function() { + if ($(this).is(':visible')) { + $(this).slideUp({ + duration: 350, + easing: "easeOutQuart", + queue: false, + complete: + function() { + $(this).css('height', ''); + execCallbacks($(this).siblings('.collapsible-header')); + } + }); + } + }); } // Expandable Open function expandableOpen(object) { if (object.hasClass('active')) { - object.parent().addClass('active'); + object.parent().addClass('active'); } else { - object.parent().removeClass('active'); + object.parent().removeClass('active'); } if (object.parent().hasClass('active')){ object.siblings('.collapsible-body').stop(true,false).slideDown({ duration: 350, easing: "easeOutQuart", queue: false, complete: function() {$(this).css('height', '');}}); } - else{ + else { object.siblings('.collapsible-body').stop(true,false).slideUp({ duration: 350, easing: "easeOutQuart", queue: false, complete: function() {$(this).css('height', '');}}); } } + // Open collapsible. object: .collapsible-header + function collapsibleOpen(object) { + if (options.accordion || collapsible_type === "accordion" || collapsible_type === undefined) { // Handle Accordion + accordionOpen(object); + } else { // Handle Expandables + expandableOpen(object); + } + + execCallbacks(object); + } + + // Handle callbacks + function execCallbacks(object) { + if (object.hasClass('active')) { + if (typeof(options.onOpen) === "function") { + options.onOpen.call(this, object.parent()); + } + } else { + if (typeof(options.onClose) === "function") { + options.onClose.call(this, object.parent()); + } + } + } + /** * Check if object is children of panel header * @param {Object} object Jquery object * @return {Boolean} true if it is children */ @@ -95,43 +127,34 @@ // Add click handler to only direct collapsible header children $this.on('click.collapse', '> li > .collapsible-header', function(e) { - var $header = $(this), - element = $(e.target); + var element = $(e.target); if (isChildrenOfPanelHeader(element)) { element = getPanelHeader(element); } element.toggleClass('active'); - if (options.accordion || collapsible_type === "accordion" || collapsible_type === undefined) { // Handle Accordion - accordionOpen(element); - } else { // Handle Expandables - expandableOpen(element); - - if ($header.hasClass('active')) { - expandableOpen($header); - } - } + collapsibleOpen(element); }); + // Open first active - var $panel_headers = $this.find('> li > .collapsible-header'); if (options.accordion || collapsible_type === "accordion" || collapsible_type === undefined) { // Handle Accordion - accordionOpen($panel_headers.filter('.active').first()); - } - else { // Handle Expandables + collapsibleOpen($panel_headers.filter('.active').first()); + + } else { // Handle Expandables $panel_headers.filter('.active').each(function() { - expandableOpen($(this)); + collapsibleOpen($(this)); }); } }); }; - $(document).ready(function(){ + $(document).on('turbolinks:load', function(){ $('.collapsible').collapsible(); }); -}( jQuery )); \ No newline at end of file +}( jQuery ));