import {ObjectHelper} from './object-helper'; import {Indexable} from '../interfaces/indexable'; /** * Helpers for View */ export class ViewHelper { /** * Mark text yellow inside an element. * @param {Node} element * @param {string} input */ static markElement(element: Node | ChildNode, input: string): void { if (input != '') { element.childNodes.forEach((n: Node) => { const value: string = n.nodeValue || ''; if (n.nodeName === '#text' && value.indexOf(input) >= 0) { (element as HTMLElement).innerHTML = (n as Indexable)['data'] .split(input) .join('' + input + ''); } else { ViewHelper.markElement(n, input); } }); } } /** * Toggle Visibility for Element * @param {HTMLElement} element * @param {boolean} show */ static toggleVisibility(element: HTMLElement, show: boolean) { if (show) { element.classList.remove('is-hidden'); } else { element.classList.add('is-hidden'); } } /** * Toggle Requirement for Element * @param {HTMLElement} element * @param {boolean} show */ static toggleRequirement(element: HTMLElement, show: boolean) { console.log(element); element.querySelectorAll('input, select, textarea').forEach((element) => { (element as HTMLInputElement).required = show; }); } /** * Getting URL-Parameter from address * @param {string} key * @return {string} */ static getParameter(key: string): string | null { const urlString = window.location.href; const url = new URL(urlString); const value = url.searchParams.get(key); return value; } /** * Regex to fill keys in template * @return {RegExp} */ static get regexTemplate(): RegExp { return /{{([a-zA-Z0-9\.ß\[\]]+)(\|[a-zA-Z]+){0,1}}}/; } /** * Replacing Placeholders in template from the loaded element * @param {HTMLElement} template * @param {Indexable} e */ static replacePlaceholders(template: HTMLElement, e: Indexable): void { Array.from(template.childNodes) .filter((n) => n.nodeType === 3) .forEach((node) => { this.replacePlaceholdersInNodeText(node, e); }); if (!template.innerHTML.includes('template')) { this.replacePlaceholdersInnerHTML(template, e); } Array.from(template.children) .filter((child) => child.tagName != 'TEMPLATE') .forEach((child) => { this.replacePlaceholders(child as HTMLElement, e); }); } /** * Format View with a attached Pipe * @param {string} pipeName * @param {string} value * @return {string} */ static pipeUse(pipeName: string, value: string) : string { switch (pipeName) { case '|date': return ObjectHelper.formatDate(value); default: return value; } } /** * Replace Placeholders in Incoming Text * @param {string} text * @param {Indexable} e * @return {string} */ public static replacePlaceholdersInText(text: string, e: Indexable ): string { let match = null; while (match = text.match(ViewHelper.regexTemplate)) { let value = ObjectHelper.get(e, match[1]); value = value || ''; value = this.pipeUse(match[2], value); text = text.replace(match[0], value); } return text; } /** * @private * Replace Placeholders in Incoming InnerHTML * @param {HTMLElement} template * @param {Indexable} e */ private static replacePlaceholdersInnerHTML( template: HTMLElement, e: Indexable) { let match = null; let text = template.innerHTML || ''; while (match = text.match(ViewHelper.regexTemplate)) { let value = ObjectHelper.get(e, match[1]); value = value || ''; value = this.pipeUse(match[2], value); template.innerHTML = text.replace(match[0], value); text = template.innerHTML; } } /** * @private * Replace Placeholders in Incoming NodeText * @param {ChildNode} template * @param {Indexable} e */ private static replacePlaceholdersInNodeText( template: ChildNode, e: Indexable) { let match = null; let text = template.textContent || ''; while (match = text.match(ViewHelper.regexTemplate)) { let value = ObjectHelper.get(e, match[1]); value = value || ''; template.textContent = text.replace(match[0], value); text = template.textContent; } } }