ConfigTemplate.vue 3.9 KB

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