Form.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <a-form
  3. :form="form.fc">
  4. <a-form-item :label="$t('cloudenv.text_95')" v-bind="formItemLayout" v-if="!updateValue">
  5. <a-input v-decorator="decorators.name" :placeholder="$t('validator.resourceName')" />
  6. </a-form-item>
  7. <a-form-item :label="$t('common.description')" v-bind="formItemLayout" v-if="!updateValue">
  8. <a-textarea :auto-size="{ minRows: 1, maxRows: 3 }" v-decorator="decorators.description" :placeholder="$t('common_367')" />
  9. </a-form-item>
  10. <a-form-item required :label="$t('cloudenv.text_413')" v-bind="formItemLayout">
  11. <strategy-radio
  12. :isNone="false"
  13. :decorator="decorators.strategy" />
  14. </a-form-item>
  15. <a-form-item :label="$t('cloudenv.text_18')" v-bind="formItemLayout">
  16. <base-select
  17. v-decorator="decorators.schedtag"
  18. resource="schedtags"
  19. :params="schedtagParams"
  20. :select-props="{ placeholder: $t('cloudenv.text_378') }"
  21. :autoLoadDefaultSelect="false" />
  22. </a-form-item>
  23. <condition-select @conditionValsChange="conditionValsChange" :decorators="decorators" :formItemLayout="formItemLayout" />
  24. </a-form>
  25. </template>
  26. <script>
  27. import StrategyRadio from '@Cloudenv/sections/StrategyRadio'
  28. import ConditionSelect from '@Cloudenv/sections/ConditionSelect'
  29. export default {
  30. name: 'SchedpolicyForm',
  31. components: {
  32. StrategyRadio,
  33. ConditionSelect,
  34. },
  35. props: {
  36. updateValue: Object,
  37. },
  38. data () {
  39. const getInitValue = () => {
  40. let strategy = 'exclude'
  41. let schedtag
  42. let conditionVals
  43. if (this.updateValue) {
  44. strategy = this.updateValue.strategy
  45. schedtag = this.updateValue.schedtag
  46. if (this.updateValue.condition.includes('in')) {
  47. const key = this.updateValue.condition.substring(this.updateValue.condition.indexOf('(') + 1, this.updateValue.condition.length - 1)
  48. /* eslint-disable no-useless-escape */
  49. conditionVals = key.replace(/\"/g, '').split(',')
  50. }
  51. }
  52. return {
  53. strategy,
  54. schedtag,
  55. conditionVals,
  56. }
  57. }
  58. const initValue = getInitValue()
  59. return {
  60. scope: this.$store.getters.scope,
  61. form: {
  62. fc: this.$form.createForm(this),
  63. fd: {},
  64. },
  65. decorators: {
  66. name: [
  67. 'name',
  68. {
  69. validateFirst: true,
  70. rules: [
  71. { required: true, message: this.$t('common.text00042') },
  72. { validator: this.$validate('resourceName') },
  73. ],
  74. },
  75. ],
  76. description: ['description'],
  77. strategy: [
  78. 'strategy',
  79. {
  80. initialValue: initValue.strategy,
  81. rules: [
  82. { required: true, message: this.$t('common_620') },
  83. ],
  84. },
  85. ],
  86. schedtag: [
  87. 'schedtag',
  88. {
  89. initialValue: initValue.schedtag,
  90. },
  91. ],
  92. conditionKey: [
  93. 'conditionKey',
  94. {
  95. initialValue: 'projects',
  96. },
  97. ],
  98. conditionVals: [
  99. 'conditionVals',
  100. {
  101. initialValue: initValue.conditionVals,
  102. rules: [
  103. { required: true, message: this.$t('common_621') },
  104. ],
  105. },
  106. ],
  107. },
  108. formItemLayout: {
  109. wrapperCol: {
  110. span: 20,
  111. },
  112. labelCol: {
  113. span: 4,
  114. },
  115. },
  116. }
  117. },
  118. computed: {
  119. schedtagParams () {
  120. return {
  121. limit: 0,
  122. scope: this.scope,
  123. filter: 'resource_type.equals(hosts)',
  124. }
  125. },
  126. },
  127. methods: {
  128. conditionValsChange (val) {
  129. if (val) {
  130. const conditions = []
  131. val.forEach((item) => {
  132. /* eslint-disable no-useless-escape */
  133. conditions.push(`\"${item}\"`)
  134. })
  135. this.form.fd.condition = `server.owner_tenant_id.in(${conditions.join(',')})`
  136. }
  137. },
  138. validateForm () {
  139. return new Promise((resolve, reject) => {
  140. this.form.fc.validateFields((err, values) => {
  141. if (!err) {
  142. resolve({ ...values, condition: this.form.fd.condition })
  143. } else {
  144. reject(err)
  145. }
  146. })
  147. })
  148. },
  149. },
  150. }
  151. </script>