cbf74b7afc5ebcf504da8938607da3977e6826653248016472fd9811691eda48da8cab69556abcb4ea10a188280a5d4a5c0ec105dca38c80f71db029a45757 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { getCurrentInstance, ref, unref } from 'vue';
  2. import { isNull } from 'lodash-unified';
  3. import { getRowIdentity } from '../util.mjs';
  4. function useCurrent(watcherData) {
  5. const instance = getCurrentInstance();
  6. const _currentRowKey = ref(null);
  7. const currentRow = ref(null);
  8. const setCurrentRowKey = (key) => {
  9. instance.store.assertRowKey();
  10. _currentRowKey.value = key;
  11. setCurrentRowByKey(key);
  12. };
  13. const restoreCurrentRowKey = () => {
  14. _currentRowKey.value = null;
  15. };
  16. const setCurrentRowByKey = (key) => {
  17. var _a;
  18. const { data, rowKey } = watcherData;
  19. let _currentRow = null;
  20. if (rowKey.value) {
  21. _currentRow = (_a = (unref(data) || []).find((item) => getRowIdentity(item, rowKey.value) === key)) != null ? _a : null;
  22. }
  23. currentRow.value = _currentRow != null ? _currentRow : null;
  24. instance.emit("current-change", currentRow.value, null);
  25. };
  26. const updateCurrentRow = (_currentRow) => {
  27. const oldCurrentRow = currentRow.value;
  28. if (_currentRow && _currentRow !== oldCurrentRow) {
  29. currentRow.value = _currentRow;
  30. instance.emit("current-change", currentRow.value, oldCurrentRow);
  31. return;
  32. }
  33. if (!_currentRow && oldCurrentRow) {
  34. currentRow.value = null;
  35. instance.emit("current-change", null, oldCurrentRow);
  36. }
  37. };
  38. const updateCurrentRowData = () => {
  39. const rowKey = watcherData.rowKey.value;
  40. const data = watcherData.data.value || [];
  41. const oldCurrentRow = currentRow.value;
  42. if (oldCurrentRow && !data.includes(oldCurrentRow)) {
  43. if (rowKey) {
  44. const currentRowKey = getRowIdentity(oldCurrentRow, rowKey);
  45. setCurrentRowByKey(currentRowKey);
  46. } else {
  47. currentRow.value = null;
  48. }
  49. if (isNull(currentRow.value)) {
  50. instance.emit("current-change", null, oldCurrentRow);
  51. }
  52. } else if (_currentRowKey.value) {
  53. setCurrentRowByKey(_currentRowKey.value);
  54. restoreCurrentRowKey();
  55. }
  56. };
  57. return {
  58. setCurrentRowKey,
  59. restoreCurrentRowKey,
  60. setCurrentRowByKey,
  61. updateCurrentRow,
  62. updateCurrentRowData,
  63. states: {
  64. _currentRowKey,
  65. currentRow
  66. }
  67. };
  68. }
  69. export { useCurrent as default };
  70. //# sourceMappingURL=current.mjs.map