2024-07-01 15:45:11 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
import { tplPoll } from '../templates/poll.js'
|
|
|
|
import { CustomElement } from 'shared/components/element.js'
|
2024-07-05 12:48:02 +00:00
|
|
|
import { converse, _converse, api } from '@converse/headless/core'
|
2024-07-01 15:45:11 +00:00
|
|
|
import '../styles/poll.scss'
|
|
|
|
|
|
|
|
export default class MUCPollView extends CustomElement {
|
|
|
|
static get properties () {
|
|
|
|
return {
|
2024-07-04 13:15:28 +00:00
|
|
|
model: { type: Object, attribute: true },
|
|
|
|
collapsed: { type: Boolean, attribute: false },
|
|
|
|
buttonDisabled: { type: Boolean, attribute: false }
|
2024-07-01 15:45:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async initialize () {
|
2024-07-04 13:15:28 +00:00
|
|
|
this.collapsed = false
|
|
|
|
this.buttonDisabled = false
|
2024-07-01 15:45:11 +00:00
|
|
|
if (!this.model) {
|
|
|
|
return
|
|
|
|
}
|
2024-07-04 13:15:28 +00:00
|
|
|
this.listenTo(this.model, 'change:current_poll', () => {
|
|
|
|
this.buttonDisabled = false
|
|
|
|
this.requestUpdate()
|
|
|
|
})
|
2024-07-05 12:48:02 +00:00
|
|
|
this.listenTo(this.model.occupants, 'change:role', occupant => {
|
|
|
|
if (occupant.get('jid') !== _converse.bare_jid) { // only for myself
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// visitors cant vote. So we must refresh the polls results when current occupant role changes.
|
|
|
|
this.requestUpdate()
|
|
|
|
})
|
2024-07-01 15:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
const currentPoll = this.model?.get('current_poll')
|
2024-07-05 09:00:37 +00:00
|
|
|
const entered = this.model.session.get('connection_status') === converse.ROOMSTATUS.ENTERED
|
|
|
|
const canVote = entered && this.model.getOwnRole() !== 'visitor'
|
|
|
|
return tplPoll(this, currentPoll, canVote)
|
2024-07-01 15:45:11 +00:00
|
|
|
}
|
2024-07-04 13:15:28 +00:00
|
|
|
|
2024-07-04 15:49:28 +00:00
|
|
|
toggle (ev) {
|
|
|
|
ev.preventDefault()
|
2024-07-04 13:15:28 +00:00
|
|
|
this.collapsed = !this.collapsed
|
|
|
|
}
|
|
|
|
|
|
|
|
voteFor (choice) {
|
|
|
|
if (this.buttonDisabled) { return }
|
|
|
|
|
|
|
|
const currentPoll = this.model?.get('current_poll')
|
|
|
|
if (!currentPoll) { return }
|
|
|
|
if (currentPoll.over) { return }
|
|
|
|
|
|
|
|
console.info('User has voted for choice: ', choice)
|
|
|
|
// We disable vote buttons until next refresh:
|
|
|
|
this.buttonDisabled = true
|
|
|
|
this.requestUpdate()
|
|
|
|
|
|
|
|
this.model.sendMessage({
|
|
|
|
body: '!' + choice.choice
|
|
|
|
})
|
|
|
|
}
|
2024-07-04 13:34:32 +00:00
|
|
|
|
|
|
|
closePoll (ev) {
|
|
|
|
ev.preventDefault()
|
|
|
|
this.model.set('current_poll', undefined)
|
|
|
|
}
|
2024-07-01 15:45:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
api.elements.define('livechat-converse-muc-poll', MUCPollView)
|