-
Notifications
You must be signed in to change notification settings - Fork 7
/
bootstrap-carousel.js
240 lines (197 loc) · 7.15 KB
/
bootstrap-carousel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* ========================================================================
* Bootstrap-carousel by @silviomoreto
* Bootstrap-carousel is a wrapper of the Carousel plugin from Bootstrap
* The plugin main object is to reduce the typing from creating
* a new Carousel while automating some tasks.
*
* Plugin developed in the book Bootstrap by Example
* https://www.packtpub.com/web-development/bootstrap-example
*
* Copyright 2016 PACKT Publishing.
* ========================================================================
* Licensed under MIT
* ======================================================================== */
+function ($) {
'use strict';
// BOOTSTRAP CAROUSEL CLASS DEFINITION
// ======================
var BootstrapCarousel = function (element, options) {
this.$element = $(element);
this.options = $.extend({}, BootstrapCarousel.DEFAULTS, options);
// Expose public methods
this.addSlide = BootstrapCarousel.prototype.addSlide;
this.reload = BootstrapCarousel.prototype.load;
this.init();
}
BootstrapCarousel.VERSION = '1.0.0'
BootstrapCarousel.DEFAULTS = {
indicators: true,
controls: true,
defaultTitle: '',
defaultContent: '',
nextIcon: 'glyphicon glyphicon-chevron-right',
nextText: 'Next',
previousIcon: 'glyphicon glyphicon-chevron-left',
previousText: 'Previous',
interval: 5000,
pause: 'hover',
wrap: true,
keyboard: true,
};
BootstrapCarousel.prototype = {
template: {
slide: '<img class="hide" src="{itemImg}" data-title="{itemTitle}" data-content="{itemContent}">',
carouselInner: '<div class="carousel-inner" role="listbox">{innerContent}</div>',
carouselItem: '' +
'<div class="item {activeClass}">' +
'<img src="{itemImg}">' +
'<div class="carousel-caption">' +
'<h3>{itemTitle}</h3>' +
'<p>{itemContent}</p>' +
'</div>'+
'</div>',
carouselIndicator: '<ol class="carousel-indicators">{indicators}</ol>',
carouselIndicatorItem: '<li data-target="#{elementId}" data-slide-to="{slideNumber}" {activeClass}></li>',
carouselControls: '' +
'<a class="left carousel-control" href="#{elementId}" role="button" data-slide="prev">' +
'<span class="{previousIcon}" aria-hidden="true"></span>' +
'<span class="sr-only">{previousText}</span>' +
'</a>' +
'<a class="right carousel-control" href="#{elementId}" role="button" data-slide="next">' +
'<span class="{nextIcon}" aria-hidden="true"></span>' +
'<span class="sr-only">{nextText}</span>' +
'</a>',
},
init: function () {
// Check if element has ID
// case not, throw an exception for the user
if(!this.$element.attr('id')){
throw 'You must provide an id for the Bootstrap Carousel element.';
}
this.$element.addClass('slide carousel');
this.load();
},
initPlugin: function() {
this.$element.carousel({
interval: this.options.interval,
pause: this.options.pause,
wrap: this.options.wrap,
keyboyard: this.options.keyboard
});
},
addSlide: function(itemImg, itemTitle, itemContent) {
var newSlide = this.template.slide
.replace(/{itemImg}/, itemImg)
.replace(/{itemTitle}/, itemTitle)
.replace(/{itemContent}/, itemContent);
this.$element.append(newSlide);
this.load();
},
load: function() {
this.$element.find('.carousel-inner, .carousel-indicators, .carousel-control').remove();
this.$slides = this.$element.find('> img');
this.$slides.hide();
// workaround to remove carousel data for reinitiation
this.$element.carousel('pause');
this.$element.removeData('bs.carousel');
this.$element.append(this.createCarousel());
this.initPlugin();
},
createCarousel: function() {
var template = '';
// create slides
template += this.createSlideDeck();
// create indicators
if(this.options.indicators) {
template += this.createIndicators();
}
// create controls
if(this.options.controls) {
template += this.createControls();
}
return template
},
createIndicators: function() {
var indicatorTemplate = '',
slide,
elementId = this.$element.attr('id');
for (var i = 0; i < this.$slides.length; i++) {
slide = this.$slides.get(i);
indicatorTemplate += this.template.carouselIndicatorItem
.replace(/{elementId}/, elementId)
.replace(/{slideNumber}/, i)
.replace(/{activeClass}/, i == 0 ? 'class="active"' : '');
}
return this.template.carouselIndicator.replace(/{indicators}/, indicatorTemplate);
},
createControls: function() {
var elementId = this.$element.attr('id');
return this.template.carouselControls
.replace(/{elementId}/g, elementId)
.replace(/{previousIcon}/, this.options.previousIcon)
.replace(/{previousText}/, this.options.previousText)
.replace(/{nextIcon}/, this.options.nextIcon)
.replace(/{nextText}/, this.options.nextText);
},
createSlideDeck: function() {
var slideTemplate = '',
slide;
for (var i = 0; i < this.$slides.length; i++) {
slide = this.$slides.get(i);
slideTemplate += this.createSlide(
i == 0 ? 'active' : '',
slide.src,
slide.dataset.title,
slide.dataset.content
);
};
return this.template.carouselInner.replace(/{innerContent}/, slideTemplate);
},
createSlide: function(active, itemImg, itemTitle, itemContent) {
return this.template.carouselItem
.replace(/{activeClass}/, active)
.replace(/{itemImg}/, itemImg)
.replace(/{itemTitle}/, itemTitle || this.options.defaultTitle)
.replace(/{itemContent}/, itemContent || this.options.defaultContent);
}
};
// BOOTSTRAP CAROUSEL PLUGIN DEFINITION
// =======================
function Plugin(option) {
var args = arguments;
[].shift.apply(args);
return this.each(function () {
var $this = $(this),
data = $this.data('bootstrap-carousel'),
options = $.extend({}, BootstrapCarousel.DEFAULTS, $this.data(), typeof option == 'object' && option),
value;
if (!data) {
$this.data('bootstrap-carousel', (data = new BootstrapCarousel(this, options)));
}
if (typeof option == 'string') {
if (data[option] instanceof Function) {
value = data[option].apply(data, args);
} else {
value = data.options[option];
}
}
})
}
var old = $.fn.bCarousel;
$.fn.bCarousel = Plugin;
$.fn.bCarousel.Constructor = BootstrapCarousel;
// BOOTSTRAP CAROUSEL NO CONFLICT
// =================
$.fn.bCarousel.noConflict = function () {
$.fn.bCarousel = old;
return this;
}
// BOOTSTRAP CAROUSEL CLASS LOAD
// ==============
$(window).on('load', function () {
$('.bootstrap-carousel').each(function () {
var $carousel = $(this);
Plugin.call($carousel, $carousel.data());
})
})
}(jQuery);