CustomData.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <a-form-item :label="$t('compute.custom_data')" :extra="form.fd.custom_data_type ? customDataExtra : ''">
  3. <a-radio-group v-decorator="decorators.custom_data_type" class="mb-2" @change="handleTypeChange">
  4. <a-radio-button value="">{{ $t('compute.text_116') }}</a-radio-button>
  5. <a-radio-button value="input">{{ $t('compute.custom_data_input') }}</a-radio-button>
  6. <a-radio-button value="file">{{ $t('compute.custom_data_file') }}</a-radio-button>
  7. </a-radio-group>
  8. <div style="max-width: 800px" class="mt-2">
  9. <code-mirror v-if="form.fd.custom_data_type === 'input'" v-model="codeMirrorData" :options="cmOptions" @input="handleCodeInput" />
  10. </div>
  11. <div style="max-width: 800px" class="mt-2 mb-2">
  12. <a-upload-dragger
  13. v-if="form.fd.custom_data_type === 'file'"
  14. list-type="text"
  15. :beforeUpload="beforeUpload"
  16. :remove="handleRemove"
  17. :fileList="fileList"
  18. :accept="accept">
  19. <div class="pt-3 pb-3">
  20. <p class="ant-upload-drag-icon"><a-icon type="inbox" /></p>
  21. <p class="ant-upload-text">{{$t('system.text_505')}}</p>
  22. <p class="ant-upload-hint">{{$t('compute.custom_data_file_limit')}}</p>
  23. </div>
  24. </a-upload-dragger>
  25. </div>
  26. <div v-if="errorTip" class="err-tip">
  27. {{errorTip}}
  28. </div>
  29. </a-form-item>
  30. </template>
  31. <script>
  32. import 'codemirror/theme/material.css'
  33. import 'codemirror/addon/edit/matchbrackets'
  34. import { HYPERVISORS_MAP } from '@/constants'
  35. // import yaml from 'js-yaml'
  36. // import * as R from 'ramda'
  37. export default {
  38. props: {
  39. decorators: Object,
  40. form: Object,
  41. },
  42. data () {
  43. return {
  44. cmOptions: {
  45. tabSize: 2,
  46. styleActiveLine: true,
  47. lineNumbers: true,
  48. line: true,
  49. theme: 'material',
  50. mode: 'application/json',
  51. lint: true,
  52. matchBrackets: true,
  53. },
  54. fileList: [],
  55. errorTip: '',
  56. codeMirrorData: '',
  57. customData: [],
  58. accept: '.sh,.json,.yaml',
  59. }
  60. },
  61. computed: {
  62. isKvm () {
  63. return this.form.fd.hypervisor === 'kvm'
  64. },
  65. isEsxi () {
  66. return this.form.fd.hypervisor === HYPERVISORS_MAP.esxi.key
  67. },
  68. customDataExtra () {
  69. return this.isEsxi ? this.$t('compute.custom_data.esxi_extra') : this.$t('compute.custom_data.extra')
  70. },
  71. },
  72. methods: {
  73. handleRemove (file) {
  74. const index = this.fileList.indexOf(file)
  75. const newFileList = this.fileList.slice()
  76. newFileList.splice(index, 1)
  77. this.fileList = newFileList
  78. },
  79. beforeUpload (file) {
  80. if (file.size > 32768) {
  81. this.$message.error(this.$t('compute.custom_data_file_limit'))
  82. return false
  83. }
  84. var reader = new FileReader()// 这里是核心!!!读取操作就是由它完成的。
  85. reader.readAsText(file)// 读取文件的内容
  86. reader.onload = (info) => {
  87. const result = info.target.result
  88. this.fileList = [file]
  89. this.customData = result
  90. }
  91. return false
  92. },
  93. handleTypeChange () {
  94. this.errorTip = ''
  95. this.fileList = []
  96. this.codeMirrorData = ''
  97. this.customData = []
  98. },
  99. handleCodeInput (_value) {
  100. this.customData = _value
  101. },
  102. handleMirrorDataChange (_value) {
  103. this.codeMirrorData = _value
  104. },
  105. },
  106. }
  107. </script>
  108. <style lang="less" scoped>
  109. .err-tip {
  110. color: red;
  111. }
  112. </style>