@glidejs/glide
Version:
Glide.js is a dependency-free JavaScript ES6 slider and carousel. It’s lightweight, flexible and fast. Designed to slide. No less, no more
194 lines (163 loc) • 4.26 kB
JavaScript
import { define } from '../utils/object'
import { toInt, isNumber } from '../utils/unit'
export default function (Glide, Components, Events) {
const Run = {
/**
* Initializes autorunning of the glide.
*
* @return {Void}
*/
mount () {
this._o = false
},
/**
* Makes glides running based on the passed moving schema.
*
* @param {String} move
*/
make (move) {
if (!Glide.disabled) {
Glide.disable()
this.move = move
Events.emit('run.before', this.move)
this.calculate()
Events.emit('run', this.move)
Components.Transition.after(() => {
if (this.isOffset('<') || this.isOffset('>')) {
this._o = false
Events.emit('run.offset', this.move)
}
Events.emit('run.after', this.move)
Glide.enable()
})
}
},
/**
* Calculates current index based on defined move.
*
* @return {Void}
*/
calculate () {
let { move, length } = this
let { steps, direction } = move
let countableSteps = (isNumber(toInt(steps))) && (toInt(steps) !== 0)
switch (direction) {
case '>':
if (steps === '>') {
Glide.index = length
} else if (this.isEnd()) {
if (!(Glide.isType('slider') && !Glide.settings.rewind)) {
this._o = true
Glide.index = 0
}
Events.emit('run.end', move)
} else if (countableSteps) {
Glide.index += Math.min(length - Glide.index, -toInt(steps))
} else {
Glide.index++
}
break
case '<':
if (steps === '<') {
Glide.index = 0
} else if (this.isStart()) {
if (!(Glide.isType('slider') && !Glide.settings.rewind)) {
this._o = true
Glide.index = length
}
Events.emit('run.start', move)
} else if (countableSteps) {
Glide.index -= Math.min(Glide.index, toInt(steps))
} else {
Glide.index--
}
break
case '=':
Glide.index = steps
break
}
},
/**
* Checks if we are on the first slide.
*
* @return {Boolean}
*/
isStart () {
return Glide.index === 0
},
/**
* Checks if we are on the last slide.
*
* @return {Boolean}
*/
isEnd () {
return Glide.index === this.length
},
/**
* Checks if we are making a offset run.
*
* @param {String} direction
* @return {Boolean}
*/
isOffset (direction) {
return this._o && this.move.direction === direction
}
}
define(Run, 'move', {
/**
* Gets value of the move schema.
*
* @returns {Object}
*/
get () {
return this._m
},
/**
* Sets value of the move schema.
*
* @returns {Object}
*/
set (value) {
this._m = {
direction: value.substr(0, 1),
steps: value.substr(1) ? value.substr(1) : 0
}
}
})
define(Run, 'length', {
/**
* Gets value of the running distance based
* on zero-indexing number of slides.
*
* @return {Number}
*/
get () {
let { settings } = Glide
let { length } = Components.Html.slides
// While number of slides inside instance is smaller
// that `perView` settings we should't run at all.
// Running distance has to be zero.
if (settings.perView > length) {
return 0
}
// If the `bound` option is acitve, a maximum running distance should be
// reduced by `perView` and `focusAt` settings. Running distance
// should end before creating an empty space after instance.
if (Glide.isType('slider') && settings.focusAt !== 'center' && settings.bound) {
return (length - 1) - (toInt(settings.perView) - 1) + toInt(settings.focusAt)
}
return length - 1
}
})
define(Run, 'offset', {
/**
* Gets status of the offsetting flag.
*
* @return {Boolean}
*/
get () {
return this._o
}
})
return Run
}