Skip to content

Commit

Permalink
refactor: move to const variables and simplify conditionals (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmed0saber authored Jul 18, 2023
1 parent 8dbad8c commit 236210c
Show file tree
Hide file tree
Showing 18 changed files with 67 additions and 88 deletions.
4 changes: 2 additions & 2 deletions src/components/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export default function (Glide, Components, Events) {
* @return {Void}
*/
activeClass () {
let classes = Glide.settings.classes
let slide = Components.Html.slides[Glide.index]
const classes = Glide.settings.classes
const slide = Components.Html.slides[Glide.index]

if (slide) {
slide.classList.add(classes.slide.active)
Expand Down
14 changes: 7 additions & 7 deletions src/components/clones.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ export default function (Glide, Components, Events) {
* @return {[]}
*/
collect (items = []) {
let { slides } = Components.Html
let { perView, classes, cloningRatio } = Glide.settings
const { slides } = Components.Html
const { perView, classes, cloningRatio } = Glide.settings

if (slides.length !== 0) {
if (slides.length > 0) {
const peekIncrementer = +!!Glide.settings.peek
const cloneCount = perView + peekIncrementer + Math.round(perView / 2)
const append = slides.slice(0, cloneCount).reverse()
const prepend = slides.slice(cloneCount * -1)

for (let r = 0; r < Math.max(cloningRatio, Math.floor(perView / slides.length)); r++) {
for (let i = 0; i < append.length; i++) {
let clone = append[i].cloneNode(true)
const clone = append[i].cloneNode(true)

clone.classList.add(classes.slide.clone)

items.push(clone)
}

for (let i = 0; i < prepend.length; i++) {
let clone = prepend[i].cloneNode(true)
const clone = prepend[i].cloneNode(true)

clone.classList.add(classes.slide.clone)

Expand All @@ -56,8 +56,8 @@ export default function (Glide, Components, Events) {
* @return {Void}
*/
append () {
let { items } = this
let { wrapper, slides } = Components.Html
const { items } = this
const { wrapper, slides } = Components.Html

const half = Math.floor(items.length / 2)
const prepend = items.slice(0, half).reverse()
Expand Down
18 changes: 7 additions & 11 deletions src/components/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (Glide, Components, Events) {
*/
const Binder = new EventsBinder()

let capture = (supportsPassive) ? { passive: true } : false
const capture = (supportsPassive) ? { passive: true } : false

const Controls = {
/**
Expand Down Expand Up @@ -93,13 +93,11 @@ export default function (Glide, Components, Events) {
return
}

if (item) {
item.classList.add(settings.classes.nav.active)
item.classList.add(settings.classes.nav.active)

siblings(item).forEach(sibling => {
sibling.classList.remove(settings.classes.nav.active)
})
}
siblings(item).forEach(sibling => {
sibling.classList.remove(settings.classes.nav.active)
})
},

/**
Expand All @@ -109,11 +107,9 @@ export default function (Glide, Components, Events) {
* @return {Void}
*/
removeClass (controls) {
let item = controls[Glide.index]
const item = controls[Glide.index]

if (item) {
item.classList.remove(Glide.settings.classes.nav.active)
}
item?.classList.remove(Glide.settings.classes.nav.active)
},

/**
Expand Down
2 changes: 1 addition & 1 deletion src/components/direction.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default function (Glide, Components, Events) {
* @returns {String}
*/
resolve (pattern) {
let token = pattern.slice(0, 1)
const token = pattern.slice(0, 1)

if (this.is('rtl')) {
return pattern.split(token).join(FLIPED_MOVEMENTS[token])
Expand Down
8 changes: 4 additions & 4 deletions src/components/gaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default function (Glide, Components, Events) {
*/
apply (slides) {
for (let i = 0, len = slides.length; i < len; i++) {
let style = slides[i].style
let direction = Components.Direction.value
const style = slides[i].style
const direction = Components.Direction.value

if (i !== 0) {
style[MARGIN_TYPE[direction][0]] = `${this.value / 2}px`
Expand All @@ -43,7 +43,7 @@ export default function (Glide, Components, Events) {
*/
remove (slides) {
for (let i = 0, len = slides.length; i < len; i++) {
let style = slides[i].style
const style = slides[i].style

style.marginLeft = ''
style.marginRight = ''
Expand Down Expand Up @@ -82,7 +82,7 @@ export default function (Glide, Components, Events) {
* @returns {Number}
*/
get () {
let perView = Glide.settings.perView
const perView = Glide.settings.perView

return (Gaps.value * (perView - 1)) / perView
}
Expand Down
9 changes: 3 additions & 6 deletions src/components/keyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,9 @@ export default function (Glide, Components, Events) {
press (event) {
const { perSwipe } = Glide.settings

if (event.code === 'ArrowRight') {
Components.Run.make(Components.Direction.resolve(`${perSwipe}>`))
}

if (event.code === 'ArrowLeft') {
Components.Run.make(Components.Direction.resolve(`${perSwipe}<`))
const arrowSymbols = { ArrowRight: '>', ArrowLeft: '<' }
if (['ArrowRight', 'ArrowLeft'].includes(event.code)) {
Components.Run.make(Components.Direction.resolve(`${perSwipe}${arrowSymbols[event.code]}`))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/move.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ export default function (Glide, Components, Events) {
* @return {Number}
*/
get () {
let offset = this.offset
let translate = this.translate
const offset = this.offset
const translate = this.translate

if (Components.Direction.is('rtl')) {
return translate + offset
Expand Down
4 changes: 2 additions & 2 deletions src/components/peek.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ export default function (Glide, Components, Events) {
* @returns {Number}
*/
get () {
let value = Peek.value
let perView = Glide.settings.perView
const value = Peek.value
const perView = Glide.settings.perView

if (isObject(value)) {
return (value.before / perView) + (value.after / perView)
Expand Down
6 changes: 3 additions & 3 deletions src/components/sizes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export default function (Glide, Components, Events) {
* @return {Void}
*/
setupSlides () {
let width = `${this.slideWidth}px`
let slides = Components.Html.slides
const width = `${this.slideWidth}px`
const slides = Components.Html.slides

for (let i = 0; i < slides.length; i++) {
slides[i].style.width = width
Expand All @@ -31,7 +31,7 @@ export default function (Glide, Components, Events) {
* @returns {Void}
*/
remove () {
let slides = Components.Html.slides
const slides = Components.Html.slides

for (let i = 0; i < slides.length; i++) {
slides[i].style.width = ''
Expand Down
28 changes: 14 additions & 14 deletions src/components/swipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function (Glide, Components, Events) {
let swipeStartX = 0
let swipeStartY = 0
let disabled = false
let capture = (supportsPassive) ? { passive: true } : false
const capture = (supportsPassive) ? { passive: true } : false

const Swipe = {
/**
Expand All @@ -43,7 +43,7 @@ export default function (Glide, Components, Events) {
if (!disabled && !Glide.disabled) {
this.disable()

let swipe = this.touches(event)
const swipe = this.touches(event)

swipeSin = null
swipeStartX = toInt(swipe.pageX)
Expand All @@ -65,14 +65,14 @@ export default function (Glide, Components, Events) {
if (!Glide.disabled) {
const { touchAngle, touchRatio, classes } = Glide.settings

let swipe = this.touches(event)
const swipe = this.touches(event)

let subExSx = toInt(swipe.pageX) - swipeStartX
let subEySy = toInt(swipe.pageY) - swipeStartY
let powEX = Math.abs(subExSx << 2)
let powEY = Math.abs(subEySy << 2)
let swipeHypotenuse = Math.sqrt(powEX + powEY)
let swipeCathetus = Math.sqrt(powEY)
const subExSx = toInt(swipe.pageX) - swipeStartX
const subEySy = toInt(swipe.pageY) - swipeStartY
const powEX = Math.abs(subExSx << 2)
const powEY = Math.abs(subEySy << 2)
const swipeHypotenuse = Math.sqrt(powEX + powEY)
const swipeCathetus = Math.sqrt(powEY)

swipeSin = Math.asin(swipeCathetus / swipeHypotenuse)

Expand Down Expand Up @@ -100,11 +100,11 @@ export default function (Glide, Components, Events) {
if (!Glide.disabled) {
const { perSwipe, touchAngle, classes } = Glide.settings

let swipe = this.touches(event)
let threshold = this.threshold(event)
const swipe = this.touches(event)
const threshold = this.threshold(event)

let swipeDistance = swipe.pageX - swipeStartX
let swipeDeg = swipeSin * 180 / Math.PI
const swipeDistance = swipe.pageX - swipeStartX
const swipeDeg = swipeSin * 180 / Math.PI

this.enable()

Expand Down Expand Up @@ -216,7 +216,7 @@ export default function (Glide, Components, Events) {
* @return {Number}
*/
threshold (event) {
let settings = Glide.settings
const settings = Glide.settings

if (MOUSE_EVENTS.indexOf(event.type) > -1) {
return settings.dragThreshold
Expand Down
10 changes: 5 additions & 5 deletions src/components/transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export default function (Glide, Components, Events) {
* @return {String}
*/
compose (property) {
let settings = Glide.settings
const settings = Glide.settings

if (!disabled) {
return `${property} ${this.duration}ms ${settings.animationTimingFunc}`
if (disabled) {
return `${property} 0ms ${settings.animationTimingFunc}`
}

return `${property} 0ms ${settings.animationTimingFunc}`
return `${property} ${this.duration}ms ${settings.animationTimingFunc}`
},

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ export default function (Glide, Components, Events) {
* @return {Number}
*/
get () {
let settings = Glide.settings
const settings = Glide.settings

if (Glide.isType('slider') && Components.Run.offset) {
return settings.rewindDuration
Expand Down
2 changes: 1 addition & 1 deletion src/components/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function (Glide, Components, Events) {
* @return {Void}
*/
set (value) {
let transform = mutator(Glide, Components).mutate(value)
const transform = mutator(Glide, Components).mutate(value)
const translate3d = `translate3d(${-1 * transform}px, 0px, 0px)`

Components.Html.wrapper.style.mozTransform = translate3d // needed for supported Firefox 10-15
Expand Down
2 changes: 1 addition & 1 deletion src/core/event/events-bus.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class EventsBus {
}

// Add the handler to queue
var index = this.events[event].push(handler) - 1
const index = this.events[event].push(handler) - 1

// Provide handle back for removal of event
return {
Expand Down
8 changes: 4 additions & 4 deletions src/mutator/transformers/focusing.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export default function (Glide, Components) {
* @return {Number}
*/
modify (translate) {
let gap = Components.Gaps.value
let width = Components.Sizes.width
let focusAt = Glide.settings.focusAt
let slideWidth = Components.Sizes.slideWidth
const gap = Components.Gaps.value
const width = Components.Sizes.width
const focusAt = Glide.settings.focusAt
const slideWidth = Components.Sizes.slideWidth

if (focusAt === 'center') {
return translate - (width / 2 - slideWidth / 2)
Expand Down
2 changes: 1 addition & 1 deletion src/mutator/transformers/peeking.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default function (Glide, Components) {
*/
modify (translate) {
if (Glide.settings.focusAt >= 0) {
let peek = Components.Peek.value
const peek = Components.Peek.value

if (isObject(peek)) {
return translate - peek.before
Expand Down
2 changes: 1 addition & 1 deletion src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export function siblings (node) {
if (node && node.parentNode) {
let n = node.parentNode.firstChild
let matched = []
const matched = []

for (; n; n = n.nextSibling) {
if (n.nodeType === 1 && n !== node) {
Expand Down
25 changes: 6 additions & 19 deletions src/utils/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,12 @@ export function mergeOptions (defaults, settings) {
if (settings.hasOwnProperty('classes')) {
options.classes = Object.assign({}, defaults.classes, settings.classes)

if (settings.classes.hasOwnProperty('direction')) {
options.classes.direction = Object.assign({}, defaults.classes.direction, settings.classes.direction)
}

if (settings.classes.hasOwnProperty('type')) {
options.classes.type = Object.assign({}, defaults.classes.type, settings.classes.type)
}

if (settings.classes.hasOwnProperty('slide')) {
options.classes.slide = Object.assign({}, defaults.classes.slide, settings.classes.slide)
}

if (settings.classes.hasOwnProperty('arrow')) {
options.classes.arrow = Object.assign({}, defaults.classes.arrow, settings.classes.arrow)
}

if (settings.classes.hasOwnProperty('nav')) {
options.classes.nav = Object.assign({}, defaults.classes.nav, settings.classes.nav)
}
const properties = ['direction', 'type', 'slide', 'arrow', 'nav']
properties.forEach(property => {
if (settings.classes.hasOwnProperty(property)) {
options.classes[property] = { ...defaults.classes[property], ...settings.classes[property] }
}
})
}

if (settings.hasOwnProperty('breakpoints')) {
Expand Down
7 changes: 3 additions & 4 deletions src/utils/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ import { now } from './time'
*
* @see https://github.com/jashkenas/underscore
*/
export function throttle (func, wait, options) {
export function throttle (func, wait, options = {}) {
let timeout, context, args, result
let previous = 0
if (!options) options = {}

let later = function () {
const later = function () {
previous = options.leading === false ? 0 : now()
timeout = null
result = func.apply(context, args)
if (!timeout) context = args = null
}

let throttled = function () {
const throttled = function () {
let at = now()
if (!previous && options.leading === false) previous = at
let remaining = wait - (at - previous)
Expand Down

0 comments on commit 236210c

Please sign in to comment.