js/bootstrap-carousel.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-carousel.js v2.3.1
indvd00m@0
     3
 * http://twitter.github.com/bootstrap/javascript.html#carousel
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
 /* CAROUSEL CLASS DEFINITION
indvd00m@0
    27
  * ========================= */
indvd00m@0
    28
indvd00m@0
    29
  var Carousel = function (element, options) {
indvd00m@0
    30
    this.$element = $(element)
indvd00m@0
    31
    this.$indicators = this.$element.find('.carousel-indicators')
indvd00m@0
    32
    this.options = options
indvd00m@0
    33
    this.options.pause == 'hover' && this.$element
indvd00m@0
    34
      .on('mouseenter', $.proxy(this.pause, this))
indvd00m@0
    35
      .on('mouseleave', $.proxy(this.cycle, this))
indvd00m@0
    36
  }
indvd00m@0
    37
indvd00m@0
    38
  Carousel.prototype = {
indvd00m@0
    39
indvd00m@0
    40
    cycle: function (e) {
indvd00m@0
    41
      if (!e) this.paused = false
indvd00m@0
    42
      if (this.interval) clearInterval(this.interval);
indvd00m@0
    43
      this.options.interval
indvd00m@0
    44
        && !this.paused
indvd00m@0
    45
        && (this.interval = setInterval($.proxy(this.next, this), this.options.interval))
indvd00m@0
    46
      return this
indvd00m@0
    47
    }
indvd00m@0
    48
indvd00m@0
    49
  , getActiveIndex: function () {
indvd00m@0
    50
      this.$active = this.$element.find('.item.active')
indvd00m@0
    51
      this.$items = this.$active.parent().children()
indvd00m@0
    52
      return this.$items.index(this.$active)
indvd00m@0
    53
    }
indvd00m@0
    54
indvd00m@0
    55
  , to: function (pos) {
indvd00m@0
    56
      var activeIndex = this.getActiveIndex()
indvd00m@0
    57
        , that = this
indvd00m@0
    58
indvd00m@0
    59
      if (pos > (this.$items.length - 1) || pos < 0) return
indvd00m@0
    60
indvd00m@0
    61
      if (this.sliding) {
indvd00m@0
    62
        return this.$element.one('slid', function () {
indvd00m@0
    63
          that.to(pos)
indvd00m@0
    64
        })
indvd00m@0
    65
      }
indvd00m@0
    66
indvd00m@0
    67
      if (activeIndex == pos) {
indvd00m@0
    68
        return this.pause().cycle()
indvd00m@0
    69
      }
indvd00m@0
    70
indvd00m@0
    71
      return this.slide(pos > activeIndex ? 'next' : 'prev', $(this.$items[pos]))
indvd00m@0
    72
    }
indvd00m@0
    73
indvd00m@0
    74
  , pause: function (e) {
indvd00m@0
    75
      if (!e) this.paused = true
indvd00m@0
    76
      if (this.$element.find('.next, .prev').length && $.support.transition.end) {
indvd00m@0
    77
        this.$element.trigger($.support.transition.end)
indvd00m@0
    78
        this.cycle(true)
indvd00m@0
    79
      }
indvd00m@0
    80
      clearInterval(this.interval)
indvd00m@0
    81
      this.interval = null
indvd00m@0
    82
      return this
indvd00m@0
    83
    }
indvd00m@0
    84
indvd00m@0
    85
  , next: function () {
indvd00m@0
    86
      if (this.sliding) return
indvd00m@0
    87
      return this.slide('next')
indvd00m@0
    88
    }
indvd00m@0
    89
indvd00m@0
    90
  , prev: function () {
indvd00m@0
    91
      if (this.sliding) return
indvd00m@0
    92
      return this.slide('prev')
indvd00m@0
    93
    }
indvd00m@0
    94
indvd00m@0
    95
  , slide: function (type, next) {
indvd00m@0
    96
      var $active = this.$element.find('.item.active')
indvd00m@0
    97
        , $next = next || $active[type]()
indvd00m@0
    98
        , isCycling = this.interval
indvd00m@0
    99
        , direction = type == 'next' ? 'left' : 'right'
indvd00m@0
   100
        , fallback  = type == 'next' ? 'first' : 'last'
indvd00m@0
   101
        , that = this
indvd00m@0
   102
        , e
indvd00m@0
   103
indvd00m@0
   104
      this.sliding = true
indvd00m@0
   105
indvd00m@0
   106
      isCycling && this.pause()
indvd00m@0
   107
indvd00m@0
   108
      $next = $next.length ? $next : this.$element.find('.item')[fallback]()
indvd00m@0
   109
indvd00m@0
   110
      e = $.Event('slide', {
indvd00m@0
   111
        relatedTarget: $next[0]
indvd00m@0
   112
      , direction: direction
indvd00m@0
   113
      })
indvd00m@0
   114
indvd00m@0
   115
      if ($next.hasClass('active')) return
indvd00m@0
   116
indvd00m@0
   117
      if (this.$indicators.length) {
indvd00m@0
   118
        this.$indicators.find('.active').removeClass('active')
indvd00m@0
   119
        this.$element.one('slid', function () {
indvd00m@0
   120
          var $nextIndicator = $(that.$indicators.children()[that.getActiveIndex()])
indvd00m@0
   121
          $nextIndicator && $nextIndicator.addClass('active')
indvd00m@0
   122
        })
indvd00m@0
   123
      }
indvd00m@0
   124
indvd00m@0
   125
      if ($.support.transition && this.$element.hasClass('slide')) {
indvd00m@0
   126
        this.$element.trigger(e)
indvd00m@0
   127
        if (e.isDefaultPrevented()) return
indvd00m@0
   128
        $next.addClass(type)
indvd00m@0
   129
        $next[0].offsetWidth // force reflow
indvd00m@0
   130
        $active.addClass(direction)
indvd00m@0
   131
        $next.addClass(direction)
indvd00m@0
   132
        this.$element.one($.support.transition.end, function () {
indvd00m@0
   133
          $next.removeClass([type, direction].join(' ')).addClass('active')
indvd00m@0
   134
          $active.removeClass(['active', direction].join(' '))
indvd00m@0
   135
          that.sliding = false
indvd00m@0
   136
          setTimeout(function () { that.$element.trigger('slid') }, 0)
indvd00m@0
   137
        })
indvd00m@0
   138
      } else {
indvd00m@0
   139
        this.$element.trigger(e)
indvd00m@0
   140
        if (e.isDefaultPrevented()) return
indvd00m@0
   141
        $active.removeClass('active')
indvd00m@0
   142
        $next.addClass('active')
indvd00m@0
   143
        this.sliding = false
indvd00m@0
   144
        this.$element.trigger('slid')
indvd00m@0
   145
      }
indvd00m@0
   146
indvd00m@0
   147
      isCycling && this.cycle()
indvd00m@0
   148
indvd00m@0
   149
      return this
indvd00m@0
   150
    }
indvd00m@0
   151
indvd00m@0
   152
  }
