Skip to content

Commit

Permalink
Local styles and scripts: Rewrite components to accept shared base, u…
Browse files Browse the repository at this point in the history
…tilities, and config such as CSS class prefix
  • Loading branch information
eliot-akira committed Sep 19, 2024
1 parent 462d976 commit 4886b75
Show file tree
Hide file tree
Showing 41 changed files with 4,579 additions and 3,814 deletions.
141 changes: 73 additions & 68 deletions alert.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,85 @@
import BaseComponent, { DATA_PREFIX } from './base-component'
import EventHandler from './dom/event-handler'
import { enableDismissTrigger } from './utilities/component-functions'
import { defineJQueryPlugin } from './utilities'

/**
* Constants
*/

const NAME = 'alert'
const DATA_KEY = `${DATA_PREFIX}.alert`
const EVENT_KEY = `.${DATA_KEY}`

const EVENT_CLOSE = `close${EVENT_KEY}`
const EVENT_CLOSED = `closed${EVENT_KEY}`
const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_SHOW = 'show'

/**
* Class definition
*/

class Alert extends BaseComponent {
// Getters
static get NAME() {
return NAME
}

// Public
close() {
const closeEvent = EventHandler.trigger(this._element, EVENT_CLOSE)

if (closeEvent.defaultPrevented) {
return
export default function createAlert({
BaseComponent,
CLASS_PREFIX,
DATA_PREFIX,
enableDismissTrigger,
EventHandler,
}) {
/**
* Constants
*/

const NAME = 'alert'
const DATA_KEY = `${DATA_PREFIX}.alert`
const EVENT_KEY = `.${DATA_KEY}`

const EVENT_CLOSE = `close${EVENT_KEY}`
const EVENT_CLOSED = `closed${EVENT_KEY}`
const CLASS_NAME_FADE = `${CLASS_PREFIX}fade`
const CLASS_NAME_SHOW = `${CLASS_PREFIX}show`

/**
* Class definition
*/

class Alert extends BaseComponent {
// Getters
static get NAME() {
return NAME
}

this._element.classList.remove(CLASS_NAME_SHOW)

const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
this._queueCallback(() => this._destroyElement(), this._element, isAnimated)
}

// Private
_destroyElement() {
this._element.remove()
EventHandler.trigger(this._element, EVENT_CLOSED)
this.dispose()
}

// Static
static jQueryInterface(config) {
return this.each(function () {
const data = Alert.getOrCreateInstance(this)
// Public
close() {
const closeEvent = EventHandler.trigger(this._element, EVENT_CLOSE)

if (typeof config !== 'string') {
if (closeEvent.defaultPrevented) {
return
}

if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
throw new TypeError(`No method named "${config}"`)
}
this._element.classList.remove(CLASS_NAME_SHOW)

data[config](this)
})
}
}
const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
this._queueCallback(
() => this._destroyElement(),
this._element,
isAnimated,
)
}

/**
* Data API implementation
*/
// Private
_destroyElement() {
this._element.remove()
EventHandler.trigger(this._element, EVENT_CLOSED)
this.dispose()
}

enableDismissTrigger(Alert, 'close')
// Static
static jQueryInterface(config) {
return this.each(function () {
const data = Alert.getOrCreateInstance(this)

if (typeof config !== 'string') {
return
}

if (
data[config] === undefined ||
config.startsWith('_') ||
config === 'constructor'
) {
throw new TypeError(`No method named "${config}"`)
}

data[config](this)
})
}
}

/**
* jQuery
*/
/**
* Data API implementation
*/

// defineJQueryPlugin(Alert)
enableDismissTrigger(Alert, 'close')

export default Alert
return Alert
}
32 changes: 32 additions & 0 deletions all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { create } from './index'

// These can be imported individually
import Alert from './alert'
import Button from './button'
import Carousel from './carousel'
import Collapse from './collapse'
import Dropdown from './dropdown'
import Modal from './modal'
import Offcanvas from './offcanvas'
import Popover from './popover'
import ScrollSpy from './scrollspy'
import Tab from './tab'
import Toast from './toast'
import Tooltip from './tooltip'

create({
components: {
Alert,
Button,
Carousel,
Collapse,
Dropdown,
Modal,
Offcanvas,
Popover,
ScrollSpy,
Tab,
Toast,
Tooltip,
},
})
92 changes: 0 additions & 92 deletions base-component.ts

This file was deleted.

97 changes: 97 additions & 0 deletions base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import Data from './dom/data'
import EventHandler from './dom/event-handler'
// import Config from './utilities/config'
import { executeAfterTransition, getElement } from './utilities'

/**
* Constants
*/

const VERSION = '2024.09.18' // Fork of Bootstrap 5.3.3

export default function createBaseComponent({
Config,
DATA_PREFIX,
DATA_PREFIX_BASE,
}) {
/**
* Class definition
*/

class BaseComponent extends Config {
constructor(element, config) {
super()

element = getElement(element)
if (!element) {
return
}

this._element = element
this._config = this._getConfig(config)

Data.set(this._element, this.constructor.DATA_KEY, this)
}

// Public
dispose() {
Data.remove(this._element, this.constructor.DATA_KEY)
EventHandler.off(this._element, this.constructor.EVENT_KEY)

for (const propertyName of Object.getOwnPropertyNames(this)) {
this[propertyName] = null
}
}

_queueCallback(callback, element, isAnimated = true) {
executeAfterTransition(callback, element, isAnimated)
}

_getConfig(config) {
config = this._mergeConfigObj(config, this._element)
config = this._configAfterMerge(config)
this._typeCheckConfig(config)
return config
}

// Static
static getInstance(element) {
return Data.get(getElement(element), this.DATA_KEY)
}

static getOrCreateInstance(element, config = {}) {
return (
this.getInstance(element) ||
new this(element, typeof config === 'object' ? config : null)
)
}

static get VERSION() {
return VERSION
}

static get DATA_PREFIX() {
return DATA_PREFIX
}

static set DATA_PREFIX(_prefix: string) {
let prefix = _prefix.replace(/\-$/, '')
DATA_PREFIX_BASE = prefix
DATA_PREFIX = `${prefix}-`
}

static get DATA_KEY() {
return `${this.DATA_PREFIX}.${this.NAME}`
}

static get EVENT_KEY() {
return `.${this.DATA_KEY}`
}

static eventName(name) {
return `${name}${this.EVENT_KEY}`
}
}

return BaseComponent
}
2 changes: 1 addition & 1 deletion build/design.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/design.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 4886b75

Please sign in to comment.