微件:SaltVoteContainer:修订间差异

添加599字节 、​ 2024年3月23日 (星期六)
编辑“微件:SaltVoteContainer” // 维基盐编辑器
(编辑“微件:SaltVoteContainer” // 维基盐编辑器)
(编辑“微件:SaltVoteContainer” // 维基盐编辑器)
 
<noinclude>用于展示投票箱</noinclude><includeonly><script>
"use strict";
/**
*
document.body.querySelectorAll("div.salt-vote-container.not-init")
);
// const traceElements = getAllNotInitContainer();
 
const at = (arr, index) => {
};
 
/** 渲染条状图内容 @type {(collect: {name: string, set: string[], li: HTMLElement, count: HTMLElement}[], color: string[], hasBar: boolean) => { barHTML: string }} */
const renderBarrenderPercent = (collect, color, hasBar) => {
const countAll = collect.reduce((prev, curr) => {
return prev + curr.set.length;
}, 0);
// 计算百分比
const divList = collect.map(
const percent = collect.map(({ nameset, set }li, icount }) => {
const res = }%">${Number(((set.length * 100) / countAll).toFixed(2))}%</div>`;
`<div class="salt-vote-bar-item" title="${name.replace(
li.style.setProperty("--vote-colorpercent", at(color, liIndex)`${res}%`); // 顺便把百分比记录上去
/"/g,
count.textContent = `${set.length} (${res}%)`;
"'"
return res;
)}" style="--vote-color:${at(color, i)};width:${
});
set.length ? set.length * 100 : 0
const divList = collect.map(hasBar
}%">${Number(((set.length * 100) / countAll).toFixed(2))}%</div>`
); ? collect.map(
({ name, set }, i) =>
return divList.join("");
`<div class="salt-vote-bar-item" title="${name.replace(
/"/g,
"'"
)}" style="--vote-color:${at(color, i)};width:${
set.length ? set.length *? 100percent[i] : 0
}%">${Number(percent[i])}%</div>`
)
: [];
return { barHTML: divList.join("") };
};
 
const descUl = el.querySelector(".salt-vote-describe");
const descLi = descUl && descUl.querySelectorAll("li");
/** 收集的投票信息 @type {{name: string, set: string[], li: HTMLElement, count: HTMLElement, desc?: HTMLElement}[]} */
const collect = [];
/** 收集的投票信息 @type {{name: string, desc: HTMLElement}[]} */
const title = option.textContent || "";
const voters = Array.from(voter.querySelectorAll("a"));
const count = document.createElement("div");
// if (title in collect) return;
const thisCollect = { name: title, set: [], li: li, count };
voters.forEach((v) => {
const res = v.title
});
collect.push(thisCollect);
// 配色外观
li.appendChild(count);
li.style.setProperty("--vote-color", at(color, liIndex));
count.className = "salt-vote-option-count";
li.style.setProperty("--vote-color", at(color, liIndex)); // 配色
li.style.setProperty("--vote-count", `${thisCollect.set.length}`);
// 点击展开选项描述
const desc = descCollect.find(({ name }) => name === title);
});
// 绘制图形
const { barHTML } = renderPercent(collect, color, !!barEl);
if (barEl) {
if (barEl) barEl.innerHTML = renderBar(collect, color)barHTML;
}
};