index.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <template>
  2. <div class="server-create-index">
  3. <page-header :title="headerTitle" :tabs="cloudEnvOptions" :current-tab.sync="cloudEnv" />
  4. <component :is="component" type="idc" />
  5. </div>
  6. </template>
  7. <script>
  8. import { getCloudEnvOptions } from '@/utils/common/hypervisor'
  9. import IDC from './form/IDC'
  10. export default {
  11. name: 'ServerCreate',
  12. components: {
  13. IDC,
  14. },
  15. data () {
  16. const cloudEnvOptions = getCloudEnvOptions('compute_engine_brands', true)
  17. const queryType = this.$route.query.type
  18. let cloudEnv = queryType === 'idc' ? 'onpremise' : this.$route.query.type
  19. let routerQuery = this.$route.query.type
  20. if (!cloudEnvOptions.find(val => val.key === cloudEnv)) {
  21. cloudEnv = cloudEnvOptions[0].key
  22. routerQuery = cloudEnv === 'onpremise' ? 'idc' : cloudEnv
  23. }
  24. return {
  25. cloudEnvOptions: { key: 'onpremise', label: this.$t('dictionary.onpremise_env') } || cloudEnvOptions,
  26. cloudEnv,
  27. routerQuery,
  28. }
  29. },
  30. computed: {
  31. type () {
  32. const { type = 'idc' } = this.$route.query
  33. switch (type) {
  34. case 'private':
  35. return 'private'
  36. case 'public':
  37. return 'public'
  38. default:
  39. return 'idc'
  40. }
  41. },
  42. component () {
  43. const { type = 'idc' } = this.$route.query
  44. switch (type) {
  45. case 'private':
  46. return 'Private'
  47. case 'public':
  48. return 'Public'
  49. default:
  50. return 'IDC'
  51. }
  52. },
  53. headerTitle () {
  54. return this.$t('compute.text_1161', [this.$t('compute.vminstance-container')])
  55. },
  56. },
  57. watch: {
  58. cloudEnv (val) {
  59. this.$nextTick(() => {
  60. const query = this.getQuery(this.$router.history.current.query)
  61. const path = this.$router.history.current.path
  62. const newQuery = JSON.parse(JSON.stringify(query))
  63. newQuery.type = val === 'onpremise' ? 'idc' : val
  64. this.$router.push({ path, query: newQuery })
  65. })
  66. },
  67. },
  68. created () {
  69. if (this.routerQuery !== this.$route.query.type) {
  70. this.$router.push({
  71. path: this.$router.history.current.path,
  72. query: {
  73. type: this.routerQuery,
  74. },
  75. })
  76. }
  77. },
  78. mounted () {
  79. this.detectBack() // 在用户点击 back 的时候,表单的处理方案
  80. },
  81. beforeDestroy () {
  82. window.removeEventListener('popstate', this.popstate)
  83. },
  84. methods: {
  85. detectBack () {
  86. window.addEventListener('popstate', this.popstate, false)
  87. },
  88. popstate (e) {
  89. if (e.state) {
  90. // 侦测是用户触发的后退操作, dosomething
  91. // 这里刷新当前 url
  92. window.location.reload()
  93. }
  94. },
  95. getQuery (query) {
  96. if (query.sence === 'image') {
  97. return { type: query.type }
  98. }
  99. return query
  100. },
  101. },
  102. }
  103. </script>