kmaterialize 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/js/slider.js ADDED
@@ -0,0 +1,359 @@
1
+ (function($, anim) {
2
+ 'use strict';
3
+
4
+ let _defaults = {
5
+ indicators: true,
6
+ height: 400,
7
+ duration: 500,
8
+ interval: 6000
9
+ };
10
+
11
+ /**
12
+ * @class
13
+ *
14
+ */
15
+ class Slider extends Component {
16
+ /**
17
+ * Construct Slider instance and set up overlay
18
+ * @constructor
19
+ * @param {Element} el
20
+ * @param {Object} options
21
+ */
22
+ constructor(el, options) {
23
+ super(Slider, el, options);
24
+
25
+ this.el.M_Slider = this;
26
+
27
+ /**
28
+ * Options for the modal
29
+ * @member Slider#options
30
+ * @prop {Boolean} [indicators=true] - Show indicators
31
+ * @prop {Number} [height=400] - height of slider
32
+ * @prop {Number} [duration=500] - Length in ms of slide transition
33
+ * @prop {Number} [interval=6000] - Length in ms of slide interval
34
+ */
35
+ this.options = $.extend({}, Slider.defaults, options);
36
+
37
+ // setup
38
+ this.$slider = this.$el.find('.slides');
39
+ this.$slides = this.$slider.children('li');
40
+ this.activeIndex = this.$slides
41
+ .filter(function(item) {
42
+ return $(item).hasClass('active');
43
+ })
44
+ .first()
45
+ .index();
46
+ if (this.activeIndex != -1) {
47
+ this.$active = this.$slides.eq(this.activeIndex);
48
+ }
49
+
50
+ this._setSliderHeight();
51
+
52
+ // Set initial positions of captions
53
+ this.$slides.find('.caption').each((el) => {
54
+ this._animateCaptionIn(el, 0);
55
+ });
56
+
57
+ // Move img src into background-image
58
+ this.$slides.find('img').each((el) => {
59
+ let placeholderBase64 =
60
+ 'data:image/gif;base64,R0lGODlhAQABAIABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==';
61
+ if ($(el).attr('src') !== placeholderBase64) {
62
+ $(el).css('background-image', 'url("' + $(el).attr('src') + '")');
63
+ $(el).attr('src', placeholderBase64);
64
+ }
65
+ });
66
+
67
+ this._setupIndicators();
68
+
69
+ // Show active slide
70
+ if (this.$active) {
71
+ this.$active.css('display', 'block');
72
+ } else {
73
+ this.$slides.first().addClass('active');
74
+ anim({
75
+ targets: this.$slides.first()[0],
76
+ opacity: 1,
77
+ duration: this.options.duration,
78
+ easing: 'easeOutQuad'
79
+ });
80
+
81
+ this.activeIndex = 0;
82
+ this.$active = this.$slides.eq(this.activeIndex);
83
+
84
+ // Update indicators
85
+ if (this.options.indicators) {
86
+ this.$indicators.eq(this.activeIndex).addClass('active');
87
+ }
88
+ }
89
+
90
+ // Adjust height to current slide
91
+ this.$active.find('img').each((el) => {
92
+ anim({
93
+ targets: this.$active.find('.caption')[0],
94
+ opacity: 1,
95
+ translateX: 0,
96
+ translateY: 0,
97
+ duration: this.options.duration,
98
+ easing: 'easeOutQuad'
99
+ });
100
+ });
101
+
102
+ this._setupEventHandlers();
103
+
104
+ // auto scroll
105
+ this.start();
106
+ }
107
+
108
+ static get defaults() {
109
+ return _defaults;
110
+ }
111
+
112
+ static init(els, options) {
113
+ return super.init(this, els, options);
114
+ }
115
+
116
+ /**
117
+ * Get Instance
118
+ */
119
+ static getInstance(el) {
120
+ let domElem = !!el.jquery ? el[0] : el;
121
+ return domElem.M_Slider;
122
+ }
123
+
124
+ /**
125
+ * Teardown component
126
+ */
127
+ destroy() {
128
+ this.pause();
129
+ this._removeIndicators();
130
+ this._removeEventHandlers();
131
+ this.el.M_Slider = undefined;
132
+ }
133
+
134
+ /**
135
+ * Setup Event Handlers
136
+ */
137
+ _setupEventHandlers() {
138
+ this._handleIntervalBound = this._handleInterval.bind(this);
139
+ this._handleIndicatorClickBound = this._handleIndicatorClick.bind(this);
140
+
141
+ if (this.options.indicators) {
142
+ this.$indicators.each((el) => {
143
+ el.addEventListener('click', this._handleIndicatorClickBound);
144
+ });
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Remove Event Handlers
150
+ */
151
+ _removeEventHandlers() {
152
+ if (this.options.indicators) {
153
+ this.$indicators.each((el) => {
154
+ el.removeEventListener('click', this._handleIndicatorClickBound);
155
+ });
156
+ }
157
+ }
158
+
159
+ /**
160
+ * Handle indicator click
161
+ * @param {Event} e
162
+ */
163
+ _handleIndicatorClick(e) {
164
+ let currIndex = $(e.target).index();
165
+ this.set(currIndex);
166
+ }
167
+
168
+ /**
169
+ * Handle Interval
170
+ */
171
+ _handleInterval() {
172
+ let newActiveIndex = this.$slider.find('.active').index();
173
+ if (this.$slides.length === newActiveIndex + 1) newActiveIndex = 0;
174
+ // loop to start
175
+ else newActiveIndex += 1;
176
+
177
+ this.set(newActiveIndex);
178
+ }
179
+
180
+ /**
181
+ * Animate in caption
182
+ * @param {Element} caption
183
+ * @param {Number} duration
184
+ */
185
+ _animateCaptionIn(caption, duration) {
186
+ let animOptions = {
187
+ targets: caption,
188
+ opacity: 0,
189
+ duration: duration,
190
+ easing: 'easeOutQuad'
191
+ };
192
+
193
+ if ($(caption).hasClass('center-align')) {
194
+ animOptions.translateY = -100;
195
+ } else if ($(caption).hasClass('right-align')) {
196
+ animOptions.translateX = 100;
197
+ } else if ($(caption).hasClass('left-align')) {
198
+ animOptions.translateX = -100;
199
+ }
200
+
201
+ anim(animOptions);
202
+ }
203
+
204
+ /**
205
+ * Set height of slider
206
+ */
207
+ _setSliderHeight() {
208
+ // If fullscreen, do nothing
209
+ if (!this.$el.hasClass('fullscreen')) {
210
+ if (this.options.indicators) {
211
+ // Add height if indicators are present
212
+ this.$el.css('height', this.options.height + 40 + 'px');
213
+ } else {
214
+ this.$el.css('height', this.options.height + 'px');
215
+ }
216
+ this.$slider.css('height', this.options.height + 'px');
217
+ }
218
+ }
219
+
220
+ /**
221
+ * Setup indicators
222
+ */
223
+ _setupIndicators() {
224
+ if (this.options.indicators) {
225
+ this.$indicators = $('<ul class="indicators"></ul>');
226
+ this.$slides.each((el, index) => {
227
+ let $indicator = $('<li class="indicator-item"></li>');
228
+ this.$indicators.append($indicator[0]);
229
+ });
230
+ this.$el.append(this.$indicators[0]);
231
+ this.$indicators = this.$indicators.children('li.indicator-item');
232
+ }
233
+ }
234
+
235
+ /**
236
+ * Remove indicators
237
+ */
238
+ _removeIndicators() {
239
+ this.$el.find('ul.indicators').remove();
240
+ }
241
+
242
+ /**
243
+ * Cycle to nth item
244
+ * @param {Number} index
245
+ */
246
+ set(index) {
247
+ // Wrap around indices.
248
+ if (index >= this.$slides.length) index = 0;
249
+ else if (index < 0) index = this.$slides.length - 1;
250
+
251
+ // Only do if index changes
252
+ if (this.activeIndex != index) {
253
+ this.$active = this.$slides.eq(this.activeIndex);
254
+ let $caption = this.$active.find('.caption');
255
+ this.$active.removeClass('active');
256
+
257
+ anim({
258
+ targets: this.$active[0],
259
+ opacity: 0,
260
+ duration: this.options.duration,
261
+ easing: 'easeOutQuad',
262
+ complete: () => {
263
+ this.$slides.not('.active').each((el) => {
264
+ anim({
265
+ targets: el,
266
+ opacity: 0,
267
+ translateX: 0,
268
+ translateY: 0,
269
+ duration: 0,
270
+ easing: 'easeOutQuad'
271
+ });
272
+ });
273
+ }
274
+ });
275
+
276
+ this._animateCaptionIn($caption[0], this.options.duration);
277
+
278
+ // Update indicators
279
+ if (this.options.indicators) {
280
+ this.$indicators.eq(this.activeIndex).removeClass('active');
281
+ this.$indicators.eq(index).addClass('active');
282
+ }
283
+
284
+ anim({
285
+ targets: this.$slides.eq(index)[0],
286
+ opacity: 1,
287
+ duration: this.options.duration,
288
+ easing: 'easeOutQuad'
289
+ });
290
+
291
+ anim({
292
+ targets: this.$slides.eq(index).find('.caption')[0],
293
+ opacity: 1,
294
+ translateX: 0,
295
+ translateY: 0,
296
+ duration: this.options.duration,
297
+ delay: this.options.duration,
298
+ easing: 'easeOutQuad'
299
+ });
300
+
301
+ this.$slides.eq(index).addClass('active');
302
+ this.activeIndex = index;
303
+
304
+ // Reset interval
305
+ this.start();
306
+ }
307
+ }
308
+
309
+ /**
310
+ * Pause slider interval
311
+ */
312
+ pause() {
313
+ clearInterval(this.interval);
314
+ }
315
+
316
+ /**
317
+ * Start slider interval
318
+ */
319
+ start() {
320
+ clearInterval(this.interval);
321
+ this.interval = setInterval(
322
+ this._handleIntervalBound,
323
+ this.options.duration + this.options.interval
324
+ );
325
+ }
326
+
327
+ /**
328
+ * Move to next slide
329
+ */
330
+ next() {
331
+ let newIndex = this.activeIndex + 1;
332
+
333
+ // Wrap around indices.
334
+ if (newIndex >= this.$slides.length) newIndex = 0;
335
+ else if (newIndex < 0) newIndex = this.$slides.length - 1;
336
+
337
+ this.set(newIndex);
338
+ }
339
+
340
+ /**
341
+ * Move to previous slide
342
+ */
343
+ prev() {
344
+ let newIndex = this.activeIndex - 1;
345
+
346
+ // Wrap around indices.
347
+ if (newIndex >= this.$slides.length) newIndex = 0;
348
+ else if (newIndex < 0) newIndex = this.$slides.length - 1;
349
+
350
+ this.set(newIndex);
351
+ }
352
+ }
353
+
354
+ M.Slider = Slider;
355
+
356
+ if (M.jQueryLoaded) {
357
+ M.initializeJqueryWrapper(Slider, 'slider', 'M_Slider');
358
+ }
359
+ })(cash, M.anime);