mixin.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. import XEUtils from 'xe-utils'
  2. import { getColumnList } from '../../src/util'
  3. import type { VxeColumnPropTypes, VxeTableConstructor, VxeTablePrivateMethods, VxeTableDefines, TableReactData, TableInternalData } from '../../../../types'
  4. function calcMaxHeight ($xeTable: VxeTableConstructor & VxeTablePrivateMethods) {
  5. const $xeGantt = $xeTable.$xeGantt
  6. const reactData = $xeTable as unknown as TableReactData
  7. const { customStore } = reactData
  8. let wrapperEl = $xeTable.$refs.refElem as HTMLDivElement
  9. // 判断面板不能大于表格高度
  10. let tableHeight = 0
  11. if ($xeGantt) {
  12. const ganttContainerElem = $xeGantt.$refs.refGanttContainerElem as HTMLDivElement
  13. if (ganttContainerElem) {
  14. wrapperEl = ganttContainerElem
  15. }
  16. }
  17. if (wrapperEl) {
  18. tableHeight = wrapperEl.clientHeight - 28
  19. }
  20. customStore.maxHeight = Math.max(88, tableHeight)
  21. }
  22. function emitCustomEvent ($xeTable: VxeTableConstructor & VxeTablePrivateMethods, type: VxeTableDefines.CustomType, evnt: Event) {
  23. const $xeGrid = $xeTable.$xeGrid
  24. const $xeGantt = $xeTable.$xeGantt
  25. const comp = $xeGrid || $xeGantt || $xeTable
  26. comp.dispatchEvent('custom', { type }, evnt)
  27. }
  28. export default {
  29. methods: {
  30. _getCustomVisible () {
  31. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  32. const reactData = $xeTable as unknown as TableReactData
  33. const { customStore } = reactData
  34. return customStore.visible
  35. },
  36. _openCustom () {
  37. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  38. const reactData = $xeTable as unknown as TableReactData
  39. const { initStore, customStore } = reactData
  40. customStore.visible = true
  41. initStore.custom = true
  42. $xeTable.handleUpdateCustomColumn()
  43. $xeTable.checkCustomStatus()
  44. calcMaxHeight($xeTable)
  45. return $xeTable.$nextTick().then(() => calcMaxHeight($xeTable))
  46. },
  47. _closeCustom () {
  48. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  49. const reactData = $xeTable as unknown as TableReactData
  50. const { customStore } = reactData
  51. const customOpts = $xeTable.computeCustomOpts
  52. if (customStore.visible) {
  53. customStore.visible = false
  54. if (!customOpts.immediate) {
  55. $xeTable.handleCustom()
  56. }
  57. }
  58. return $xeTable.$nextTick()
  59. },
  60. _toggleCustom () {
  61. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  62. const reactData = $xeTable as unknown as TableReactData
  63. const { customStore } = reactData
  64. if (customStore.visible) {
  65. return $xeTable.closeCustom()
  66. }
  67. return $xeTable.openCustom()
  68. },
  69. _saveCustom () {
  70. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  71. const reactData = $xeTable as unknown as TableReactData
  72. const internalData = $xeTable as unknown as TableInternalData
  73. const { customColumnList, aggHandleFields, rowGroupList } = reactData
  74. const customOpts = $xeTable.computeCustomOpts
  75. const { allowVisible, allowSort, allowFixed, allowResizable, allowGroup, allowValues } = customOpts
  76. XEUtils.eachTree(customColumnList, (column, index, items, path, parentColumn) => {
  77. if (parentColumn) {
  78. // 更新子列信息
  79. column.fixed = parentColumn.fixed
  80. } else {
  81. if (allowSort) {
  82. const sortIndex = index + 1
  83. column.renderSortNumber = sortIndex
  84. }
  85. if (allowFixed) {
  86. column.fixed = column.renderFixed
  87. }
  88. }
  89. if (allowResizable) {
  90. if (column.renderVisible && (!column.children || column.children.length)) {
  91. if (column.renderResizeWidth !== column.renderWidth) {
  92. column.resizeWidth = column.renderResizeWidth
  93. column.renderWidth = column.renderResizeWidth
  94. }
  95. }
  96. }
  97. if (allowVisible) {
  98. column.visible = column.renderVisible
  99. }
  100. if (allowGroup && allowValues) {
  101. column.aggFunc = column.renderAggFn
  102. }
  103. })
  104. reactData.isCustomStatus = true
  105. if (allowGroup && allowValues && !!$xeTable.handlePivotTableAggregateData) {
  106. if (rowGroupList.length !== aggHandleFields.length || rowGroupList.some((conf, i) => conf.field !== aggHandleFields[i])) {
  107. // 更新数据分组
  108. if (aggHandleFields.length) {
  109. $xeTable.setRowGroups(aggHandleFields)
  110. } else {
  111. $xeTable.clearRowGroups()
  112. }
  113. } else if (allowValues) {
  114. // 更新聚合函数
  115. $xeTable.handleUpdateAggData()
  116. }
  117. }
  118. if (allowSort) {
  119. internalData.collectColumn = customColumnList
  120. }
  121. return $xeTable.saveCustomStore('confirm')
  122. },
  123. _cancelCustom () {
  124. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  125. const reactData = $xeTable as unknown as TableReactData
  126. const { customColumnList, customStore } = reactData
  127. const { oldSortMaps, oldFixedMaps, oldVisibleMaps } = customStore
  128. const customOpts = $xeTable.computeCustomOpts
  129. const { allowVisible, allowSort, allowFixed, allowResizable } = customOpts
  130. XEUtils.eachTree(customColumnList, column => {
  131. const colid = column.getKey()
  132. const visible = !!oldVisibleMaps[colid]
  133. const fixed = oldFixedMaps[colid] || ''
  134. if (allowVisible) {
  135. column.renderVisible = visible
  136. column.visible = visible
  137. }
  138. if (allowFixed) {
  139. column.renderFixed = fixed
  140. column.fixed = fixed
  141. }
  142. if (allowSort) {
  143. column.renderSortNumber = oldSortMaps[colid] || 0
  144. }
  145. if (allowResizable) {
  146. column.renderResizeWidth = column.renderWidth
  147. }
  148. }, { children: 'children' })
  149. return $xeTable.$nextTick()
  150. },
  151. _resetCustom (options: any) {
  152. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  153. const reactData = $xeTable as unknown as TableReactData
  154. const internalData = $xeTable as unknown as TableInternalData
  155. const { rowGroupList } = reactData
  156. const { collectColumn } = internalData
  157. const customOpts = $xeTable.computeCustomOpts
  158. const { checkMethod } = customOpts
  159. const opts = Object.assign({
  160. visible: true,
  161. resizable: options === true,
  162. fixed: options === true,
  163. sort: options === true,
  164. aggFunc: options === true
  165. }, options)
  166. const allCols: VxeTableDefines.ColumnInfo[] = []
  167. XEUtils.eachTree(collectColumn, (column) => {
  168. if (opts.resizable) {
  169. column.resizeWidth = 0
  170. }
  171. if (opts.fixed) {
  172. column.fixed = column.defaultFixed
  173. }
  174. if (opts.sort) {
  175. column.renderSortNumber = column.sortNumber
  176. column.parentId = column.defaultParentId
  177. }
  178. if (!checkMethod || checkMethod({ $table: $xeTable, column })) {
  179. column.visible = column.defaultVisible
  180. }
  181. if (opts.aggFunc) {
  182. column.aggFunc = column.defaultAggFunc
  183. column.renderAggFn = column.defaultAggFunc
  184. }
  185. column.renderResizeWidth = column.renderWidth
  186. allCols.push(column)
  187. })
  188. if (opts.sort) {
  189. const newCollectCols = XEUtils.toArrayTree(XEUtils.orderBy(allCols, 'renderSortNumber'), { key: 'id', parentKey: 'parentId', children: 'children' })
  190. internalData.collectColumn = newCollectCols
  191. internalData.tableFullColumn = getColumnList(newCollectCols)
  192. }
  193. reactData.isCustomStatus = false
  194. return $xeTable.handleCustom().then(() => {
  195. if (opts.aggFunc && ($xeTable as any).handlePivotTableAggregateData) {
  196. const rowGroupFields = $xeTable.computeRowGroupFields
  197. if (rowGroupFields ? rowGroupFields.length : rowGroupList.length) {
  198. if (rowGroupFields && rowGroupFields.length) {
  199. $xeTable.setRowGroups(rowGroupFields)
  200. } else {
  201. $xeTable.clearRowGroups()
  202. }
  203. } else {
  204. $xeTable.handleUpdateAggData()
  205. }
  206. }
  207. $xeTable.saveCustomStore('reset')
  208. })
  209. },
  210. _toggleCustomAllCheckbox () {
  211. const { customStore } = this
  212. const isAll = !customStore.isAll
  213. return this.setCustomAllCheckbox(isAll)
  214. },
  215. _setCustomAllCheckbox (checked: boolean) {
  216. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  217. const reactData = $xeTable as unknown as TableReactData
  218. const { customStore } = reactData
  219. const { customColumnList } = reactData
  220. const customOpts = $xeTable.computeCustomOpts
  221. const { checkMethod, visibleMethod } = customOpts
  222. const isAll = !!checked
  223. if (customOpts.immediate) {
  224. XEUtils.eachTree(customColumnList, (column) => {
  225. if (visibleMethod && !visibleMethod({ $table: $xeTable, column })) {
  226. return
  227. }
  228. if (checkMethod && !checkMethod({ $table: $xeTable, column })) {
  229. return
  230. }
  231. column.visible = isAll
  232. column.renderVisible = isAll
  233. column.halfVisible = false
  234. })
  235. customStore.isAll = isAll
  236. reactData.isCustomStatus = true
  237. $xeTable.handleCustom()
  238. $xeTable.saveCustomStore('update:visible')
  239. } else {
  240. XEUtils.eachTree(customColumnList, (column) => {
  241. if (visibleMethod && !visibleMethod({ $table: $xeTable, column })) {
  242. return
  243. }
  244. if (checkMethod && !checkMethod({ $table: $xeTable, column })) {
  245. return
  246. }
  247. column.renderVisible = isAll
  248. column.halfVisible = false
  249. })
  250. customStore.isAll = isAll
  251. }
  252. $xeTable.checkCustomStatus()
  253. },
  254. checkCustomStatus () {
  255. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  256. const reactData = $xeTable as unknown as TableReactData
  257. const internalData = $xeTable as unknown as TableInternalData
  258. const { customStore } = reactData
  259. const { collectColumn } = internalData
  260. const customOpts = $xeTable.computeCustomOpts
  261. const { checkMethod } = customOpts
  262. customStore.isAll = collectColumn.every((column) => (checkMethod ? !checkMethod({ $table: $xeTable, column }) : false) || column.renderVisible)
  263. customStore.isIndeterminate = !customStore.isAll && collectColumn.some((column) => (!checkMethod || checkMethod({ $table: $xeTable, column })) && (column.renderVisible || column.halfVisible))
  264. },
  265. emitCustomEvent (type: any, evnt: any) {
  266. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  267. const $xeGrid = $xeTable.$xeGrid
  268. const $xeGantt = $xeTable.$xeGantt
  269. const comp = $xeGrid || $xeGantt || $xeTable
  270. comp.dispatchEvent('custom', { type }, evnt)
  271. },
  272. triggerCustomEvent (evnt: any) {
  273. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  274. const reactData = $xeTable as unknown as TableReactData
  275. const { customStore } = reactData
  276. if (customStore.visible) {
  277. this.closeCustom()
  278. emitCustomEvent($xeTable, 'close', evnt)
  279. } else {
  280. customStore.btnEl = evnt.target
  281. this.openCustom()
  282. emitCustomEvent($xeTable, 'open', evnt)
  283. }
  284. },
  285. customOpenEvent (evnt: any) {
  286. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  287. const reactData = $xeTable as unknown as TableReactData
  288. const { customStore } = reactData
  289. if (customStore.visible) {
  290. customStore.activeBtn = true
  291. customStore.btnEl = evnt.target
  292. $xeTable.openCustom()
  293. emitCustomEvent($xeTable, 'open', evnt)
  294. }
  295. },
  296. customCloseEvent (evnt: any) {
  297. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  298. const reactData = $xeTable as unknown as TableReactData
  299. const { customStore } = reactData
  300. if (customStore.visible) {
  301. customStore.activeBtn = false
  302. $xeTable.closeCustom()
  303. emitCustomEvent($xeTable, 'close', evnt)
  304. }
  305. },
  306. handleUpdateCustomColumn () {
  307. const $xeTable = this as VxeTableConstructor & VxeTablePrivateMethods
  308. const reactData = $xeTable as unknown as TableReactData
  309. const internalData = $xeTable as unknown as TableInternalData
  310. const { customStore } = reactData
  311. const { collectColumn } = internalData
  312. if (customStore.visible) {
  313. const sortMaps: Record<string, number> = {}
  314. const fixedMaps: Record<string, VxeColumnPropTypes.Fixed> = {}
  315. const visibleMaps: Record<string, boolean> = {}
  316. XEUtils.eachTree(collectColumn, column => {
  317. const colid = column.getKey()
  318. column.renderFixed = column.fixed
  319. column.renderVisible = column.visible
  320. column.renderResizeWidth = column.renderWidth
  321. sortMaps[colid] = column.renderSortNumber
  322. fixedMaps[colid] = column.fixed
  323. visibleMaps[colid] = column.visible
  324. })
  325. customStore.oldSortMaps = sortMaps
  326. customStore.oldFixedMaps = fixedMaps
  327. customStore.oldVisibleMaps = visibleMaps
  328. reactData.customColumnList = collectColumn.slice(0)
  329. }
  330. }
  331. } as any
  332. }