PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.10
Smart Grid-Layout Design for Contact Form 7 v4.10
4.18.0 4.17.0 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 4.0.0 4.0.1 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10 4.11 4.12 4.13 4.14 All 119 releases
cf7-grid-layout / assets / glider-js / glider.js

glider.js in Smart Grid-Layout Design for Contact Form 7 4.10, at assets/glider-js/glider.js

569 lines 14.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /* @preserve
2 _____ __ _ __ _
3 / ___// /(_)___/ /___ ____ (_)___
4 / (_ // // // _ // -_)/ __/_ / /(_-<
5 \___//_//_/ \_,_/ \__//_/ (_)__/ //___/
6 |___/
7
8 Version: 1.7.4
9 Author: Nick Piscitelli (pickykneee)
10 Website: https://nickpiscitelli.com
11 Documentation: http://nickpiscitelli.github.io/Glider.js
12 License: MIT License
13 Release Date: October 25th, 2018
14
15 */
16
17 /* global define */
18
19 (function (factory) {
20 typeof define === 'function' && define.amd
21 ? define(factory)
22 : typeof exports === 'object'
23 ? (module.exports = factory())
24 : factory()
25 })(function () {
26 ('use strict') // eslint-disable-line no-unused-expressions
27
28 /* globals window:true */
29 var _window = typeof window !== 'undefined' ? window : this
30
31 var Glider = (_window.Glider = function (element, settings) {
32 var _ = this
33
34 if (element._glider) return element._glider
35
36 _.ele = element
37 _.ele.classList.add('glider')
38
39 // expose glider object to its DOM element
40 _.ele._glider = _
41
42 // merge user setting with defaults
43 _.opt = Object.assign(
44 {},
45 {
46 slidesToScroll: 1,
47 slidesToShow: 1,
48 resizeLock: true,
49 duration: 0.5,
50 // easeInQuad
51 easing: function (x, t, b, c, d) {
52 return c * (t /= d) * t + b
53 }
54 },
55 settings
56 )
57
58 // set defaults
59 _.animate_id = _.page = _.slide = _.scrollLeft = 0
60 _.arrows = {}
61
62 // preserve original options to
63 // extend breakpoint settings
64 _._opt = _.opt
65
66 if (_.opt.skipTrack) {
67 // first and only child is the track
68 _.track = _.ele.children[0]
69 } else {
70 // create track and wrap slides
71 _.track = document.createElement('div')
72 _.ele.appendChild(_.track)
73 while (_.ele.children.length !== 1) {
74 _.track.appendChild(_.ele.children[0])
75 }
76 }
77
78 _.track.classList.add('glider-track')
79
80 // start glider
81 _.init()
82
83 // set events
84 _.resize = _.init.bind(_, true)
85 _.event(_.ele, 'add', {
86 scroll: _.updateControls.bind(_)
87 })
88 _.event(_window, 'add', {
89 resize: _.resize
90 })
91 })
92
93 var gliderPrototype = Glider.prototype
94 gliderPrototype.init = function (refresh, paging) {
95 var _ = this
96
97 var width = 0
98
99 var height = 0
100
101 _.slides = _.track.children;
102
103
104 [].forEach.call(_.slides, function (_, i) {
105 _.classList.add('glider-slide')
106 _.setAttribute('data-gslide', i)
107 })
108
109 _.containerWidth = _.ele.clientWidth
110
111 var breakpointChanged = _.settingsBreakpoint()
112 if (!paging) paging = breakpointChanged
113
114 if (
115 _.opt.slidesToShow === 'auto' ||
116 typeof _.opt._autoSlide !== 'undefined'
117 ) {
118 var slideCount = _.containerWidth / _.opt.itemWidth
119
120 _.opt._autoSlide = _.opt.slidesToShow = _.opt.exactWidth
121 ? slideCount
122 : Math.floor(slideCount)
123 }
124 if (_.opt.slidesToScroll === 'auto') {
125 _.opt.slidesToScroll = Math.floor(_.opt.slidesToShow)
126 }
127
128 _.itemWidth = _.opt.exactWidth
129 ? _.opt.itemWidth
130 : _.containerWidth / _.opt.slidesToShow;
131
132 // set slide dimensions
133 [].forEach.call(_.slides, function (__) {
134 __.style.height = 'auto'
135 __.style.width = _.itemWidth + 'px'
136 width += _.itemWidth
137 height = Math.max(__.offsetHeight, height)
138 })
139
140 _.track.style.width = width + 'px'
141 _.trackWidth = width
142 _.isDrag = false
143 _.preventClick = false
144
145 _.opt.resizeLock && _.scrollTo(_.slide * _.itemWidth, 0)
146
147 if (breakpointChanged || paging) {
148 _.bindArrows()
149 _.buildDots()
150 _.bindDrag()
151 }
152
153 _.updateControls()
154
155 _.emit(refresh ? 'refresh' : 'loaded')
156 }
157
158 gliderPrototype.bindDrag = function () {
159 var _ = this
160 _.mouse = _.mouse || _.handleMouse.bind(_)
161
162 var mouseup = function () {
163 _.mouseDown = undefined
164 _.ele.classList.remove('drag')
165 if (_.isDrag) {
166 _.preventClick = true
167 }
168 _.isDrag = false
169 }
170
171 var events = {
172 mouseup: mouseup,
173 mouseleave: mouseup,
174 mousedown: function (e) {
175 e.preventDefault()
176 e.stopPropagation()
177 _.mouseDown = e.clientX
178 _.ele.classList.add('drag')
179 },
180 mousemove: _.mouse,
181 click: function (e) {
182 if (_.preventClick) {
183 e.preventDefault()
184 e.stopPropagation()
185 }
186 _.preventClick = false
187 }
188 }
189
190 _.ele.classList.toggle('draggable', _.opt.draggable === true)
191 _.event(_.ele, 'remove', events)
192 if (_.opt.draggable) _.event(_.ele, 'add', events)
193 }
194
195 gliderPrototype.buildDots = function () {
196 var _ = this
197
198 if (!_.opt.dots) {
199 if (_.dots) _.dots.innerHTML = ''
200 return
201 }
202
203 if (typeof _.opt.dots === 'string') {
204 _.dots = document.querySelector(_.opt.dots)
205 } else _.dots = _.opt.dots
206 if (!_.dots) return
207
208 _.dots.innerHTML = ''
209 _.dots.classList.add('glider-dots')
210
211 for (var i = 0; i < Math.ceil(_.slides.length / _.opt.slidesToShow); ++i) {
212 var dot = document.createElement('button')
213 dot.dataset.index = i
214 dot.setAttribute('aria-label', 'Page ' + (i + 1))
215 dot.className = 'glider-dot ' + (i ? '' : 'active')
216 _.event(dot, 'add', {
217 click: _.scrollItem.bind(_, i, true)
218 })
219 _.dots.appendChild(dot)
220 }
221 }
222
223 gliderPrototype.bindArrows = function () {
224 var _ = this
225 if (!_.opt.arrows) {
226 Object.keys(_.arrows).forEach(function (direction) {
227 var element = _.arrows[direction]
228 _.event(element, 'remove', { click: element._func })
229 })
230 return
231 }
232 ['prev', 'next'].forEach(function (direction) {
233 var arrow = _.opt.arrows[direction]
234 if (arrow) {
235 if (typeof arrow === 'string') arrow = document.querySelector(arrow)
236 if (arrow) {
237 arrow._func = arrow._func || _.scrollItem.bind(_, direction)
238 _.event(arrow, 'remove', {
239 click: arrow._func
240 })
241 _.event(arrow, 'add', {
242 click: arrow._func
243 })
244 _.arrows[direction] = arrow
245 }
246 }
247 })
248 }
249
250 gliderPrototype.updateControls = function (event) {
251 var _ = this
252
253 if (event && !_.opt.scrollPropagate) {
254 event.stopPropagation()
255 }
256
257 var disableArrows = _.containerWidth >= _.trackWidth
258
259 if (!_.opt.rewind) {
260 if (_.arrows.prev) {
261 _.arrows.prev.classList.toggle(
262 'disabled',
263 _.ele.scrollLeft <= 0 || disableArrows
264 )
265 }
266 if (_.arrows.next) {
267 _.arrows.next.classList.toggle(
268 'disabled',
269 Math.ceil(_.scrollLeft + _.containerWidth) >=
270 Math.floor(_.trackWidth) || disableArrows
271 )
272 }
273 }
274
275 _.slide = Math.round(_.scrollLeft / _.itemWidth)
276 _.page = Math.round(_.scrollLeft / _.containerWidth)
277
278 var middle = _.slide + Math.floor(Math.floor(_.opt.slidesToShow) / 2)
279
280 var extraMiddle = Math.floor(_.opt.slidesToShow) % 2 ? 0 : middle + 1
281 if (Math.floor(_.opt.slidesToShow) === 1) {
282 extraMiddle = 0
283 }
284
285 // the last page may be less than one half of a normal page width so
286 // the page is rounded down. when at the end, force the page to turn
287 if (_.scrollLeft + _.containerWidth >= Math.floor(_.trackWidth)) {
288 _.page = _.dots ? _.dots.children.length - 1 : 0
289 }
290
291 [].forEach.call(_.slides, function (slide, index) {
292 var slideClasses = slide.classList
293
294 var wasVisible = slideClasses.contains('visible')
295
296 var start = _.scrollLeft
297
298 var end = _.scrollLeft + _.containerWidth
299
300 var itemStart = _.itemWidth * index
301
302 var itemEnd = itemStart + _.itemWidth;
303
304 [].forEach.call(slideClasses, function (className) {
305 /^left|right/.test(className) && slideClasses.remove(className)
306 })
307 slideClasses.toggle('active', _.slide === index)
308 if (middle === index || (extraMiddle && extraMiddle === index)) {
309 slideClasses.add('center')
310 } else {
311 slideClasses.remove('center')
312 slideClasses.add(
313 [
314 index < middle ? 'left' : 'right',
315 Math.abs(index - (index < middle ? middle : extraMiddle || middle))
316 ].join('-')
317 )
318 }
319
320 var isVisible =
321 Math.ceil(itemStart) >= start && Math.floor(itemEnd) <= end
322 slideClasses.toggle('visible', isVisible)
323 if (isVisible !== wasVisible) {
324 _.emit('slide-' + (isVisible ? 'visible' : 'hidden'), {
325 slide: index
326 })
327 }
328 })
329 if (_.dots) {
330 [].forEach.call(_.dots.children, function (dot, index) {
331 dot.classList.toggle('active', _.page === index)
332 })
333 }
334
335 if (event && _.opt.scrollLock) {
336 clearTimeout(_.scrollLock)
337 _.scrollLock = setTimeout(function () {
338 clearTimeout(_.scrollLock)
339 // dont attempt to scroll less than a pixel fraction - causes looping
340 if (Math.abs(_.ele.scrollLeft / _.itemWidth - _.slide) > 0.02) {
341 if (!_.mouseDown) {
342 // Only scroll if not at the end (#94)
343 if (_.trackWidth > _.containerWidth + _.ele.scrollLeft) {
344 _.scrollItem(_.getCurrentSlide())
345 }
346 }
347 }
348 }, _.opt.scrollLockDelay || 250)
349 }
350 }
351
352 gliderPrototype.getCurrentSlide = function () {
353 var _ = this
354 return _.round(_.ele.scrollLeft / _.itemWidth)
355 }
356
357 gliderPrototype.scrollItem = function (slide, dot, e) {
358 if (e) e.preventDefault()
359
360 var _ = this
361
362 var originalSlide = slide
363 ++_.animate_id
364
365 if (dot === true) {
366 slide = slide * _.containerWidth
367 slide = Math.round(slide / _.itemWidth) * _.itemWidth
368 } else {
369 if (typeof slide === 'string') {
370 var backwards = slide === 'prev'
371
372 // use precise location if fractional slides are on
373 if (_.opt.slidesToScroll % 1 || _.opt.slidesToShow % 1) {
374 slide = _.getCurrentSlide()
375 } else {
376 slide = _.slide
377 }
378
379 if (backwards) slide -= _.opt.slidesToScroll
380 else slide += _.opt.slidesToScroll
381
382 if (_.opt.rewind) {
383 var scrollLeft = _.ele.scrollLeft
384 slide =
385 backwards && !scrollLeft
386 ? _.slides.length
387 : !backwards &&
388 scrollLeft + _.containerWidth >= Math.floor(_.trackWidth)
389 ? 0
390 : slide
391 }
392 }
393
394 slide = Math.max(Math.min(slide, _.slides.length), 0)
395
396 _.slide = slide
397 slide = _.itemWidth * slide
398 }
399
400 _.scrollTo(
401 slide,
402 _.opt.duration * Math.abs(_.ele.scrollLeft - slide),
403 function () {
404 _.updateControls()
405 _.emit('animated', {
406 value: originalSlide,
407 type:
408 typeof originalSlide === 'string' ? 'arrow' : dot ? 'dot' : 'slide'
409 })
410 }
411 )
412
413 return false
414 }
415
416 gliderPrototype.settingsBreakpoint = function () {
417 var _ = this
418
419 var resp = _._opt.responsive
420
421 if (resp) {
422 // Sort the breakpoints in mobile first order
423 resp.sort(function (a, b) {
424 return b.breakpoint - a.breakpoint
425 })
426
427 for (var i = 0; i < resp.length; ++i) {
428 var size = resp[i]
429 if (_window.innerWidth >= size.breakpoint) {
430 if (_.breakpoint !== size.breakpoint) {
431 _.opt = Object.assign({}, _._opt, size.settings)
432 _.breakpoint = size.breakpoint
433 return true
434 }
435 return false
436 }
437 }
438 }
439 // set back to defaults in case they were overriden
440 var breakpointChanged = _.breakpoint !== 0
441 _.opt = Object.assign({}, _._opt)
442 _.breakpoint = 0
443 return breakpointChanged
444 }
445
446 gliderPrototype.scrollTo = function (scrollTarget, scrollDuration, callback) {
447 var _ = this
448
449 var start = new Date().getTime()
450
451 var animateIndex = _.animate_id
452
453 var animate = function () {
454 var now = new Date().getTime() - start
455 _.scrollLeft = _.scrollLeft + (scrollTarget - _.scrollLeft) * _.opt.easing(0, now, 0, 1, scrollDuration)
456 _.ele.scrollLeft = _.scrollLeft
457
458 if (now < scrollDuration && animateIndex === _.animate_id) {
459 _window.requestAnimationFrame(animate)
460 } else {
461 _.ele.scrollLeft = _.scrollLeft = scrollTarget
462 callback && callback.call(_)
463 }
464 }
465
466 _window.requestAnimationFrame(animate)
467 }
468
469 gliderPrototype.removeItem = function (index) {
470 var _ = this
471
472 if (_.slides.length) {
473 _.track.removeChild(_.slides[index])
474 _.refresh(true)
475 _.emit('remove')
476 }
477 }
478
479 gliderPrototype.addItem = function (ele) {
480 var _ = this
481
482 _.track.appendChild(ele)
483 _.refresh(true)
484 _.emit('add')
485 }
486
487 gliderPrototype.handleMouse = function (e) {
488 var _ = this
489 if (_.mouseDown) {
490 _.isDrag = true
491 _.scrollLeft +=
492 (_.mouseDown - e.clientX) * (_.opt.dragVelocity || 3.3)
493 _.mouseDown = e.clientX
494 _.ele.scrollLeft = _.scrollLeft
495 }
496 }
497
498 // used to round to the nearest 0.XX fraction
499 gliderPrototype.round = function (double) {
500 var _ = this
501 var step = _.opt.slidesToScroll % 1 || 1
502 var inv = 1.0 / step
503 return Math.round(double * inv) / inv
504 }
505
506 gliderPrototype.refresh = function (paging) {
507 var _ = this
508 _.init(true, paging)
509 }
510
511 gliderPrototype.setOption = function (opt, global) {
512 var _ = this
513
514 if (_.breakpoint && !global) {
515 _._opt.responsive.forEach(function (v) {
516 if (v.breakpoint === _.breakpoint) {
517 v.settings = Object.assign({}, v.settings, opt)
518 }
519 })
520 } else {
521 _._opt = Object.assign({}, _._opt, opt)
522 }
523
524 _.breakpoint = 0
525 _.settingsBreakpoint()
526 }
527
528 gliderPrototype.destroy = function () {
529 var _ = this
530
531 var replace = _.ele.cloneNode(true)
532
533 var clear = function (ele) {
534 ele.removeAttribute('style');
535 [].forEach.call(ele.classList, function (className) {
536 /^glider/.test(className) && ele.classList.remove(className)
537 })
538 }
539 // remove track
540 replace.children[0].outerHTML = replace.children[0].innerHTML
541 clear(replace);
542 [].forEach.call(replace.getElementsByTagName('*'), clear)
543 _.ele.parentNode.replaceChild(replace, _.ele)
544 _.event(_window, 'remove', {
545 resize: _.resize
546 })
547 _.emit('destroy')
548 }
549
550 gliderPrototype.emit = function (name, arg) {
551 var _ = this
552
553 var e = new _window.CustomEvent('glider-' + name, {
554 bubbles: !_.opt.eventPropagate,
555 detail: arg
556 })
557 _.ele.dispatchEvent(e)
558 }
559
560 gliderPrototype.event = function (ele, type, args) {
561 var eventHandler = ele[type + 'EventListener'].bind(ele)
562 Object.keys(args).forEach(function (k) {
563 eventHandler(k, args[k])
564 })
565 }
566
567 return Glider
568 })
569