72 lines
3.9 KiB
JavaScript
72 lines
3.9 KiB
JavaScript
/* Abstimmungen: Balkenbreiten (CSP erlaubt keine style-Attribute) und das Formular fuer neue Abstimmungen. */
|
|
(function () {
|
|
'use strict';
|
|
|
|
for (const bar of document.querySelectorAll('.poll-bar span[data-pct]')) {
|
|
bar.style.width = Math.max(0, Math.min(100, Number(bar.dataset.pct) || 0)) + '%';
|
|
}
|
|
|
|
const raw = document.getElementById('poll-data');
|
|
if (!raw) return;
|
|
const { draft, roles } = JSON.parse(raw.textContent);
|
|
const { el } = window.SielEditor;
|
|
const form = document.getElementById('neu');
|
|
const list = document.getElementById('poll-options');
|
|
const addBtn = document.getElementById('poll-add');
|
|
|
|
const options = (draft && draft.options && draft.options.length ? draft.options : [{ label: '' }, { label: '' }])
|
|
.map((o) => (typeof o === 'string' ? { label: o, emoji: '' } : { label: o.label || '', emoji: o.emoji || '' }));
|
|
|
|
function paint() {
|
|
list.textContent = '';
|
|
options.forEach((o, idx) => {
|
|
const emoji = el('input', { type: 'text', class: 'emoji-input', maxlength: '80', placeholder: String(idx + 1) });
|
|
emoji.value = o.emoji;
|
|
emoji.addEventListener('input', () => { o.emoji = emoji.value.trim(); });
|
|
const label = el('input', { type: 'text', maxlength: '80', placeholder: 'Antwort ' + (idx + 1) });
|
|
label.value = o.label;
|
|
label.addEventListener('input', () => { o.label = label.value; });
|
|
const remove = el('button', { type: 'button', class: 'icon-btn danger', text: '✕', title: 'Entfernen', disabled: options.length <= 2 });
|
|
remove.addEventListener('click', () => { options.splice(idx, 1); paint(); });
|
|
list.append(el('div', { class: 'poll-option' }, emoji, label, remove));
|
|
});
|
|
addBtn.hidden = options.length >= 10;
|
|
}
|
|
addBtn.addEventListener('click', () => { options.push({ label: '', emoji: '' }); paint(); list.lastChild.querySelector('input:not(.emoji-input)').focus(); });
|
|
paint();
|
|
|
|
const roleBox = document.getElementById('poll-roles');
|
|
for (const r of roles.filter((x) => !x.managed)) {
|
|
const cb = el('input', { type: 'checkbox', value: r.id });
|
|
cb.checked = !!(draft && (draft.roles || []).includes(r.id));
|
|
const dot = el('span', { class: 'role-dot' });
|
|
if (r.color) dot.style.background = r.color;
|
|
roleBox.append(el('label', { class: 'role-item' }, cb, dot, r.name));
|
|
}
|
|
|
|
if (draft) {
|
|
document.getElementById('poll-question').value = draft.question || '';
|
|
document.getElementById('poll-description').value = draft.description || '';
|
|
document.getElementById('poll-max').value = draft.max_choices || 1;
|
|
document.getElementById('poll-live').checked = draft.live_results !== false;
|
|
document.getElementById('poll-anon').checked = !!draft.anonymous;
|
|
document.getElementById('poll-ping').value = draft.ping_role || '';
|
|
document.getElementById('poll-duration').value = draft.duration || '';
|
|
form.elements.channel_id.value = draft.channel_id || '';
|
|
}
|
|
|
|
form.addEventListener('submit', () => {
|
|
document.getElementById('poll-payload').value = JSON.stringify({
|
|
channel_id: form.elements.channel_id.value || null,
|
|
question: document.getElementById('poll-question').value,
|
|
description: document.getElementById('poll-description').value,
|
|
options: options.filter((o) => o.label.trim()),
|
|
duration: document.getElementById('poll-duration').value || null,
|
|
max_choices: Number(document.getElementById('poll-max').value) || 1,
|
|
live_results: document.getElementById('poll-live').checked,
|
|
anonymous: document.getElementById('poll-anon').checked,
|
|
ping_role: document.getElementById('poll-ping').value || null,
|
|
roles: Array.from(roleBox.querySelectorAll('input:checked')).map((i) => i.value),
|
|
});
|
|
});
|
|
})();
|