thedesk/app/js/tl/poll.js

133 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-03-06 19:08:48 +11:00
//アンケートのトグル
function pollToggle() {
2019-05-19 17:39:30 +10:00
if ($("#poll").hasClass("hide")) {
2019-03-06 19:08:48 +11:00
$("#right-side").show()
2019-09-01 00:35:18 +10:00
$("#right-side").css("width", "300px")
$("#left-side").css("width", "calc(100% - 300px)")
var width = localStorage.getItem("postbox-width").replace("px", "") * 1 + 300;
if (!width) {
width = 600
}
$("#post-box").css("width", width + "px")
2019-05-19 17:39:30 +10:00
$("#poll").removeClass("hide")
} else {
2019-03-06 19:08:48 +11:00
$("#right-side").hide()
2019-09-01 00:35:18 +10:00
$("#left-side").css("width", "100%")
$("#right-side").css("width", "300px")
var width = localStorage.getItem("postbox-width").replace("px", "") * 1;
if (!width) {
width = 300
}
$("#post-box").css("width", width + "px")
2019-03-06 19:08:48 +11:00
$("#emoji").addClass("hide")
2019-05-19 17:39:30 +10:00
$("#poll").addClass("hide")
}
2019-03-06 19:08:48 +11:00
}
2019-05-19 17:39:30 +10:00
function pollProviderCk() {
2019-03-06 19:08:48 +11:00
$(".poll-provider").addClass("hide");
2019-05-19 17:39:30 +10:00
$("#" + $("#poll-sel").val()).removeClass("hide");
2019-03-06 19:08:48 +11:00
}
/*
function pollAddtime(num){
var last=$("#expires_in").val();
last=last*1-(num*-1);
$("#expires_in").val(last);
pollCalc();
}
*/
2019-05-19 17:39:30 +10:00
function pollCalc() {
var days = $("#days_poll").val();
var hrs = $("#hours_poll").val();
var mins = $("#mins_poll").val();
console.log("Poll calc:" + days * 86400 + hrs * 3600 + mins * 60)
return days * 86400 + hrs * 3600 + mins * 60;
2019-03-06 19:08:48 +11:00
}
//Vote
2019-05-19 17:39:30 +10:00
function voteSelMastodon(acct_id, id, to, mul) {
if ($('.vote_' + acct_id + '_' + id + '_' + to).hasClass("sel")) {
$('.vote_' + acct_id + '_' + id + '_' + to).css("background-color", "transparent")
$('.vote_' + acct_id + '_' + id + '_' + to).removeClass("sel");
} else {
if (!mul) {
$('.vote_' + acct_id + '_' + id + ' div').each(function (i, elem) {
if (i == to) {
$(this).css("background-color", "var(--emphasized)");
2019-03-06 19:08:48 +11:00
$(this).addClass("sel");
2019-05-19 17:39:30 +10:00
} else {
$(this).css("background-color", "transparent")
2019-03-06 19:08:48 +11:00
$(this).removeClass("sel");
}
});
2019-05-19 17:39:30 +10:00
} else {
$('.vote_' + acct_id + '_' + id + '_' + to).css("background-color", "var(--emphasized)")
$('.vote_' + acct_id + '_' + id + '_' + to).addClass("sel");
2019-03-06 19:08:48 +11:00
}
}
2019-05-19 17:39:30 +10:00
2019-03-06 19:08:48 +11:00
}
2019-05-19 17:39:30 +10:00
function voteMastodon(acct_id, id) {
var choice = [];
$('.vote_' + acct_id + '_' + id + ' div').each(function (i, elem) {
if ($(this).hasClass("sel")) {
choice.push(i + "");
2019-03-06 19:08:48 +11:00
}
});
var domain = localStorage.getItem("domain_" + acct_id);
2019-05-19 17:39:30 +10:00
var at = localStorage.getItem("acct_" + acct_id + "_at");
var start = "https://" + domain + "/api/v1/polls/" + id + "/votes";
if (localStorage.getItem("mode_" + domain) == "misskey") {
2019-03-06 19:08:48 +11:00
return false;
}
2019-05-19 17:39:30 +10:00
var httpreq = new XMLHttpRequest();
httpreq.open('POST', start, true);
2019-03-06 19:08:48 +11:00
httpreq.setRequestHeader('Content-Type', 'application/json');
httpreq.setRequestHeader('Authorization', 'Bearer ' + at);
2019-03-08 05:19:26 +11:00
httpreq.responseType = "json";
2019-05-19 17:39:30 +10:00
httpreq.send(JSON.stringify({ choices: choice }));
httpreq.onreadystatechange = function () {
voteMastodonrefresh(acct_id, id)
}
2019-03-06 19:08:48 +11:00
}
2019-05-19 17:39:30 +10:00
function showResult(acct_id, id) {
$('.vote_' + acct_id + '_' + id + '_result').toggleClass("hide")
2019-03-06 19:08:48 +11:00
}
2019-05-19 17:39:30 +10:00
function voteMastodonrefresh(acct_id, id) {
2019-03-06 19:08:48 +11:00
var datetype = localStorage.getItem("datetype");
if (!datetype) {
2019-05-19 17:39:30 +10:00
datetype = "absolute";
}
2019-03-06 19:08:48 +11:00
var httpreqd = new XMLHttpRequest();
var domain = localStorage.getItem("domain_" + acct_id);
2019-05-19 17:39:30 +10:00
var at = localStorage.getItem("acct_" + acct_id + "_at");
var start = "https://" + domain + "/api/v1/polls/" + id;
httpreqd.open('GET', start, true);
httpreqd.setRequestHeader('Content-Type', 'application/json');
httpreqd.setRequestHeader('Authorization', 'Bearer ' + at);
2019-03-06 19:08:48 +11:00
httpreqd.responseType = 'json';
httpreqd.send();
2019-05-19 17:39:30 +10:00
httpreqd.onreadystatechange = function () {
if (httpreqd.readyState == 4) {
2019-03-06 19:08:48 +11:00
var json = httpreqd.response;
2019-05-19 17:39:30 +10:00
console.log(["Refresh vote", json]);
if (!json) {
2019-03-06 19:08:48 +11:00
return false;
}
2019-05-19 17:39:30 +10:00
var poll = "";
var choices = json.options;
var myvote = lang.lang_parse_voted;
var result_hide = "";
Object.keys(choices).forEach(function (keyc) {
var choice = choices[keyc];
if (!json.voted) {
votesel = 'voteSelMastodon(\'' + acct_id + '\',\'' + json.id + '\',' + keyc + ',' + json.multiple + ')';
}
poll = poll + '<div class="pointer vote vote_' + acct_id + '_' + json.id + '_' + keyc + '" onclick="' + votesel + '">' + escapeHTML(choice.title) + '<span class="vote_' + acct_id + '_' + json.id + '_result ' + result_hide + '">(' + choice.votes_count + ')</span></div>';
});
poll = poll + myvote + '<a onclick="voteMastodonrefresh(\'' + acct_id + '\',\'' + json.id + '\')" class="pointer">' + lang.lang_manager_refresh + '</a><span class="cbadge cbadge-hover" title="' + date(json.expires_at, 'absolute') +
2019-05-19 17:39:30 +10:00
'"><i class="far fa-calendar-times"></i>' +
date(json.expires_at, datetype) + '</span>';
$('.vote_' + acct_id + '_' + json.id).html(poll)
}
}
2019-03-06 19:08:48 +11:00
}