ConfigTemplate.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <script setup lang="ts">
  2. import {useGettext} from 'vue3-gettext'
  3. import template from '@/api/template'
  4. import {computed, provide, ref, watch} from 'vue'
  5. import {storeToRefs} from 'pinia'
  6. import {useSettingsStore} from '@/pinia'
  7. import Template from '@/views/template/Template.vue'
  8. import DirectiveEditor from '@/views/domain/ngx_conf/directive/DirectiveEditor.vue'
  9. import LocationEditor from '@/views/domain/ngx_conf/LocationEditor.vue'
  10. import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
  11. import TemplateForm from '@/views/domain/ngx_conf/config_template/TemplateForm.vue'
  12. import * as wasi from 'wasi'
  13. import _ from 'lodash'
  14. const {$gettext} = useGettext()
  15. const {language} = storeToRefs(useSettingsStore())
  16. const props = defineProps(['ngx_config', 'current_server_index'])
  17. const blocks = ref([])
  18. const data: any = ref({})
  19. const visible = ref(false)
  20. const name = ref('')
  21. function get_block_list() {
  22. template.get_block_list().then(r => {
  23. blocks.value = r.data
  24. })
  25. }
  26. get_block_list()
  27. function view(n: string) {
  28. visible.value = true
  29. name.value = n
  30. template.get_block(n).then(r => {
  31. data.value = r
  32. })
  33. }
  34. const trans_description = computed(() => {
  35. return (item: any) => item.description?.[language.value] ?? item.description?.en ?? ''
  36. })
  37. async function add() {
  38. if (data.value.custom) {
  39. props.ngx_config.custom += '\n' + data.value.custom
  40. }
  41. props.ngx_config.custom = props.ngx_config.custom.trim()
  42. if (data.value.locations) {
  43. props.ngx_config.servers[props.current_server_index].locations.push(...data.value.locations)
  44. }
  45. if (data.value.directives) {
  46. props.ngx_config.servers[props.current_server_index].directives.push(...data.value.directives)
  47. }
  48. visible.value = false
  49. }
  50. const variables = computed(() => {
  51. return data.value.variables
  52. })
  53. function build_template() {
  54. template.build_block(name.value, variables.value).then(r => {
  55. data.value.directives = r.directives
  56. data.value.locations = r.locations
  57. data.value.custom = r.custom
  58. })
  59. }
  60. provide('build_template', build_template)
  61. </script>
  62. <template>
  63. <div>
  64. <h2 v-translate>Config Templates</h2>
  65. <div class="config-list-wrapper">
  66. <a-list
  67. :grid="{ gutter: 16, xs: 1, sm: 2, md: 2, lg: 2, xl: 2, xxl: 2, xxxl: 2 }"
  68. :data-source="blocks"
  69. >
  70. <template #renderItem="{ item }">
  71. <a-list-item>
  72. <a-card size="small" :title="item.name">
  73. <template #extra>
  74. <a-button type="link"
  75. size="small" @click="view(item.filename)">{{ $gettext('View') }}
  76. </a-button>
  77. </template>
  78. <p>{{ $gettext('Author') }}: {{ item.author }}</p>
  79. <p>{{ $gettext('Description') }}: {{ trans_description(item) }}</p>
  80. </a-card>
  81. </a-list-item>
  82. </template>
  83. </a-list>
  84. </div>
  85. <a-modal
  86. :title="data.name"
  87. v-model:visible="visible"
  88. :mask="false"
  89. :ok-text="$gettext('Add')"
  90. @ok="add"
  91. >
  92. <p>{{ $gettext('Author') }}: {{ data.author }}</p>
  93. <p>{{ $gettext('Description') }}: {{ trans_description(data) }}</p>
  94. <template-form :data="data.variables"/>
  95. <template v-if="data.custom">
  96. <h2>{{ $gettext('Custom') }}</h2>
  97. <code-editor v-model:content="data.custom" default-height="150px"/>
  98. </template>
  99. <directive-editor v-if="data.directives" :ngx_directives="data.directives" :readonly="true"/>
  100. <br/>
  101. <location-editor v-if="data.locations" :locations="data.locations" :readonly="true"/>
  102. </a-modal>
  103. </div>
  104. </template>
  105. <style lang="less" scoped>
  106. .config-list-wrapper {
  107. max-height: 200px;
  108. overflow-y: scroll;
  109. overflow-x: hidden;
  110. }
  111. </style>