Skip to content

Commit ea83a2b

Browse files
committed
version 0.0.2
1 parent 2c48fcc commit ea83a2b

File tree

3 files changed

+172
-3
lines changed

3 files changed

+172
-3
lines changed

dist/hmpl-dom.runtime.js

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
});

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "hmpl-dom",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A module for using HMPL syntax directly in HTML, without the need for compilation on the JavaScript side",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)