use-table.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 _ from 'lodash'
  18. import { h, ref, reactive } from 'vue'
  19. import { useI18n } from 'vue-i18n'
  20. import { useRouter } from 'vue-router'
  21. import type { Router } from 'vue-router'
  22. import type { TableColumns, RowKey } from 'naive-ui/es/data-table/src/interface'
  23. import { useAsyncState } from '@vueuse/core'
  24. import {
  25. batchCopyByCodes,
  26. batchDeleteByCodes,
  27. batchExportByCodes,
  28. deleteByCode,
  29. queryListPaging,
  30. release
  31. } from '@/service/modules/process-definition'
  32. import TableAction from './components/table-action'
  33. import styles from './index.module.scss'
  34. import { NTag } from 'naive-ui'
  35. import ButtonLink from '@/components/button-link'
  36. import {
  37. COLUMN_WIDTH_CONFIG,
  38. calculateTableWidth,
  39. DefaultTableWidth
  40. } from '@/utils/column-width-config'
  41. import type { IDefinitionParam } from './types'
  42. export function useTable() {
  43. const { t } = useI18n()
  44. const router: Router = useRouter()
  45. const variables = reactive({
  46. columns: [],
  47. tableWidth: DefaultTableWidth,
  48. checkedRowKeys: [] as Array<RowKey>,
  49. row: {},
  50. tableData: [],
  51. projectCode: ref(Number(router.currentRoute.value.params.projectCode)),
  52. page: ref(1),
  53. pageSize: ref(10),
  54. searchVal: ref(),
  55. totalPage: ref(1),
  56. showRef: ref(false),
  57. startShowRef: ref(false),
  58. timingShowRef: ref(false),
  59. versionShowRef: ref(false),
  60. copyShowRef: ref(false)
  61. })
  62. const createColumns = (variables: any) => {
  63. variables.columns = [
  64. {
  65. type: 'selection',
  66. disabled: (row) => row.releaseState === 'ONLINE',
  67. className: 'btn-selected',
  68. ...COLUMN_WIDTH_CONFIG['selection']
  69. },
  70. {
  71. title: '#',
  72. key: 'id',
  73. ...COLUMN_WIDTH_CONFIG['index'],
  74. render: (row, index) => index + 1
  75. },
  76. {
  77. title: t('project.workflow.workflow_name'),
  78. key: 'name',
  79. className: 'workflow-name',
  80. ...COLUMN_WIDTH_CONFIG['name'],
  81. render: (row) =>
  82. h(
  83. ButtonLink,
  84. {
  85. onClick: () =>
  86. void router.push({
  87. name: 'workflow-definition-detail',
  88. params: { code: row.code }
  89. })
  90. },
  91. { default: () => row.name }
  92. )
  93. },
  94. {
  95. title: t('project.workflow.status'),
  96. key: 'releaseState',
  97. ...COLUMN_WIDTH_CONFIG['state'],
  98. render: (row) =>
  99. row.releaseState === 'ONLINE'
  100. ? t('project.workflow.up_line')
  101. : t('project.workflow.down_line')
  102. },
  103. {
  104. title: t('project.workflow.create_time'),
  105. key: 'createTime',
  106. ...COLUMN_WIDTH_CONFIG['time']
  107. },
  108. {
  109. title: t('project.workflow.update_time'),
  110. key: 'updateTime',
  111. ...COLUMN_WIDTH_CONFIG['time']
  112. },
  113. {
  114. title: t('project.workflow.description'),
  115. key: 'description',
  116. ...COLUMN_WIDTH_CONFIG['note']
  117. },
  118. {
  119. title: t('project.workflow.create_user'),
  120. key: 'userName',
  121. ...COLUMN_WIDTH_CONFIG['userName']
  122. },
  123. {
  124. title: t('project.workflow.modify_user'),
  125. key: 'modifyBy',
  126. ...COLUMN_WIDTH_CONFIG['userName']
  127. },
  128. {
  129. title: t('project.workflow.schedule_publish_status'),
  130. key: 'scheduleReleaseState',
  131. ...COLUMN_WIDTH_CONFIG['state'],
  132. render: (row) => {
  133. if (row.scheduleReleaseState === 'ONLINE') {
  134. return h(
  135. NTag,
  136. { type: 'success', size: 'small' },
  137. {
  138. default: () => t('project.workflow.up_line')
  139. }
  140. )
  141. } else if (row.scheduleReleaseState === 'OFFLINE') {
  142. return h(
  143. NTag,
  144. { type: 'warning', size: 'small' },
  145. {
  146. default: () => t('project.workflow.down_line')
  147. }
  148. )
  149. } else {
  150. return '-'
  151. }
  152. }
  153. },
  154. {
  155. title: t('project.workflow.operation'),
  156. key: 'operation',
  157. ...COLUMN_WIDTH_CONFIG['operation'](8.5),
  158. className: styles.operation,
  159. render: (row) =>
  160. h(TableAction, {
  161. row,
  162. onEditWorkflow: () => editWorkflow(row),
  163. onStartWorkflow: () => startWorkflow(row),
  164. onTimingWorkflow: () => timingWorkflow(row),
  165. onVersionWorkflow: () => versionWorkflow(row),
  166. onDeleteWorkflow: () => deleteWorkflow(row),
  167. onReleaseWorkflow: () => releaseWorkflow(row),
  168. onCopyWorkflow: () => copyWorkflow(row),
  169. onExportWorkflow: () => exportWorkflow(row),
  170. onGotoTimingManage: () => gotoTimingManage(row),
  171. onGotoWorkflowTree: () => gotoWorkflowTree(row)
  172. })
  173. }
  174. ] as TableColumns<any>
  175. if (variables.tableWidth) {
  176. variables.tableWidth = calculateTableWidth(variables.columns)
  177. }
  178. }
  179. const editWorkflow = (row: any) => {
  180. variables.row = row
  181. router.push({
  182. name: 'workflow-definition-detail',
  183. params: { code: row.code }
  184. })
  185. }
  186. const startWorkflow = (row: any) => {
  187. variables.startShowRef = true
  188. variables.row = row
  189. }
  190. const timingWorkflow = (row: any) => {
  191. variables.timingShowRef = true
  192. variables.row = row
  193. }
  194. const versionWorkflow = (row: any) => {
  195. variables.versionShowRef = true
  196. variables.row = row
  197. }
  198. const deleteWorkflow = (row: any) => {
  199. deleteByCode(variables.projectCode, row.code).then(() => {
  200. window.$message.success(t('project.workflow.success'))
  201. getTableData({
  202. pageSize: variables.pageSize,
  203. pageNo: variables.page,
  204. searchVal: variables.searchVal
  205. })
  206. })
  207. }
  208. const batchDeleteWorkflow = () => {
  209. const data = {
  210. codes: _.join(variables.checkedRowKeys, ',')
  211. }
  212. batchDeleteByCodes(data, variables.projectCode).then(() => {
  213. window.$message.success(t('project.workflow.success'))
  214. if (
  215. variables.tableData.length === variables.checkedRowKeys.length &&
  216. variables.page > 1
  217. ) {
  218. variables.page -= 1
  219. }
  220. variables.checkedRowKeys = []
  221. getTableData({
  222. pageSize: variables.pageSize,
  223. pageNo: variables.page,
  224. searchVal: variables.searchVal
  225. })
  226. })
  227. }
  228. const batchExportWorkflow = () => {
  229. const fileName = 'workflow_' + new Date().getTime()
  230. const data = {
  231. codes: _.join(variables.checkedRowKeys, ',')
  232. }
  233. batchExportByCodes(data, variables.projectCode).then((res: any) => {
  234. downloadBlob(res, fileName)
  235. window.$message.success(t('project.workflow.success'))
  236. variables.checkedRowKeys = []
  237. })
  238. }
  239. const batchCopyWorkflow = () => {}
  240. const releaseWorkflow = (row: any) => {
  241. const data = {
  242. name: row.name,
  243. releaseState: (row.releaseState === 'ONLINE' ? 'OFFLINE' : 'ONLINE') as
  244. | 'OFFLINE'
  245. | 'ONLINE'
  246. }
  247. release(data, variables.projectCode, row.code).then(() => {
  248. window.$message.success(t('project.workflow.success'))
  249. getTableData({
  250. pageSize: variables.pageSize,
  251. pageNo: variables.page,
  252. searchVal: variables.searchVal
  253. })
  254. })
  255. }
  256. const copyWorkflow = (row: any) => {
  257. const data = {
  258. codes: String(row.code),
  259. targetProjectCode: variables.projectCode
  260. }
  261. batchCopyByCodes(data, variables.projectCode).then(() => {
  262. window.$message.success(t('project.workflow.success'))
  263. getTableData({
  264. pageSize: variables.pageSize,
  265. pageNo: variables.page,
  266. searchVal: variables.searchVal
  267. })
  268. })
  269. }
  270. const downloadBlob = (data: any, fileNameS = 'json') => {
  271. if (!data) {
  272. return
  273. }
  274. const blob = new Blob([data])
  275. const fileName = `${fileNameS}.json`
  276. if ('download' in document.createElement('a')) {
  277. // Not IE
  278. const url = window.URL.createObjectURL(blob)
  279. const link = document.createElement('a')
  280. link.style.display = 'none'
  281. link.href = url
  282. link.setAttribute('download', fileName)
  283. document.body.appendChild(link)
  284. link.click()
  285. document.body.removeChild(link) // remove element after downloading is complete.
  286. window.URL.revokeObjectURL(url) // release blob object
  287. } else {
  288. // IE 10+
  289. if (window.navigator.msSaveBlob) {
  290. window.navigator.msSaveBlob(blob, fileName)
  291. }
  292. }
  293. }
  294. const exportWorkflow = (row: any) => {
  295. const fileName = 'workflow_' + new Date().getTime()
  296. const data = {
  297. codes: String(row.code)
  298. }
  299. batchExportByCodes(data, variables.projectCode).then((res: any) => {
  300. downloadBlob(res, fileName)
  301. })
  302. }
  303. const gotoTimingManage = (row: any) => {
  304. router.push({
  305. name: 'workflow-definition-timing',
  306. params: { projectCode: variables.projectCode, definitionCode: row.code }
  307. })
  308. }
  309. const gotoWorkflowTree = (row: any) => {
  310. router.push({
  311. name: 'workflow-definition-tree',
  312. params: { projectCode: variables.projectCode, definitionCode: row.code }
  313. })
  314. }
  315. const getTableData = (params: IDefinitionParam) => {
  316. const { state } = useAsyncState(
  317. queryListPaging({ ...params }, variables.projectCode).then((res: any) => {
  318. variables.totalPage = res.totalPage
  319. variables.tableData = res.totalList.map((item: any) => {
  320. return { ...item }
  321. })
  322. }),
  323. { total: 0, table: [] }
  324. )
  325. return state
  326. }
  327. return {
  328. variables,
  329. createColumns,
  330. getTableData,
  331. batchDeleteWorkflow,
  332. batchExportWorkflow,
  333. batchCopyWorkflow
  334. }
  335. }