index.vue 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <a-select show-search :filter-option="filterOption" option-filter-prop="children" :disabled="disabled" :value="value" @change="clusterChange" :placeholder="$t('k8s.text_30')">
  3. <a-select-option
  4. v-for="item in clusterOps"
  5. :key="item.id"
  6. :title="item.name"
  7. :value="item.id">
  8. {{ item.name }}
  9. </a-select-option>
  10. </a-select>
  11. </template>
  12. <script>
  13. import { mapState } from 'vuex'
  14. import _ from 'lodash'
  15. export default {
  16. name: 'K8SClusterSelect',
  17. props: {
  18. value: {
  19. required: true,
  20. },
  21. setDefault: { // 设置默认
  22. type: Boolean,
  23. default: true,
  24. },
  25. disabled: {
  26. type: Boolean,
  27. default: false,
  28. },
  29. },
  30. data () {
  31. return {
  32. clusterOps: [],
  33. }
  34. },
  35. computed: {
  36. ...mapState('common', {
  37. cluster: state => state.k8s.cluster,
  38. }),
  39. },
  40. created () {
  41. this.clusterM = new this.$Manager('kubeclusters', 'v1')
  42. this.fetchClusterOps()
  43. },
  44. methods: {
  45. syncCluster (id) {
  46. const obj = this.clusterOps.find(v => v.id === id)
  47. this.$emit('update:clusterObj', obj)
  48. this.$emit('change', id)
  49. },
  50. clusterChange (cluster) {
  51. this.$emit('input', cluster)
  52. this.syncCluster(cluster)
  53. },
  54. fetchClusterOps () {
  55. this.clusterM.list({
  56. params: {
  57. limit: 0,
  58. 'filter.0': 'status.equals(running)',
  59. scope: this.$store.getters.scope,
  60. },
  61. }).then(({ data: { data = [] } }) => {
  62. data = data.sort((a, b) => b.update_version - a.update_version)
  63. if (data.length) {
  64. const systemItemIdx = data.findIndex(val => val.provider === 'system')
  65. if (systemItemIdx >= 0) { // 把 system 的集群放在最后,点击时并提升
  66. const [systemItem] = data.splice(systemItemIdx, 1)
  67. data.push(systemItem)
  68. }
  69. this.clusterOps = data
  70. if (this.setDefault) {
  71. if (!this.value) {
  72. const clusterObj = this.clusterOps[0]
  73. this.$emit('input', clusterObj.id)
  74. this.syncCluster(clusterObj.id)
  75. } else {
  76. this.syncCluster(this.value)
  77. }
  78. }
  79. }
  80. })
  81. },
  82. filterOption (input, option) {
  83. const text = _.get(option.componentOptions, 'children[0].text') || ''
  84. return (
  85. text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  86. )
  87. },
  88. },
  89. }
  90. </script>