RequestUserAgent.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!--
  2. - @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  3. -
  4. - @author Julius Härtl <jus@bitgrid.net>
  5. -
  6. - @license GNU AGPL version 3 or any later version
  7. -
  8. - This program is free software: you can redistribute it and/or modify
  9. - it under the terms of the GNU Affero General Public License as
  10. - published by the Free Software Foundation, either version 3 of the
  11. - License, or (at your option) any later version.
  12. -
  13. - This program is distributed in the hope that it will be useful,
  14. - but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. - GNU Affero General Public License for more details.
  17. -
  18. - You should have received a copy of the GNU Affero General Public License
  19. - along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. -
  21. -->
  22. <template>
  23. <div>
  24. <Multiselect
  25. :value="currentValue"
  26. :placeholder="t('workflowengine', 'Select a user agent')"
  27. label="label"
  28. track-by="pattern"
  29. group-values="children"
  30. group-label="label"
  31. :options="options" :multiple="false" :tagging="false"
  32. @input="setValue">
  33. <template slot="singleLabel" slot-scope="props">
  34. <span class="option__icon" :class="props.option.icon" />
  35. <span class="option__title option__title_single">{{ props.option.label }}</span>
  36. </template>
  37. <template slot="option" slot-scope="props">
  38. <span class="option__icon" :class="props.option.icon" />
  39. <span class="option__title">{{ props.option.label }} {{ props.option.$groupLabel }}</span>
  40. </template>
  41. </Multiselect>
  42. <input v-if="!isPredefined" type="text" :value="currentValue.pattern"
  43. @input="updateCustom">
  44. </div>
  45. </template>
  46. <script>
  47. import { Multiselect } from 'nextcloud-vue/dist/Components/Multiselect'
  48. import valueMixin from '../../mixins/valueMixin'
  49. export default {
  50. name: 'RequestUserAgent',
  51. components: {
  52. Multiselect
  53. },
  54. mixins: [
  55. valueMixin
  56. ],
  57. data() {
  58. return {
  59. newValue: '',
  60. predefinedTypes: [
  61. {
  62. label: t('workflowengine', 'Sync clients'),
  63. children: [
  64. { pattern: 'android', label: t('workflowengine', 'Android client'), icon: 'icon-phone' },
  65. { pattern: 'ios', label: t('workflowengine', 'iOS client'), icon: 'icon-phone' },
  66. { pattern: 'desktop', label: t('workflowengine', 'Desktop client'), icon: 'icon-desktop' },
  67. { pattern: 'mail', label: t('workflowengine', 'Thunderbird & Outlook addons'), icon: 'icon-mail' }
  68. ]
  69. }
  70. ]
  71. }
  72. },
  73. computed: {
  74. options() {
  75. return [...this.predefinedTypes, this.customValue]
  76. },
  77. isPredefined() {
  78. const matchingPredefined = this.predefinedTypes.map(groups => groups.children).flat().find((type) => this.newValue === type.pattern)
  79. if (matchingPredefined) {
  80. return true
  81. }
  82. return false
  83. },
  84. customValue() {
  85. return {
  86. label: t('workflowengine', 'Others'),
  87. children: [
  88. {
  89. icon: 'icon-settings-dark',
  90. label: t('workflowengine', 'Custom user agent'),
  91. pattern: ''
  92. }
  93. ]
  94. }
  95. },
  96. currentValue() {
  97. const matchingPredefined = this.predefinedTypes.map(groups => groups.children).flat().find((type) => this.newValue === type.pattern)
  98. if (matchingPredefined) {
  99. return matchingPredefined
  100. }
  101. return {
  102. icon: 'icon-settings-dark',
  103. label: t('workflowengine', 'Custom user agent'),
  104. pattern: this.newValue
  105. }
  106. }
  107. },
  108. methods: {
  109. validateRegex(string) {
  110. var regexRegex = /^\/(.*)\/([gui]{0,3})$/
  111. var result = regexRegex.exec(string)
  112. return result !== null
  113. },
  114. setValue(value) {
  115. // TODO: check if value requires a regex and set the check operator according to that
  116. if (value !== null) {
  117. this.newValue = value.pattern
  118. this.$emit('input', this.newValue)
  119. }
  120. },
  121. updateCustom(event) {
  122. this.newValue = event.target.value
  123. this.$emit('input', this.newValue)
  124. }
  125. }
  126. }
  127. </script>
  128. <style scoped src="./../../css/multiselect.css"></style>