|
| 1 | +"use strict"; |
| 2 | +var __create = Object.create; |
| 3 | +var __defProp = Object.defineProperty; |
| 4 | +var __getOwnPropDesc = Object.getOwnPropertyDescriptor; |
| 5 | +var __getOwnPropNames = Object.getOwnPropertyNames; |
| 6 | +var __getProtoOf = Object.getPrototypeOf; |
| 7 | +var __hasOwnProp = Object.prototype.hasOwnProperty; |
| 8 | +var __export = (target, all) => { |
| 9 | + for (var name in all) |
| 10 | + __defProp(target, name, { get: all[name], enumerable: true }); |
| 11 | +}; |
| 12 | +var __copyProps = (to, from, except, desc) => { |
| 13 | + if ((from && typeof from === "object") || typeof from === "function") { |
| 14 | + for (let key of __getOwnPropNames(from)) |
| 15 | + if (!__hasOwnProp.call(to, key) && key !== except) |
| 16 | + __defProp(to, key, { |
| 17 | + get: () => from[key], |
| 18 | + enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable |
| 19 | + }); |
| 20 | + } |
| 21 | + return to; |
| 22 | +}; |
| 23 | +var __toESM = (mod, isNodeMode, target) => ( |
| 24 | + (target = mod != null ? __create(__getProtoOf(mod)) : {}), |
| 25 | + __copyProps( |
| 26 | + isNodeMode || !mod || !mod.__esModule |
| 27 | + ? __defProp(target, "default", { value: mod, enumerable: true }) |
| 28 | + : target, |
| 29 | + mod |
| 30 | + ) |
| 31 | +); |
| 32 | +var __toCommonJS = (mod) => |
| 33 | + __copyProps(__defProp({}, "__esModule", { value: true }), mod); |
| 34 | + |
| 35 | +var main_exports = {}; |
| 36 | +__export(main_exports, { |
| 37 | + init: () => init |
| 38 | +}); |
| 39 | +module.exports = __toCommonJS(main_exports); |
| 40 | +var import_hmpl_js = __toESM(require("hmpl-js")); |
| 41 | +var INIT_ERROR = `InitError`; |
| 42 | +var TEMPLATE_ERROR = `TemplateError`; |
| 43 | +var DATA_HMPL_ATTR = "data-hmpl"; |
| 44 | +var HMPL_ATTR = "hmpl"; |
| 45 | +var DATA_CONFIG_ID_ATTR = "data-config-id"; |
| 46 | +var CONFIG_ID_ATTR = "configId"; |
| 47 | +var createError = (text) => { |
| 48 | + throw new Error(text); |
| 49 | +}; |
| 50 | +var checkObject = (val) => { |
| 51 | + return typeof val === "object" && !Array.isArray(val) && val !== null; |
| 52 | +}; |
| 53 | +var validateInitOption = (option) => { |
| 54 | + if (!checkObject(option)) { |
| 55 | + createError(`${INIT_ERROR}: HMPLTemplateConfig must be an object`); |
| 56 | + } |
| 57 | + if (!option.hasOwnProperty("id") || !option.hasOwnProperty("value")) { |
| 58 | + createError(`${INIT_ERROR}: Missing "id" or "value" property`); |
| 59 | + } |
| 60 | + if (typeof option.id !== "string") { |
| 61 | + createError(`${INIT_ERROR}: ID must be a string`); |
| 62 | + } |
| 63 | + if (!checkObject(option.value)) { |
| 64 | + createError(`${INIT_ERROR}: Value must be an object`); |
| 65 | + } |
| 66 | +}; |
| 67 | +var validateDuplicateIds = (options) => { |
| 68 | + const ids = []; |
| 69 | + for (let i = 0; i < options.length; i++) { |
| 70 | + const id = options[i].id; |
| 71 | + if (ids.indexOf(id) > -1) { |
| 72 | + createError(`${INIT_ERROR}: ID with value "${id}" already exists`); |
| 73 | + } else { |
| 74 | + ids.push(id); |
| 75 | + } |
| 76 | + } |
| 77 | +}; |
| 78 | +var initialized = false; |
| 79 | +var initOptionsMap = /* @__PURE__ */ new Map(); |
| 80 | +var onDocumentLoad = (callback) => { |
| 81 | + const isDocumentLoaded = |
| 82 | + document.readyState === "complete" || document.readyState === "interactive"; |
| 83 | + void (isDocumentLoaded |
| 84 | + ? callback() |
| 85 | + : document.addEventListener("DOMContentLoaded", callback)); |
| 86 | +}; |
| 87 | +var mountTemplates = () => { |
| 88 | + const templates = document.querySelectorAll( |
| 89 | + `template[${DATA_HMPL_ATTR}], template[${HMPL_ATTR}]` |
| 90 | + ); |
| 91 | + for (let i = 0; i < templates.length; i++) { |
| 92 | + const template = templates[i]; |
| 93 | + const hasDataHmpl = template.hasAttribute(DATA_HMPL_ATTR); |
| 94 | + const hasHmpl = template.hasAttribute(HMPL_ATTR); |
| 95 | + if (hasDataHmpl && hasHmpl) { |
| 96 | + createError( |
| 97 | + `${TEMPLATE_ERROR}: Cannot use both ${DATA_HMPL_ATTR} and ${HMPL_ATTR} attributes` |
| 98 | + ); |
| 99 | + } |
| 100 | + const hasDataConfigId = template.hasAttribute(DATA_CONFIG_ID_ATTR); |
| 101 | + const hasConfigId = template.hasAttribute(CONFIG_ID_ATTR); |
| 102 | + if (hasDataConfigId && hasConfigId) { |
| 103 | + createError( |
| 104 | + `${TEMPLATE_ERROR}: Cannot use both ${DATA_CONFIG_ID_ATTR} and ${CONFIG_ID_ATTR} attributes` |
| 105 | + ); |
| 106 | + } |
| 107 | + const configId = |
| 108 | + template.getAttribute(DATA_CONFIG_ID_ATTR) || |
| 109 | + template.getAttribute(CONFIG_ID_ATTR); |
| 110 | + let option = void 0; |
| 111 | + if (configId === "") { |
| 112 | + createError( |
| 113 | + `${TEMPLATE_ERROR}: configId cannot be empty. Use ${DATA_CONFIG_ID_ATTR} or ${CONFIG_ID_ATTR} attribute` |
| 114 | + ); |
| 115 | + } |
| 116 | + if (configId) { |
| 117 | + option = initOptionsMap.get(configId); |
| 118 | + if (!option) { |
| 119 | + createError( |
| 120 | + `${TEMPLATE_ERROR}: Option with id "${configId}" not found. Make sure to define it in init() call` |
| 121 | + ); |
| 122 | + } |
| 123 | + } |
| 124 | + const templateHTML = template.innerHTML; |
| 125 | + if (template.content.children.length > 1) { |
| 126 | + createError( |
| 127 | + `${TEMPLATE_ERROR}: Template must contain exactly one root element, found ${template.content.children.length}` |
| 128 | + ); |
| 129 | + } |
| 130 | + if (template.content.children.length === 0) { |
| 131 | + createError( |
| 132 | + `${TEMPLATE_ERROR}: Template must contain at least one element` |
| 133 | + ); |
| 134 | + } |
| 135 | + const compileOptions = option?.value.compile || {}; |
| 136 | + const templateFunctionOptions = option?.value.templateFunction || {}; |
| 137 | + const templateFn = import_hmpl_js.default.compile( |
| 138 | + templateHTML, |
| 139 | + compileOptions |
| 140 | + ); |
| 141 | + const result = templateFn(templateFunctionOptions); |
| 142 | + if (result && result.response) { |
| 143 | + template.replaceWith(result.response); |
| 144 | + } |
| 145 | + } |
| 146 | +}; |
| 147 | +var init = (configs = []) => { |
| 148 | + if (initialized) { |
| 149 | + createError(`${INIT_ERROR}: init() can only be called once`); |
| 150 | + } |
| 151 | + initialized = true; |
| 152 | + validateDuplicateIds(configs); |
| 153 | + for (let i = 0; i < configs.length; i++) { |
| 154 | + const option = configs[i]; |
| 155 | + validateInitOption(option); |
| 156 | + initOptionsMap.set(option.id, option); |
| 157 | + } |
| 158 | + onDocumentLoad(mountTemplates); |
| 159 | +}; |
| 160 | +queueMicrotask(() => { |
| 161 | + if (!initialized) { |
| 162 | + onDocumentLoad(mountTemplates); |
| 163 | + } |
| 164 | +}); |
| 165 | +// Annotate the CommonJS export names for ESM import in node: |
| 166 | +0 && |
| 167 | + (module.exports = { |
| 168 | + init |
| 169 | + }); |
0 commit comments