logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://hacktivis.me/git/pleroma-fe.git

poll_form.js (3219B)


  1. import * as DateUtils from 'src/services/date_utils/date_utils.js'
  2. import { uniq } from 'lodash'
  3. import { library } from '@fortawesome/fontawesome-svg-core'
  4. import Select from '../select/select.vue'
  5. import {
  6. faTimes,
  7. faPlus
  8. } from '@fortawesome/free-solid-svg-icons'
  9. library.add(
  10. faTimes,
  11. faPlus
  12. )
  13. export default {
  14. components: {
  15. Select
  16. },
  17. name: 'PollForm',
  18. props: ['visible'],
  19. data: () => ({
  20. pollType: 'single',
  21. options: ['', ''],
  22. expiryAmount: 10,
  23. expiryUnit: 'minutes'
  24. }),
  25. computed: {
  26. pollLimits () {
  27. return this.$store.state.instance.pollLimits
  28. },
  29. maxOptions () {
  30. return this.pollLimits.max_options
  31. },
  32. maxLength () {
  33. return this.pollLimits.max_option_chars
  34. },
  35. expiryUnits () {
  36. const allUnits = ['minutes', 'hours', 'days']
  37. const expiry = this.convertExpiryFromUnit
  38. return allUnits.filter(
  39. unit => this.pollLimits.max_expiration >= expiry(unit, 1)
  40. )
  41. },
  42. minExpirationInCurrentUnit () {
  43. return Math.ceil(
  44. this.convertExpiryToUnit(
  45. this.expiryUnit,
  46. this.pollLimits.min_expiration
  47. )
  48. )
  49. },
  50. maxExpirationInCurrentUnit () {
  51. return Math.floor(
  52. this.convertExpiryToUnit(
  53. this.expiryUnit,
  54. this.pollLimits.max_expiration
  55. )
  56. )
  57. }
  58. },
  59. methods: {
  60. clear () {
  61. this.pollType = 'single'
  62. this.options = ['', '']
  63. this.expiryAmount = 10
  64. this.expiryUnit = 'minutes'
  65. },
  66. nextOption (index) {
  67. const element = this.$el.querySelector(`#poll-${index + 1}`)
  68. if (element) {
  69. element.focus()
  70. } else {
  71. // Try adding an option and try focusing on it
  72. const addedOption = this.addOption()
  73. if (addedOption) {
  74. this.$nextTick(function () {
  75. this.nextOption(index)
  76. })
  77. }
  78. }
  79. },
  80. addOption () {
  81. if (this.options.length < this.maxOptions) {
  82. this.options.push('')
  83. return true
  84. }
  85. return false
  86. },
  87. deleteOption (index, event) {
  88. if (this.options.length > 2) {
  89. this.options.splice(index, 1)
  90. this.updatePollToParent()
  91. }
  92. },
  93. convertExpiryToUnit (unit, amount) {
  94. // Note: we want seconds and not milliseconds
  95. return DateUtils.secondsToUnit(unit, amount)
  96. },
  97. convertExpiryFromUnit (unit, amount) {
  98. return DateUtils.unitToSeconds(unit, amount)
  99. },
  100. expiryAmountChange () {
  101. this.expiryAmount =
  102. Math.max(this.minExpirationInCurrentUnit, this.expiryAmount)
  103. this.expiryAmount =
  104. Math.min(this.maxExpirationInCurrentUnit, this.expiryAmount)
  105. this.updatePollToParent()
  106. },
  107. updatePollToParent () {
  108. const expiresIn = this.convertExpiryFromUnit(
  109. this.expiryUnit,
  110. this.expiryAmount
  111. )
  112. const options = uniq(this.options.filter(option => option !== ''))
  113. if (options.length < 2) {
  114. this.$emit('update-poll', { error: this.$t('polls.not_enough_options') })
  115. return
  116. }
  117. this.$emit('update-poll', {
  118. options,
  119. multiple: this.pollType === 'multiple',
  120. expiresIn
  121. })
  122. }
  123. }
  124. }