use-columns.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import { h } from 'vue'
  18. import { useI18n } from 'vue-i18n'
  19. import {
  20. NPopover,
  21. NButton,
  22. NIcon,
  23. NPopconfirm,
  24. NSpace,
  25. NTooltip
  26. } from 'naive-ui'
  27. import { EditOutlined, DeleteOutlined } from '@vicons/antd'
  28. import JsonHighlight from './json-highlight'
  29. import ButtonLink from '@/components/button-link'
  30. import {
  31. COLUMN_WIDTH_CONFIG,
  32. calculateTableWidth,
  33. DefaultTableWidth
  34. } from '@/common/column-width-config'
  35. import type { TableColumns } from './types'
  36. export function useColumns(onCallback: Function) {
  37. const { t } = useI18n()
  38. const getColumns = (): { columns: TableColumns; tableWidth: number } => {
  39. const columns = [
  40. {
  41. title: '#',
  42. key: 'index',
  43. render: (unused, rowIndex) => rowIndex + 1,
  44. ...COLUMN_WIDTH_CONFIG['index']
  45. },
  46. {
  47. title: t('datasource.datasource_name'),
  48. key: 'name',
  49. ...COLUMN_WIDTH_CONFIG['name']
  50. },
  51. {
  52. title: t('datasource.datasource_user_name'),
  53. key: 'userName',
  54. ...COLUMN_WIDTH_CONFIG['userName']
  55. },
  56. {
  57. title: t('datasource.datasource_type'),
  58. key: 'type',
  59. width: 180
  60. },
  61. {
  62. title: t('datasource.datasource_parameter'),
  63. key: 'parameter',
  64. width: 180,
  65. render: (rowData) => {
  66. return h(
  67. NPopover,
  68. { trigger: 'click' },
  69. {
  70. trigger: () =>
  71. h(ButtonLink, null, {
  72. default: () => t('datasource.click_to_view')
  73. }),
  74. default: () => h(JsonHighlight, { rowData })
  75. }
  76. )
  77. }
  78. },
  79. {
  80. title: t('datasource.description'),
  81. key: 'note',
  82. ...COLUMN_WIDTH_CONFIG['note'],
  83. render: (rowData) => rowData.note || '-'
  84. },
  85. {
  86. title: t('datasource.create_time'),
  87. key: 'createTime',
  88. ...COLUMN_WIDTH_CONFIG['time']
  89. },
  90. {
  91. title: t('datasource.update_time'),
  92. key: 'updateTime',
  93. ...COLUMN_WIDTH_CONFIG['time']
  94. },
  95. {
  96. title: t('datasource.operation'),
  97. key: 'operation',
  98. ...COLUMN_WIDTH_CONFIG['operation'](2),
  99. render: (rowData) => {
  100. return h(NSpace, null, {
  101. default: () => [
  102. h(NTooltip, null, {
  103. trigger: () =>
  104. h(
  105. NButton,
  106. {
  107. circle: true,
  108. type: 'info',
  109. size: 'small',
  110. onClick: () => void onCallback(rowData.id, 'edit')
  111. },
  112. {
  113. default: () =>
  114. h(NIcon, null, { default: () => h(EditOutlined) })
  115. }
  116. ),
  117. default: () => t('datasource.edit')
  118. }),
  119. h(NTooltip, null, {
  120. trigger: () =>
  121. h(
  122. NPopconfirm,
  123. {
  124. onPositiveClick: () =>
  125. void onCallback(rowData.id, 'delete'),
  126. negativeText: t('datasource.cancel'),
  127. positiveText: t('datasource.confirm')
  128. },
  129. {
  130. trigger: () =>
  131. h(
  132. NButton,
  133. {
  134. circle: true,
  135. type: 'error',
  136. size: 'small',
  137. class: 'btn-delete'
  138. },
  139. {
  140. default: () =>
  141. h(NIcon, null, {
  142. default: () => h(DeleteOutlined)
  143. })
  144. }
  145. ),
  146. default: () => t('datasource.delete_confirm')
  147. }
  148. ),
  149. default: () => t('datasource.delete')
  150. })
  151. ]
  152. })
  153. }
  154. }
  155. ] as TableColumns
  156. return {
  157. columns,
  158. tableWidth: calculateTableWidth(columns) || DefaultTableWidth
  159. }
  160. }
  161. return {
  162. getColumns
  163. }
  164. }