DetachDialog.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="params.name || $t('dictionary.server')" :count="params.data.length" :action="params.title" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form
  8. :form="form.fc">
  9. <a-form-item :label="$t('compute.text_494')" v-if="isShowAutoStart" v-bind="formItemLayout" :extra="$t('compute.text_495')">
  10. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.autoStart" />
  11. </a-form-item>
  12. </a-form>
  13. </div>
  14. <div slot="footer">
  15. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  16. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  17. </div>
  18. </base-dialog>
  19. </template>
  20. <script>
  21. import DialogMixin from '@/mixins/dialog'
  22. import WindowsMixin from '@/mixins/windows'
  23. export default {
  24. name: 'DetachGpuDialog',
  25. mixins: [DialogMixin, WindowsMixin],
  26. data () {
  27. return {
  28. loading: false,
  29. form: {
  30. fc: this.$form.createForm(this),
  31. },
  32. decorators: {
  33. autoStart: [
  34. 'autoStart',
  35. {
  36. valuePropName: 'checked',
  37. initialValue: true,
  38. },
  39. ],
  40. },
  41. formItemLayout: {
  42. wrapperCol: {
  43. span: 21,
  44. },
  45. labelCol: {
  46. span: 3,
  47. },
  48. },
  49. }
  50. },
  51. computed: {
  52. selectedItems () {
  53. return this.params.data
  54. },
  55. isGuestAllRunning () {
  56. return this.selectedItems.every(o => o.guest_status === 'running')
  57. },
  58. isShowAutoStart () {
  59. return !this.isGuestAllRunning
  60. },
  61. },
  62. methods: {
  63. validateForm () {
  64. return new Promise((resolve, reject) => {
  65. this.form.fc.validateFields((err, values) => {
  66. if (!err) {
  67. resolve(values)
  68. } else {
  69. reject(err)
  70. }
  71. })
  72. })
  73. },
  74. doUpdate (values, device) {
  75. return new this.$Manager('servers').performAction({
  76. id: device ? device.guest_id : this.params.data[0].id,
  77. action: 'detach-isolated-device',
  78. data: {
  79. auto_start: this.isShowAutoStart ? values.autoStart : false,
  80. device: device ? device.id : this.params.device.id,
  81. },
  82. })
  83. },
  84. async handleConfirm () {
  85. try {
  86. this.loading = true
  87. const values = await this.validateForm()
  88. if (this.params.devices) {
  89. for (let i = 0; i < this.params.devices.length; i++) {
  90. await this.doUpdate(values, this.params.devices[i])
  91. }
  92. } else {
  93. if (this.params.data.length === 1) {
  94. await this.doUpdate(values)
  95. } else {
  96. await this.doDetachSubmit(values)
  97. }
  98. }
  99. this.loading = false
  100. this.params.refresh()
  101. this.cancelDialog()
  102. } catch (error) {
  103. this.loading = false
  104. }
  105. },
  106. doDetachSubmit (values) {
  107. const data = {
  108. detach_all: true,
  109. auto_start: this.isShowAutoStart ? values.autoStart : false,
  110. }
  111. const selectedIds = this.params.data.map((item) => {
  112. return item.guest_id
  113. })
  114. return new this.$Manager('servers').batchPerformAction({
  115. ids: selectedIds,
  116. action: 'detach-isolated-device',
  117. data,
  118. })
  119. },
  120. },
  121. }
  122. </script>