VSeed, an elegant data composer, transforming complexity into simplicity.
!!!###!!!title=4.5 Event to State Update Process——VisActor/VTable Contributing Documents!!!###!!!!!!###!!!description=---title: 4.5 Event to State Update Process
key words: VisActor,VChart,VTable,VStrory,VMind,VGrammar,VRender,Visualization,Chart,Data,Table,Graph,Gis,LLM---
!!!###!!!
Introduction
VTable divides the implementation of interactive effects into three modules for processing, which are:
State module stateManager: The state module is responsible for maintaining the current state of various interactions in the table, and changes in state will lead to the re-rendering of the scene tree;
The event module is eventManager: The event module is responsible for listening to events and changing states based on different events;
Scene tree scenegraph: The scene tree is responsible for re-rendering the table, which is the final step in achieving interaction;
Next, we will look at the process of updating events to states from six common interactions. \r
Interaction Implementation
Cell select
Core State
In the state module, the core state value that determines whether a cell is selected is select.ranges. VTable uses this field to determine if the current cell is selected. Changing select.ranges can change the selection state of the cell.
In the process of the select cell in the state module, the core difference between single-select cells and box-select cells lies in the difference of stateManger.interactionState:
stateManager.interactionState === 'grabing' indicates the process of selecting cells is currently ongoing
stateManager.interactionState === 'default' indicates a single selection cell
The update process regarding the selection state in state management is as follows: \r
updateSelectPos
Scrollbar Scrolling
The scrolling effect mainly listens to the wheel event. By using the wheel event, it changes the current scrollbar state, updates scrollTop and scrollLeft, and adjusts the x, y coordinates of the table to achieve the scrolling effect.
The state records the index and coordinates of the current dragged row and column. In the subsequent actual dragging, only the corresponding row or column of columnResize.col or rowResize.row will be adjusted.
Adjusting Process
Receive pointerdown event, checked by the event module to see if it enters the drag to adjust column width. If confirmed to enter row height and column width adjustment, update state.interactionState to grabing;
First, based on the click coordinates provided by pointerdown, calculate whether the drag hotspot is hit. If it is hit, return the corresponding row and column index.
Based on the row and column index, initialize the state of columnResize and rowResize through the state module, triggering the next frame rendering; \r
Handle the pointermove event, and determine whether it is dragging a row or a column through the state module;
If interactionState === 'grabing', it means currently in the interaction of dragging row height or column width;
Determine whether the current action is dragging row height or column width by columnResize.resizing and rowResize.resizing;
Use the event module as an intermediary to handle drag events eventManager.dealColumnResize(x, y);
Trigger RESIZE_COLUMN and RESIZE_ROW events; \r
const globalPointermoveCallback = (e: MouseEvent) => {
// ... const { x, y } = table._getMouseAbstractPoint(e, false);
if (stateManager.interactionState === InteractionState.grabing) {
if (stateManager.isResizeCol()) {
eventManager.dealColumnResize(x, y);
if ((table as any).hasListeners(TABLE_EVENT_TYPE.RESIZE_COLUMN)) {
table.fireListeners(TABLE_EVENT_TYPE.RESIZE_COLUMN, {
col: table.stateManager.columnResize.col,
colWidth: table.getColWidth(table.stateManager.columnResize.col)
});
}
} elseif (stateManager.isResizeRow()) {
eventManager.dealRowResize(x, y);
if ((table as any).hasListeners(TABLE_EVENT_TYPE.RESIZE_ROW)) {
table.fireListeners(TABLE_EVENT_TYPE.RESIZE_ROW, {
row: table.stateManager.rowResize.row,
rowHeight: table.getRowHeight(table.stateManager.rowResize.row)
});
}
}
}
// ... }
document.body.addEventListener('pointermove', globalPointermoveCallback);
Handle the pointermove event through the state module, and update the column width/row height at the corresponding index of columnResize.col and rowResize.row using the current pointer coordinates.
columnRemove stores the original index and target index of the dragged row or column, as well as a flag indicating whether it is in motion. By changing colTarget and rowTarget, you can achieve the function of replacing the selected row/column to the target position.
Processing Flow
Dragging to change rows and columns also relies on three events to complete: pointerdown, pointermove, pointerup
Flowchart
Fixed Column
VTable provides a built-in frozen column operation, which can be enabled by configuring allowFrozenColCount.
Core State
VTable maintains the current actual number of frozen columns through the tableInstance.internalProps.frozenColCount state, and internally adjusts the number of frozen columns on the left side based on this field, applying special styles.
Processing Flow
The operation of freezing columns is mainly implemented by pointertap and the custom event ICON_CLICK.
In the event module, it is determined by eventArgsSet whether the icon element is clicked. If the clicked element is an icon, the custom event ICON_CLICK is triggered.
The ICON_CLICK event is registered as early as the event module initialization, and the ICON_CLICK event will determine whether the current clicked icon type is frozen;
The status module handles the click fronzen event. Based on the index of the currently clicked column, it updates this.internalProps.frozenColCount. If the currently clicked column is the same as the frozenColCount maintained in the state, it resets frozenColCount to 0; if different, it updates frozenColCount to col.;