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