js/bootstrap-collapse.js
changeset 0 ba8ab09f730e
equal deleted inserted replaced
-1:000000000000 0:ba8ab09f730e
       
     1 /* =============================================================
       
     2  * bootstrap-collapse.js v2.3.1
       
     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 || this.$element.hasClass('in')) 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       $.support.transition && this.$element[dimension](this.$element[0][scroll])
       
    71     }
       
    72 
       
    73   , hide: function () {
       
    74       var dimension
       
    75       if (this.transitioning || !this.$element.hasClass('in')) 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  /* COLLAPSE PLUGIN DEFINITION
       
   124   * ========================== */
       
   125 
       
   126   var old = $.fn.collapse
       
   127 
       
   128   $.fn.collapse = function (option) {
       
   129     return this.each(function () {
       
   130       var $this = $(this)
       
   131         , data = $this.data('collapse')
       
   132         , options = $.extend({}, $.fn.collapse.defaults, $this.data(), typeof option == 'object' && option)
       
   133       if (!data) $this.data('collapse', (data = new Collapse(this, options)))
       
   134       if (typeof option == 'string') data[option]()
       
   135     })
       
   136   }
       
   137 
       
   138   $.fn.collapse.defaults = {
       
   139     toggle: true
       
   140   }
       
   141 
       
   142   $.fn.collapse.Constructor = Collapse
       
   143 
       
   144 
       
   145  /* COLLAPSE NO CONFLICT
       
   146   * ==================== */
       
   147 
       
   148   $.fn.collapse.noConflict = function () {
       
   149     $.fn.collapse = old
       
   150     return this
       
   151   }
       
   152 
       
   153 
       
   154  /* COLLAPSE DATA-API
       
   155   * ================= */
       
   156 
       
   157   $(document).on('click.collapse.data-api', '[data-toggle=collapse]', function (e) {
       
   158     var $this = $(this), href
       
   159       , target = $this.attr('data-target')
       
   160         || e.preventDefault()
       
   161         || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
       
   162       , option = $(target).data('collapse') ? 'toggle' : $this.data()
       
   163     $this[$(target).hasClass('in') ? 'addClass' : 'removeClass']('collapsed')
       
   164     $(target).collapse(option)
       
   165   })
       
   166 
       
   167 }(window.jQuery);