scope.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <div>
  3. <a-row>
  4. <a-col :span="16">
  5. <a-radio-group v-model="select.scope" @change="onChange">
  6. <a-radio-button v-for="o of scopeOptions" :value="o.value" :key="o.key">
  7. {{ o.label }}
  8. </a-radio-button>
  9. </a-radio-group>
  10. </a-col>
  11. </a-row>
  12. <a-row>
  13. <a-col :span="16">
  14. <a-select
  15. v-model="select.id"
  16. dropdownClassName="oc-select-dropdown"
  17. show-search
  18. v-if="showSelect"
  19. @search="handleSearch"
  20. @change="handleSelectChange"
  21. :filterOption="false"
  22. :loading="loading">
  23. <template v-for="option of options">
  24. <a-select-option :key="option.id" :value="option.id" :label="option.label">
  25. <scope-option :scope="select.scope" :option="option" />
  26. </a-select-option>
  27. </template>
  28. </a-select>
  29. </a-col>
  30. </a-row>
  31. </div>
  32. </template>
  33. <script>
  34. import _ from 'lodash'
  35. export const ScopeOption = {
  36. name: 'ScopeSelectOption',
  37. props: {
  38. scope: {
  39. type: String,
  40. required: true,
  41. },
  42. option: {
  43. type: Object,
  44. required: true,
  45. },
  46. },
  47. render (createElement, context) {
  48. if (this.scope === 'system') {
  49. return <div><span class="text-color-secondary option-prefix">{ this.$t(`dictionary.${this.scope}`) }: </span>{ this.option.name }</div>
  50. } else if (this.scope === 'domain') {
  51. return <div><span class="text-color-secondary option-prefix">{ this.$t(`dictionary.${this.scope}`) }: </span>{ this.option.name }</div>
  52. } else {
  53. return <div><span class="text-color-secondary option-prefix">{ this.$t(`dictionary.${this.scope}`) }: </span>{ this.option.name }<span className="ml-4 text-color-secondary"> {'(' + this.$t('monitor.text_107') + ' : '}</span>{ this.option.data.project_domain + ')'}</div>
  54. }
  55. },
  56. }
  57. export default {
  58. name: 'ScopeSelect',
  59. components: {
  60. ScopeOption,
  61. },
  62. props: {
  63. value: {
  64. type: Object,
  65. default: () => ({ scope: '', id: '' }),
  66. },
  67. },
  68. data () {
  69. const scope = this.$store.getters.scope
  70. let scopeId = ''
  71. if (scope === 'domain') {
  72. scopeId = this.$store.getters.userInfo.projectDomainId
  73. } else if (scope === 'project') {
  74. scopeId = this.$store.getters.userInfo.projectId
  75. }
  76. this.fetchOptions = _.debounce(this.fetchOptions, 300)
  77. return {
  78. scope: scope,
  79. select: { scope: scope, id: scopeId },
  80. options: [],
  81. }
  82. },
  83. computed: {
  84. scopeLevel () {
  85. if (this.scope === 'system') return 2
  86. if (this.scope === 'domain') return 1
  87. return 0
  88. },
  89. scopeOptions () {
  90. const options = []
  91. if (this.scopeLevel >= 2) options.push({ value: 'system', key: 'system', label: this.$t('shareScope.system') })
  92. if (this.scopeLevel >= 1) options.push({ value: 'domain', key: 'domain_id', label: this.$t('shareScope.domain') })
  93. if (this.scopeLevel >= 0) options.push({ value: 'project', key: 'project_id', label: this.$t('shareScope.project') })
  94. return options
  95. },
  96. showSelect () {
  97. return this.select.scope !== this.scope
  98. },
  99. manager () {
  100. const selectedScope = this.select.scope
  101. if (selectedScope === 'domain') return new this.$Manager('domains', 'v1')
  102. if (selectedScope === 'project') return new this.$Manager('projects', 'v1')
  103. return undefined
  104. },
  105. },
  106. methods: {
  107. onChange (v) {
  108. this.select.id = ''
  109. this.fetchOptions()
  110. },
  111. handleSearch (v) {
  112. this.fetchOptions(v)
  113. },
  114. handleSelectChange (v) {
  115. this.select.id = v
  116. this.$emit('change', this.select)
  117. },
  118. async _fetchOptions (params) {
  119. if (this.showSelect && this.manager) {
  120. const { data: { data } } = await this.manager.list({ params: params })
  121. return data.map((item) => { return { id: item.id, name: item.name, label: item.name, data: item } })
  122. }
  123. return []
  124. },
  125. async fetchOptions (search) {
  126. this.loading = true
  127. try {
  128. const params = {
  129. scope: this.scope,
  130. limit: 50,
  131. }
  132. if (search && search.length > 0) {
  133. params['filter.0'] = `name.contains(${search})`
  134. }
  135. this.options = await this._fetchOptions(params)
  136. if (this.options[0]) {
  137. this.select.id = this.options[0].id
  138. this.$emit('change', this.select)
  139. }
  140. this.loading = false
  141. } catch (error) {
  142. throw error
  143. } finally {
  144. this.loading = false
  145. }
  146. },
  147. },
  148. }
  149. </script>
  150. <style scoped>
  151. </style>