index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <a-select
  3. show-search
  4. :disabled="disabled"
  5. @change="change"
  6. :placeholder="$t('k8s.text_61')"
  7. style="min-width: 200px;"
  8. :filter-option="filterOption"
  9. option-filter-prop="children"
  10. :loading="loading"
  11. :value="value">
  12. <a-select-option v-for="item in options" :value="item.id" :key="item.id">
  13. <div class="d-flex">
  14. <span class="text-truncate flex-fill mr-2">{{ item.label || item.name }}</span>
  15. <span style="color: #8492a6; font-size: 13px" v-if="R.is(Number, item.num)">{{ item.num }}</span>
  16. </div>
  17. </a-select-option>
  18. </a-select>
  19. </template>
  20. <script>
  21. import * as R from 'ramda'
  22. import _ from 'lodash'
  23. export default {
  24. name: 'K8sComponentsNamespace',
  25. props: {
  26. value: { // String
  27. },
  28. supportAllNamespace: {
  29. type: Boolean,
  30. default: false,
  31. },
  32. setDefault: { // 设置默认
  33. type: Boolean,
  34. default: true,
  35. },
  36. cluster: {
  37. type: String,
  38. },
  39. disabled: {
  40. type: Boolean,
  41. default: false,
  42. },
  43. namespaceMap: {
  44. type: Object,
  45. default: () => ({}),
  46. },
  47. },
  48. data () {
  49. return {
  50. options: [],
  51. R,
  52. loading: false,
  53. }
  54. },
  55. watch: {
  56. cluster (v) {
  57. if (v) this._fetchNamespace()
  58. },
  59. namespaceMap () {
  60. this.$nextTick(this.addNum)
  61. },
  62. },
  63. created () {
  64. this._fetchNamespace()
  65. },
  66. methods: {
  67. addNum () {
  68. if (R.isNil(this.namespaceMap)) return
  69. this.options = this.options.map(val => {
  70. const item = R.clone(val)
  71. if (R.is(Array, this.namespaceMap[item.id])) {
  72. item.num = this.namespaceMap[item.id].length
  73. } else {
  74. if (item.id !== 'all_namespace') {
  75. item.num = 0
  76. }
  77. }
  78. return item
  79. })
  80. },
  81. change (v) {
  82. this.$emit('input', v)
  83. this.$emit('change', v)
  84. if (v) {
  85. const obj = this.options.find(val => val.id === v)
  86. this.$emit('update:namespaceObj', obj)
  87. } else {
  88. this.$emit('update:namespaceObj', {})
  89. }
  90. },
  91. async _fetchNamespace () {
  92. const namespacesM = new this.$Manager('namespaces', 'v1')
  93. const params = {
  94. cluster: this.cluster,
  95. limit: 0,
  96. scope: this.$store.getters.scope,
  97. }
  98. if (!this.$store.getters.isAdminMode) {
  99. params.project = this.$store.getters.userInfo.projectId
  100. }
  101. if (!params.cluster) return
  102. try {
  103. this.loading = true
  104. const { data: { data = [] } } = await namespacesM.list({ params })
  105. this.options = data.map(val => ({ ...val, label: val.name }))
  106. this.loading = false
  107. if (this.supportAllNamespace) {
  108. this.options = data.concat({ id: 'all_namespace', label: this.$t('k8s.text_379') })
  109. }
  110. const isErrorNamespace = !this.options.find(v => v.id === this.value)
  111. const isEmptyNamespace = R.isEmpty(this.value) || R.isNil(this.value)
  112. if (this.setDefault && (isEmptyNamespace || isErrorNamespace)) {
  113. this.setDefaultNamespace(this.options)
  114. } else if (this.setDefault && !isEmptyNamespace && !isErrorNamespace) {
  115. this.change(this.value)
  116. }
  117. } catch (error) {
  118. this.loading = false
  119. throw error
  120. }
  121. },
  122. setDefaultNamespace (opts) {
  123. const all = opts.find(v => v.id === 'all_namespace')
  124. if (opts && opts.length) {
  125. if (all) {
  126. this.namespace = all.id
  127. } else {
  128. this.namespace = opts[0].id
  129. }
  130. this.change(this.namespace)
  131. } else {
  132. this.change(undefined)
  133. }
  134. },
  135. filterOption (input, option) {
  136. const text = _.get(option.componentOptions, 'children[0].children[0].children[0].text') || ''
  137. return (
  138. text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  139. )
  140. },
  141. },
  142. }
  143. </script>