Skip to content

Commit

Permalink
JS: minor refactoring
Browse files Browse the repository at this point in the history
* shorten block comments
* reorder constants
  • Loading branch information
XhmikosR committed Oct 22, 2021
1 parent 4f0187c commit fe73da9
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 309 deletions.
24 changes: 6 additions & 18 deletions js/src/alert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import $ from 'jquery'
import Util from './util'

/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/

const NAME = 'alert'
Expand All @@ -21,20 +19,18 @@ const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]

const SELECTOR_DISMISS = '[data-dismiss="alert"]'
const CLASS_NAME_ALERT = 'alert'
const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_SHOW = 'show'

const EVENT_CLOSE = `close${EVENT_KEY}`
const EVENT_CLOSED = `closed${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

const CLASS_NAME_ALERT = 'alert'
const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_SHOW = 'show'
const SELECTOR_DISMISS = '[data-dismiss="alert"]'

/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
* Class definition
*/

class Alert {
Expand All @@ -43,13 +39,11 @@ class Alert {
}

// Getters

static get VERSION() {
return VERSION
}

// Public

close(element) {
let rootElement = this._element
if (element) {
Expand All @@ -71,7 +65,6 @@ class Alert {
}

// Private

_getRootElement(element) {
const selector = Util.getSelectorFromElement(element)
let parent = false
Expand Down Expand Up @@ -117,7 +110,6 @@ class Alert {
}

// Static

static _jQueryInterface(config) {
return this.each(function () {
const $element = $(this)
Expand Down Expand Up @@ -146,9 +138,7 @@ class Alert {
}

/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
* Data API implementation
*/

$(document).on(
Expand All @@ -158,9 +148,7 @@ $(document).on(
)

/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/

$.fn[NAME] = Alert._jQueryInterface
Expand Down
25 changes: 7 additions & 18 deletions js/src/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import $ from 'jquery'

/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/

const NAME = 'button'
Expand All @@ -24,6 +22,11 @@ const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_BUTTON = 'btn'
const CLASS_NAME_FOCUS = 'focus'

const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_FOCUS_BLUR_DATA_API = `focus${EVENT_KEY}${DATA_API_KEY} ` +
`blur${EVENT_KEY}${DATA_API_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`

const SELECTOR_DATA_TOGGLE_CARROT = '[data-toggle^="button"]'
const SELECTOR_DATA_TOGGLES = '[data-toggle="buttons"]'
const SELECTOR_DATA_TOGGLE = '[data-toggle="button"]'
Expand All @@ -32,15 +35,8 @@ const SELECTOR_INPUT = 'input:not([type="hidden"])'
const SELECTOR_ACTIVE = '.active'
const SELECTOR_BUTTON = '.btn'

const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
const EVENT_FOCUS_BLUR_DATA_API = `focus${EVENT_KEY}${DATA_API_KEY} ` +
`blur${EVENT_KEY}${DATA_API_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`

/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
* Class definition
*/

class Button {
Expand All @@ -50,13 +46,11 @@ class Button {
}

// Getters

static get VERSION() {
return VERSION
}

// Public

toggle() {
let triggerChangeEvent = true
let addAriaPressed = true
Expand Down Expand Up @@ -111,7 +105,6 @@ class Button {
}

// Static

static _jQueryInterface(config, avoidTriggerChange) {
return this.each(function () {
const $element = $(this)
Expand All @@ -132,9 +125,7 @@ class Button {
}

/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
* Data API implementation
*/

$(document)
Expand Down Expand Up @@ -194,9 +185,7 @@ $(window).on(EVENT_LOAD_DATA_API, () => {
})

/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/

$.fn[NAME] = Button._jQueryInterface
Expand Down
69 changes: 29 additions & 40 deletions js/src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import $ from 'jquery'
import Util from './util'

/**
* ------------------------------------------------------------------------
* Constants
* ------------------------------------------------------------------------
*/

const NAME = 'carousel'
Expand All @@ -25,23 +23,14 @@ const ARROW_RIGHT_KEYCODE = 39 // KeyboardEvent.which value for right arrow key
const TOUCHEVENT_COMPAT_WAIT = 500 // Time for mouse compat events to fire after touch
const SWIPE_THRESHOLD = 40

const Default = {
interval: 5000,
keyboard: true,
slide: false,
pause: 'hover',
wrap: true,
touch: true
}

const DefaultType = {
interval: '(number|boolean)',
keyboard: 'boolean',
slide: '(boolean|string)',
pause: '(string|boolean)',
wrap: 'boolean',
touch: 'boolean'
}
const CLASS_NAME_CAROUSEL = 'carousel'
const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_SLIDE = 'slide'
const CLASS_NAME_RIGHT = 'carousel-item-right'
const CLASS_NAME_LEFT = 'carousel-item-left'
const CLASS_NAME_NEXT = 'carousel-item-next'
const CLASS_NAME_PREV = 'carousel-item-prev'
const CLASS_NAME_POINTER_EVENT = 'pointer-event'

const DIRECTION_NEXT = 'next'
const DIRECTION_PREV = 'prev'
Expand All @@ -62,15 +51,6 @@ const EVENT_DRAG_START = `dragstart${EVENT_KEY}`
const EVENT_LOAD_DATA_API = `load${EVENT_KEY}${DATA_API_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`

const CLASS_NAME_CAROUSEL = 'carousel'
const CLASS_NAME_ACTIVE = 'active'
const CLASS_NAME_SLIDE = 'slide'
const CLASS_NAME_RIGHT = 'carousel-item-right'
const CLASS_NAME_LEFT = 'carousel-item-left'
const CLASS_NAME_NEXT = 'carousel-item-next'
const CLASS_NAME_PREV = 'carousel-item-prev'
const CLASS_NAME_POINTER_EVENT = 'pointer-event'

const SELECTOR_ACTIVE = '.active'
const SELECTOR_ACTIVE_ITEM = '.active.carousel-item'
const SELECTOR_ITEM = '.carousel-item'
Expand All @@ -80,16 +60,33 @@ const SELECTOR_INDICATORS = '.carousel-indicators'
const SELECTOR_DATA_SLIDE = '[data-slide], [data-slide-to]'
const SELECTOR_DATA_RIDE = '[data-ride="carousel"]'

const Default = {
interval: 5000,
keyboard: true,
slide: false,
pause: 'hover',
wrap: true,
touch: true
}

const DefaultType = {
interval: '(number|boolean)',
keyboard: 'boolean',
slide: '(boolean|string)',
pause: '(string|boolean)',
wrap: 'boolean',
touch: 'boolean'
}

const PointerType = {
TOUCH: 'touch',
PEN: 'pen'
}

/**
* ------------------------------------------------------------------------
* Class Definition
* ------------------------------------------------------------------------
* Class definition
*/

class Carousel {
constructor(element, config) {
this._items = null
Expand All @@ -111,7 +108,6 @@ class Carousel {
}

// Getters

static get VERSION() {
return VERSION
}
Expand All @@ -121,7 +117,6 @@ class Carousel {
}

// Public

next() {
if (!this._isSliding) {
this._slide(DIRECTION_NEXT)
Expand Down Expand Up @@ -220,7 +215,6 @@ class Carousel {
}

// Private

_getConfig(config) {
config = {
...Default,
Expand Down Expand Up @@ -508,7 +502,6 @@ class Carousel {
}

// Static

static _jQueryInterface(config) {
return this.each(function () {
let data = $(this).data(DATA_KEY)
Expand Down Expand Up @@ -580,9 +573,7 @@ class Carousel {
}

/**
* ------------------------------------------------------------------------
* Data Api implementation
* ------------------------------------------------------------------------
* Data API implementation
*/

$(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_SLIDE, Carousel._dataApiClickHandler)
Expand All @@ -596,9 +587,7 @@ $(window).on(EVENT_LOAD_DATA_API, () => {
})

/**
* ------------------------------------------------------------------------
* jQuery
* ------------------------------------------------------------------------
*/

$.fn[NAME] = Carousel._jQueryInterface
Expand Down
Loading

0 comments on commit fe73da9

Please sign in to comment.