136 lines
3.4 KiB
JavaScript
136 lines
3.4 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
var registeredUtils = [];
|
|
var activeUtilInstances = [];
|
|
|
|
var DEBUG_MODE = /localhost/.test(window.location.href) && 2;
|
|
|
|
// Registry
|
|
// (revealing module pattern)
|
|
window.UtilRegistry = (function() {
|
|
|
|
/**
|
|
* function registerUtil
|
|
*
|
|
* utils need to have at least these properties:
|
|
* name: string | utils name, e.g. 'example'
|
|
* selector: string | utils selector, e.g. '[uw-example]'
|
|
* setup: Function | utils setup function, see below
|
|
*
|
|
* setup function must return instance object with at least these properties:
|
|
* name: string | utils name
|
|
* element: HTMLElement | element the util is applied to
|
|
* destroy: Function | function to destroy the util and remove any listeners
|
|
*
|
|
* @param util Object Utility that should be added to the registry
|
|
*/
|
|
function registerUtil(util) {
|
|
if (DEBUG_MODE > 2) {
|
|
console.log('registering util "' + util.name + '"');
|
|
console.log({ util });
|
|
}
|
|
|
|
registeredUtils.push(util);
|
|
}
|
|
|
|
function deregisterUtil(name, destroy) {
|
|
var utilIndex = _findUtilIndex(name);
|
|
|
|
if (utilIndex >= 0) {
|
|
if (destroy === true) {
|
|
_destroyUtilInstances(name);
|
|
}
|
|
|
|
registeredUtils.splice(utilIndex, 1);
|
|
}
|
|
}
|
|
|
|
function setupAllUtils() {
|
|
if (DEBUG_MODE > 1) {
|
|
console.info('registered js utilities:');
|
|
console.table(registeredUtils);
|
|
}
|
|
|
|
registeredUtils.forEach(function(util) {
|
|
setupUtil(util);
|
|
});
|
|
}
|
|
|
|
function setupUtil(util, scope) {
|
|
if (DEBUG_MODE > 2) {
|
|
console.log('setting up util', { util });
|
|
}
|
|
|
|
scope = scope || document;
|
|
|
|
if (util && typeof util.setup === 'function') {
|
|
const elements = _findUtilElements(util, scope);
|
|
|
|
elements.forEach(function(element) {
|
|
var utilInstance = null;
|
|
|
|
try {
|
|
utilInstance = util.setup(element);
|
|
} catch(err) {
|
|
if (DEBUG_MODE > 0) {
|
|
console.warn('Error while trying to initialize a utility!', { util , element, err });
|
|
}
|
|
}
|
|
|
|
if (utilInstance) {
|
|
if (DEBUG_MODE > 2) {
|
|
console.info('Got utility instance for utility "' + util.name + '"', { utilInstance });
|
|
}
|
|
|
|
activeUtilInstances.push(utilInstance);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function findUtil(name) {
|
|
return registeredUtils.find(function(util) {
|
|
return util.name === name;
|
|
});
|
|
}
|
|
|
|
function _findUtilElements(util, scope) {
|
|
return Array.from(scope.querySelectorAll(util.selector));
|
|
}
|
|
|
|
function _findUtilIndex(name) {
|
|
return registeredUtils.findIndex(function(util) {
|
|
return util.name === name;
|
|
});
|
|
}
|
|
|
|
function _destroyUtilInstances(name) {
|
|
console.log('TODO: destroy util instances', { name });
|
|
// call destroy on each activeUtilInstance that matches the name
|
|
}
|
|
|
|
// public API
|
|
return {
|
|
register: registerUtil,
|
|
deregister: deregisterUtil,
|
|
setupAll: setupAllUtils,
|
|
setup: setupUtil,
|
|
find: findUtil,
|
|
}
|
|
})();
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
window.UtilRegistry.setupAll();
|
|
});
|
|
|
|
|
|
// REMOVE ME. JUST HERE TO AVOID JS ERRORS
|
|
window.utils = {
|
|
setup: function(name) {
|
|
console.log('not really setting up', name);
|
|
},
|
|
};
|
|
|
|
})();
|