mixin.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. import XEUtils from 'xe-utils/ctor'
  2. import { UtilTools, DomTools } from '../../tools'
  3. const browse = DomTools.browse
  4. function getTargetOffset (target, container) {
  5. let offsetTop = 0
  6. let offsetLeft = 0
  7. const triggerCheckboxLabel = !browse.firefox && DomTools.hasClass(target, 'vxe-checkbox--label')
  8. if (triggerCheckboxLabel) {
  9. const checkboxLabelStyle = getComputedStyle(target)
  10. offsetTop -= XEUtils.toNumber(checkboxLabelStyle.paddingTop)
  11. offsetLeft -= XEUtils.toNumber(checkboxLabelStyle.paddingLeft)
  12. }
  13. while (target && target !== container) {
  14. offsetTop += target.offsetTop
  15. offsetLeft += target.offsetLeft
  16. target = target.offsetParent
  17. if (triggerCheckboxLabel) {
  18. const checkboxStyle = getComputedStyle(target)
  19. offsetTop -= XEUtils.toNumber(checkboxStyle.paddingTop)
  20. offsetLeft -= XEUtils.toNumber(checkboxStyle.paddingLeft)
  21. }
  22. }
  23. return { offsetTop, offsetLeft }
  24. }
  25. function getCheckboxRangeRows (_vm, params, targetTrElem, moveRange) {
  26. let countHeight = 0
  27. let rangeRows = []
  28. const isDown = moveRange > 0
  29. const moveSize = moveRange > 0 ? moveRange : (Math.abs(moveRange) + targetTrElem.offsetHeight)
  30. const { afterFullData, scrollYStore, scrollYLoad } = _vm
  31. if (scrollYLoad) {
  32. const _rowIndex = _vm.getVTRowIndex(params.row)
  33. if (isDown) {
  34. rangeRows = afterFullData.slice(_rowIndex, _rowIndex + Math.ceil(moveSize / scrollYStore.rowHeight))
  35. } else {
  36. rangeRows = afterFullData.slice(_rowIndex - Math.floor(moveSize / scrollYStore.rowHeight) + 1, _rowIndex + 1)
  37. }
  38. } else {
  39. const siblingProp = isDown ? 'next' : 'previous'
  40. while (targetTrElem && countHeight < moveSize) {
  41. rangeRows.push(_vm.getRowNode(targetTrElem).item)
  42. countHeight += targetTrElem.offsetHeight
  43. targetTrElem = targetTrElem[`${siblingProp}ElementSibling`]
  44. }
  45. }
  46. return rangeRows
  47. }
  48. export default {
  49. methods: {
  50. // 处理 Tab 键移动
  51. moveTabSelected (args, isLeft, evnt) {
  52. const { afterFullData, visibleColumn, editConfig, editOpts, isSeqColumn } = this
  53. let targetRow
  54. let targetRowIndex
  55. let targetColumn
  56. let targetColumnIndex
  57. const params = Object.assign({}, args)
  58. const rowIndex = afterFullData.indexOf(params.row)
  59. const columnIndex = visibleColumn.indexOf(params.column)
  60. evnt.preventDefault()
  61. if (isLeft) {
  62. // 向左
  63. for (let len = columnIndex - 1; len >= 0; len--) {
  64. if (!isSeqColumn(visibleColumn[len])) {
  65. targetColumnIndex = len
  66. targetColumn = visibleColumn[len]
  67. break
  68. }
  69. }
  70. if (!targetColumn && rowIndex > 0) {
  71. // 如果找不到从上一行开始找,如果一行都找不到就不需要继续找了,可能不存在可编辑的列
  72. targetRowIndex = rowIndex - 1
  73. targetRow = afterFullData[targetRowIndex]
  74. for (let len = visibleColumn.length - 1; len >= 0; len--) {
  75. if (!isSeqColumn(visibleColumn[len])) {
  76. targetColumnIndex = len
  77. targetColumn = visibleColumn[len]
  78. break
  79. }
  80. }
  81. }
  82. } else {
  83. // 向右
  84. for (let index = columnIndex + 1; index < visibleColumn.length; index++) {
  85. if (!isSeqColumn(visibleColumn[index])) {
  86. targetColumnIndex = index
  87. targetColumn = visibleColumn[index]
  88. break
  89. }
  90. }
  91. if (!targetColumn && rowIndex < afterFullData.length - 1) {
  92. // 如果找不到从下一行开始找,如果一行都找不到就不需要继续找了,可能不存在可编辑的列
  93. targetRowIndex = rowIndex + 1
  94. targetRow = afterFullData[targetRowIndex]
  95. for (let index = 0; index < visibleColumn.length; index++) {
  96. if (!isSeqColumn(visibleColumn[index])) {
  97. targetColumnIndex = index
  98. targetColumn = visibleColumn[index]
  99. break
  100. }
  101. }
  102. }
  103. }
  104. if (targetColumn) {
  105. if (targetRow) {
  106. params.rowIndex = targetRowIndex
  107. params.row = targetRow
  108. } else {
  109. params.rowIndex = rowIndex
  110. }
  111. params.columnIndex = targetColumnIndex
  112. params.column = targetColumn
  113. params.cell = this.getCell(params.row, params.column)
  114. if (editConfig) {
  115. if (editOpts.trigger === 'click' || editOpts.trigger === 'dblclick') {
  116. if (editOpts.mode === 'row') {
  117. this.handleActived(params, evnt)
  118. } else {
  119. this.scrollToRow(params.row, params.column)
  120. .then(() => this.handleSelected(params, evnt))
  121. }
  122. }
  123. }
  124. }
  125. },
  126. // 处理当前行方向键移动
  127. moveCurrentRow (isUpArrow, isDwArrow, evnt) {
  128. const { currentRow, treeConfig, treeOpts, afterFullData } = this
  129. let targetRow
  130. evnt.preventDefault()
  131. if (currentRow) {
  132. if (treeConfig) {
  133. const { index, items } = XEUtils.findTree(afterFullData, item => item === currentRow, treeOpts)
  134. if (isUpArrow && index > 0) {
  135. targetRow = items[index - 1]
  136. } else if (isDwArrow && index < items.length - 1) {
  137. targetRow = items[index + 1]
  138. }
  139. } else {
  140. const _rowIndex = this.getVTRowIndex(currentRow)
  141. if (isUpArrow && _rowIndex > 0) {
  142. targetRow = afterFullData[_rowIndex - 1]
  143. } else if (isDwArrow && _rowIndex < afterFullData.length - 1) {
  144. targetRow = afterFullData[_rowIndex + 1]
  145. }
  146. }
  147. } else {
  148. targetRow = afterFullData[0]
  149. }
  150. if (targetRow) {
  151. const params = { $table: this, row: targetRow }
  152. this.scrollToRow(targetRow)
  153. .then(() => this.triggerCurrentRowEvent(evnt, params))
  154. }
  155. },
  156. // 处理可编辑方向键移动
  157. moveSelected (args, isLeftArrow, isUpArrow, isRightArrow, isDwArrow, evnt) {
  158. const { afterFullData, visibleColumn, isSeqColumn } = this
  159. const params = Object.assign({}, args)
  160. const _rowIndex = this.getVTRowIndex(params.row)
  161. const _columnIndex = this.getVTColumnIndex(params.column)
  162. evnt.preventDefault()
  163. if (isUpArrow && _rowIndex > 0) {
  164. // 移动到上一行
  165. params.rowIndex = _rowIndex - 1
  166. params.row = afterFullData[params.rowIndex]
  167. } else if (isDwArrow && _rowIndex < afterFullData.length - 1) {
  168. // 移动到下一行
  169. params.rowIndex = _rowIndex + 1
  170. params.row = afterFullData[params.rowIndex]
  171. } else if (isLeftArrow && _columnIndex) {
  172. for (let len = _columnIndex - 1; len >= 0; len--) {
  173. if (!isSeqColumn(visibleColumn[len])) {
  174. params.columnIndex = len
  175. params.column = visibleColumn[len]
  176. break
  177. }
  178. }
  179. } else if (isRightArrow) {
  180. for (let index = _columnIndex + 1; index < visibleColumn.length; index++) {
  181. if (!isSeqColumn(visibleColumn[index])) {
  182. params.columnIndex = index
  183. params.column = visibleColumn[index]
  184. break
  185. }
  186. }
  187. }
  188. this.scrollToRow(params.row, params.column).then(() => {
  189. params.cell = this.getCell(params.row, params.column)
  190. this.handleSelected(params, evnt)
  191. })
  192. },
  193. /**
  194. * 表头按下事件
  195. */
  196. triggerHeaderCellMousedownEvent (evnt, params) {
  197. const { mouseConfig, mouseOpts } = this
  198. const cell = evnt.currentTarget
  199. const triggerSort = DomTools.getEventTargetNode(evnt, cell, 'vxe-cell--sort').flag
  200. const triggerFilter = DomTools.getEventTargetNode(evnt, cell, 'vxe-cell--filter').flag
  201. if (mouseConfig && mouseOpts.area && this.handleHeaderCellAreaEvent) {
  202. this.handleHeaderCellAreaEvent(evnt, Object.assign({ cell, triggerSort, triggerFilter }, params))
  203. } else if (mouseConfig && mouseOpts.checked) {
  204. this.handleHeaderCellCheckedEvent(evnt, Object.assign({ cell, triggerSort, triggerFilter }, params))
  205. }
  206. this.focus()
  207. this.closeMenu()
  208. },
  209. /**
  210. * 单元格按下事件
  211. */
  212. triggerCellMousedownEvent (evnt, params) {
  213. const cell = evnt.currentTarget
  214. params.cell = cell
  215. this.handleCellMousedownEvent(evnt, params)
  216. this.focus()
  217. this.closeFilter()
  218. this.closeMenu()
  219. },
  220. handleCellMousedownEvent (evnt, params) {
  221. const { mouseConfig, mouseOpts, checkboxConfig, checkboxOpts, editConfig, editOpts } = this
  222. const { column } = params
  223. if (mouseConfig && mouseOpts.area && this.handleCellAreaEvent) {
  224. this.handleCellAreaEvent(evnt, params)
  225. } else if (mouseConfig && mouseOpts.checked) {
  226. // 在 v3.0 中废弃 mouse-config.checked
  227. this.handleCheckedRangeEvent(evnt, params)
  228. } else {
  229. if (checkboxConfig && checkboxOpts.range) {
  230. this.handleCheckboxRangeEvent(evnt, params)
  231. }
  232. if (mouseConfig && mouseOpts.selected) {
  233. // v3.0 废弃 type=index
  234. if (!(column.type === 'seq' || column.type === 'index') && (!editConfig || editOpts.mode === 'cell')) {
  235. this.handleSelected(params, evnt)
  236. }
  237. }
  238. }
  239. },
  240. handleHeaderCellCheckedEvent (evnt, params) {
  241. const { $el, tableData, mouseConfig, mouseOpts, elemStore, handleChecked, handleHeaderChecked } = this
  242. const { button } = evnt
  243. const { column } = params
  244. const cell = evnt.currentTarget
  245. const isLeftBtn = button === 0
  246. // v3.0 废弃 type=index
  247. const isIndex = column.type === 'seq' || column.type === 'index'
  248. if (mouseConfig) {
  249. // 在 v3.0 中废弃 mouse-config.checked
  250. if (mouseOpts.checked) {
  251. const headerList = elemStore['main-header-list'].children
  252. const bodyList = elemStore['main-body-list'].children
  253. if (isIndex) {
  254. this.handleAllChecked(evnt)
  255. } else {
  256. this.clearSelected(evnt)
  257. this.clearHeaderChecked()
  258. this.clearIndexChecked()
  259. const startCell = bodyList[0].querySelector(`.${column.id}`)
  260. if (isLeftBtn) {
  261. const domMousemove = document.onmousemove
  262. const domMouseup = document.onmouseup
  263. const updateEvent = XEUtils.throttle(function (evnt) {
  264. let { flag, targetElem } = DomTools.getEventTargetNode(evnt, $el, 'vxe-header--column')
  265. if (!flag) {
  266. const nodeRest = DomTools.getEventTargetNode(evnt, $el, 'vxe-body--column')
  267. flag = nodeRest.flag
  268. targetElem = nodeRest.targetElem
  269. }
  270. if (flag && !DomTools.hasClass(targetElem, 'col--seq')) {
  271. const colIndex = [].indexOf.call(targetElem.parentNode.children, targetElem)
  272. const endCell = bodyList[bodyList.length - 1].children[colIndex]
  273. const head = headerList[0].children[colIndex]
  274. handleHeaderChecked(DomTools.getRowNodes(headerList, DomTools.getCellNodeIndex(head), DomTools.getCellNodeIndex(cell)))
  275. handleChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(startCell), DomTools.getCellNodeIndex(endCell)))
  276. }
  277. }, 80, { leading: true, trailing: true })
  278. DomTools.addClass($el, 'c--checked')
  279. document.onmousemove = evnt => {
  280. evnt.preventDefault()
  281. evnt.stopPropagation()
  282. updateEvent(evnt)
  283. }
  284. document.onmouseup = function () {
  285. DomTools.removeClass($el, 'c--checked')
  286. document.onmousemove = domMousemove
  287. document.onmouseup = domMouseup
  288. }
  289. }
  290. handleHeaderChecked([[cell]])
  291. if (bodyList.length) {
  292. const endCell = bodyList[bodyList.length - 1].querySelector(`.${column.id}`)
  293. const firstTrElem = bodyList[0]
  294. const lastTrElem = bodyList[bodyList.length - 1]
  295. const firstCell = firstTrElem.querySelector('.col--seq')
  296. params.rowIndex = 0
  297. params.row = tableData[0]
  298. params.cell = this.getCell(params.row, params.column)
  299. this.handleSelected(params, evnt)
  300. this.handleIndexChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell), DomTools.getCellNodeIndex(lastTrElem.querySelector('.col--seq'))))
  301. this.handleChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(startCell), DomTools.getCellNodeIndex(endCell)))
  302. }
  303. }
  304. }
  305. }
  306. },
  307. getCheckboxRangeRows (targetTrElem, moveRange) {
  308. let countHeight = 0
  309. const rangeRows = []
  310. const siblingProp = moveRange > 0 ? 'next' : 'previous'
  311. const moveSize = moveRange > 0 ? moveRange : (Math.abs(moveRange) + targetTrElem.offsetHeight)
  312. while (targetTrElem && countHeight < moveSize) {
  313. rangeRows.push(this.getRowNode(targetTrElem).item)
  314. countHeight += targetTrElem.offsetHeight
  315. targetTrElem = targetTrElem[`${siblingProp}ElementSibling`]
  316. }
  317. return rangeRows
  318. },
  319. handleCheckedRangeEvent (evnt, params) {
  320. const { $el, visibleColumn, editStore, mouseOpts, elemStore } = this
  321. const { checked } = editStore
  322. const { column } = params
  323. const { button } = evnt
  324. const cell = evnt.currentTarget
  325. const isLeftBtn = button === 0
  326. // v3.0 废弃 type=index
  327. const isIndex = column.type === 'seq' || column.type === 'index'
  328. this.clearHeaderChecked()
  329. this.clearIndexChecked()
  330. const bodyList = elemStore['main-body-list'].children
  331. const headerList = elemStore['main-header-list'].children
  332. const cellLastElementChild = cell.parentNode.lastElementChild
  333. const cellFirstElementChild = cell.parentNode.firstElementChild
  334. if (isLeftBtn) {
  335. const domMousemove = document.onmousemove
  336. const domMouseup = document.onmouseup
  337. const startCellNode = DomTools.getCellNodeIndex(cell)
  338. const colIndex = [].indexOf.call(cell.parentNode.children, cell)
  339. const headStart = headerList[0].children[colIndex]
  340. const updateEvent = XEUtils.throttle((evnt) => {
  341. const { flag, targetElem } = DomTools.getEventTargetNode(evnt, $el, 'vxe-body--column')
  342. if (flag) {
  343. if (isIndex) {
  344. const firstCell = targetElem.parentNode.firstElementChild
  345. this.handleChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell.nextElementSibling), DomTools.getCellNodeIndex(cellLastElementChild)))
  346. this.handleIndexChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell), DomTools.getCellNodeIndex(cell)))
  347. } else if (!DomTools.hasClass(targetElem, 'col--seq')) {
  348. const firstCell = targetElem.parentNode.firstElementChild
  349. const colIndex = [].indexOf.call(targetElem.parentNode.children, targetElem)
  350. const head = headerList[0].children[colIndex]
  351. this.handleHeaderChecked(DomTools.getRowNodes(headerList, DomTools.getCellNodeIndex(head), DomTools.getCellNodeIndex(headStart)))
  352. this.handleIndexChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell), DomTools.getCellNodeIndex(cellFirstElementChild)))
  353. this.handleChecked(DomTools.getRowNodes(bodyList, startCellNode, DomTools.getCellNodeIndex(targetElem)))
  354. }
  355. }
  356. }, 80, { leading: true, trailing: true })
  357. document.onmousemove = evnt => {
  358. evnt.preventDefault()
  359. evnt.stopPropagation()
  360. updateEvent(evnt)
  361. }
  362. document.onmouseup = function () {
  363. document.onmousemove = domMousemove
  364. document.onmouseup = domMouseup
  365. }
  366. }
  367. if (isIndex) {
  368. const firstCell = cell.parentNode.firstElementChild
  369. params.columnIndex++
  370. params.column = visibleColumn[params.columnIndex]
  371. params.cell = cell.nextElementSibling
  372. this.handleSelected(params, evnt)
  373. this.handleChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell.nextElementSibling), DomTools.getCellNodeIndex(cellLastElementChild)))
  374. this.handleHeaderChecked([headerList[0].querySelectorAll('.vxe-header--column:not(.col--seq)')])
  375. this.handleIndexChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell), DomTools.getCellNodeIndex(cell)))
  376. } else {
  377. if (isLeftBtn) {
  378. const firstCell = cell.parentNode.firstElementChild
  379. this.handleSelected(params, evnt)
  380. this.handleHeaderChecked([[headerList[0].querySelector(`.${column.id}`)]])
  381. this.handleIndexChecked([[firstCell]])
  382. } else {
  383. if (mouseOpts.selected) {
  384. // 如果右键单元格不在所有选中的范围之内则重新选中
  385. if (!checked.rowNodes || !checked.rowNodes.some(list => list.indexOf(cell) > -1)) {
  386. this.handleSelected(params, evnt)
  387. }
  388. }
  389. }
  390. }
  391. },
  392. handleCheckboxRangeEvent (evnt, params) {
  393. const { column, cell } = params
  394. const isLeftBtn = evnt.button === 0
  395. // 在 v3.0 中废弃 type=selection
  396. if (isLeftBtn && ['checkbox', 'selection'].indexOf(column.type) > -1) {
  397. const { $el, elemStore } = this
  398. const disX = evnt.clientX
  399. const disY = evnt.clientY
  400. const bodyWrapperElem = elemStore[`${column.fixed || 'main'}-body-wrapper`] || elemStore['main-body-wrapper']
  401. const checkboxRangeElem = bodyWrapperElem.querySelector('.vxe-table--checkbox-range')
  402. const domMousemove = document.onmousemove
  403. const domMouseup = document.onmouseup
  404. const trElem = cell.parentNode
  405. const selectRecords = this.getCheckboxRecords()
  406. let lastRangeRows = []
  407. const marginSize = 1
  408. const offsetRest = getTargetOffset(evnt.target, bodyWrapperElem)
  409. const startTop = offsetRest.offsetTop + evnt.offsetY
  410. const startLeft = offsetRest.offsetLeft + evnt.offsetX
  411. const startScrollTop = bodyWrapperElem.scrollTop
  412. const rowHeight = trElem.offsetHeight
  413. let mouseScrollTimeout = null
  414. let isMouseScrollDown = false
  415. let mouseScrollSpaceSize = 1
  416. const triggerEvent = (type, evnt) => {
  417. this.emitEvent(`checkbox-range-${type}`, { records: this.getCheckboxRecords(), reserves: this.getCheckboxReserveRecords() }, evnt)
  418. }
  419. const handleChecked = (evnt) => {
  420. const { clientX, clientY } = evnt
  421. const offsetLeft = clientX - disX
  422. const offsetTop = clientY - disY + (bodyWrapperElem.scrollTop - startScrollTop)
  423. let rangeHeight = Math.abs(offsetTop)
  424. let rangeWidth = Math.abs(offsetLeft)
  425. let rangeTop = startTop
  426. let rangeLeft = startLeft
  427. if (offsetTop < marginSize) {
  428. // 向上
  429. rangeTop += offsetTop
  430. if (rangeTop < marginSize) {
  431. rangeTop = marginSize
  432. rangeHeight = startTop
  433. }
  434. } else {
  435. // 向下
  436. rangeHeight = Math.min(rangeHeight, bodyWrapperElem.scrollHeight - startTop - marginSize)
  437. }
  438. if (offsetLeft < marginSize) {
  439. // 向左
  440. rangeLeft += offsetLeft
  441. if (rangeWidth > startLeft) {
  442. rangeLeft = marginSize
  443. rangeWidth = startLeft
  444. }
  445. } else {
  446. // 向右
  447. rangeWidth = Math.min(rangeWidth, bodyWrapperElem.clientWidth - startLeft - marginSize)
  448. }
  449. checkboxRangeElem.style.height = `${rangeHeight}px`
  450. checkboxRangeElem.style.width = `${rangeWidth}px`
  451. checkboxRangeElem.style.left = `${rangeLeft}px`
  452. checkboxRangeElem.style.top = `${rangeTop}px`
  453. checkboxRangeElem.style.display = 'block'
  454. const rangeRows = getCheckboxRangeRows(this, params, trElem, offsetTop < marginSize ? -rangeHeight : rangeHeight)
  455. // 至少滑动 10px 才能有效匹配
  456. if (rangeHeight > 10 && rangeRows.length !== lastRangeRows.length) {
  457. lastRangeRows = rangeRows
  458. if (evnt.ctrlKey) {
  459. rangeRows.forEach(row => {
  460. this.handleSelectRow({ row }, selectRecords.indexOf(row) === -1)
  461. })
  462. } else {
  463. this.setAllCheckboxRow(false)
  464. this.setCheckboxRow(rangeRows, true)
  465. }
  466. triggerEvent('change', evnt)
  467. }
  468. }
  469. // 停止鼠标滚动
  470. const stopMouseScroll = () => {
  471. clearTimeout(mouseScrollTimeout)
  472. mouseScrollTimeout = null
  473. }
  474. // 开始鼠标滚动
  475. const startMouseScroll = (evnt) => {
  476. stopMouseScroll()
  477. mouseScrollTimeout = setTimeout(() => {
  478. if (mouseScrollTimeout) {
  479. const { scrollLeft, scrollTop, clientHeight, scrollHeight } = bodyWrapperElem
  480. const topSize = Math.ceil(mouseScrollSpaceSize * 50 / rowHeight)
  481. if (isMouseScrollDown) {
  482. if (scrollTop + clientHeight < scrollHeight) {
  483. this.scrollTo(scrollLeft, scrollTop + topSize)
  484. startMouseScroll(evnt)
  485. handleChecked(evnt)
  486. } else {
  487. stopMouseScroll()
  488. }
  489. } else {
  490. if (scrollTop) {
  491. this.scrollTo(scrollLeft, scrollTop - topSize)
  492. startMouseScroll(evnt)
  493. handleChecked(evnt)
  494. } else {
  495. stopMouseScroll()
  496. }
  497. }
  498. }
  499. }, 50)
  500. }
  501. DomTools.addClass($el, 'drag--area')
  502. document.onmousemove = evnt => {
  503. evnt.preventDefault()
  504. evnt.stopPropagation()
  505. const { clientY } = evnt
  506. const { boundingTop } = DomTools.getAbsolutePos(bodyWrapperElem)
  507. // 如果超过可视区,触发滚动
  508. if (clientY < boundingTop) {
  509. isMouseScrollDown = false
  510. mouseScrollSpaceSize = boundingTop - clientY
  511. if (!mouseScrollTimeout) {
  512. startMouseScroll(evnt)
  513. }
  514. } else if (clientY > boundingTop + bodyWrapperElem.clientHeight) {
  515. isMouseScrollDown = true
  516. mouseScrollSpaceSize = clientY - boundingTop - bodyWrapperElem.clientHeight
  517. if (!mouseScrollTimeout) {
  518. startMouseScroll(evnt)
  519. }
  520. } else if (mouseScrollTimeout) {
  521. stopMouseScroll()
  522. }
  523. handleChecked(evnt)
  524. }
  525. document.onmouseup = (evnt) => {
  526. stopMouseScroll()
  527. DomTools.removeClass($el, 'drag--area')
  528. checkboxRangeElem.removeAttribute('style')
  529. document.onmousemove = domMousemove
  530. document.onmouseup = domMouseup
  531. triggerEvent('end', evnt)
  532. }
  533. triggerEvent('start', evnt)
  534. }
  535. },
  536. /**
  537. * 清除所有选中状态
  538. */
  539. _clearChecked () {
  540. const { $refs, editStore, mouseConfig, mouseOpts } = this
  541. const { checked } = editStore
  542. // 在 v3.0 中废弃 mouse-config.checked
  543. if (mouseConfig && mouseOpts.checked) {
  544. const tableBody = $refs.tableBody
  545. checked.rows = []
  546. checked.columns = []
  547. checked.tRows = []
  548. checked.tColumns = []
  549. const { checkBorders } = tableBody.$refs
  550. checkBorders.style.display = 'none'
  551. XEUtils.arrayEach(tableBody.$el.querySelectorAll('.col--checked'), elem => DomTools.removeClass(elem, 'col--checked'))
  552. }
  553. return this.$nextTick()
  554. },
  555. _getMouseSelecteds () {
  556. UtilTools.warn('vxe.error.delFunc', ['getMouseSelecteds', 'getSelectedCell'])
  557. return this.getSelectedCell()
  558. },
  559. _getMouseCheckeds () {
  560. // UtilTools.warn('vxe.error.delFunc', ['getMouseCheckeds', 'getSelectedRanges'])
  561. return this.getSelectedRanges()
  562. },
  563. /**
  564. * 获取选中的单元格
  565. */
  566. _getSelectedCell () {
  567. const { args, column } = this.editStore.selected
  568. if (args && column) {
  569. return Object.assign({}, args)
  570. }
  571. return null
  572. },
  573. /**
  574. * 获取所有选中的单元格
  575. */
  576. _getSelectedRanges () {
  577. const { checked } = this.editStore
  578. const { rowNodes = [] } = checked
  579. let columns = []
  580. let rows = []
  581. if (rowNodes && rowNodes.length) {
  582. rows = rowNodes.map(list => this.getRowNode(list[0].parentNode).item)
  583. columns = rowNodes[0].map(cell => this.getColumnNode(cell).item)
  584. }
  585. return {
  586. columns,
  587. rows,
  588. rowNodes
  589. }
  590. },
  591. /**
  592. * 处理所有选中
  593. */
  594. handleChecked (rowNodes) {
  595. const { checked } = this.editStore
  596. this.clearChecked()
  597. let cWidth = -2
  598. let cHeight = -2
  599. let offsetTop = 0
  600. let offsetLeft = 0
  601. XEUtils.arrayEach(rowNodes, (rows, rowIndex) => {
  602. const isTop = rowIndex === 0
  603. XEUtils.arrayEach(rows, (elem, colIndex) => {
  604. const isLeft = colIndex === 0
  605. if (isLeft && isTop) {
  606. offsetTop = elem.offsetTop
  607. offsetLeft = elem.offsetLeft
  608. }
  609. if (isTop) {
  610. cWidth += elem.offsetWidth
  611. }
  612. if (isLeft) {
  613. cHeight += elem.offsetHeight
  614. }
  615. DomTools.addClass(elem, 'col--checked')
  616. })
  617. })
  618. const { checkBorders, checkTop, checkRight, checkBottom, checkLeft } = this.$refs.tableBody.$refs
  619. checkBorders.style.display = 'block'
  620. Object.assign(checkTop.style, {
  621. top: `${offsetTop}px`,
  622. left: `${offsetLeft}px`,
  623. width: `${cWidth}px`
  624. })
  625. Object.assign(checkRight.style, {
  626. top: `${offsetTop}px`,
  627. left: `${offsetLeft + cWidth}px`,
  628. height: `${cHeight}px`
  629. })
  630. Object.assign(checkBottom.style, {
  631. top: `${offsetTop + cHeight}px`,
  632. left: `${offsetLeft}px`,
  633. width: `${cWidth}px`
  634. })
  635. Object.assign(checkLeft.style, {
  636. top: `${offsetTop}px`,
  637. left: `${offsetLeft}px`,
  638. height: `${cHeight}px`
  639. })
  640. checked.rowNodes = rowNodes
  641. },
  642. handleAllChecked (evnt) {
  643. const { tableData, visibleColumn, mouseConfig, mouseOpts, elemStore } = this
  644. // 在 v3.0 中废弃 mouse-config.checked
  645. if (mouseConfig && mouseOpts.checked) {
  646. evnt.preventDefault()
  647. const headerListElem = elemStore['main-header-list']
  648. const headerList = headerListElem.children
  649. const bodyList = elemStore['main-body-list'].children
  650. // v3.0 废弃 type=index
  651. const column = XEUtils.find(visibleColumn, column => column.type === 'seq' || column.type === 'index') || visibleColumn[0]
  652. const cell = headerListElem.querySelector(`.${column.id}`)
  653. const firstTrElem = bodyList[0]
  654. const lastTrElem = bodyList[bodyList.length - 1]
  655. const firstCell = firstTrElem.querySelector(`.${column.id}`)
  656. const params = {
  657. $table: this,
  658. rowIndex: 0,
  659. row: tableData[0],
  660. column: XEUtils.find(visibleColumn, column => column.property)
  661. }
  662. params.columnIndex = this.getColumnIndex(params.column)
  663. params.cell = this.getCell(params.row, params.column)
  664. this.handleSelected(params, evnt)
  665. this.handleHeaderChecked(DomTools.getRowNodes(headerList, DomTools.getCellNodeIndex(cell.nextElementSibling), DomTools.getCellNodeIndex(cell.parentNode.lastElementChild)))
  666. this.handleIndexChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell), DomTools.getCellNodeIndex(lastTrElem.querySelector(`.${column.id}`))))
  667. this.handleChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(firstCell.nextElementSibling), DomTools.getCellNodeIndex(lastTrElem.lastElementChild)))
  668. }
  669. },
  670. handleIndexChecked (rowNodes) {
  671. const { indexs } = this.editStore
  672. this.clearIndexChecked()
  673. XEUtils.arrayEach(rowNodes, rows => {
  674. XEUtils.arrayEach(rows, elem => {
  675. DomTools.addClass(elem, 'col--seq-checked')
  676. })
  677. })
  678. indexs.rowNodes = rowNodes
  679. },
  680. _clearIndexChecked () {
  681. const { elemStore } = this
  682. const bodyElem = elemStore['main-body-list']
  683. XEUtils.arrayEach(bodyElem.querySelectorAll('.col--seq-checked'), elem => DomTools.removeClass(elem, 'col--seq-checked'))
  684. return this.$nextTick()
  685. },
  686. handleHeaderChecked (rowNodes) {
  687. const { titles } = this.editStore
  688. this.clearHeaderChecked()
  689. XEUtils.arrayEach(rowNodes, rows => {
  690. XEUtils.arrayEach(rows, elem => {
  691. DomTools.addClass(elem, 'col--title-checked')
  692. })
  693. })
  694. titles.rowNodes = rowNodes
  695. },
  696. _clearHeaderChecked () {
  697. const { elemStore } = this
  698. const headerElem = elemStore['main-header-list']
  699. if (headerElem) {
  700. XEUtils.arrayEach(headerElem.querySelectorAll('.col--title-checked'), elem => DomTools.removeClass(elem, 'col--title-checked'))
  701. }
  702. return this.$nextTick()
  703. },
  704. /**
  705. * 清空已复制的内容
  706. */
  707. _clearCopyed () {
  708. const { $refs, editStore, keyboardConfig } = this
  709. const { copyed } = editStore
  710. if (keyboardConfig && keyboardConfig.isCut) {
  711. const tableBody = $refs.tableBody
  712. const { copyBorders } = $refs.tableBody.$refs
  713. copyed.cut = false
  714. copyed.rows = []
  715. copyed.columns = []
  716. copyBorders.style.display = 'none'
  717. XEUtils.arrayEach(tableBody.$el.querySelectorAll('.col--copyed'), elem => DomTools.removeClass(elem, 'col--copyed'))
  718. }
  719. return this.$nextTick()
  720. },
  721. /**
  722. * 处理复制
  723. */
  724. handleCopyed (cut) {
  725. const { tableData, tableColumn, editStore } = this
  726. const { copyed, checked } = editStore
  727. const rowNodes = checked.rowNodes
  728. this.clearCopyed()
  729. let cWidth = -3
  730. let cHeight = -3
  731. let offsetTop = 0
  732. let offsetLeft = 0
  733. let columns = []
  734. let rows = []
  735. if (rowNodes.length) {
  736. const firstRows = rowNodes[0]
  737. const { rowIndex, columnIndex } = DomTools.getCellNodeIndex(firstRows[0])
  738. columns = tableColumn.slice(columnIndex, columnIndex + firstRows.length)
  739. rows = tableData.slice(rowIndex, rowIndex + rowNodes.length)
  740. }
  741. XEUtils.arrayEach(rowNodes, (rows, rowIndex) => {
  742. const isTop = rowIndex === 0
  743. XEUtils.arrayEach(rows, (elem, colIndex) => {
  744. const isLeft = colIndex === 0
  745. if (isLeft && isTop) {
  746. offsetTop = elem.offsetTop
  747. offsetLeft = elem.offsetLeft
  748. }
  749. if (isTop) {
  750. cWidth += elem.offsetWidth
  751. }
  752. if (isLeft) {
  753. cHeight += elem.offsetHeight
  754. }
  755. DomTools.addClass(elem, 'col--copyed')
  756. })
  757. })
  758. const { copyBorders, copyTop, copyRight, copyBottom, copyLeft } = this.$refs.tableBody.$refs
  759. copyBorders.style.display = 'block'
  760. Object.assign(copyTop.style, {
  761. top: `${offsetTop}px`,
  762. left: `${offsetLeft}px`,
  763. width: `${cWidth}px`
  764. })
  765. Object.assign(copyRight.style, {
  766. top: `${offsetTop}px`,
  767. left: `${offsetLeft + cWidth}px`,
  768. height: `${cHeight}px`
  769. })
  770. Object.assign(copyBottom.style, {
  771. top: `${offsetTop + cHeight}px`,
  772. left: `${offsetLeft}px`,
  773. width: `${cWidth}px`
  774. })
  775. Object.assign(copyLeft.style, {
  776. top: `${offsetTop}px`,
  777. left: `${offsetLeft}px`,
  778. height: `${cHeight}px`
  779. })
  780. copyed.cut = cut
  781. copyed.rows = rows
  782. copyed.columns = columns
  783. copyed.rowNodes = rowNodes
  784. },
  785. /**
  786. * 处理粘贴
  787. */
  788. handlePaste () {
  789. const { tableData, visibleColumn, editStore, elemStore } = this
  790. const { copyed, selected } = editStore
  791. const { cut, rows, columns } = copyed
  792. if (rows.length && columns.length && selected.row && selected.column) {
  793. const { rowIndex, columnIndex } = selected.args
  794. XEUtils.arrayEach(rows, (row, rIndex) => {
  795. const offsetRow = tableData[rowIndex + rIndex]
  796. if (offsetRow) {
  797. XEUtils.arrayEach(columns, (column, cIndex) => {
  798. const offsetColumn = visibleColumn[columnIndex + cIndex]
  799. if (offsetColumn) {
  800. UtilTools.setCellValue(offsetRow, offsetColumn, UtilTools.getCellValue(row, column))
  801. }
  802. if (cut) {
  803. UtilTools.setCellValue(row, column, null)
  804. }
  805. })
  806. }
  807. })
  808. if (cut) {
  809. this.clearCopyed()
  810. }
  811. const bodyList = elemStore['main-body-list'].children
  812. const cell = selected.args.cell
  813. const trElem = cell.parentNode
  814. const colIndex = XEUtils.arrayIndexOf(trElem.children, cell)
  815. const rIndex = XEUtils.arrayIndexOf(bodyList, trElem)
  816. const targetTrElem = bodyList[rIndex + rows.length - 1]
  817. const targetCell = targetTrElem.children[colIndex + columns.length - 1]
  818. this.handleChecked(DomTools.getRowNodes(bodyList, DomTools.getCellNodeIndex(cell), DomTools.getCellNodeIndex(targetCell)))
  819. }
  820. }
  821. }
  822. }