Skip to content
Snippets Groups Projects
Select Git revision
  • 613f9cfb6ab88fb8986de882a2b53c4488616fec
  • master default protected
  • 1.31
  • 4.38.2
  • 4.38.1
  • 4.38.0
  • 4.37.2
  • 4.37.1
  • 4.37.0
  • 4.36.0
  • 4.35.0
  • 4.34.1
  • 4.34.0
  • 4.33.1
  • 4.33.0
  • 4.32.2
  • 4.32.1
  • 4.32.0
  • 4.31.0
  • 4.30.1
  • 4.30.0
  • 4.29.1
  • 4.29.0
23 results

assembler.mjs

Blame
  • Volker Schukai's avatar
    Volker Schukai authored
    026a00a0
    History
    assembler.mjs 2.00 KiB
    /**
     * Copyright schukai GmbH and contributors 2023. All Rights Reserved.
     * Node module: @schukai/monster
     * This file is licensed under the AGPLv3 License.
     * License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
     */
    
    import { Base } from "../types/base.mjs";
    import { getGlobalFunction } from "../types/global.mjs";
    import { ProxyObserver } from "../types/proxyobserver.mjs";
    import { validateInstance, validateString } from "../types/validate.mjs";
    
    export { ATTRIBUTEPREFIX, Assembler };
    
    /**
     * attribute prefix
     *
     * @type {string}
     * @memberOf Monster.DOM
     */
    const ATTRIBUTEPREFIX = "data-monster-";
    
    /**
     * Assembler class
     *
     * @license AGPLv3
     * @since 1.6.0
     * @copyright schukai GmbH
     * @memberOf Monster.DOM
     * @summary Allows you to build an html fragment
     */
    class Assembler extends Base {
        /**
         * @param {DocumentFragment} fragment
         * @throws {TypeError} value is not an instance of
         * @throws {TypeError} value is not a function
         * @throws {Error} the function is not defined
         */
        constructor(fragment) {
            super();
            this.attributePrefix = ATTRIBUTEPREFIX;
            validateInstance(fragment, getGlobalFunction("DocumentFragment"));
            this.fragment = fragment;
        }
    
        /**
         *
         * @param {string} prefix
         * @returns {Assembler}
         * @throws {TypeError} value is not a string
         */
        setAttributePrefix(prefix) {
            validateString(prefix);
            this.attributePrefix = prefix;
            return this;
        }
    
        /**
         *
         * @returns {string}
         */
        getAttributePrefix() {
            return this.attributePrefix;
        }
    
        /**
         *
         * @param {ProxyObserver|undefined} data
         * @return {DocumentFragment}
         * @throws {TypeError} value is not an instance of
         */
        createDocumentFragment(data) {
            if (data === undefined) {
                data = new ProxyObserver({});
            }
    
            validateInstance(data, ProxyObserver);
            let fragment = this.fragment.cloneNode(true);
            return fragment;
        }
    }