Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SettingsField.vue 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <MenuItem
  3. :label="fieldLabel"
  4. :description="fieldDescription"
  5. :tooltip="fieldTooltip"
  6. :data-id="field.id">
  7. <slot>
  8. <div v-if="!readonly" class="flex-1">
  9. <!-- Select -->
  10. <select v-if="field.input === 'select'"
  11. v-model="localValue"
  12. class="input"
  13. :disabled="disabled"
  14. v-bind="inputAttrs">
  15. <option v-for="(option, index) in selectOptions" :key="index" :value="option.value">
  16. {{ option.label }}
  17. </option>
  18. </select>
  19. <!-- Input -->
  20. <input v-if="field.input === 'input'"
  21. v-model="localValue"
  22. class="input"
  23. :disabled="disabled"
  24. v-bind="inputAttrs">
  25. </div>
  26. <span v-else>{{ valueLabel }}</span>
  27. </slot>
  28. </MenuItem>
  29. </template>
  30. <script>
  31. import {
  32. get, set, find, isObject,
  33. } from 'lodash'
  34. import { VModel } from '~/mixins'
  35. export default {
  36. mixins: [VModel],
  37. props: {
  38. fieldId: {
  39. type: String,
  40. required: true,
  41. },
  42. label: {
  43. type: String,
  44. default: null,
  45. },
  46. description: {
  47. type: String,
  48. default: null,
  49. },
  50. tooltip: {
  51. type: String,
  52. default: null,
  53. },
  54. options: {
  55. type: Array,
  56. default: null,
  57. },
  58. optionsField: {
  59. type: String,
  60. default: 'options',
  61. },
  62. attrs: {
  63. type: Object,
  64. default: null,
  65. },
  66. readonly: {
  67. type: Boolean,
  68. default: false,
  69. },
  70. disabled: {
  71. type: Boolean,
  72. default: false,
  73. },
  74. ignoreHardcoded: {
  75. type: Boolean,
  76. defalt: false,
  77. },
  78. },
  79. data: () => ({
  80. localValue: null,
  81. field: null,
  82. firstWatchIgnored: false,
  83. }),
  84. computed: {
  85. selectOptions() {
  86. if (this.options) {
  87. return this.options
  88. }
  89. return this.field[this.optionsField]
  90. },
  91. valueLabel() {
  92. if (this.field.input === 'select') {
  93. const option = find(this.selectOptions, { value: this.localValue })
  94. return option.label
  95. }
  96. return this.localValue
  97. },
  98. localFieldId() {
  99. let fieldId = this.field.id
  100. // FIXME: HARD CODED!
  101. if (!this.ignoreHardcoded && fieldId.includes('preferences.')) {
  102. fieldId = fieldId.substring('preferences.'.length)
  103. }
  104. return fieldId
  105. },
  106. fieldLabel() {
  107. if (this.label) {
  108. return this.label
  109. }
  110. return this.field.label
  111. },
  112. fieldDescription() {
  113. if (this.description) {
  114. return this.description
  115. }
  116. return this.field.description
  117. },
  118. fieldTooltip() {
  119. if (this.tooltip) {
  120. return this.tooltip
  121. }
  122. return this.field.tooltip
  123. },
  124. inputAttrs() {
  125. if (this.attrs) {
  126. return this.attrs
  127. }
  128. return this.field.attrs || {}
  129. },
  130. },
  131. watch: {
  132. localValue(value) {
  133. if (!this.firstWatchIgnored) {
  134. this.firstWatchIgnored = true
  135. return
  136. }
  137. if (this.readonly || this.disabled) {
  138. return
  139. }
  140. if (this.value$ && !isObject(this.value$)) {
  141. console.warn('Saving has been prevented because value$ is not an object.')
  142. return
  143. }
  144. if (this.value$) {
  145. this.value$ = set(this.value$, this.localFieldId, value)
  146. } else {
  147. this.$settings.set(this.field.id, value)
  148. }
  149. this.$emit('change')
  150. },
  151. },
  152. created() {
  153. this.field = this.$settings.getField(this.fieldId)
  154. if (!this.field) {
  155. throw new Error(`Invalid field ID: ${this.fieldId}`)
  156. }
  157. if (isObject(this.value)) {
  158. this.localValue = get(this.value, this.localFieldId)
  159. } else if (this.value) {
  160. this.localValue = this.value
  161. } else {
  162. this.localValue = this.$settings.get(this.fieldId)
  163. }
  164. },
  165. }
  166. </script>
  167. <style lang="scss" scoped>
  168. .item {
  169. &::v-deep {
  170. .item__action {
  171. @apply flex items-center justify-center;
  172. //max-width: 300px;
  173. }
  174. }
  175. }
  176. </style>