diff --git a/frontend/src/app.js b/frontend/src/app.js index 6c7fa215f..b2db86c31 100644 --- a/frontend/src/app.js +++ b/frontend/src/app.js @@ -2,6 +2,7 @@ import { HttpClient } from './services/http-client/http-client'; import { HtmlHelpers } from './services/html-helpers/html-helpers'; import { I18n } from './services/i18n/i18n'; import { UtilRegistry } from './services/util-registry/util-registry'; +import { isValidUtility } from './core/utility'; export class App { httpClient = new HttpClient(); @@ -11,6 +12,8 @@ export class App { constructor() { this.utilRegistry.setApp(this); + + document.addEventListener('DOMContentLoaded', () => this.utilRegistry.setupAll()); } registerUtilities(utils) { @@ -18,7 +21,7 @@ export class App { throw new Error('Utils are expected to be passed as array!'); } - utils.forEach((util) => { + utils.filter(isValidUtility).forEach((util) => { this.utilRegistry.register(util); }); } diff --git a/frontend/src/app.spec.js b/frontend/src/app.spec.js index 8ce962b2d..21a381a68 100644 --- a/frontend/src/app.spec.js +++ b/frontend/src/app.spec.js @@ -16,6 +16,12 @@ describe('App', () => { expect(app).toBeTruthy(); }); + it('should setup all utlites when page is done loading', () => { + spyOn(app.utilRegistry, 'setupAll'); + document.dispatchEvent(new Event('DOMContentLoaded')); + expect(app.utilRegistry.setupAll).toHaveBeenCalled(); + }); + describe('provides services', () => { it('HttpClient as httpClient', () => { expect(app.httpClient).toBeTruthy(); diff --git a/frontend/src/core/utility.js b/frontend/src/core/utility.js new file mode 100644 index 000000000..6b74920dd --- /dev/null +++ b/frontend/src/core/utility.js @@ -0,0 +1,22 @@ +export function isValidUtility(utility) { + if (!utility) { + return false; + } + + if (Utility.selector) { + return false; + } + + return true; +}; + +export function Utility(metadata) { + if (!metadata.selector) { + throw new Error('Utility needs to have a selector!'); + } + + return function (target) { + target.selector = metadata.selector; + target.isUtility = true; + }; +}; diff --git a/frontend/src/services/util-registry/util-registry.js b/frontend/src/services/util-registry/util-registry.js index 18b656b8c..5e71554bc 100644 --- a/frontend/src/services/util-registry/util-registry.js +++ b/frontend/src/services/util-registry/util-registry.js @@ -6,10 +6,6 @@ export class UtilRegistry { _activeUtilInstances = []; _appInstance; - constructor() { - document.addEventListener('DOMContentLoaded', () => this.setupAll()); - } - /** * function registerUtil * @@ -63,14 +59,14 @@ export class UtilRegistry { console.log('setting up util', { util }); } - if (util && typeof util.setup === 'function') { + if (util) { const elements = this._findUtilElements(util, scope); elements.forEach((element) => { let utilInstance = null; try { - utilInstance = util.setup(element, this._appInstance); + utilInstance = new util(element, this._appInstance); } catch(err) { if (DEBUG_MODE > 0) { console.warn('Error while trying to initialize a utility!', { util , element, err }); diff --git a/frontend/src/services/util-registry/util-registry.spec.js b/frontend/src/services/util-registry/util-registry.spec.js index e3f69f63c..2e4afedf2 100644 --- a/frontend/src/services/util-registry/util-registry.spec.js +++ b/frontend/src/services/util-registry/util-registry.spec.js @@ -21,12 +21,6 @@ describe('UtilRegistry', () => { expect(utilRegistry).toBeTruthy(); }); - it('should setup all utlites when page is done loading', () => { - spyOn(utilRegistry, 'setupAll'); - document.dispatchEvent(new Event('DOMContentLoaded')); - expect(utilRegistry.setupAll).toHaveBeenCalled(); - }); - describe('register()', () => { it('should allow to add utilities', () => { utilRegistry.register(TEST_UTILS[0]); diff --git a/frontend/src/utils/alerts/alerts.js b/frontend/src/utils/alerts/alerts.js index 3e49080bc..e7e04ddbb 100644 --- a/frontend/src/utils/alerts/alerts.js +++ b/frontend/src/utils/alerts/alerts.js @@ -1,191 +1,158 @@ +import { Utility } from '../../core/utility'; import './alerts.scss'; -/** - * - * Alerts Utility - * makes alerts interactive - * - * Attribute: uw-alerts - * - * Types of alerts: - * [default] - * Regular Info Alert - * Disappears automatically after 30 seconds - * Disappears after x seconds if explicitly specified via data-decay='x' - * Can be told not to disappear with data-decay='0' - * - * [success] - * Currently no special visual appearance - * Disappears automatically after 30 seconds - * - * [warning] - * Will be coloured warning-orange regardless of user's selected theme - * Does not disappear - * - * [error] - * Will be coloured error-red regardless of user's selected theme - * Does not disappear - * - * Example usage: - *