AssociatedHostDialog.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{this.params.title}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$t('dictionary.blockstorage')" :count="params.data.length" :action="this.params.title" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form :form="form.fc" v-bind="formItemLayout">
  8. <a-form-item :label="$t('storage.text_50')">
  9. <a-select :loading="hostsLoading" allowClear showSearch :filterOption="filterOption" mode="multiple" v-decorator="decorators.host" :placeholder="$t('storage.text_51')">
  10. <a-select-option v-for="item in hosts" :key="item.id" :value="item.id">{{item.name}}</a-select-option>
  11. </a-select>
  12. </a-form-item>
  13. <a-form-item :label="$t('storage.text_52')" v-if="!isHiddenDir">
  14. <a-input v-decorator="decorators.dir" :placeholder="$t('storage.text_53')" />
  15. </a-form-item>
  16. </a-form>
  17. </div>
  18. <div slot="footer">
  19. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t("dialog.ok") }}</a-button>
  20. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  21. </div>
  22. </base-dialog>
  23. </template>
  24. <script>
  25. import DialogMixin from '@/mixins/dialog'
  26. import WindowsMixin from '@/mixins/windows'
  27. export default {
  28. name: 'AssociatedHostDialog',
  29. mixins: [DialogMixin, WindowsMixin],
  30. data () {
  31. return {
  32. loading: false,
  33. hostsLoading: false,
  34. hosts: [],
  35. form: {
  36. fc: this.$form.createForm(this),
  37. },
  38. formItemLayout: {
  39. labelCol: { span: 4 },
  40. wrapperCol: { span: 20 },
  41. },
  42. }
  43. },
  44. computed: {
  45. decorators () {
  46. return {
  47. host: [
  48. 'host',
  49. {
  50. rules: [
  51. { required: true, message: this.$t('storage.text_54') },
  52. ],
  53. },
  54. ],
  55. dir: [
  56. 'dir',
  57. {
  58. rules: [
  59. { required: true, message: this.$t('storage.text_53') },
  60. ],
  61. },
  62. ],
  63. }
  64. },
  65. isHiddenDir () {
  66. return ['rbd', 'slvm'].includes(this.params.data[0].storage_type)
  67. },
  68. },
  69. created () {
  70. this.queryHostList()
  71. },
  72. methods: {
  73. filterOption (input, option) {
  74. return (
  75. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  76. )
  77. },
  78. async queryHostList () {
  79. const params = { hypervisor: 'kvm', storage_not_attached: true, storage: this.params.data[0].id }
  80. const manager = new this.$Manager('hosts', 'v2')
  81. this.hostsLoading = true
  82. try {
  83. const { data = {} } = await manager.list({ params })
  84. this.hosts = data.data || []
  85. } catch (err) {
  86. throw err
  87. } finally {
  88. this.hostsLoading = false
  89. }
  90. },
  91. async handleConfirm () {
  92. this.loading = true
  93. try {
  94. const values = await this.form.fc.validateFields()
  95. const manager = new this.$Manager('hosts', 'v2')
  96. if (!this.isHiddenDir) {
  97. values.mount_point = values.dir
  98. }
  99. await manager.batchPerformAction({
  100. ids: values.host,
  101. action: '',
  102. data: values,
  103. ctx: [['storages', this.params.data[0].id]],
  104. })
  105. this.cancelDialog()
  106. this.params.refresh()
  107. this.loading = false
  108. } catch (error) {
  109. this.loading = false
  110. throw error
  111. }
  112. },
  113. },
  114. }
  115. </script>