'use strict';
/**
* @author schukai GmbH
*/
import {Monster, Base} from '../types/base.js';
import {validateString} from "../types/validate.js";
import {getGlobalFunction, getGlobalObject} from '../types/global.js';
import {validateInstance} from "../types/validate.js";
import {findDocumentTheme} from "./theme.js";
/**
* you can call the method via the monster namespace `new Monster.DOM.Template()`.
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/dom/template.js';
* console.log(new Monster.DOM.Template())
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {Template} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/dom/template.js';
* console.log(new Template())
* </script>
* ```
*
* @since 1.6.0
* @copyright schukai GmbH
* @memberOf Monster/DOM
*/
class Template extends Base {
/**
*
* @param {HTMLTemplateElement} template
* @throws {TypeError} value is not an instance of
* @throws {TypeError} value is not a function
* @throws {Error} the function is not defined
*/
constructor(template) {
super();
const HTMLTemplateElement = getGlobalFunction('HTMLTemplateElement');
validateInstance(template, HTMLTemplateElement);
this.template = template;
}
/**
*
* @returns {HTMLTemplateElement}
*/
getTemplateElement() {
return this.template;
}
/**
*
* @return {DocumentFragment}
* @throws {TypeError} value is not an instance of
*/
createDocumentFragment() {
return this.template.content.cloneNode(true);
}
}
/**
*
*
* you can call the method via the monster namespace `Monster.DOM.findDocumentTemplate()`.
*
* ```
* <script type="module">
* import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/dom/template.js';
* console.log(Monster.DOM.findDocumentTemplate())
* </script>
* ```
*
* Alternatively, you can also integrate this function individually.
*
* ```
* <script type="module">
* import {findTemplate} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.7.0/dist/modules/dom/template.js';
* console.log(findDocumentTemplate())
* </script>
* ```
*
* @param {string} id
* @return {Template}
* @since 1.7.0
* @copyright schukai GmbH
* @memberOf Monster/DOM
* @throws {Error} template id not found.
* @throws {TypeError} value is not a string
*/
function findDocumentTemplate(id) {
validateString(id);
const document = getGlobalObject('document');
const HTMLTemplateElement = getGlobalFunction('HTMLTemplateElement');
let theme = findDocumentTheme()
let themedID = id + '-' + theme.getName();
let template = document.getElementById(themedID);
if (template instanceof HTMLTemplateElement) {
return new Template(template);
}
template = document.getElementById(id);
if (template instanceof HTMLTemplateElement) {
return new Template(template);
}
throw new Error("template " + id + " not found.")
}
Monster.assignToNamespace('Monster.DOM', Template, findDocumentTemplate);
export {Monster, Template, findDocumentTemplate}