function escapeXML(text) { return $("").text(text).html(); } async function pollRetractVote(id) { let poll = $(`.poll[data-id=${id}]`); poll.addClass("loading"); try { let html = (await API.Polls.unvote(poll.data("id"))).html; poll.prop("outerHTML", html); } catch(e) { MessageBox(tr("error"), "Sorry: " + e.message, ["OK"], [Function.noop]); } finally { poll.removeClass("loading"); } } async function pollFormSubmit(e, form) { e.preventDefault(); form = $(form); let options; let isMultiple = form.data("multi"); let pollId = form.data("pid"); let formData = form.serializeArray(); if(!isMultiple) { options = [Number(formData[0].value)]; } else { options = []; formData.forEach(function(record) { if(!record.name.startsWith("option") || record.value !== "on") return; options.push(Number(record.name.substr(6))); }); } let poll = form.parent(); poll.addClass("loading"); try { let html = (await API.Polls.vote(pollId, options.join(","))).html; poll.prop("outerHTML", html); } catch(e) { MessageBox(tr("error"), "Sorry: " + e.message, ["OK"], [Function.noop]); } finally { poll.removeClass("loading"); } } function pollCheckBoxPressed(cb) { cb = $(cb); let form = cb.parent().parent().parent().parent(); let checked = $("input:checked", form); if(checked.length >= 1) $("input[type=submit]", form).removeAttr("disabled"); else $("input[type=submit]", form).attr("disabled", "disabled"); } function pollRadioPressed(radio) { let form = $(radio).parent().parent().parent().parent(); form.submit(); } function initPoll(event) { let form = $(event.target.closest('.post-buttons')).parent(); const id = random_int(0, 100) let mBody = `




${tr("poll_editor_tips")}
`; const msg = MessageBox(tr("create_poll"), mBody, [tr("attach"), tr("cancel")], [ function() { let dialog = $(msg.getNode().nodes[0]); $("input", dialog).unbind(); let title = $("input[name=title]", dialog).val(); let anon = $("input[name=anon]", dialog).prop("checked") ? "yes" : "no"; let multi = $("input[name=multi]", dialog).prop("checked") ? "yes" : "no"; let lock = $("input[name=locked]", dialog).prop("checked") ? "yes" : "no"; let expires = "infinite"; if($("input[name=expires]", dialog).prop("checked")) expires = $("select[name=expires_in]", dialog).val(); let options = ""; $(".poll-option", dialog).each(function() { if($(this).val().length === 0) return; options += ``; }); let xml = ` ${options} `; $("input[name=poll]", form).val(xml); $(".post-has-poll", form).show(); }, function() { $("input", $(msg.getNode())).unbind(); } ], true); let editor = $(`#poll_editor${id}`); $("input[name=newOption]", editor).bind("focus", function() { let newOption = $(''); newOption.appendTo($(".poll-options", editor)); newOption.focus(); newOption.bind("keydown", function(e) { if(e.key === "Enter" && $(this).next().length === 0) { $("input[name=newOption]", editor).focus(); return; } if($(this).val().length > 0) return; if(e.key !== "Backspace") return; if($(this).siblings().length === 0) return; if($(this).prev().length === 0) $(this).next().focus(); else $(this).prev().focus(); e.preventDefault(); $(this).unbind("keydown"); $(this).remove(); }); }); }