/* 获取直属子元素 */
function getChildren(el, className) {
for (let item of el.children) if (item.className === className) return item;
return null;
}
function parseExpression(expression, occupied) {
if (expression === "${full}") {
return occupied;
}
const match = expression.replaceAll("full", occupied).match(/^\$\{([<>=]{1,2}.+)\?(.+):(.+)}$/);
if (match) {
return eval(`occupied${match[1]} ? ${match[2]} : ${match[3]}`);
}
throw new Error(`Invalid expression "${expression}"`);
}
function extractHeight(occupied, width, height) {
const occupiedWidth = width.endsWith("%")
? occupied * (Number(width.slice(0, -1)) / 100)
: Number(width);
height = height.replaceAll("cwidth", occupiedWidth);
if (height.startsWith("${") && height.endsWith("}")) {
return parseExpression(height, occupied);
} else {
return height;
}
}
// 跳转链接的卡片
document.addEventListener("DOMContentLoaded", () => {
// 分栏 tab
customElements.define(
"hao-tabs",
class HaoTabs extends HTMLElement {
constructor() {
super();
this.options = {
id: this.getAttribute("id") || '',
index: this.getAttribute("index") || ''
};
const id = this.options.id
const index = this.options.index
const _temp = getChildren(this, "_tpl");
let _innerHTML = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let navs = "";
let contents = "";
let newIndex = 0;
_innerHTML.replace(
/{tabs-item([^}]*)}([\s\S]*?){\/tabs-item}/g,
function ($0, $1, $2) {
newIndex +=1;
let active =''
if(index!='' && index!=null){
if(newIndex == index){
active = 'active';
}
}else{
if(newIndex==1){
active = 'active'
}
}
navs += `
`;
contents += `
${$2.trim().replace(/^(
)|(
)$/g, "")}
`;
}
);
let htmlStr = `
`;
this.innerHTML = htmlStr;
}
}
);
// 彩虹虚线
customElements.define(
"hao-dotted",
class DottedDom extends HTMLElement {
constructor() {
super();
this.startColor = this.getAttribute("begin") || "#ff6c6c";
this.endColor = this.getAttribute("end") || "#1989fa";
this.innerHTML = `
`;
}
}
);
// 进度条
customElements.define(
"hao-progress",
class ProgressDom extends HTMLElement {
constructor() {
super();
this.options = {
percentage: /^\d{1,3}%$/.test(this.getAttribute("pct"))
? this.getAttribute("pct")
: "50%",
color: this.getAttribute("color") || "#ff6c6c",
};
this.innerHTML = `
${this.options.percentage}
`;
}
}
);
// 小标记
customElements.define(
"hao-sign",
class SignDom extends HTMLElement {
constructor() {
super();
this.options = {
type: this.getAttribute("type"), // 小标签类型
content: this.innerHTML, // 内容
};
this.render();
}
render() {
this.innerHTML = `${this.options.content}`;
}
}
);
// B站视频
customElements.define(
"hao-bilibili",
class BiliBiliDom extends HTMLElement {
constructor() {
super();
this.options = {
bvid: this.getAttribute("bvid"),
page: +(this.getAttribute("page") || "1"),
width: this.getAttribute("width") || "100%",
height: this.getAttribute("height") || "500",
autoplay: this.getAttribute("autoplay") || 0,
};
this.render();
}
render() {
if (!this.options.bvid) return (this.innerHTML = "请填写正确的bvid");
const realHeight = extractHeight(this.parentElement.offsetWidth, this.options.width, this.options.height);
this.setAttribute("height", realHeight);
this.innerHTML = `
`;
}
}
);
// pdf
customElements.define(
"hao-pdf",
class PDFDom extends HTMLElement {
constructor() {
super();
this.options = {
src: this.getAttribute("src") || "",
width: this.getAttribute("width") || "100%",
height: this.getAttribute("height") || "500",
};
this.render();
}
render() {
if (!this.options.src) return (this.innerHTML = "请填写正确的pdf链接");
const realHeight = extractHeight(this.parentElement.offsetWidth, this.options.width, this.options.height);
this.setAttribute("height", realHeight);
this.innerHTML = `
`;
}
}
);
customElements.define(
"hao-introduction-card",
class HaoIntroductionCard extends HTMLElement {
constructor() {
super();
this.options = {
link: this.getAttribute("link") || 'https://0206.ink/',
img: this.getAttribute("img"),
tip: this.getAttribute("tip") || '小标题',
cardTitle: this.getAttribute("cardTitle") || '标题',
logo: this.getAttribute("logo"),
title: this.getAttribute("title"),
subTitle: this.getAttribute("subTitle"),
};
let style1 = ''
let style2 = ''
if(this.options.logo==null && this.options.title==null && this.options.subTitle==null){
style1 = 'height:416px'
style2 = 'height:100%;border-radius:15px'
}
let innerHTMLs = `
${this.options.tip}
${this.options.cardTitle}
`;
if(this.options.logo!=null && this.options.title!=null && this.options.subTitle!=null){
innerHTMLs += `
${this.options.title}
${this.options.subTitle}
`;
}
innerHTMLs += `
`;
this.innerHTML = innerHTMLs;
}
}
);
// 折叠框 folding
customElements.define(
"hao-folding",
class HaoFolding extends HTMLElement {
constructor() {
super();
this.options = {
title: this.getAttribute("title"),
color: this.getAttribute("color") || '',
type: this.getAttribute("type") || ''
};
const _temp = getChildren(this, "_tpl");
let contents = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let htmlStr = `
${this.options.title}
${contents}
`;
this.innerHTML = htmlStr;
}
}
);
// 链接卡片 link
customElements.define(
"hao-tag-link",
class HaoTagLink extends HTMLElement {
constructor() {
super();
this.options = {
link: this.getAttribute("link"),
logo: this.getAttribute("logo") || '',
title: this.getAttribute("title") || '',
described: this.getAttribute("described") || '',
};
let tagLinkLeft = `
`
if(this.options.logo!=null && this.options.logo!=''){
tagLinkLeft = `
`
}
let htmlStr = `
`;
this.innerHTML = htmlStr;
}
}
);
// Note (Bootstrap Callout)
customElements.define(
"hao-note",
class HaoNote extends HTMLElement {
constructor() {
super();
this.options = {
class: this.getAttribute("class") || '',
noIcon: this.getAttribute("noIcon") || '',
style: this.getAttribute("style") || ''
};
let htmlStr = `
${this.innerHTML.trim().replace(/^(
)|(
)$/g, "")}
`;
this.innerHTML = htmlStr;
}
}
);
// 上标标签 tip
customElements.define(
"hao-tip",
class HaoTip extends HTMLElement {
constructor() {
super();
this.options = {
class: this.getAttribute("class") || 'info',
noIcon: this.getAttribute("noIcon") || ''
};
let htmlStr = `
${this.innerHTML.trim().replace(/^(
)|(
)$/g, "")}
`;
this.innerHTML = htmlStr;
}
}
);
// timeline
customElements.define(
"hao-timeline",
class HaoTimeline extends HTMLElement {
constructor() {
super();
this.options = {
title: this.getAttribute("title") || '',
color: this.getAttribute("color") || ''
};
const _temp = getChildren(this, "_tpl");
let _innerHTML = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let content = "";
_innerHTML.replace(
/{timeline-item([^}]*)}([\s\S]*?){\/timeline-item}/g,
function ($0, $1, $2) {
content += `
${$2.trim().replace(/^(
)|(
)$/g, "")}
`;
}
);
let htmlStr = `
`;
this.innerHTML = htmlStr;
}
}
);
// 按钮 btns
customElements.define(
"hao-btns",
class HaoBtns extends HTMLElement {
constructor() {
super();
this.options = {
class: this.getAttribute("class") || '',
style: this.getAttribute("style") || '',
grid: this.getAttribute("grid") || '',
};
const _temp = getChildren(this, "_tpl");
let _innerHTML = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let content = "";
if(this.options.class == 'rounded'){
_innerHTML.replace(
/{([^}]*)}/g,
function ($0, $1) {
var str = $1.split(",", 5);
if(str.length==5){
content += `
${str[0]}
${str[1]}
`;
}else{
content += `
${str[0]}
`;
}
}
);
}
if(this.options.class == 'circle'){
_innerHTML.replace(
/{([^}]*)}/g,
function ($0, $1) {
var str = $1.split(",", 5);
if(str.length==5){
content += `
${str[0]}
${str[1]}
`;
}else{
content += `
${str[0]}
`;
}
}
);
}
let htmlStr = `
${content}
`;
this.innerHTML = htmlStr;
}
}
);
// gallerygroup 相册图库
customElements.define(
"hao-gallery-group",
class HaoGalleryGroup extends HTMLElement {
constructor() {
super();
const _temp = getChildren(this, "_tpl");
let _innerHTML = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let contents = "";
_innerHTML.replace(
/{([^}]*)}/g,
function ($0, $1) {
var str = $1.split(",",4);
contents += `
`;
}
);
let htmlStr = `
${contents}
`;
this.innerHTML = htmlStr;
}
}
);
// gallery 相册
customElements.define(
"hao-gallery",
class HaoGallery extends HTMLElement {
constructor() {
super();
const _temp = getChildren(this, "_tpl");
let _innerHTML = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let contents = "";
_innerHTML.replace(
/{([^}]*)}/g,
function ($0, $1) {
var str = $1.split(",");
str.forEach((item) => {
contents += `
`;
});
}
);
let htmlStr = `
`;
this.innerHTML = htmlStr;
}
}
);
// flink 友链标签
customElements.define(
"hao-flink",
class HaoFlink extends HTMLElement {
constructor() {
super();
this.options = {
name: this.getAttribute("name"),
desc: this.getAttribute("desc"),
style: this.getAttribute("style"),
};
const _temp = getChildren(this, "_tpl");
let _innerHTML = _temp.innerHTML.trim().replace(/^(
)|(
)$/g, "");
let style = this.options.style;
let content = "";
let contents = "";
let class_desc = "";
_innerHTML.replace(
/{([^}]*)}/g,
function ($0, $1) {
var flink = $1.split(",",5);
if(style=='beautify'){
contents +=`
`
}
if(style=='default'){
contents +=`
`
}
}
);
if(this.options.desc!=null && this.options.desc!=''){
class_desc =`
${this.options.desc}
`
}
if(this.options.style=='beautify'){
content =`
${contents}
`
}
if(this.options.style=='default'){
content =`
${contents}
`
}
let htmlStr = `
${this.options.name}
${class_desc}
${content}
`;
this.innerHTML = htmlStr;
}
}
);
// 复选列表 checkbox
customElements.define(
"hao-checkbox",
class HaoCheckbox extends HTMLElement {
constructor() {
super();
this.options = {
class: this.getAttribute("class") || '',
colour: this.getAttribute("colour") || '',
status: this.getAttribute("status") || ''
};
let htmlStr = `
`;
this.innerHTML = htmlStr;
}
}
);
// tag-hide
customElements.define(
"hao-tag-hide",
class HaoCheckbox extends HTMLElement {
constructor() {
super();
this.options = {
display: this.getAttribute("display") || '查看',
bg: this.getAttribute("bg") || '',
color: this.getAttribute("color") || ''
};
let htmlStr = `
${this.innerHTML.trim().replace(/^(
)|(
)$/g, "")}
`;
this.innerHTML = htmlStr;
}
}
);
customElements.define(
"hao-dplayer",
class HaoDplayer extends HTMLElement {
constructor() {
super();
this.options = {
src: this.getAttribute("src") || "",
player:
this.getAttribute("player") ||
`/themes/theme-hao/assets/libs/dplayer/dplayer.html?url=`,
width: this.getAttribute("width") || "100%",
height: this.getAttribute("height") || "500px",
};
this.render();
}
render() {
if (this.options.src)
this.innerHTML = ``;
else this.innerHTML = "视频地址未填写!";
}
}
);
});