indvd00m@0
   153
indvd00m@0
   154
indvd00m@0
   155
 /* CAROUSEL PLUGIN DEFINITION
indvd00m@0
   156
  * ========================== */
indvd00m@0
   157
indvd00m@0
   158
  var old = $.fn.carousel
indvd00m@0
   159
indvd00m@0
   160
  $.fn.carousel = function (option) {
indvd00m@0
   161
    return this.each(function () {
indvd00m@0
   162
      var $this = $(this)
indvd00m@0
   163
        , data = $this.data('carousel')
indvd00m@0
   164
        , options = $.extend({}, $.fn.carousel.defaults, typeof option == 'object' && option)
indvd00m@0
   165
        , action = typeof option == 'string' ? option : options.slide
indvd00m@0
   166
      if (!data) $this.data('carousel', (data = new Carousel(this, options)))
indvd00m@0
   167
      if (typeof option == 'number') data.to(option)
indvd00m@0
   168
      else if (action) data[action]()
indvd00m@0
   169
      else if (options.interval) data.pause().cycle()
indvd00m@0
   170
    })
indvd00m@0
   171
  }
indvd00m@0
   172
indvd00m@0
   173
  $.fn.carousel.defaults = {
indvd00m@0
   174
    interval: 5000
indvd00m@0
   175
  , pause: 'hover'
indvd00m@0
   176
  }
indvd00m@0
   177
indvd00m@0
   178
  $.fn.carousel.Constructor = Carousel
indvd00m@0
   179
indvd00m@0
   180
indvd00m@0
   181
 /* CAROUSEL NO CONFLICT
indvd00m@0
   182
  * ==================== */
indvd00m@0
   183
indvd00m@0
   184
  $.fn.carousel.noConflict = function () {
indvd00m@0
   185
    $.fn.carousel = old
indvd00m@0
   186
    return this
indvd00m@0
   187
  }
indvd00m@0
   188
indvd00m@0
   189
 /* CAROUSEL DATA-API
indvd00m@0
   190
  * ================= */
indvd00m@0
   191
indvd00m@0
   192
  $(document).on('click.carousel.data-api', '[data-slide], [data-slide-to]', function (e) {
indvd00m@0
   193
    var $this = $(this), href
indvd00m@0
   194
      , $target = $($this.attr('data-target') || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')) //strip for ie7
indvd00m@0
   195
      , options = $.extend({}, $target.data(), $this.data())
indvd00m@0
   196
      , slideIndex
indvd00m@0
   197
indvd00m@0
   198
    $target.carousel(options)
indvd00m@0
   199
indvd00m@0
   200
    if (slideIndex = $this.attr('data-slide-to')) {
indvd00m@0
   201
      $target.data('carousel').pause().to(slideIndex).cycle()
indvd00m@0
   202
    }
indvd00m@0
   203
indvd00m@0
   204
    e.preventDefault()
indvd00m@0
   205
  })
indvd00m@0
   206
indvd00m@0
   207
}(window.jQuery);