all files / src/ gluebert.bootstrap.js

100% Statements 22/22
81.25% Branches 13/16
100% Functions 7/7
100% Lines 22/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107                                                                                                                                                                         
import { ModuleLauncher } from './module/module.launcher';
import { DataObserver } from './data/data.observer';
import { DataManager } from './data/data.manager';
import { ElementBuilder } from './element/element.builder';
 
const DEFAULT_OPTIONS = {
    elementReadyClass: `gb-ready`,
};
 
/**
 * Class represents Gluebert
 */
class Gluebert {
 
    /**
     * @param {ModuleSignature[]} modules
     * @param {DataSignature[]} data
     * @param {object} options
     */
    constructor(modules, data = [], options = {}) {
 
        this._options = Object.assign(
            {},
            DEFAULT_OPTIONS,
            options,
        );
 
        this._modules = (modules instanceof Array) ? modules : null;
        this._elements = (this._modules) ? this._extractElements(modules) : null;
        this._data = (data instanceof Array) ? data : null;
        this._schemaValidator = null;
        this._templateEngine = null;
 
    }
 
    start() {
        this._init();
        return this;
    }
 
    /**
     * start binding methods and properties
     * @private
     */
    _init() {
 
        this.elementBuilder = new ElementBuilder(
            this._elements,
            this._templateEngine,
            this._schemaValidator,
            this._options,
        );
 
        this.dataObserver = new DataObserver();
        this.dataManager = new DataManager(this.dataObserver, this._data);
        this.moduleLauncher = new ModuleLauncher(this._modules, this.dataObserver, this.elementBuilder);
    }
 
    /**
     * extract elements list from modules
     * @param {ModuleSignature[]} modules
     * @return {ElementSignature[]}
     * @private
     */
    _extractElements(modules) {
 
        return modules.reduce((a, b) => {
            let elements = b.getElementSignatures();
            if(elements.size) {
                a.push(...elements);
            }
            return a;
        }, []);
    }
 
    /**
     * set JSON Schema validator Gluebert uses for elements
     * @param schemaValidator
     * @return {Gluebert}
     */
    setSchemaValidator(schemaValidator) {
        this._schemaValidator = (typeof schemaValidator === 'function') ? schemaValidator : null;
        return this;
    }
 
    /**
     * set template engine
     * @param templateEngine
     * @return {Gluebert}
     */
    setTemplateEngine(templateEngine) {
        this._templateEngine = (
            templateEngine &&
            typeof templateEngine === 'object'
        )
            ? templateEngine
            : null;
 
        return this;
    }
 
 
}
 
export {
    Gluebert,
};