ptut-vhost/webpack/lib/onblur.js

81 lines
1.9 KiB
JavaScript
Raw Normal View History

/* OnBlur Manager Class */
export class OnBlurManager{
/* (1) Initialize an OnBlurManager
*
* @root<Element> The root element to work in
*
---------------------------------------------------------*/
constructor(root){
/* (1) Error: invalid @root argument */
if( !(root instanceof Element) )
throw new Error(`[OnBlurManager::new] expected argument to be of type (Element), received (${typeof root})`);
/* (2) Store as attribute */
this.root = root;
/* (3) Initialize @callback list */
this.callback = {};
/* (4) Bind to event */
this.root.addEventListener('click', function(e){
// launch each callback
for( var c of Object.keys(this.callback) )
this.callback[c](e);
}.bind(this) );
}
/* (2) Add a @callback
*
* @index<String> String to name the callback for removing
* @callback<Function> Function to link to callback
*
* @return linked<bool> Whether the callback has been linked
*
---------------------------------------------------------*/
link(index, callback){
/* (1) Fail: invalid @index or @callback arguments */
if( typeof index !== 'string' || !(callback instanceof Function) ){
console.error(`[OnBlurManager::link] expected (String, Function), received (${typeof index}, ${typeof callback})`);
return false;
}
/* (2) Add to list of callbacks */
return ( this.callback[index] = callback ) instanceof Function;
}
/* (3) Unlink a @callback
*
* @index<String> Name of the callback to remove
*
* @return unlinked<bool> Whether the callback has been unlinked
*
---------------------------------------------------------*/
unlink(index, callback){
/* (1) Fail: invalid @index argument */
if( typeof index !== 'string' ){
console.error(`[OnBlurManager::unlink] expected (String), received (${typeof index})`);
return false;
}
/* (2) Remove from list of callbacks */
return ( delete this.callback[index] );
}
}