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 14, 2021
1 parent 0cf4ad7 commit 902aee3
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 253 deletions.
16 changes: 2 additions & 14 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 @@ -32,9 +30,7 @@ const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_SHOW = 'show'

/**
* ------------------------------------------------------------------------
* 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
15 changes: 2 additions & 13 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 Down Expand Up @@ -38,9 +36,7 @@ const EVENT_FOCUS_BLUR_DATA_API = `focus${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
53 changes: 21 additions & 32 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,24 +23,6 @@ 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 DIRECTION_NEXT = 'next'
const DIRECTION_PREV = 'prev'
const DIRECTION_LEFT = 'left'
Expand Down Expand Up @@ -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
36 changes: 12 additions & 24 deletions js/src/collapse.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 = 'collapse'
Expand All @@ -21,16 +19,6 @@ const EVENT_KEY = `.${DATA_KEY}`
const DATA_API_KEY = '.data-api'
const JQUERY_NO_CONFLICT = $.fn[NAME]

const Default = {
toggle: true,
parent: ''
}

const DefaultType = {
toggle: 'boolean',
parent: '(string|element)'
}

const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_HIDE = `hide${EVENT_KEY}`
Expand All @@ -48,10 +36,18 @@ const DIMENSION_HEIGHT = 'height'
const SELECTOR_ACTIVES = '.show, .collapsing'
const SELECTOR_DATA_TOGGLE = '[data-toggle="collapse"]'

const Default = {
toggle: true,
parent: ''
}

const DefaultType = {
toggle: 'boolean',
parent: '(string|element)'
}

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

class Collapse {
Expand Down Expand Up @@ -89,7 +85,6 @@ class Collapse {
}

// Getters

static get VERSION() {
return VERSION
}
Expand All @@ -99,7 +94,6 @@ class Collapse {
}

// Public

toggle() {
if ($(this._element).hasClass(CLASS_NAME_SHOW)) {
this.hide()
Expand Down Expand Up @@ -262,7 +256,6 @@ class Collapse {
}

// Private

_getConfig(config) {
config = {
...Default,
Expand Down Expand Up @@ -316,7 +309,6 @@ class Collapse {
}

// Static

static _getTargetFromElement(element) {
const selector = Util.getSelectorFromElement(element)
return selector ? document.querySelector(selector) : null
Expand Down Expand Up @@ -353,9 +345,7 @@ class Collapse {
}

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

$(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
Expand All @@ -377,9 +367,7 @@ $(document).on(EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (event) {
})

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

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

0 comments on commit 902aee3

Please sign in to comment.