logo

pleroma-fe

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

poll_form.js (3298B)


  1. import * as DateUtils from 'src/services/date_utils/date_utils.js'
  2. import { pollFallback } from 'src/services/poll/poll.service.js'
  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: {
  19. visible: {},
  20. params: {
  21. type: Object,
  22. required: true
  23. }
  24. },
  25. computed: {
  26. pollType: {
  27. get () { return pollFallback(this.params, 'pollType') },
  28. set (newVal) { this.params.pollType = newVal }
  29. },
  30. options () {
  31. const hasOptions = !!this.params.options
  32. if (!hasOptions) {
  33. this.params.options = pollFallback(this.params, 'options')
  34. }
  35. return this.params.options
  36. },
  37. expiryAmount: {
  38. get () { return pollFallback(this.params, 'expiryAmount') },
  39. set (newVal) { this.params.expiryAmount = newVal }
  40. },
  41. expiryUnit: {
  42. get () { return pollFallback(this.params, 'expiryUnit') },
  43. set (newVal) { this.params.expiryUnit = newVal }
  44. },
  45. pollLimits () {
  46. return this.$store.state.instance.pollLimits
  47. },
  48. maxOptions () {
  49. return this.pollLimits.max_options
  50. },
  51. maxLength () {
  52. return this.pollLimits.max_option_chars
  53. },
  54. expiryUnits () {
  55. const allUnits = ['minutes', 'hours', 'days']
  56. const expiry = this.convertExpiryFromUnit
  57. return allUnits.filter(
  58. unit => this.pollLimits.max_expiration >= expiry(unit, 1)
  59. )
  60. },
  61. minExpirationInCurrentUnit () {
  62. return Math.ceil(
  63. this.convertExpiryToUnit(
  64. this.expiryUnit,
  65. this.pollLimits.min_expiration
  66. )
  67. )
  68. },
  69. maxExpirationInCurrentUnit () {
  70. return Math.floor(
  71. this.convertExpiryToUnit(
  72. this.expiryUnit,
  73. this.pollLimits.max_expiration
  74. )
  75. )
  76. }
  77. },
  78. methods: {
  79. clear () {
  80. this.pollType = 'single'
  81. this.options = ['', '']
  82. this.expiryAmount = 10
  83. this.expiryUnit = 'minutes'
  84. },
  85. nextOption (index) {
  86. const element = this.$el.querySelector(`#poll-${index + 1}`)
  87. if (element) {
  88. element.focus()
  89. } else {
  90. // Try adding an option and try focusing on it
  91. const addedOption = this.addOption()
  92. if (addedOption) {
  93. this.$nextTick(function () {
  94. this.nextOption(index)
  95. })
  96. }
  97. }
  98. },
  99. addOption () {
  100. if (this.options.length < this.maxOptions) {
  101. this.options.push('')
  102. return true
  103. }
  104. return false
  105. },
  106. deleteOption (index, event) {
  107. if (this.options.length > 2) {
  108. this.options.splice(index, 1)
  109. }
  110. },
  111. convertExpiryToUnit (unit, amount) {
  112. // Note: we want seconds and not milliseconds
  113. return DateUtils.secondsToUnit(unit, amount)
  114. },
  115. convertExpiryFromUnit (unit, amount) {
  116. return DateUtils.unitToSeconds(unit, amount)
  117. },
  118. expiryAmountChange () {
  119. this.expiryAmount =
  120. Math.max(this.minExpirationInCurrentUnit, this.expiryAmount)
  121. this.expiryAmount =
  122. Math.min(this.maxExpirationInCurrentUnit, this.expiryAmount)
  123. }
  124. }
  125. }