PageRenderTime 37ms CodeModel.GetById 24ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/webapp/resources/js/bootstrap-collapse.js

https://bitbucket.org/lshoo/thymeleaf-coffeescript-springmvc-example
JavaScript | 138 lines | 86 code | 30 blank | 22 comment | 18 complexity | c8f1c8322bd15bb8ecfeeeec6e706fb4 MD5 | raw file
  1/* =============================================================
  2 * bootstrap-collapse.js v2.0.2
  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!function( $ ){
 21
 22  "use strict"
 23
 24  var Collapse = function ( element, options ) {
 25  	this.$element = $(element)
 26    this.options = $.extend({}, $.fn.collapse.defaults, options)
 27
 28    if (this.options["parent"]) {
 29      this.$parent = $(this.options["parent"])
 30    }
 31
 32    this.options.toggle && this.toggle()
 33  }
 34
 35  Collapse.prototype = {
 36
 37    constructor: Collapse
 38
 39  , dimension: function () {
 40      var hasWidth = this.$element.hasClass('width')
 41      return hasWidth ? 'width' : 'height'
 42    }
 43
 44  , show: function () {
 45      var dimension = this.dimension()
 46        , scroll = $.camelCase(['scroll', dimension].join('-'))
 47        , actives = this.$parent && this.$parent.find('.in')
 48        , hasData
 49
 50      if (actives && actives.length) {
 51        hasData = actives.data('collapse')
 52        actives.collapse('hide')
 53        hasData || actives.data('collapse', null)
 54      }
 55
 56      this.$element[dimension](0)
 57      this.transition('addClass', 'show', 'shown')
 58      this.$element[dimension](this.$element[0][scroll])
 59
 60    }
 61
 62  , hide: function () {
 63      var dimension = this.dimension()
 64      this.reset(this.$element[dimension]())
 65      this.transition('removeClass', 'hide', 'hidden')
 66      this.$element[dimension](0)
 67    }
 68
 69  , reset: function ( size ) {
 70      var dimension = this.dimension()
 71
 72      this.$element
 73        .removeClass('collapse')
 74        [dimension](size || 'auto')
 75        [0].offsetWidth
 76
 77      this.$element[size ? 'addClass' : 'removeClass']('collapse')
 78
 79      return this
 80    }
 81
 82  , transition: function ( method, startEvent, completeEvent ) {
 83      var that = this
 84        , complete = function () {
 85            if (startEvent == 'show') that.reset()
 86            that.$element.trigger(completeEvent)
 87          }
 88
 89      this.$element
 90        .trigger(startEvent)
 91        [method]('in')
 92
 93      $.support.transition && this.$element.hasClass('collapse') ?
 94        this.$element.one($.support.transition.end, complete) :
 95        complete()
 96  	}
 97
 98  , toggle: function () {
 99      this[this.$element.hasClass('in') ? 'hide' : 'show']()
100  	}
101
102  }
103
104  /* COLLAPSIBLE PLUGIN DEFINITION
105  * ============================== */
106
107  $.fn.collapse = function ( option ) {
108    return this.each(function () {
109      var $this = $(this)
110        , data = $this.data('collapse')
111        , options = typeof option == 'object' && option
112      if (!data) $this.data('collapse', (data = new Collapse(this, options)))
113      if (typeof option == 'string') data[option]()
114    })
115  }
116
117  $.fn.collapse.defaults = {
118    toggle: true
119  }
120
121  $.fn.collapse.Constructor = Collapse
122
123
124 /* COLLAPSIBLE DATA-API
125  * ==================== */
126
127  $(function () {
128    $('body').on('click.collapse.data-api', '[data-toggle=collapse]', function ( e ) {
129      var $this = $(this), href
130        , target = $this.attr('data-target')
131          || e.preventDefault()
132          || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
133        , option = $(target).data('collapse') ? 'toggle' : $this.data()
134      $(target).collapse(option)
135    })
136  })
137
138}( window.jQuery );