all files / gluebert/src/element/ element.signature.js

95.24% Statements 20/21
100% Branches 11/11
90% Functions 9/10
100% Lines 18/18
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                                14×       14× 14× 14×   14×                                                                                                                        
import { ElementAbstract } from './element.abstract';
 
/**
 * Class represents ElementSignature
 * @class
 * @example new ElementSignature('xyz.element', () => import('./xyz.template.twig'))
 * @example new ElementSignature('xyz.element', () => import('./xyz.template.html'))
 * @example new ElementSignature('xyz.element')
 */
class ElementSignature {
 
    /**
     * @param {string} name - name of the element usage will be referenced to this given name
     * @param {function|null} template - callback function for template import
     */
    constructor(name, template = null) {
        this.name = (typeof name === 'string')
            ? name
            : null;
 
        this.schemaImport = () => Promise.resolve();
        this.templateImport = () => Promise.resolve(`<div>Missing template specification for ${name}.</div>`);
        this.elementImport = () => Promise.resolve(ElementAbstract);
 
        if(template) {
            this.setTemplateImport(template);
        }
 
    }
 
    /**
     * set import schema callback
     * @param {function} schema
     * @return {ElementSignature}
     * @example signature.setImportSchema(() => import('./xyz.schema.json'));
     */
    setImportSchema(schema) {
        if(typeof schema === 'function') {
            this.schemaImport = schema;
        }
        return this;
    }
 
    /**
     * get import schema callback
     * @return {function|null}
     */
    getImportSchema() {
        return this.schemaImport;
    }
 
    /**
     * set template import callback
     * @param {function} template
     * @return {ElementSignature}
     * @example signature.setTemplateImport(() => import('./xyz.template.html'));
     * @example signature.setTemplateImport(() => import('./xyz.template.twig'));
     */
    setTemplateImport(template) {
        if(typeof template === 'function') {
            this.templateImport = template;
        }
        return this;
    }
 
    /**
     * get template import promise
     * @return {function|null}
     */
    getTemplateImport() {
        return this.templateImport;
    }
 
    /**
     * set element import callback
     * @param {function} element
     * @return {ElementSignature}
     * @example signature.setElementImport(() => import('./xyz.element').then((element) => element.XyzElement));
     */
    setElementImport(element) {
        if(typeof element === 'function') {
            this.elementImport = element;
        }
        return this;
    }
 
    /**
     * get element import callback
     * @return {function|null}
     */
    getElementImport() {
        return this.elementImport;
    }
}
 
export {
    ElementSignature,
};