1
0
mirror of https://github.com/locomotivemtl/locomotive-boilerplate.git synced 2026-01-15 00:55:08 +08:00

Add removeCustomEvent util

This commit is contained in:
Lucas Vallenet
2022-05-26 13:32:52 +02:00
parent de0c4993cb
commit e20111fd2e

View File

@@ -36,7 +36,17 @@ const isValidElement = $el => (isWindow($el) || isDomElement($el))
* @return {Boolean} True if the event is already attached to the element * @return {Boolean} True if the event is already attached to the element
*/ */
const eventIsListened = ($el, event) => CUSTOM_EVENT_LISTENERS.findIndex(e => e.$el === $el && e.event === event) > -1 const getCustomEventIndex = ($el, event) => CUSTOM_EVENT_LISTENERS.findIndex(e => e.$el === $el && e.event === event)
/**
* Check if element already has the event attached
* @param {Element} $el - Element where the event is attached
* @param {String} event - The event name
* @return {Boolean} True if the event is already attached to the element
*/
const customEventIsDefined = ($el, event) => getCustomEventIndex($el, event) > -1
/** /**
@@ -46,8 +56,8 @@ const eventIsListened = ($el, event) => CUSTOM_EVENT_LISTENERS.findIndex(e => e.
* @return {void} * @return {void}
*/ */
const addToEventListeners = ($el, event) => { const addCustomEvent = ($el, event) => {
if(!eventIsListened($el, event)) { if(!customEventIsDefined($el, event)) {
CUSTOM_EVENT_LISTENERS.push({ CUSTOM_EVENT_LISTENERS.push({
$el, $el,
event event
@@ -56,6 +66,21 @@ const addToEventListeners = ($el, event) => {
} }
/**
* Remove custom event to event storage
* @param {Element} $el - Element where the event is attached
* @param {String} event - The event name
* @return {void}
*/
const removeCustomEvent = ($el, event) => {
const customEventIndex = getCustomEventIndex($el, event)
if(customEventIndex > -1) {
CUSTOM_EVENT_LISTENERS.splice(customEventIndex, 1)
}
}
/** /**
* Create a sarting event for the event triggered * Create a sarting event for the event triggered
* @param {Element} $el - Element to bind the event to * @param {Element} $el - Element to bind the event to
@@ -74,12 +99,12 @@ const addStartEvent = ($el, event, delay = 200) => {
const eventName = `${event}Start` const eventName = `${event}Start`
// Check if event already exists // Check if event already exists
if(eventIsListened($el, eventName)) { if(customEventIsDefined($el, eventName)) {
return return
} }
// Register element and event // Register element and event
addToEventListeners($el, eventName) addCustomEvent($el, eventName)
// Create event // Create event
const startEvent = new CustomEvent(eventName) const startEvent = new CustomEvent(eventName)
@@ -106,12 +131,12 @@ const addEndEvent = ($el, event, delay = 200) => {
const eventName = `${event}End` const eventName = `${event}End`
// Check if event already exists // Check if event already exists
if(eventIsListened($el, eventName)) { if(customEventIsDefined($el, eventName)) {
return return
} }
// Register element and event // Register element and event
addToEventListeners($el, eventName) addCustomEvent($el, eventName)
// Create event // Create event
const endEvent = new CustomEvent(eventName) const endEvent = new CustomEvent(eventName)
@@ -200,5 +225,6 @@ export {
addStartEvent, addStartEvent,
addEndEvent, addEndEvent,
addScrollUpEvent, addScrollUpEvent,
addScrollDownEvent addScrollDownEvent,
removeCustomEvent
} }