pleroma-fe/src/components/poll/poll_form.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

2019-06-18 20:28:31 +00:00
import * as DateUtils from 'src/services/date_utils/date_utils.js'
2023-03-10 20:16:03 -05:00
import { pollFallback } from 'src/services/poll/poll.service.js'
import { useInstanceStore } from 'src/stores/instance.js'
import Select from '../select/select.vue'
2026-01-08 17:26:52 +02:00
import { library } from '@fortawesome/fontawesome-svg-core'
import { faPlus, faTimes } from '@fortawesome/free-solid-svg-icons'
2026-01-06 16:22:52 +02:00
library.add(faTimes, faPlus)
2019-06-18 20:28:31 +00:00
export default {
components: {
2026-01-06 16:22:52 +02:00
Select,
},
2019-06-18 20:28:31 +00:00
name: 'PollForm',
2023-03-10 20:16:03 -05:00
props: {
visible: {},
2023-05-02 08:06:55 -04:00
params: {
2023-03-10 20:16:03 -05:00
type: Object,
2026-01-06 16:22:52 +02:00
required: true,
},
2023-03-10 20:16:03 -05:00
},
2019-06-18 20:28:31 +00:00
computed: {
2023-03-10 20:16:03 -05:00
pollType: {
2026-01-06 16:22:52 +02:00
get() {
return pollFallback(this.params, 'pollType')
},
set(newVal) {
this.params.pollType = newVal
},
2023-03-10 20:16:03 -05:00
},
2026-01-06 16:22:52 +02:00
options() {
2023-05-02 08:06:55 -04:00
const hasOptions = !!this.params.options
if (!hasOptions) {
this.params.options = pollFallback(this.params, 'options')
}
return this.params.options
2023-03-10 20:16:03 -05:00
},
expiryAmount: {
2026-01-06 16:22:52 +02:00
get() {
return pollFallback(this.params, 'expiryAmount')
},
set(newVal) {
this.params.expiryAmount = newVal
},
2023-03-10 20:16:03 -05:00
},
expiryUnit: {
2026-01-06 16:22:52 +02:00
get() {
return pollFallback(this.params, 'expiryUnit')
},
set(newVal) {
this.params.expiryUnit = newVal
},
2023-03-10 20:16:03 -05:00
},
2026-01-06 16:22:52 +02:00
pollLimits() {
return useInstanceStore().pollLimits
2019-06-18 20:28:31 +00:00
},
2026-01-06 16:22:52 +02:00
maxOptions() {
2019-06-18 20:28:31 +00:00
return this.pollLimits.max_options
},
2026-01-06 16:22:52 +02:00
maxLength() {
2019-06-18 20:28:31 +00:00
return this.pollLimits.max_option_chars
},
2026-01-06 16:22:52 +02:00
expiryUnits() {
2019-06-18 20:28:31 +00:00
const allUnits = ['minutes', 'hours', 'days']
const expiry = this.convertExpiryFromUnit
return allUnits.filter(
2026-01-06 16:22:52 +02:00
(unit) => this.pollLimits.max_expiration >= expiry(unit, 1),
2019-06-18 20:28:31 +00:00
)
},
2026-01-06 16:22:52 +02:00
minExpirationInCurrentUnit() {
2019-06-18 20:28:31 +00:00
return Math.ceil(
this.convertExpiryToUnit(
this.expiryUnit,
2026-01-06 16:22:52 +02:00
this.pollLimits.min_expiration,
),
2019-06-18 20:28:31 +00:00
)
},
2026-01-06 16:22:52 +02:00
maxExpirationInCurrentUnit() {
2019-06-18 20:28:31 +00:00
return Math.floor(
this.convertExpiryToUnit(
this.expiryUnit,
2026-01-06 16:22:52 +02:00
this.pollLimits.max_expiration,
),
2019-06-18 20:28:31 +00:00
)
2026-01-06 16:22:52 +02:00
},
2019-06-18 20:28:31 +00:00
},
methods: {
2026-01-06 16:22:52 +02:00
clear() {
2019-06-18 20:28:31 +00:00
this.pollType = 'single'
this.options = ['', '']
this.expiryAmount = 10
this.expiryUnit = 'minutes'
},
2026-01-06 16:22:52 +02:00
nextOption(index) {
2019-06-18 20:28:31 +00:00
const element = this.$el.querySelector(`#poll-${index + 1}`)
if (element) {
element.focus()
} else {
// Try adding an option and try focusing on it
const addedOption = this.addOption()
if (addedOption) {
this.$nextTick(function () {
this.nextOption(index)
})
}
}
},
2026-01-06 16:22:52 +02:00
addOption() {
2019-06-18 20:28:31 +00:00
if (this.options.length < this.maxOptions) {
2023-04-05 23:36:52 -04:00
this.options.push('')
2019-06-18 20:28:31 +00:00
return true
}
return false
},
2026-01-06 16:22:52 +02:00
deleteOption(index) {
2019-06-18 20:28:31 +00:00
if (this.options.length > 2) {
this.options.splice(index, 1)
}
},
2026-01-06 16:22:52 +02:00
convertExpiryToUnit(unit, amount) {
2019-06-18 20:28:31 +00:00
// Note: we want seconds and not milliseconds
2022-04-30 11:08:19 -04:00
return DateUtils.secondsToUnit(unit, amount)
2019-06-18 20:28:31 +00:00
},
2026-01-06 16:22:52 +02:00
convertExpiryFromUnit(unit, amount) {
2022-04-30 11:08:19 -04:00
return DateUtils.unitToSeconds(unit, amount)
2019-06-18 20:28:31 +00:00
},
2026-01-06 16:22:52 +02:00
expiryAmountChange() {
this.expiryAmount = Math.max(
this.minExpirationInCurrentUnit,
this.expiryAmount,
)
this.expiryAmount = Math.min(
this.maxExpirationInCurrentUnit,
this.expiryAmount,
)
},
},
2019-06-18 20:28:31 +00:00
}