all files / src/module/ module.signature.js

100% Statements 20/20
100% Branches 17/17
100% Functions 10/10
100% Lines 20/20
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                          10×           10×             10× 10× 10×   10×                                                                                                                                                                                              
import { DependencyManager } from './module.dependency';
 
/**
 * Class representing ModuleSignature
 */
class ModuleSignature {
 
    /**
     * creates new ModuleSignature object
     * @param {string} name - Module name
     * @param {string|null} selector
     */
    constructor(name, selector = null) {
        this.name = (
            name && typeof name === 'string'
        )
            ? name
            : null;
 
        this.selector = (
            selector &&
            typeof selector === 'string'
        )
            ? selector
            : null;
 
        this.importController = null;
        this.importStyles = null;
        this.elements = new Set();
 
        this.dependencyManager = new DependencyManager();
 
    }
 
    /**
     * set controller selector
     * @param {string} selector - selector for document.querySelectorAll()
     * @example new ModuleSignature('example').setSelector('.example-class-selector')
     * @return {ModuleSignature}
     */
    setSelector(selector) {
        this.selector = (
            selector &&
            typeof selector === 'string'
        )
            ? selector
            : null;
 
        return this;
    }
 
 
    /**
     * get defined selector
     * @return {string|null}
     */
    getSelector() {
        return this.selector;
    }
 
    /**
     * set controller import
     * @param {function} controller
     * @example
     * new ModuleSignature('example')
     *  .setImportController(() => import('./example.controller'));
     * @return {AbstractController}
     */
    setImportController(controller) {
        this.importController = (typeof controller === 'function')
            ? controller
            : null;
 
        return this;
    }
 
    /**
     * get controller import method
     * @return {function|null}
     */
    getImportController() {
        return this.importController;
    }
 
    /**
     * set styles import
     * @param {function} styles
     * @return {ModuleSignature}
     */
    setImportStyles(styles) {
        this.importStyles = (typeof styles === 'function')
            ? styles
            : null;
 
        return this;
    }
 
    /**
     * get styles import method
     * @return {function|null}
     */
    getImportStyles() {
        return this.importStyles;
    }
 
    /**
     * add element signature
     * @param {ElementSignature} elementSignature
     * @return {ModuleSignature}
     */
    addElementSignature(elementSignature) {
        this.elements.add(elementSignature);
        return this;
    }
 
    /**
     * get elements signatures
     * @return {Set} - set of ElementSignature
     */
    getElementSignatures() {
        return this.elements;
    }
 
    /**
     * add dependencies
     * @param {string} key
     * @param {function} module
     * @return {ModuleSignature}
     */
    addDependency(key, module) {
        this.dependencyManager.add(key, module);
        return this;
    }
 
 
}
 
export {
    ModuleSignature,
};