From 87f607bf0b8a31e89cf359c4d6c615efac00960d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Har=C4=99=C5=BClak?= Date: Fri, 14 Oct 2022 09:57:32 +0200 Subject: [PATCH] Prefer const over let & update variables names --- addon/-private/state.js | 16 +++++++--------- addon/components/burger-menu.js | 26 ++++++++++++-------------- addon/computed/style-for.js | 6 +++--- addon/mixins/swipe-support.js | 30 ++++++++++++------------------ addon/utils/css-stringify.js | 8 ++++---- 5 files changed, 38 insertions(+), 48 deletions(-) diff --git a/addon/-private/state.js b/addon/-private/state.js index 5d987d2..4bb62a4 100644 --- a/addon/-private/state.js +++ b/addon/-private/state.js @@ -18,32 +18,30 @@ export default class State extends EmberObject { @computed('animation', 'itemAnimation', 'customAnimation') get styles() { - let animation = this.animation; - let itemAnimation = this.itemAnimation; - let customAnimation = this.customAnimation; + const { animation, itemAnimation, customAnimation } = this; return getAnimation(customAnimation || animation, itemAnimation).create(); } @action - openMenu(e) { - e?.stopPropagation?.(); + openMenu(event) { + event?.stopPropagation?.(); if (!this.locked) { this.open = true; } } @action - closeMenu(e) { - e?.stopPropagation?.(); + closeMenu(event) { + event?.stopPropagation?.(); if (!this.locked) { this.open = false; } } @action - toggleMenu(e) { - e?.stopPropagation?.(); + toggleMenu(event) { + event?.stopPropagation?.(); if (!this.locked) { this.open = !this.open; } diff --git a/addon/components/burger-menu.js b/addon/components/burger-menu.js index 69c5bc0..31ebd69 100644 --- a/addon/components/burger-menu.js +++ b/addon/components/burger-menu.js @@ -39,33 +39,33 @@ export default Component.extend(SwipeSupportMixin, { style: computedStyleFor('container').readOnly(), animationClass: computed('state.styles.animation', function () { - let animation = this.get('state.styles.animation'); + const animation = this.get('state.styles.animation'); return animation ? `bm--${animation}` : ''; }).readOnly(), itemAnimationClass: computed('state.styles.itemAnimation', function () { - let itemAnimation = this.get('state.styles.itemAnimation'); + const itemAnimation = this.get('state.styles.itemAnimation'); return itemAnimation ? `bm-item--${itemAnimation}` : ''; }).readOnly(), init() { this._super(...arguments); - this.onClick = (e) => { + this.onClick = (event) => { if (this.dismissOnClick) { - let elementId = this.elementId; + const { elementId } = this; // Close the menu if clicked outside of it - if (!e.target.closest(`#${elementId} .bm-menu`)) { + if (!event.target.closest(`#${elementId} .bm-menu`)) { this.get('state').closeMenu(); } } }; - this.onKeyup = (e) => { - if (this.dismissOnEsc) { - if (e.keyCode === 27) { - this.get('state').closeMenu(); - } + this.onKeyup = (event) => { + const escapeKeyPressed = event.keyCode === 27; + + if (this.dismissOnEsc && escapeKeyPressed) { + this.get('state').closeMenu(); } }; @@ -99,10 +99,8 @@ export default Component.extend(SwipeSupportMixin, { }, onSwipe(direction, target) { - let position = this.position; - let open = this.open; - let gesturesEnabled = this.gesturesEnabled; - let isMenuSwipe = target.closest('.bm-menu'); + const { position, open, gesturesEnabled } = this; + const isMenuSwipe = target.closest('.bm-menu'); if (!gesturesEnabled) { return; diff --git a/addon/computed/style-for.js b/addon/computed/style-for.js index 4539bec..73d1cb1 100644 --- a/addon/computed/style-for.js +++ b/addon/computed/style-for.js @@ -2,11 +2,11 @@ import { computed } from '@ember/object'; export default function computedStyleFor(type) { return computed('state.{styles,open,width,position}', 'index', function () { - let state = this.state; - let args = state.getProperties(['open', 'width', 'position']); + const { state, index } = this; + const args = state.getProperties(['open', 'width', 'position']); if (type === 'menuItem') { - args.index = this.index; + args.index = index; } return state.get('styles').generateCSSFor(type, args); diff --git a/addon/mixins/swipe-support.js b/addon/mixins/swipe-support.js index 4d845c9..96b23fc 100644 --- a/addon/mixins/swipe-support.js +++ b/addon/mixins/swipe-support.js @@ -10,33 +10,29 @@ export default Mixin.create({ onSwipe(/* direction, target */) {}, - touchStart(e) { + touchStart(event) { this._super(...arguments); - // jscs:disable - let touch = e.touches[0]; - // jscs:enable + const { pageX, pageY } = event.touches[0]; meta = { - target: e.target, + target: event.target, start: { - x: touch.pageX, - y: touch.pageY, + x: pageX, + y: pageY, time: new Date().getTime(), }, }; }, - touchMove(e) { + touchMove({ touches = [] }) { this._super(...arguments); - // jscs:disable - let touch = e.touches[0]; - // jscs:enable + const { pageX, pageY } = touches[0]; meta.differences = { - x: touch.pageX - meta.start.x, - y: touch.pageY - meta.start.y, + x: pageX - meta.start.x, + y: pageY - meta.start.y, }; // Compute swipe direction @@ -46,17 +42,15 @@ export default Mixin.create({ } // A valid swipe event uses only one finger - if (e.touches.length > 1) { + if (touches.length > 1) { meta.isInvalid = true; } }, touchEnd() { this._super(...arguments); - - let minSwipeDistance = this.minSwipeDistance; - let maxSwipeTime = this.maxSwipeTime; - let elapsedTime = new Date().getTime() - meta.start.time; + const { minSwipeDistance, maxSwipeTime } = this; + const elapsedTime = new Date().getTime() - meta.start.time; if ( meta.isHorizontal && diff --git a/addon/utils/css-stringify.js b/addon/utils/css-stringify.js index 2feef9d..b09d8e3 100644 --- a/addon/utils/css-stringify.js +++ b/addon/utils/css-stringify.js @@ -19,7 +19,7 @@ export default function cssStringify(hash = {}) { return htmlSafe( Object.keys(hash) .reduce((css, key) => { - let value = hash[key]; + const value = hash[key]; if (!isEmpty(value)) { css = css.concat(buildProp(key, value)); @@ -33,11 +33,11 @@ export default function cssStringify(hash = {}) { function buildProp(key, value) { key = dasherize(key); - let css = [`${key}: ${value}`]; + const css = [`${key}: ${value}`]; if (PREFIXED_PROPS.indexOf(key) > -1) { - PREFIXES.forEach((p) => { - css.push(`-${p}-${key}: ${value}`); + PREFIXES.forEach((prefix) => { + css.push(`-${prefix}-${key}: ${value}`); }); }