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