thedesk/app/js/tl/poll.js

124 lines
3.7 KiB
JavaScript
Raw Permalink Normal View History

2019-03-06 19:08:48 +11:00
//アンケートのトグル
function pollToggle() {
2019-11-09 00:52:54 +11:00
if ($('#poll').hasClass('hide')) {
$('#right-side').show()
$('#right-side').css('width', '300px')
$('#left-side').css('width', 'calc(100% - 300px)')
var width = localStorage.getItem('postbox-width')
if (width) {
width = width.replace('px', '') * 1 + 300
} else {
width = 600
}
$('#post-box').css('width', width + 'px')
$('#poll').removeClass('hide')
$('#pollsta').text(lang.lang_yesno)
} else {
$('#right-side').hide()
$('#left-side').css('width', '100%')
$('#right-side').css('width', '300px')
var width = localStorage.getItem('postbox-width')
if (width) {
width = width.replace('px', '') * 1
} else {
width = 300
}
$('#post-box').css('width', width + 'px')
$('#emoji').addClass('hide')
2020-11-17 03:00:32 +11:00
$('#draft').addClass('hide')
2019-11-09 00:52:54 +11:00
$('#poll').addClass('hide')
$('#pollsta').text(lang.lang_no)
}
2019-03-06 19:08:48 +11:00
}
2019-05-19 17:39:30 +10:00
function pollProviderCk() {
2019-11-09 00:52:54 +11:00
$('.poll-provider').addClass('hide')
$('#' + $('#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() {
2019-11-09 00:52:54 +11:00
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
2020-02-11 04:15:54 +11:00
function voteSelMastodon(acct_id, id, to, mul, elem) {
if ($(elem).hasClass('sel')) {
$(elem).css('background-color', 'transparent')
$(elem).removeClass('sel')
2019-11-09 00:52:54 +11:00
} else {
if (!mul) {
2020-02-11 04:15:54 +11:00
$('.vote_' + acct_id + '_' + id + ' div').css('background-color', 'transparent')
$('.vote_' + acct_id + '_' + id + ' div').removeClass('sel')
$(elem).css('background-color', 'var(--emphasized)')
$(elem).addClass('sel')
2019-11-09 00:52:54 +11:00
} else {
2020-02-11 04:15:54 +11:00
$(elem).css('background-color', 'var(--emphasized)')
$(elem).addClass('sel')
2019-11-09 00:52:54 +11:00
}
}
2019-03-06 19:08:48 +11:00
}
2020-02-11 04:15:54 +11:00
function voteMastodon(acct_id, id, target) {
2019-11-09 00:52:54 +11:00
var choice = []
2020-02-11 04:15:54 +11:00
$(`#vote${target} div`).each(function(i, elem) {
2019-11-09 00:52:54 +11:00
if ($(this).hasClass('sel')) {
choice.push(i + '')
}
})
var domain = localStorage.getItem('domain_' + acct_id)
var at = localStorage.getItem('acct_' + acct_id + '_at')
var start = 'https://' + domain + '/api/v1/polls/' + id + '/votes'
if (localStorage.getItem('mode_' + domain) == 'misskey') {
return false
}
var httpreq = new XMLHttpRequest()
httpreq.open('POST', start, true)
httpreq.setRequestHeader('Content-Type', 'application/json')
httpreq.setRequestHeader('Authorization', 'Bearer ' + at)
httpreq.responseType = 'json'
httpreq.send(JSON.stringify({ choices: choice }))
httpreq.onreadystatechange = function() {
2020-02-11 04:15:54 +11:00
voteMastodonrefresh(acct_id, id, target)
2019-11-09 00:52:54 +11:00
}
2019-03-06 19:08:48 +11:00
}
2019-05-19 17:39:30 +10:00
function showResult(acct_id, id) {
2019-11-09 00:52:54 +11:00
$('.vote_' + acct_id + '_' + id + '_result').toggleClass('hide')
2019-03-06 19:08:48 +11:00
}
2020-02-11 04:15:54 +11:00
function voteMastodonrefresh(acct_id, id, target) {
2019-11-09 00:52:54 +11:00
var datetype = localStorage.getItem('datetype')
if (!datetype) {
datetype = 'absolute'
}
var httpreqd = new XMLHttpRequest()
var domain = localStorage.getItem('domain_' + acct_id)
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)
httpreqd.responseType = 'json'
httpreqd.send()
httpreqd.onreadystatechange = function() {
if (httpreqd.readyState == 4) {
if (this.status !== 200) {
setLog(start, this.status, this.response)
}
var json = httpreqd.response
console.log(['Refresh vote', json])
if (!json) {
return false
}
var poll = pollParse(json, acct_id, json.emojis)
2020-02-11 04:15:54 +11:00
$(`#vote${target}`).html(poll)
2019-11-09 00:52:54 +11:00
}
}
}