ckn.backend
Version:
CKN Framework for Backend
78 lines (68 loc) • 2.27 kB
JavaScript
class Page {
#styles = [];
#scripts = [];
addScript(script) {
if (typeof script === "string") {
this.#scripts.push({script: script});
} else if (typeof script === "object") {
this.#scripts.push(script);
}
}
addStyle(style) {
if (typeof style === "string") {
this.#styles.push({style: style});
} else if (typeof style === "object") {
this.#styles.push(style);
}
}
title = "";
contents = [];
renderScripts() {
for (let plugin of this.#plugin) {
plugin.onInitialScripts(this);
}
let html = "";
for (let script of this.#scripts) {
html += `<script src='${script.script}' ${script.integrity != null ? "integrity='" + script.integrity + "'" : ""} ${script.crossorigin != null ? "crossorigin='" + script.crossorigin + "'" : ""} ></script>`;
}
return html;
}
renderStyles() {
for (let plugin of this.#plugin) {
plugin.onInitialStyles(this);
}
let html = "";
for (let style of this.#styles) {
html += `<link rel="stylesheet" type="text/css" href="${style.style}" ${style.integrity != null ? "integrity='" + style.integrity + "'" : ""} ${style.crossorigin != null ? "crossorigin='" + style.crossorigin + "'" : ""} />`;
}
return html;
}
renderContent() {
let html = "";
html += this.contents.toText("");
return html;
}
#plugin = [];
use(plugin) {
this.#plugin.push(plugin);
}
async render() {
let html = ``;
html += `<!DOCTYPE html>`;
html += `<html>`;
html += `<head>`;
html += `<meta charset="UTF-8" />`;
html += `<meta http-equiv="X-UA-Compatible" content="IE=edge" />`;
html += `<meta name="viewport" content="width=device-width, initial-scale=1.0">`;
html += this.renderStyles();
html += `<title>${this.title}</title>`;
html += `</head>`;
html += `<body>`;
html += this.renderContent();
html += this.renderScripts();
html += `</body>`;
html += `</html>`;
return html;
}
}
export {Page}