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

100% Statements 21/21
100% Branches 11/11
100% Functions 10/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                                19×       19× 19× 19×   19×                                                                                                                        
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.importSchema = () => Promise.resolve();
        this.importTemplate = () => Promise.resolve(`<div>Missing template specification for ${name}.</div>`);
        this.importElement = () => Promise.resolve(ElementAbstract);
 
        if(template) {
            this.setImportTemplate(template);
        }
 
    }
 
    /**
     * set import schema callback
     * @param {function} schema
     * @return {ElementSignature}
     * @example signature.setImportSchema(() => import('./xyz.schema.json'));
     */
    setImportSchema(schema) {
        if(typeof schema === 'function') {
            this.importSchema = schema;
        }
        return this;
    }
 
    /**
     * get import schema callback
     * @return {function|null}
     */
    getImportSchema() {
        return this.importSchema;
    }
 
    /**
     * set template import callback
     * @param {function} template
     * @return {ElementSignature}
     * @example signature.setImportTemplate(() => import('./xyz.template.html'));
     * @example signature.setImportTemplate(() => import('./xyz.template.twig'));
     */
    setImportTemplate(template) {
        if(typeof template === 'function') {
            this.importTemplate = template;
        }
        return this;
    }
 
    /**
     * get template import promise
     * @return {function|null}
     */
    getImportTemplate() {
        return this.importTemplate;
    }
 
    /**
     * set element import callback
     * @param {function} element
     * @return {ElementSignature}
     * @example signature.setImportElement(() => import('./xyz.element').then((element) => element.XyzElement));
     */
    setImportElement(element) {
        if(typeof element === 'function') {
            this.importElement = element;
        }
        return this;
    }
 
    /**
     * get element import callback
     * @return {function|null}
     */
    getImportElement() {
        return this.importElement;
    }
}
 
export {
    ElementSignature,
};