index.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <div>
  3. <a-tooltip v-if="isPciEmpty" :title="$t('compute.text_146')">
  4. <span><a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" :value="pciEnable" :disabled="true" /></span>
  5. </a-tooltip>
  6. <a-form-item class="mb-2" v-else>
  7. <a-switch :checkedChildren="$t('compute.text_115')" :unCheckedChildren="$t('compute.text_116')" v-decorator="decorators.pciEnable" @change="change" />
  8. </a-form-item>
  9. <template v-if="pciEnable">
  10. <a-form-item v-for="(k, index) in pciForm.fc.getFieldValue('keys')" :key="k">
  11. <a-row :gutter="[8,0]">
  12. <a-col :span="4">
  13. <a-form-item>
  14. <base-select v-decorator="decorators.pciDevType(k)" :options="getRealPciDevTypeOptions(realPciDevTypeOptions, index)" @change="(val) => onChangeDevType(val, k)" />
  15. </a-form-item>
  16. </a-col>
  17. <a-col :span="9">
  18. <a-form-item>
  19. <base-select v-decorator="decorators.pciModel(k)" :options="getRealPciOptions(realPciOptions, index)" @change="(val) => onChangeModel(val, k)"
  20. :selectProps="{ placeholder: $t('compute.text_147') }" />
  21. </a-form-item>
  22. </a-col>
  23. </a-row>
  24. <a-row :gutter="[8,0]">
  25. <a-col :span="11">
  26. <a-form-item class="mb-0">
  27. <a-radio-group v-decorator="decorators.pciCount(k)" @change="(e) => onChangeCount(e, k)">
  28. <a-radio-button
  29. v-for="item in pciCountOptions"
  30. :value="item.key"
  31. :disabled="item.disabled"
  32. :key="item.key">{{ item.label }}</a-radio-button>
  33. </a-radio-group>
  34. </a-form-item>
  35. </a-col>
  36. <a-col :span="4">
  37. <a-button v-if="index > 0" shape="circle" icon="minus" size="small" @click="remove(k)" />
  38. </a-col>
  39. </a-row>
  40. </a-form-item>
  41. <div v-if="!isNvidiaVgpu" class="d-flex align-items-center mb-2">
  42. <a-button type="primary" shape="circle" icon="plus" size="small" @click="add" />
  43. <a-button type="link" @click="add">{{ $t('compute.pci.add_transparent_device') }}</a-button>
  44. </div>
  45. </template>
  46. </div>
  47. </template>
  48. <script>
  49. import { GPU_COUNT_OPTIONS, GPU_DEV_TYPE_OPTION_MAP } from '@Compute/constants'
  50. let id = 0
  51. export default {
  52. name: 'Pci',
  53. props: {
  54. form: {
  55. type: Object,
  56. require: true,
  57. },
  58. decorators: {
  59. type: Object,
  60. validator: val => val.pciEnable && val.pciDevType && val.pciModel && val.pciCount,
  61. },
  62. pciDevTypeOptions: {
  63. type: Array,
  64. default: () => [],
  65. },
  66. pciOptions: {
  67. type: Array,
  68. default: () => [],
  69. },
  70. },
  71. data () {
  72. return {
  73. pciEnable: false,
  74. curPciDevType: '',
  75. }
  76. },
  77. computed: {
  78. isPciEmpty () {
  79. return (this.pciOptions && this.pciOptions.length === 0) || !this.realPciDevTypeOptions.length
  80. },
  81. pciCountOptions () {
  82. const target = this.realPciOptions.filter(item => item.dev_type === this.curPciDevType)
  83. if (target.length) {
  84. return GPU_COUNT_OPTIONS.map(item => {
  85. return {
  86. ...item,
  87. disabled: item.key > target[0].count,
  88. }
  89. })
  90. }
  91. return GPU_COUNT_OPTIONS
  92. },
  93. realPciDevTypeOptions () {
  94. const pciDevTypeOptions = this.pciDevTypeOptions.map(item => {
  95. const vgpuVal = GPU_DEV_TYPE_OPTION_MAP.VGPU.value
  96. const dev_type = item.dev_type.endsWith(`-${vgpuVal}`) ? vgpuVal : item.dev_type
  97. return {
  98. key: dev_type,
  99. label: GPU_DEV_TYPE_OPTION_MAP[dev_type]?.label || dev_type,
  100. }
  101. })
  102. return pciDevTypeOptions
  103. },
  104. realPciOptions () {
  105. return this.pciOptions.filter(item => item.dev_type.endsWith(this.curPciDevType))
  106. },
  107. isNvidiaVgpu () {
  108. const curPciModel = this.form.fd['pciModel[0]']
  109. const curPci = this.pciOptions.find(o => o.key === curPciModel)
  110. return curPci?.dev_type === 'LEGACY-VGPU'
  111. },
  112. },
  113. watch: {
  114. pciEnable (val) {
  115. if (val) {
  116. this.init()
  117. } else {
  118. const keys = this.pciForm.fc.getFieldValue('keys')
  119. keys.forEach(key => {
  120. this.$delete(this.form.fd, `pciDevType[${key}]`)
  121. this.$delete(this.form.fd, `pciModel[${key}]`)
  122. this.$delete(this.form.fd, `pciCount[${key}]`)
  123. })
  124. }
  125. },
  126. realPciDevTypeOptions (val, oldVal) {
  127. this.changeSelectedValue()
  128. },
  129. isPciEmpty (val) {
  130. if (val) this.pciEnable = false
  131. },
  132. },
  133. beforeCreate () {
  134. this.pciForm = {}
  135. this.pciForm.fc = this.$form.createForm(this, { name: 'gpu_form' })
  136. this.pciForm.fc.getFieldDecorator('keys', { initialValue: [id], preserve: true })
  137. },
  138. methods: {
  139. init () {
  140. this.initialValue()
  141. },
  142. initialValue (id = 0) {
  143. this.$nextTick(() => {
  144. this.form.fc.setFieldsValue({
  145. [`pciDevType[${id}]`]: this.curPciDevType || (this.getRealPciDevTypeOptions(this.realPciDevTypeOptions).length ? this.getRealPciDevTypeOptions(this.realPciDevTypeOptions)[0].key : ''),
  146. [`pciModel[${id}]`]: this.form.fd['pciModel[0]'] || (this.getRealPciOptions(this.realPciOptions).length ? this.getRealPciOptions(this.realPciOptions)[0].key : ''),
  147. [`pciCount[${id}]`]: 1,
  148. })
  149. })
  150. },
  151. changeSelectedValue () {
  152. const keys = this.pciForm.fc.getFieldValue('keys')
  153. keys.forEach(key => {
  154. if (this.form.fd[`pciDevType[${key}]`] && !this.getRealPciDevTypeOptions(this.realPciDevTypeOptions).some(item => item.key === this.form.fd[`pciDevType[${key}]`])) {
  155. if (this.getRealPciDevTypeOptions(this.realPciDevTypeOptions).length) {
  156. this.form.fc.setFieldsValue({
  157. [`pciDevType[${key}]`]: this.getRealPciDevTypeOptions(this.realPciDevTypeOptions)[0].key,
  158. })
  159. this.curPciDevType = this.getRealPciDevTypeOptions(this.realPciDevTypeOptions)[0].key
  160. } else {
  161. this.form.fc.setFieldsValue({
  162. [`pciDevType[${key}]`]: undefined,
  163. })
  164. this.curPciDevType = undefined
  165. }
  166. }
  167. this.$nextTick(() => {
  168. if (this.form.fd[`pciModel[${key}]`] && !this.getRealPciOptions(this.realPciOptions, key).some(item => item.key === this.form.fd[`pciModel[${key}]`])) {
  169. if (this.getRealPciOptions(this.realPciOptions, key).length && this.form.fd[`pciDevType[${key}]`]) {
  170. this.form.fc.setFieldsValue({
  171. [`pciModel[${key}]`]: this.getRealPciOptions(this.realPciOptions, key)[0].key,
  172. })
  173. this.form.fd[`pciModel[${key}]`] = this.getRealPciOptions(this.realPciOptions, key)[0].key
  174. } else {
  175. this.form.fc.setFieldsValue({
  176. [`pciModel[${key}]`]: undefined,
  177. })
  178. this.form.fd[`pciModel[${key}]`] = undefined
  179. }
  180. }
  181. })
  182. })
  183. },
  184. removeValue (id) {
  185. this.$delete(this.form.fd, `pciDevType[${id}]`)
  186. this.$delete(this.form.fd, `pciModel[${id}]`)
  187. this.$delete(this.form.fd, `pciCount[${id}]`)
  188. },
  189. change (val) {
  190. this.pciEnable = val
  191. this.$emit('change', val)
  192. },
  193. onChangeDevType (val, key) {
  194. this.curPciDevType = val
  195. this.form.fc.setFieldsValue({
  196. [`pciDevType[${key}]`]: val,
  197. [`pciModel[${key}]`]: this.realPciOptions[0].key,
  198. [`pciCount[${key}]`]: 1,
  199. })
  200. this.reset()
  201. },
  202. onChangeModel (val, key) {
  203. this.form.fc.setFieldsValue({
  204. [`pciModel[${key}]`]: val,
  205. })
  206. const curPci = this.pciOptions.find(o => o.key === val)
  207. if (curPci.dev_type === 'LEGACY-VGPU') {
  208. this.reset()
  209. }
  210. },
  211. onChangeCount (e, key) {
  212. this.form.fc.setFieldsValue({
  213. [`pciCount[${key}]`]: e.target.value,
  214. })
  215. },
  216. add () {
  217. const { pciForm } = this
  218. const keys = pciForm.fc.getFieldValue('keys')
  219. const nextKey = ++id
  220. const nextKeys = keys.concat(nextKey)
  221. pciForm.fc.setFieldsValue({ keys: nextKeys })
  222. this.initialValue(nextKey)
  223. },
  224. remove (k) {
  225. const { pciForm } = this
  226. const keys = pciForm.fc.getFieldValue('keys')
  227. if (keys.length === 1) {
  228. return
  229. }
  230. pciForm.fc.setFieldsValue({
  231. keys: keys.filter(key => key !== k),
  232. })
  233. this.removeValue(k)
  234. },
  235. reset () {
  236. const { pciForm } = this
  237. const keys = pciForm.fc.getFieldValue('keys')
  238. pciForm.fc.setFieldsValue({
  239. keys: keys.filter(key => key === 0),
  240. })
  241. keys.forEach(k => {
  242. if (k !== 0) {
  243. this.removeValue(k)
  244. }
  245. })
  246. },
  247. getRealPciDevTypeOptions (realPciDevTypeOptions, index = 0) {
  248. let options = realPciDevTypeOptions
  249. if (index !== 0) {
  250. options = realPciDevTypeOptions.filter(o => {
  251. return this.form.fd['pciDevType[0]'] === o.key
  252. })
  253. }
  254. if (!this.curPciDevType) {
  255. this.curPciDevType = options[0]?.key
  256. }
  257. return options
  258. },
  259. getRealPciOptions (realPciOptions, index = 0) {
  260. let options = realPciOptions
  261. if (index !== 0) {
  262. options = realPciOptions.filter(o => {
  263. return this.form.fd['pciModel[0]'] === o.key
  264. })
  265. }
  266. return options
  267. },
  268. },
  269. }
  270. </script>