- bindMultiClick()
- bindNodeList()
- CallbackEvents()
- debounce()
- docReady()
- Draggables()
- EventDispatcher
- getFocusable
- tabFocusLock
bindMultiClick - Bind single and double click on one element
bindMultiClick( element, single, double, timeout = 350, beforeDelayed = null ) // void
Bind a single and a double click event on the same element without causing event collisions.
Parameter | Type | Default | Description |
---|---|---|---|
element | HTMLElement | - | Element to bind handlers to |
single | Function | - | Single click callback |
double | Function | - | Double click callback |
timeout | Number | 350 | Click frequency timeout |
beforeDelayed | Function | null | Callback to run for each delayed single click |
Type/Value | Description |
---|---|
void | None. |
// Bind the events
bindMultiClick( element, () => {
console.log( 'single click action' );
}, () => {
console.log( 'double click action' );
} );
bindNodeList - Bind multiple events to each node
bindNodeList( elements, events, unbind = false ) // void
Bind or unbind multiple event listeners on each element in a NodeList or Array.
Parameter | Type | Default | Description |
---|---|---|---|
elements | Array/NodeList | - | Array or NodeList of elements to bind |
events | Array | - | Array of listener argument arrays |
unbind | Boolean | false | Remove event listeners |
Type/Value | Description |
---|---|
void | None. |
const events = [
[ 'click', ( event ) => console.log( 'click' ) ],
[ 'focus', ( event ) => console.log( 'focus' ) ],
[ 'blur', ( event ) => console.log( 'blur' ) ],
];
// Bind the events
bindNodeList( document.querySelectorAll( 'a' ), events );
// Unbind the events
bindNodeList( document.querySelectorAll( 'a' ), events, true );
CallbackEvents class - Abstract class used for creating dom event connected or EventTarget alike objects. The class extends EventDispatcher class.
class CallbackEvents extends EventDispatcher {
constructor( element = null, scope = window, prefix = 'callback.', debug = null ) {}
}
For more details check the CallbackEvents source file.
All EventDispatcher methods are available and event names should be the callback name that is called on given context.
debounce - Debounce event
debounce( func, delay = null ) // Function
Function wrapper to debounce event listeners on events like window.resize, window.scroll or mouse.move and cause them to cancel until below a given frequency.
Parameter | Type | Default | Description |
---|---|---|---|
func | Function | - | Function to debounce |
delay | Number | 350 | Frequency delay in ms |
Type/Value | Description |
---|---|
void | None. |
window.addEventListener( 'resize', debounce( () => console.log( 'resize::complete' ) ) );
docReady - Document ready callback
docReady( callback ) // void
Run a given callback when the document is ready, checks document.readyState complete or binds a DOMContentLoaded listener to run the callback.
Parameter | Type | Default | Description |
---|---|---|---|
callback | Function | - | Callback to run when ready |
Type/Value | Description |
---|---|
void | None. |
docReady( () => console.log( 'ready steady go!' ) );
Draggables class - Mouse drag handler for multiple elements.
class Draggables {
constructor( draggables = null, context = window ) {}
thresholdX : Number
thresholdY : Number
bind( draggables ) {} // void
}
Draggable object definition:
const draggable = {
draggable : HTMLElement,
container : HTMLElement,
onbefore : ( event, _dgbl ) => {}, // udefined|false
onstart : ( event, _dgbl ) => {}, // void
onend : ( event, position, delta, _dgbl ) => {}, // void
onmove : ( event, position, delta, _dgbl ) => {}, // void
onclick : ( event, position, delta, _dgbl ) => {}, // void
axis : ('both'|'x'|'y'),
offsetX : ('start'|'left'|'center'|'middle'|'right'|'end'),
offsetY : ('start'|'top'|'center'|'middle'|'bottom'|'end'),
overflowX : Boolean,
overflowY : Boolean,
local : Boolean,
}
For more details check the Draggables source file.
EventDispatcher class - Abstract class used for creating dom event connected or EventTarget alike objects.
class EventDispatcher {
static isCompat( obj ) {} // Boolean
constructor( element = null, parent = null, debug = null ) {}
debug : null|Console
target : null|window|document|HTMLElement|EventTarget
parent : null|EventDispatcher
isSimulated : Boolean
hasSimulated( name ) {} // Boolean
dispatchEvent( name, detail = null, bubbles = true, cancelable = false ) {} // Boolean
addEventListener( name, callback, useCaptureOptions = false ) {} // void
removeEventListener( name, callback, useCaptureOptions = false ) {} // void
addEventList( events ) {} // void
}
For more details check the EventDispatcher source file.
When using the simulated mode (with null as target), the events are bubbled to the parent element manually unless event.stopPropagation() was called in a listener. When bubbling manually, you can use event.detail.target and event.detail.current just as target and currentTarget with normal dom events, when a valid object is bound as target then the detail properties will always be the object that triggered the event.
getFocusable - Get focusable element from context
getFocusable( context, last = false, selector = null ) // null|HTMLElement
Get first or last focusable element from given context using a selector.
Parameter | Type | Default | Description |
---|---|---|---|
context | HTMLElement | - | Context to select from |
last | Boolean | false | Get first or last element, false for first element, true for last |
selector | Null/String | null | Element selector, default: a, button, input, select, textarea, [tabindex]:not([tabindex="-1"]) |
Type/Value | Description |
---|---|
HTMLElement | Focusable element |
null | Empty. |
const element = getFocusable( document.getElementById( 'dialog-id' ) );
if ( element ) element.focus();
tabFocusLock - Lock tab focus to context and return unbind function.
tabFocusLock( context, condition = true, loop = true, selector = null ) // Function
Restrict tab focus to a given element context, will loop tab focus, by focusing the corresponding first or last element when leaving the context.
Parameter | Type | Default | Description |
---|---|---|---|
context | HTMLElement | - | Context to restrict tab focus to |
condition | Boolean/Function | true | Function that return a boolean to enable or disable tab focus restriction |
loop | Boolean | true | Loop element focus |
selector | HTMLElement | null | Focusable selector, see the getFocusable function |
Type/Value | Description |
---|---|
Function | Function to unbind event handler. |
const remove = tabFocusLock( document.getElementById( 'dialog-id' ) );
// Call remove(); to unbind the handler if not needed anymore.