Home | History | Annotate | Download | only in js
      1 /* =============================================================
      2  * bootstrap-collapse.js v2.0.4
      3  * http://twitter.github.com/bootstrap/javascript.html#collapse
      4  * =============================================================
      5  * Copyright 2012 Twitter, Inc.
      6  *
      7  * Licensed under the Apache License, Version 2.0 (the "License");
      8  * you may not use this file except in compliance with the License.
      9  * You may obtain a copy of the License at
     10  *
     11  * http://www.apache.org/licenses/LICENSE-2.0
     12  *
     13  * Unless required by applicable law or agreed to in writing, software
     14  * distributed under the License is distributed on an "AS IS" BASIS,
     15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16  * See the License for the specific language governing permissions and
     17  * limitations under the License.
     18  * ============================================================ */
     19 
     20 
     21 !function ($) {
     22 
     23   "use strict"; // jshint ;_;
     24 
     25 
     26  /* COLLAPSE PUBLIC CLASS DEFINITION
     27   * ================================ */
     28 
     29   var Collapse = function (element, options) {
     30     this.$element = $(element)
     31     this.options = $.extend({}, $.fn.collapse.defaults, options)
     32 
     33     if (this.options.parent) {
     34       this.$parent = $(this.options.parent)
     35     }
     36 
     37     this.options.toggle && this.toggle()
     38   }
     39 
     40   Collapse.prototype = {
     41 
     42     constructor: Collapse
     43 
     44   , dimension: function () {
     45       var hasWidth = this.$element.hasClass('width')
     46       return hasWidth ? 'width' : 'height'
     47     }
     48 
     49   , show: function () {
     50       var dimension
     51         , scroll
     52         , actives
     53         , hasData
     54 
     55       if (this.transitioning) return
     56 
     57       dimension = this.dimension()
     58       scroll = $.camelCase(['scroll', dimension].join('-'))
     59       actives = this.$parent && this.$parent.find('> .accordion-group > .in')
     60 
     61       if (actives && actives.length) {
     62         hasData = actives.data('collapse')
     63         if (hasData && hasData.transitioning) return
     64         actives.collapse('hide')
     65         hasData || actives.data('collapse', null)
     66       }
     67 
     68       this.$element[dimension](0)
     69       this.transition('addClass', $.Event('show'), 'shown')
     70       this.$element[dimension](this.$element[0][scroll])
     71     }
     72 
     73   , hide: function () {
     74       var dimension
     75       if (this.transitioning) return
     76       dimension = this.dimension()
     77       this.reset(this.$element[dimension]())
     78       this.transition('removeClass', $.Event('hide'), 'hidden')
     79       this.$element[dimension](0)
     80     }
     81 
     82   , reset: function (size) {
     83       var dimension = this.dimension()
     84 
     85       this.$element
     86         .removeClass('collapse')
     87         [dimension](size || 'auto')
     88         [0].offsetWidth
     89 
     90       this.$element[size !== null ? 'addClass' : 'removeClass']('collapse')
     91 
     92       return this
     93     }
     94 
     95   , transition: function (method, startEvent, completeEvent) {
     96       var that = this
     97         , complete = function () {
     98             if (startEvent.type == 'show') that.reset()
     99             that.transitioning = 0
    100             that.$element.trigger(completeEvent)
    101           }
    102 
    103       this.$element.trigger(startEvent)
    104 
    105       if (startEvent.isDefaultPrevented()) return
    106 
    107       this.transitioning = 1
    108 
    109       this.$element[method]('in')
    110 
    111       $.support.transition && this.$element.hasClass('collapse') ?
    112         this.$element.one($.support.transition.end, complete) :
    113         complete()
    114     }
    115 
    116   , toggle: function () {
    117       this[this.$element.hasClass('in') ? 'hide' : 'show']()
    118     }
    119 
    120   }
    121 
    122 
    123  /* COLLAPSIBLE PLUGIN DEFINITION
    124   * ============================== */
    125 
    126   $.fn.collapse = function (option) {
    127     return this.each(function () {
    128       var $this = $(this)
    129         , data = $this.data('collapse')
    130         , options = typeof option == 'object' && option
    131       if (!data) $this.data('collapse', (data = new Collapse(this, options)))
    132       if (typeof option == 'string') data[option]()
    133     })
    134   }
    135 
    136   $.fn.collapse.defaults = {
    137     toggle: true
    138   }
    139 
    140   $.fn.collapse.Constructor = Collapse
    141 
    142 
    143  /* COLLAPSIBLE DATA-API
    144   * ==================== */
    145 
    146   $(function () {
    147     $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function ( e ) {
    148       var $this = $(this), href
    149         , target = $this.attr('data-target')
    150           || e.preventDefault()
    151           || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
    152         , option = $(target).data('collapse') ? 'toggle' : $this.data()
    153       $(target).collapse(option)
    154     })
    155   })
    156 
    157 }(window.jQuery);