create.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{$t('monitor.dashboard.dialog.create')}}</div>
  4. <div slot="body">
  5. <a-form :form="form" v-bind="formLayout">
  6. <a-form-item :label="$t('IAM.text_1')">
  7. <scope-select v-decorator="decorators.scope" />
  8. </a-form-item>
  9. <a-form-item :label="$t('compute.text_228')">
  10. <a-input v-decorator="decorators.name" :placeholder="$t('common.placeholder')" />
  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. import ScopeSelect from '@Monitor/components/MonitorCard/sections/select/scope'
  24. export default {
  25. name: 'CreateMonitorDashboard',
  26. components: {
  27. ScopeSelect,
  28. },
  29. mixins: [DialogMixin, WindowsMixin],
  30. data () {
  31. return {
  32. loading: false,
  33. scope: this.$store.getters.scope,
  34. form: this.$form.createForm(this),
  35. decorators: {
  36. scope: [
  37. 'scope',
  38. {
  39. initialValue: { id: '', scope: this.$store.getters.scope },
  40. rules: [
  41. { validator: this.validateScope },
  42. ],
  43. },
  44. ],
  45. name: [
  46. 'name',
  47. {
  48. rules: [
  49. { required: true, message: `${this.$t('common.placeholder')}${this.$t('common.name')}` },
  50. ],
  51. },
  52. ],
  53. },
  54. formLayout: {
  55. wrapperCol: {
  56. md: { span: 20 },
  57. xl: { span: 19 },
  58. xxl: { span: 21 },
  59. },
  60. labelCol: {
  61. md: { span: 4 },
  62. xl: { span: 5 },
  63. xxl: { span: 3 },
  64. },
  65. },
  66. }
  67. },
  68. methods: {
  69. validateScope (rule, value, callback) {
  70. if (this.scope === value.scope) {
  71. callback()
  72. } else {
  73. if (value.id) {
  74. callback()
  75. } else {
  76. const msg = this.$t('common.placeholder')
  77. callback(msg)
  78. }
  79. }
  80. },
  81. validateForm () {
  82. return new Promise((resolve, reject) => {
  83. this.form.validateFields((err, values) => {
  84. if (!err) {
  85. resolve(values)
  86. } else {
  87. reject(err)
  88. }
  89. })
  90. })
  91. },
  92. async handleConfirm () {
  93. this.loading = true
  94. try {
  95. const values = await this.validateForm()
  96. const data = {
  97. scope: values.scope.scope,
  98. name: values.name,
  99. }
  100. if (this.scope !== values.scope.scope && values.scope.id && values.scope.id.length > 0) {
  101. data[`${values.scope.scope}_id`] = values.scope.id
  102. }
  103. await new this.$Manager('alertdashboards', 'v1').create({ data })
  104. this.loading = false
  105. if (this.params.refresh && typeof this.params.refresh === 'function') {
  106. this.params.refresh()
  107. }
  108. this.cancelDialog()
  109. } catch (error) {
  110. this.loading = false
  111. throw error
  112. }
  113. },
  114. },
  115. }
  116. </script>
  117. <style scoped>
  118. </style>