misskey/packages/frontend/src/components/grid/MkGrid.vue

1339 lines
41 KiB
Vue
Raw Normal View History

2024-02-27 12:10:09 +00:00
<!--
SPDX-FileCopyrightText: syuilo and other misskey contributors
SPDX-License-Identifier: AGPL-3.0-only
-->
2024-01-22 01:37:44 +00:00
<template>
2024-07-07 08:10:10 +00:00
<div
2024-01-27 03:02:50 +00:00
ref="rootEl"
2024-07-07 08:10:10 +00:00
class="mk_grid_border"
:class="[$style.grid]"
2024-02-01 11:59:30 +00:00
@mousedown.prevent="onMouseDown"
2024-01-26 13:16:17 +00:00
@keydown="onKeyDown"
2024-02-01 11:59:30 +00:00
@contextmenu.prevent.stop="onContextMenu"
2024-01-23 06:34:52 +00:00
>
2024-07-07 08:10:10 +00:00
<div class="mk_grid_thead">
2024-01-23 06:34:52 +00:00
<MkHeaderRow
:columns="columns"
2024-02-10 13:31:04 +00:00
:gridSetting="rowSetting"
2024-01-23 06:34:52 +00:00
:bus="bus"
2024-01-26 13:16:17 +00:00
@operation:beginWidthChange="onHeaderCellWidthBeginChange"
@operation:endWidthChange="onHeaderCellWidthEndChange"
@operation:widthLargest="onHeaderCellWidthLargest"
@change:width="onHeaderCellChangeWidth"
@change:contentSize="onHeaderCellChangeContentSize"
2024-01-23 06:34:52 +00:00
/>
2024-07-07 08:10:10 +00:00
</div>
<div class="mk_grid_tbody">
2024-01-23 06:34:52 +00:00
<MkDataRow
v-for="row in rows"
2024-02-04 23:35:26 +00:00
v-show="row.using"
2024-01-23 06:34:52 +00:00
:key="row.index"
:row="row"
2024-02-03 03:19:37 +00:00
:cells="cells[row.index].cells"
2024-02-10 13:31:04 +00:00
:setting="rowSetting"
2024-01-23 06:34:52 +00:00
:bus="bus"
2024-02-04 23:35:26 +00:00
:using="row.using"
2024-01-26 13:16:17 +00:00
@operation:beginEdit="onCellEditBegin"
@operation:endEdit="onCellEditEnd"
@change:value="onChangeCellValue"
@change:contentSize="onChangeCellContentSize"
2024-01-23 06:34:52 +00:00
/>
2024-07-07 08:10:10 +00:00
</div>
</div>
2024-01-22 01:37:44 +00:00
</template>
<script setup lang="ts">
2024-02-04 23:35:26 +00:00
import { computed, onMounted, ref, toRefs, watch } from 'vue';
2024-02-10 13:31:04 +00:00
import { DataSource, GridEventEmitter, GridSetting, GridState, Size } from '@/components/grid/grid.js';
2024-01-23 06:34:52 +00:00
import MkDataRow from '@/components/grid/MkDataRow.vue';
import MkHeaderRow from '@/components/grid/MkHeaderRow.vue';
2024-02-03 02:17:16 +00:00
import { cellValidation } from '@/components/grid/cell-validators.js';
2024-02-04 23:35:26 +00:00
import { CELL_ADDRESS_NONE, CellAddress, CellValue, createCell, GridCell, resetCell } from '@/components/grid/cell.js';
2024-02-20 11:25:42 +00:00
import {
copyGridDataToClipboard,
equalCellAddress,
getCellAddress,
getCellElement,
pasteToGridFromClipboard,
removeDataFromGrid,
} from '@/components/grid/grid-utils.js';
2024-01-30 23:39:56 +00:00
import { MenuItem } from '@/types/menu.js';
import * as os from '@/os.js';
2024-02-15 22:41:29 +00:00
import { GridContext, GridEvent } from '@/components/grid/grid-event.js';
2024-02-10 13:31:04 +00:00
import { createColumn, GridColumn } from '@/components/grid/column.js';
import { createRow, defaultGridRowSetting, GridRow, GridRowSetting, resetRow } from '@/components/grid/row.js';
2024-02-18 15:00:16 +00:00
import { handleKeyEvent } from '@/scripts/key-event.js';
2024-01-22 01:37:44 +00:00
2024-02-03 03:19:37 +00:00
type RowHolder = {
2024-02-04 23:35:26 +00:00
row: GridRow,
2024-02-03 03:19:37 +00:00
cells: GridCell[],
origin: DataSource,
}
2024-02-04 23:35:26 +00:00
const emit = defineEmits<{
2024-02-22 10:24:47 +00:00
(ev: 'event', event: GridEvent, context: GridContext): void;
2024-02-04 23:35:26 +00:00
}>();
const props = defineProps<{
2024-02-10 13:31:04 +00:00
settings: GridSetting,
2024-01-22 01:37:44 +00:00
data: DataSource[]
2024-01-27 03:02:50 +00:00
}>();
2024-02-04 23:35:26 +00:00
// non-reactive
2024-07-07 08:10:10 +00:00
2024-02-10 13:31:04 +00:00
const rowSetting: Required<GridRowSetting> = {
...defaultGridRowSetting,
...props.settings.row,
2024-02-04 23:35:26 +00:00
};
// non-reactive
2024-07-07 08:10:10 +00:00
2024-02-10 13:31:04 +00:00
const columnSettings = props.settings.cols;
2024-02-04 23:35:26 +00:00
2024-02-15 22:41:29 +00:00
// non-reactive
const cellSettings = props.settings.cells ?? {};
2024-02-04 23:35:26 +00:00
const { data } = toRefs(props);
2024-02-02 00:32:49 +00:00
// #region Event Definitions
// region Event Definitions
2024-01-30 00:53:47 +00:00
/**
2024-02-01 11:59:30 +00:00
* grid -> 各子コンポーネントのイベント経路を担う{@link GridEventEmitter}おもにpropsでの伝搬が難しいイベントを伝搬するために使用する
2024-01-30 00:53:47 +00:00
* 子コンポーネント -> gridのイベントでは原則使用せず{@link emit}を使用する
*/
2024-01-26 13:16:17 +00:00
const bus = new GridEventEmitter();
2024-01-30 00:53:47 +00:00
/**
* テーブルコンポーネントのリサイズイベントを監視するための{@link ResizeObserver}
* 表示切替を検知しサイズの再計算要求を発行するために使用するマウント時にコンテンツが表示されていない場合初手のサイズの自動計算が正常に働かないため
*
* {@link setTimeout}を経由している理由は{@link onResize}の中でサイズ再計算要求サイズ変更が発生するとループとみなされ
2024-01-30 01:07:54 +00:00
* ResizeObserver loop completed with undelivered notifications.という警告が発生するため再計算が完全に終われば通知は発生しなくなるので実際にはループしない
2024-01-30 00:53:47 +00:00
*
* @see {@link onResize}
*/
const resizeObserver = new ResizeObserver((entries) => setTimeout(() => onResize(entries)));
2024-01-26 13:16:17 +00:00
2024-01-27 03:02:50 +00:00
const rootEl = ref<InstanceType<typeof HTMLTableElement>>();
2024-02-02 00:32:49 +00:00
/**
* グリッドの最も上位にある状態
*/
const state = ref<GridState>('normal');
/**
2024-02-04 23:35:26 +00:00
* グリッドの列定義列定義の元の設定値は非リアクティブなので初期値を生成して以降は変更しない
2024-02-02 00:32:49 +00:00
*/
2024-02-04 23:35:26 +00:00
const columns = ref<GridColumn[]>(columnSettings.map(createColumn));
2024-02-02 00:32:49 +00:00
/**
* グリッドの行定義propsで受け取った{@link data}をもとに{@link refreshData}で再計算される
*/
2024-01-23 06:34:52 +00:00
const rows = ref<GridRow[]>([]);
2024-02-02 00:32:49 +00:00
/**
* グリッドのセル定義propsで受け取った{@link data}をもとに{@link refreshData}で再計算される
*/
2024-02-03 03:19:37 +00:00
const cells = ref<RowHolder[]>([]);
2024-02-02 00:32:49 +00:00
/**
* mousemoveイベントが発生した際にイベントから取得したセルアドレスを保持するための変数
* セルアドレスが変わった瞬間にイベントを起こしたい時のために前回値として使用する
*/
2024-01-26 13:16:17 +00:00
const previousCellAddress = ref<CellAddress>(CELL_ADDRESS_NONE);
2024-02-02 00:32:49 +00:00
/**
* 編集中のセルのアドレスを保持するための変数
*/
2024-01-26 13:16:17 +00:00
const editingCellAddress = ref<CellAddress>(CELL_ADDRESS_NONE);
2024-02-02 00:32:49 +00:00
/**
* 列の範囲選択をする際の開始地点となるインデックスを保持するための変数
* この開始地点からマウスが動いた地点までの範囲を選択する
*/
2024-01-26 13:16:17 +00:00
const firstSelectionColumnIdx = ref<number>(CELL_ADDRESS_NONE.col);
2024-02-02 00:32:49 +00:00
/**
* 行の範囲選択をする際の開始地点となるインデックスを保持するための変数
* この開始地点からマウスが動いた地点までの範囲を選択する
*/
2024-01-26 13:16:17 +00:00
const firstSelectionRowIdx = ref<number>(CELL_ADDRESS_NONE.row);
2024-02-02 00:32:49 +00:00
/**
* 選択状態のセルを取得するための計算プロパティ選択状態とは{@link GridCell.selected}がtrueのセルのこと
*/
2024-01-26 03:48:29 +00:00
const selectedCell = computed(() => {
2024-02-03 03:19:37 +00:00
const selected = cells.value.flatMap(it => it.cells).filter(it => it.selected);
2024-01-26 03:48:29 +00:00
return selected.length > 0 ? selected[0] : undefined;
});
2024-02-02 00:32:49 +00:00
/**
* 範囲選択状態のセルを取得するための計算プロパティ範囲選択状態とは{@link GridCell.ranged}がtrueのセルのこと
*/
2024-02-03 03:19:37 +00:00
const rangedCells = computed(() => cells.value.flatMap(it => it.cells).filter(it => it.ranged));
2024-02-02 00:32:49 +00:00
/**
* 範囲選択状態のセルの範囲を取得するための計算プロパティ左上のセル番地と右下のセル番地を計算する
*/
2024-01-27 03:02:50 +00:00
const rangedBounds = computed(() => {
const _cells = rangedCells.value;
2024-02-02 00:32:49 +00:00
const _cols = _cells.map(it => it.address.col);
const _rows = _cells.map(it => it.address.row);
2024-02-01 11:59:30 +00:00
2024-01-27 03:02:50 +00:00
const leftTop = {
2024-02-02 00:32:49 +00:00
col: Math.min(..._cols),
row: Math.min(..._rows),
2024-01-27 03:02:50 +00:00
};
const rightBottom = {
2024-02-02 00:32:49 +00:00
col: Math.max(..._cols),
row: Math.max(..._rows),
2024-01-27 03:02:50 +00:00
};
2024-02-01 11:59:30 +00:00
2024-01-27 03:02:50 +00:00
return {
leftTop,
rightBottom,
};
});
2024-02-02 00:32:49 +00:00
/**
* グリッドの中で使用可能なセルの範囲を取得するための計算プロパティ左上のセル番地と右下のセル番地を計算する
*/
2024-01-27 03:02:50 +00:00
const availableBounds = computed(() => {
const leftTop = {
col: 0,
row: 0,
};
const rightBottom = {
col: Math.max(...columns.value.map(it => it.index)),
2024-02-04 23:35:26 +00:00
row: Math.max(...rows.value.filter(it => it.using).map(it => it.index)),
2024-01-27 03:02:50 +00:00
};
return { leftTop, rightBottom };
});
2024-02-02 00:32:49 +00:00
/**
* 範囲選択状態の行を取得するための計算プロパティ範囲選択状態とは{@link GridRow.ranged}がtrueの行のこと
*/
2024-01-29 04:41:13 +00:00
const rangedRows = computed(() => rows.value.filter(it => it.ranged));
2024-01-22 01:37:44 +00:00
2024-02-04 23:35:26 +00:00
const lastLine = computed(() => rows.value.filter(it => it.using).length - 1);
2024-02-02 00:32:49 +00:00
// endregion
// #endregion
2024-02-03 02:17:16 +00:00
watch(data, patchData, { deep: true });
2024-01-27 03:02:50 +00:00
2024-01-23 06:34:52 +00:00
if (_DEV_) {
2024-01-30 00:53:47 +00:00
watch(state, (value, oldValue) => {
console.log(`[grid][state] ${oldValue} -> ${value}`);
2024-01-23 06:34:52 +00:00
});
}
2024-02-02 00:32:49 +00:00
// #region Event Handlers
// region Event Handlers
2024-01-30 00:53:47 +00:00
function onResize(entries: ResizeObserverEntry[]) {
if (entries.length !== 1 || entries[0].target !== rootEl.value) {
return;
}
const contentRect = entries[0].contentRect;
if (_DEV_) {
console.log(`[grid][resize] contentRect: ${contentRect.width}x${contentRect.height}`);
}
switch (state.value) {
case 'hidden': {
if (contentRect.width > 0 && contentRect.height > 0) {
2024-01-30 01:07:54 +00:00
// 先に状態を変更しておき、再計算要求が複数回走らないようにする
2024-01-30 00:53:47 +00:00
state.value = 'normal';
// 選択状態が狂うかもしれないので解除しておく
2024-02-02 00:32:49 +00:00
unSelectionRangeAll();
2024-01-30 22:25:26 +00:00
// 再計算要求を発行。各セル側で最低限必要な横幅を算出し、emitで返してくるようになっている
2024-01-30 00:53:47 +00:00
bus.emit('forceRefreshContentSize');
}
break;
}
default: {
if (contentRect.width === 0 || contentRect.height === 0) {
state.value = 'hidden';
}
break;
}
}
}
2024-01-26 13:16:17 +00:00
function onKeyDown(ev: KeyboardEvent) {
2024-02-18 15:00:16 +00:00
const { ctrlKey, shiftKey, code } = ev;
if (_DEV_) {
console.log(`[grid][key] ctrl: ${ctrlKey}, shift: ${shiftKey}, code: ${code}`);
2024-02-02 00:32:49 +00:00
}
2024-02-18 15:00:16 +00:00
function updateSelectionRange(newBounds: { leftTop: CellAddress, rightBottom: CellAddress }) {
unSelectionOutOfRange(newBounds.leftTop, newBounds.rightBottom);
expandCellRange(newBounds.leftTop, newBounds.rightBottom);
2024-01-27 03:02:50 +00:00
}
2024-01-26 13:16:17 +00:00
switch (state.value) {
case 'normal': {
2024-01-27 03:02:50 +00:00
ev.preventDefault();
2024-02-20 11:25:42 +00:00
const selectedCellAddress = selectedCell.value?.address ?? CELL_ADDRESS_NONE;
2024-02-18 15:00:16 +00:00
const max = availableBounds.value;
const bounds = rangedBounds.value;
handleKeyEvent(ev, [
2024-02-20 11:25:42 +00:00
{
code: 'Delete', handler: () => {
if (rangedRows.value.length > 0) {
if (rowSetting.events.delete) {
rowSetting.events.delete(rangedRows.value);
}
} else {
const context = createContext();
removeDataFromGrid(context, (cell) => {
emitCellValue(cell, undefined);
});
}
},
},
{
code: 'KeyC', modifiers: ['Control'], handler: () => {
const context = createContext();
copyGridDataToClipboard(data.value, context);
},
},
{
code: 'KeyV', modifiers: ['Control'], handler: async () => {
const _cells = cells.value;
const context = createContext();
await pasteToGridFromClipboard(context, (row, col, parsedValue) => {
emitCellValue(_cells[row.index].cells[col.index], parsedValue);
});
},
},
2024-02-18 15:00:16 +00:00
{
code: 'ArrowRight', modifiers: ['Control', 'Shift'], handler: () => {
updateSelectionRange({
leftTop: { col: selectedCellAddress.col, row: bounds.leftTop.row },
rightBottom: { col: max.rightBottom.col, row: bounds.rightBottom.row },
});
},
},
{
code: 'ArrowLeft', modifiers: ['Control', 'Shift'], handler: () => {
updateSelectionRange({
leftTop: { col: max.leftTop.col, row: bounds.leftTop.row },
rightBottom: { col: selectedCellAddress.col, row: bounds.rightBottom.row },
});
},
},
{
code: 'ArrowUp', modifiers: ['Control', 'Shift'], handler: () => {
updateSelectionRange({
leftTop: { col: bounds.leftTop.col, row: max.leftTop.row },
rightBottom: { col: bounds.rightBottom.col, row: selectedCellAddress.row },
});
},
},
{
code: 'ArrowDown', modifiers: ['Control', 'Shift'], handler: () => {
updateSelectionRange({
leftTop: { col: bounds.leftTop.col, row: selectedCellAddress.row },
rightBottom: { col: bounds.rightBottom.col, row: max.rightBottom.row },
});
},
},
{
code: 'ArrowRight', modifiers: ['Shift'], handler: () => {
updateSelectionRange({
leftTop: {
col: bounds.leftTop.col < selectedCellAddress.col
? bounds.leftTop.col + 1
: selectedCellAddress.col,
row: bounds.leftTop.row,
},
rightBottom: {
col: (bounds.rightBottom.col > selectedCellAddress.col || bounds.leftTop.col === selectedCellAddress.col)
? bounds.rightBottom.col + 1
: selectedCellAddress.col,
row: bounds.rightBottom.row,
},
});
},
},
{
code: 'ArrowLeft', modifiers: ['Shift'], handler: () => {
updateSelectionRange({
leftTop: {
col: (bounds.leftTop.col < selectedCellAddress.col || bounds.rightBottom.col === selectedCellAddress.col)
? bounds.leftTop.col - 1
: selectedCellAddress.col,
row: bounds.leftTop.row,
},
rightBottom: {
col: bounds.rightBottom.col > selectedCellAddress.col
? bounds.rightBottom.col - 1
: selectedCellAddress.col,
row: bounds.rightBottom.row,
},
});
},
},
{
code: 'ArrowUp', modifiers: ['Shift'], handler: () => {
updateSelectionRange({
leftTop: {
col: bounds.leftTop.col,
row: (bounds.leftTop.row < selectedCellAddress.row || bounds.rightBottom.row === selectedCellAddress.row)
? bounds.leftTop.row - 1
: selectedCellAddress.row,
},
rightBottom: {
col: bounds.rightBottom.col,
row: bounds.rightBottom.row > selectedCellAddress.row
? bounds.rightBottom.row - 1
: selectedCellAddress.row,
},
});
},
},
{
code: 'ArrowDown', modifiers: ['Shift'], handler: () => {
updateSelectionRange({
leftTop: {
col: bounds.leftTop.col,
row: bounds.leftTop.row < selectedCellAddress.row
? bounds.leftTop.row + 1
: selectedCellAddress.row,
},
rightBottom: {
col: bounds.rightBottom.col,
row: (bounds.rightBottom.row > selectedCellAddress.row || bounds.leftTop.row === selectedCellAddress.row)
? bounds.rightBottom.row + 1
: selectedCellAddress.row,
},
});
},
},
{
code: 'ArrowDown', handler: () => {
selectionCell({ col: selectedCellAddress.col, row: selectedCellAddress.row + 1 });
},
},
{
code: 'ArrowUp', handler: () => {
selectionCell({ col: selectedCellAddress.col, row: selectedCellAddress.row - 1 });
},
},
{
code: 'ArrowRight', handler: () => {
selectionCell({ col: selectedCellAddress.col + 1, row: selectedCellAddress.row });
},
},
{
code: 'ArrowLeft', handler: () => {
selectionCell({ col: selectedCellAddress.col - 1, row: selectedCellAddress.row });
},
},
]);
2024-01-26 13:16:17 +00:00
break;
}
}
}
2024-01-23 06:34:52 +00:00
function onMouseDown(ev: MouseEvent) {
2024-01-30 23:39:56 +00:00
switch (ev.button) {
case 0: {
onLeftMouseDown(ev);
break;
}
case 2: {
onRightMouseDown(ev);
break;
}
}
}
function onLeftMouseDown(ev: MouseEvent) {
2024-07-07 08:10:10 +00:00
const cellAddress = getCellAddress(ev.target as HTMLElement);
2024-01-30 23:39:56 +00:00
if (_DEV_) {
2024-02-01 11:59:30 +00:00
console.log(`[grid][mouse-left] state:${state.value}, button: ${ev.button}, cell: ${cellAddress.row}x${cellAddress.col}`);
2024-01-30 23:39:56 +00:00
}
2024-01-23 06:34:52 +00:00
switch (state.value) {
case 'cellEditing': {
if (availableCellAddress(cellAddress) && !equalCellAddress(editingCellAddress.value, cellAddress)) {
selectionCell(cellAddress);
}
break;
}
case 'normal': {
if (availableCellAddress(cellAddress)) {
2024-02-06 23:39:12 +00:00
if (ev.shiftKey && selectedCell.value && !equalCellAddress(cellAddress, selectedCell.value.address)) {
const selectedCellAddress = selectedCell.value.address;
const leftTop = {
col: Math.min(selectedCellAddress.col, cellAddress.col),
row: Math.min(selectedCellAddress.row, cellAddress.row),
};
const rightBottom = {
col: Math.max(selectedCellAddress.col, cellAddress.col),
row: Math.max(selectedCellAddress.row, cellAddress.row),
};
unSelectionRangeAll();
expandCellRange(leftTop, rightBottom);
cells.value[selectedCellAddress.row].cells[selectedCellAddress.col].selected = true;
} else {
selectionCell(cellAddress);
}
previousCellAddress.value = cellAddress;
2024-01-26 13:16:17 +00:00
registerMouseUp();
registerMouseMove();
2024-01-23 06:34:52 +00:00
state.value = 'cellSelecting';
2024-01-26 13:16:17 +00:00
} else if (isColumnHeaderCellAddress(cellAddress)) {
2024-02-07 10:51:50 +00:00
if (ev.shiftKey) {
const rangedColumnIndexes = rangedCells.value.map(it => it.address.col);
const targetColumnIndexes = [cellAddress.col, ...rangedColumnIndexes];
unSelectionRangeAll();
const leftTop = {
col: Math.min(...targetColumnIndexes),
row: 0,
};
const rightBottom = {
col: Math.max(...targetColumnIndexes),
row: cells.value.length - 1,
};
2024-01-26 13:16:17 +00:00
2024-02-07 10:51:50 +00:00
expandCellRange(leftTop, rightBottom);
if (rangedColumnIndexes.length === 0) {
firstSelectionColumnIdx.value = cellAddress.col;
} else {
if (cellAddress.col > Math.min(...rangedColumnIndexes)) {
firstSelectionColumnIdx.value = Math.min(...rangedColumnIndexes);
} else {
firstSelectionColumnIdx.value = Math.max(...rangedColumnIndexes);
}
}
} else {
unSelectionRangeAll();
const colCells = cells.value.map(row => row.cells[cellAddress.col]);
selectionRange(...colCells.map(cell => cell.address));
firstSelectionColumnIdx.value = cellAddress.col;
}
2024-01-26 13:16:17 +00:00
registerMouseUp();
registerMouseMove();
2024-02-07 10:51:50 +00:00
previousCellAddress.value = cellAddress;
2024-01-26 13:16:17 +00:00
state.value = 'colSelecting';
2024-01-27 03:02:50 +00:00
2024-02-03 11:10:56 +00:00
// フォーカスを当てないとキーイベントが拾えないので
getCellElement(ev.target as HTMLElement)?.focus();
2024-01-26 13:16:17 +00:00
} else if (isRowNumberCellAddress(cellAddress)) {
2024-02-07 10:51:50 +00:00
if (ev.shiftKey) {
const rangedRowIndexes = rangedRows.value.map(it => it.index);
const targetRowIndexes = [cellAddress.row, ...rangedRowIndexes];
unSelectionRangeAll();
2024-01-26 13:16:17 +00:00
2024-02-07 10:51:50 +00:00
const leftTop = {
col: 0,
row: Math.min(...targetRowIndexes),
};
2024-01-26 13:16:17 +00:00
2024-02-07 10:51:50 +00:00
const rightBottom = {
col: Math.min(...cells.value.map(it => it.cells.length - 1)),
row: Math.max(...targetRowIndexes),
};
expandCellRange(leftTop, rightBottom);
expandRowRange(Math.min(...targetRowIndexes), Math.max(...targetRowIndexes));
if (rangedRowIndexes.length === 0) {
firstSelectionRowIdx.value = cellAddress.row;
} else {
if (cellAddress.col > Math.min(...rangedRowIndexes)) {
firstSelectionRowIdx.value = Math.min(...rangedRowIndexes);
} else {
firstSelectionRowIdx.value = Math.max(...rangedRowIndexes);
}
}
} else {
unSelectionRangeAll();
const rowCells = cells.value[cellAddress.row].cells;
selectionRange(...rowCells.map(cell => cell.address));
expandRowRange(cellAddress.row, cellAddress.row);
firstSelectionRowIdx.value = cellAddress.row;
}
2024-02-01 11:59:30 +00:00
2024-01-26 13:16:17 +00:00
registerMouseUp();
registerMouseMove();
2024-02-07 10:51:50 +00:00
previousCellAddress.value = cellAddress;
2024-01-26 13:16:17 +00:00
state.value = 'rowSelecting';
2024-01-27 03:02:50 +00:00
2024-02-03 11:10:56 +00:00
// フォーカスを当てないとキーイベントが拾えないので
getCellElement(ev.target as HTMLElement)?.focus();
2024-01-23 06:34:52 +00:00
}
break;
}
}
}
2024-01-30 23:39:56 +00:00
function onRightMouseDown(ev: MouseEvent) {
2024-07-07 08:10:10 +00:00
const cellAddress = getCellAddress(ev.target as HTMLElement);
2024-01-30 23:39:56 +00:00
if (_DEV_) {
console.log(`[grid][mouse-right] button: ${ev.button}, cell: ${cellAddress.row}x${cellAddress.col}`);
}
switch (state.value) {
case 'normal': {
if (!availableCellAddress(cellAddress)) {
return;
}
const _rangedCells = [...rangedCells.value];
if (!_rangedCells.some(it => equalCellAddress(it.address, cellAddress))) {
// 範囲選択外を右クリックした場合は、範囲選択を解除(範囲選択内であれば範囲選択を維持する)
selectionCell(cellAddress);
}
break;
}
}
}
2024-01-23 06:34:52 +00:00
function onMouseMove(ev: MouseEvent) {
2024-01-27 03:02:50 +00:00
ev.preventDefault();
2024-02-01 11:59:30 +00:00
2024-07-07 08:10:10 +00:00
const targetCellAddress = getCellAddress(ev.target as HTMLElement);
2024-02-01 11:59:30 +00:00
if (equalCellAddress(previousCellAddress.value, targetCellAddress)) {
2024-02-20 12:29:56 +00:00
// セルが変わるまでイベントを起こしたくない
2024-02-01 11:59:30 +00:00
return;
}
if (_DEV_) {
console.log(`[grid][mouse-move] button: ${ev.button}, cell: ${targetCellAddress.row}x${targetCellAddress.col}`);
}
2024-01-23 06:34:52 +00:00
switch (state.value) {
case 'cellSelecting': {
2024-01-26 03:48:29 +00:00
const selectedCellAddress = selectedCell.value?.address;
2024-02-20 12:29:56 +00:00
if (!availableCellAddress(targetCellAddress) || !selectedCellAddress) {
// 正しいセル範囲ではない
2024-01-26 03:48:29 +00:00
return;
}
const leftTop = {
col: Math.min(targetCellAddress.col, selectedCellAddress.col),
row: Math.min(targetCellAddress.row, selectedCellAddress.row),
};
const rightBottom = {
col: Math.max(targetCellAddress.col, selectedCellAddress.col),
row: Math.max(targetCellAddress.row, selectedCellAddress.row),
};
2024-02-20 12:29:56 +00:00
// 範囲外のセルは選択解除し、範囲内のセルは選択状態にする
2024-01-26 13:16:17 +00:00
unSelectionOutOfRange(leftTop, rightBottom);
2024-01-29 04:41:13 +00:00
expandCellRange(leftTop, rightBottom);
2024-01-26 13:16:17 +00:00
previousCellAddress.value = targetCellAddress;
break;
}
case 'colSelecting': {
if (!isColumnHeaderCellAddress(targetCellAddress) || previousCellAddress.value.col === targetCellAddress.col) {
2024-02-20 12:29:56 +00:00
// セルが変わるまでイベントを起こしたくない
2024-01-26 13:16:17 +00:00
return;
}
const leftTop = {
col: Math.min(targetCellAddress.col, firstSelectionColumnIdx.value),
row: 0,
};
const rightBottom = {
col: Math.max(targetCellAddress.col, firstSelectionColumnIdx.value),
row: cells.value.length - 1,
};
2024-02-20 12:29:56 +00:00
// 範囲外のセルは選択解除し、範囲内のセルは選択状態にする
2024-01-26 13:16:17 +00:00
unSelectionOutOfRange(leftTop, rightBottom);
2024-01-29 04:41:13 +00:00
expandCellRange(leftTop, rightBottom);
2024-01-26 13:16:17 +00:00
previousCellAddress.value = targetCellAddress;
2024-02-03 11:10:56 +00:00
// フォーカスを当てないとキーイベントが拾えないので
getCellElement(ev.target as HTMLElement)?.focus();
2024-01-26 13:16:17 +00:00
break;
}
case 'rowSelecting': {
if (!isRowNumberCellAddress(targetCellAddress) || previousCellAddress.value.row === targetCellAddress.row) {
2024-02-20 12:29:56 +00:00
// セルが変わるまでイベントを起こしたくない
2024-01-26 13:16:17 +00:00
return;
2024-01-23 06:34:52 +00:00
}
2024-01-26 13:16:17 +00:00
const leftTop = {
col: 0,
row: Math.min(targetCellAddress.row, firstSelectionRowIdx.value),
};
const rightBottom = {
2024-02-03 03:19:37 +00:00
col: Math.min(...cells.value.map(it => it.cells.length - 1)),
2024-01-26 13:16:17 +00:00
row: Math.max(targetCellAddress.row, firstSelectionRowIdx.value),
};
2024-02-20 12:29:56 +00:00
// 範囲外のセルは選択解除し、範囲内のセルは選択状態にする
2024-01-26 13:16:17 +00:00
unSelectionOutOfRange(leftTop, rightBottom);
2024-01-29 04:41:13 +00:00
expandCellRange(leftTop, rightBottom);
2024-02-20 12:29:56 +00:00
// 行も同様に
2024-02-04 04:44:58 +00:00
const rangedRowIndexes = [rows.value[targetCellAddress.row].index, ...rangedRows.value.map(it => it.index)];
2024-01-29 04:41:13 +00:00
expandRowRange(Math.min(...rangedRowIndexes), Math.max(...rangedRowIndexes));
2024-01-28 23:12:11 +00:00
2024-01-26 03:48:29 +00:00
previousCellAddress.value = targetCellAddress;
2024-02-03 11:10:56 +00:00
// フォーカスを当てないとキーイベントが拾えないので
getCellElement(ev.target as HTMLElement)?.focus();
2024-01-23 06:34:52 +00:00
break;
}
}
}
2024-01-26 13:16:17 +00:00
function onMouseUp(ev: MouseEvent) {
2024-01-27 03:02:50 +00:00
ev.preventDefault();
2024-01-26 13:16:17 +00:00
switch (state.value) {
case 'rowSelecting':
case 'colSelecting':
case 'cellSelecting': {
unregisterMouseUp();
unregisterMouseMove();
state.value = 'normal';
previousCellAddress.value = CELL_ADDRESS_NONE;
break;
}
}
}
2024-01-30 23:39:56 +00:00
function onContextMenu(ev: MouseEvent) {
2024-07-07 08:10:10 +00:00
const cellAddress = getCellAddress(ev.target as HTMLElement);
2024-01-30 23:39:56 +00:00
if (_DEV_) {
console.log(`[grid][context-menu] button: ${ev.button}, cell: ${cellAddress.row}x${cellAddress.col}`);
}
2024-02-15 22:41:29 +00:00
const context = createContext();
2024-02-01 11:59:30 +00:00
const menuItems = Array.of<MenuItem>();
2024-02-15 22:41:29 +00:00
switch (true) {
2024-02-20 12:29:56 +00:00
// 通常セルのコンテキストメニュー作成
2024-02-15 22:41:29 +00:00
case availableCellAddress(cellAddress): {
const cell = cells.value[cellAddress.row].cells[cellAddress.col];
if (cell.setting.contextMenuFactory) {
menuItems.push(...cell.setting.contextMenuFactory(cell.column, cell.row, cell.value, context));
}
break;
}
2024-02-20 12:29:56 +00:00
// 列ヘッダセルのコンテキストメニュー作成
2024-02-15 22:41:29 +00:00
case isColumnHeaderCellAddress(cellAddress): {
const col = columns.value[cellAddress.col];
if (col.setting.contextMenuFactory) {
menuItems.push(...col.setting.contextMenuFactory(col, context));
}
break;
}
2024-02-20 12:29:56 +00:00
// 行ヘッダセルのコンテキストメニュー作成
2024-02-15 22:41:29 +00:00
case isRowNumberCellAddress(cellAddress): {
const row = rows.value[cellAddress.row];
if (row.setting.contextMenuFactory) {
menuItems.push(...row.setting.contextMenuFactory(row, context));
}
break;
}
2024-02-01 11:59:30 +00:00
}
2024-01-30 23:39:56 +00:00
if (menuItems.length > 0) {
os.contextMenu(menuItems, ev);
}
}
2024-01-23 06:34:52 +00:00
function onCellEditBegin(sender: GridCell) {
state.value = 'cellEditing';
editingCellAddress.value = sender.address;
2024-02-03 03:19:37 +00:00
for (const cell of cells.value.flatMap(it => it.cells)) {
2024-01-23 06:34:52 +00:00
if (cell.address.col !== sender.address.col || cell.address.row !== sender.address.row) {
// 編集状態となったセル以外は全部選択解除
cell.selected = false;
}
}
}
function onCellEditEnd() {
editingCellAddress.value = CELL_ADDRESS_NONE;
state.value = 'normal';
}
2024-01-26 13:16:17 +00:00
function onChangeCellValue(sender: GridCell, newValue: CellValue) {
2024-02-10 13:31:04 +00:00
applyRowRules([sender]);
2024-02-01 11:59:30 +00:00
emitCellValue(sender, newValue);
2024-01-26 13:16:17 +00:00
}
function onChangeCellContentSize(sender: GridCell, contentSize: Size) {
2024-02-04 00:24:10 +00:00
const _cells = cells.value;
if (_cells.length > sender.address.row && _cells[sender.address.row].cells.length > sender.address.col) {
const currentSize = _cells[sender.address.row].cells[sender.address.col].contentSize;
if (currentSize.width !== contentSize.width || currentSize.height !== contentSize.height) {
2024-02-20 12:29:56 +00:00
// 通常セルのセル幅が確定したら、そのサイズを保持しておく内容に引っ張られて想定よりも大きいセルサイズにならないようにするためのCSS作成に使用
2024-02-04 00:24:10 +00:00
_cells[sender.address.row].cells[sender.address.col].contentSize = contentSize;
2024-02-20 12:29:56 +00:00
2024-02-04 00:24:10 +00:00
if (sender.column.setting.width === 'auto') {
calcLargestCellWidth(sender.column);
}
}
2024-01-29 02:28:08 +00:00
}
2024-01-26 13:16:17 +00:00
}
2024-01-27 03:02:50 +00:00
function onHeaderCellWidthBeginChange() {
2024-01-23 06:34:52 +00:00
switch (state.value) {
case 'normal': {
state.value = 'colResizing';
break;
}
}
}
2024-01-27 03:02:50 +00:00
function onHeaderCellWidthEndChange() {
2024-01-23 06:34:52 +00:00
switch (state.value) {
case 'colResizing': {
state.value = 'normal';
break;
}
}
}
2024-01-26 13:16:17 +00:00
function onHeaderCellChangeWidth(sender: GridColumn, width: string) {
2024-01-23 06:34:52 +00:00
switch (state.value) {
case 'colResizing': {
const column = columns.value[sender.index];
column.width = width;
break;
}
}
}
2024-01-26 13:16:17 +00:00
function onHeaderCellChangeContentSize(sender: GridColumn, newSize: Size) {
switch (state.value) {
case 'normal': {
2024-02-04 00:24:10 +00:00
const currentSize = columns.value[sender.index].contentSize;
if (currentSize.width !== newSize.width || currentSize.height !== newSize.height) {
2024-02-20 12:29:56 +00:00
// ヘッダセルのセル幅が確定したら、そのサイズを保持しておく内容に引っ張られて想定よりも大きいセルサイズにならないようにするためのCSS作成に使用
2024-02-04 00:24:10 +00:00
columns.value[sender.index].contentSize = newSize;
2024-02-20 12:29:56 +00:00
2024-02-04 00:24:10 +00:00
if (sender.setting.width === 'auto') {
calcLargestCellWidth(sender);
}
2024-01-29 02:28:08 +00:00
}
2024-01-26 13:16:17 +00:00
break;
}
}
}
2024-01-23 06:34:52 +00:00
function onHeaderCellWidthLargest(sender: GridColumn) {
switch (state.value) {
case 'normal': {
2024-01-30 00:53:47 +00:00
calcLargestCellWidth(sender);
2024-01-23 06:34:52 +00:00
break;
}
}
}
2024-02-02 00:32:49 +00:00
// endregion
// #endregion
// #region Methods
// region Methods
/**
* カラム内のコンテンツを表示しきるために必要な横幅と各セルのコンテンツを表示しきるために必要な横幅を比較し大きい方を列全体の横幅として採用する
*/
2024-01-30 00:53:47 +00:00
function calcLargestCellWidth(column: GridColumn) {
2024-01-29 02:28:08 +00:00
const _cells = cells.value;
const largestColumnWidth = columns.value[column.index].contentSize.width;
const largestCellWidth = (_cells.length > 0)
2024-01-29 04:41:13 +00:00
? _cells
2024-02-03 03:19:37 +00:00
.map(row => row.cells[column.index])
2024-01-29 02:28:08 +00:00
.reduce(
(acc, value) => Math.max(acc, value.contentSize.width),
0,
)
: 0;
2024-01-30 00:53:47 +00:00
if (_DEV_) {
console.log(`[grid][calc-largest] idx:${column.setting.bindTo}, col:${largestColumnWidth}, cell:${largestCellWidth}`);
}
2024-01-29 02:28:08 +00:00
column.width = `${Math.max(largestColumnWidth, largestCellWidth)}px`;
}
2024-02-02 00:32:49 +00:00
/**
* {@link emit}を使用してイベントを発行する
*/
2024-02-01 11:59:30 +00:00
function emitGridEvent(ev: GridEvent) {
2024-02-15 22:41:29 +00:00
const currentState: GridContext = {
2024-02-01 11:59:30 +00:00
selectedCell: selectedCell.value,
rangedCells: rangedCells.value,
rangedRows: rangedRows.value,
randedBounds: rangedBounds.value,
availableBounds: availableBounds.value,
state: state.value,
rows: rows.value,
columns: columns.value,
};
emit(
'event',
ev,
2024-02-10 02:52:34 +00:00
currentState,
2024-02-01 11:59:30 +00:00
);
}
2024-02-02 00:32:49 +00:00
/**
2024-02-20 12:29:56 +00:00
* 親コンポーネントに新しい値を通知する
* 新しい値はイベント通知元データへの反映再計算バリデーション含む再描画の流れで反映される
2024-02-02 00:32:49 +00:00
*/
2024-02-01 11:59:30 +00:00
function emitCellValue(sender: GridCell | CellAddress, newValue: CellValue) {
2024-01-27 03:02:50 +00:00
const cellAddress = 'address' in sender ? sender.address : sender;
2024-02-03 03:19:37 +00:00
const cell = cells.value[cellAddress.row].cells[cellAddress.col];
2024-01-28 13:10:24 +00:00
2024-02-03 09:51:35 +00:00
emitGridEvent({
type: 'cell-value-change',
column: cell.column,
row: cell.row,
oldValue: cell.value,
newValue: newValue,
});
if (_DEV_) {
console.log(`[grid][cell-value] row:${cell.row}, col:${cell.column.index}, value:${newValue}`);
2024-02-03 04:17:26 +00:00
}
2024-01-23 06:34:52 +00:00
}
2024-02-02 00:32:49 +00:00
/**
* {@link target}のセルを選択状態にする
* その際{@link target}以外の行およびセルの範囲選択状態を解除する
*/
2024-01-23 06:34:52 +00:00
function selectionCell(target: CellAddress) {
2024-01-26 13:16:17 +00:00
if (!availableCellAddress(target)) {
return;
}
2024-02-02 00:32:49 +00:00
unSelectionRangeAll();
2024-01-23 06:34:52 +00:00
2024-01-26 03:48:29 +00:00
const _cells = cells.value;
2024-02-03 03:19:37 +00:00
_cells[target.row].cells[target.col].selected = true;
_cells[target.row].cells[target.col].ranged = true;
2024-01-23 06:34:52 +00:00
}
2024-02-02 00:32:49 +00:00
/**
* {@link targets}のセルを範囲選択状態にする
*/
2024-01-23 06:34:52 +00:00
function selectionRange(...targets: CellAddress[]) {
const _cells = cells.value;
for (const target of targets) {
2024-02-20 11:25:42 +00:00
const row = _cells[target.row];
if (row.row.using) {
row.cells[target.col].ranged = true;
}
2024-01-23 06:34:52 +00:00
}
}
2024-02-02 00:32:49 +00:00
/**
* 行およびセルの範囲選択状態をすべて解除する
*/
function unSelectionRangeAll() {
2024-01-26 03:48:29 +00:00
const _cells = rangedCells.value;
for (const cell of _cells) {
cell.selected = false;
cell.ranged = false;
2024-01-23 06:34:52 +00:00
}
2024-01-29 04:41:13 +00:00
2024-02-20 12:29:56 +00:00
const _rows = rows.value.filter(it => it.using);
2024-01-29 04:41:13 +00:00
for (const row of _rows) {
row.ranged = false;
}
2024-01-23 06:34:52 +00:00
}
2024-02-02 00:32:49 +00:00
/**
* {@link leftTop}から{@link rightBottom}の範囲外にあるセルを範囲選択状態から外す
*/
2024-01-26 13:16:17 +00:00
function unSelectionOutOfRange(leftTop: CellAddress, rightBottom: CellAddress) {
2024-02-20 11:25:42 +00:00
const safeBounds = getSafeAddressBounds({ leftTop, rightBottom });
2024-01-26 13:16:17 +00:00
const _cells = rangedCells.value;
for (const cell of _cells) {
2024-02-20 11:25:42 +00:00
const outOfRangeCol = cell.address.col < safeBounds.leftTop.col || cell.address.col > safeBounds.rightBottom.col;
const outOfRangeRow = cell.address.row < safeBounds.leftTop.row || cell.address.row > safeBounds.rightBottom.row;
2024-01-26 13:16:17 +00:00
if (outOfRangeCol || outOfRangeRow) {
cell.ranged = false;
}
}
2024-01-29 04:41:13 +00:00
2024-02-20 11:25:42 +00:00
const outOfRangeRows = rows.value.filter((_, index) => index < safeBounds.leftTop.row || index > safeBounds.rightBottom.row);
2024-01-29 04:41:13 +00:00
for (const row of outOfRangeRows) {
row.ranged = false;
}
2024-01-26 13:16:17 +00:00
}
2024-02-02 00:32:49 +00:00
/**
* {@link leftTop}から{@link rightBottom}の範囲内にあるセルを範囲選択状態にする
*/
2024-01-29 04:41:13 +00:00
function expandCellRange(leftTop: CellAddress, rightBottom: CellAddress) {
2024-02-20 11:25:42 +00:00
const safeBounds = getSafeAddressBounds({ leftTop, rightBottom });
const targetRows = cells.value.slice(safeBounds.leftTop.row, safeBounds.rightBottom.row + 1);
2024-01-23 06:34:52 +00:00
for (const row of targetRows) {
2024-02-20 11:25:42 +00:00
for (const cell of row.cells.slice(safeBounds.leftTop.col, safeBounds.rightBottom.col + 1)) {
2024-01-23 06:34:52 +00:00
cell.ranged = true;
}
}
}
2024-02-02 00:32:49 +00:00
/**
* {@link top}から{@link bottom}までの行を範囲選択状態にする
*/
2024-01-29 04:41:13 +00:00
function expandRowRange(top: number, bottom: number) {
2024-02-10 13:31:04 +00:00
if (!rowSetting.selectable) {
2024-02-04 04:44:58 +00:00
return;
}
2024-01-29 04:41:13 +00:00
const targetRows = rows.value.slice(top, bottom + 1);
for (const row of targetRows) {
row.ranged = true;
}
}
2024-02-20 12:29:56 +00:00
/**
* 特定の条件下でのみ適用されるCSSを反映する
*/
2024-02-10 13:31:04 +00:00
function applyRowRules(targetCells: GridCell[]) {
const _rows = rows.value;
const targetRowIdxes = [...new Set(targetCells.map(it => it.address.row))];
const rowGroups = Array.of<{ row: GridRow, cells: GridCell[] }>();
for (const rowIdx of targetRowIdxes) {
const rowGroup = targetCells.filter(it => it.address.row === rowIdx);
rowGroups.push({ row: _rows[rowIdx], cells: rowGroup });
}
const _cells = cells.value;
for (const group of rowGroups.filter(it => it.row.using)) {
const row = group.row;
const targetCols = group.cells.map(it => it.column);
2024-02-20 11:25:42 +00:00
const rowCells = _cells[group.row.index].cells;
2024-02-10 13:31:04 +00:00
const newStyles = rowSetting.styleRules
2024-02-20 11:25:42 +00:00
.filter(it => it.condition({ row, targetCols, cells: rowCells }))
2024-02-10 13:31:04 +00:00
.map(it => it.applyStyle);
if (JSON.stringify(newStyles) !== JSON.stringify(row.additionalStyles)) {
row.additionalStyles = newStyles;
}
}
}
2024-01-23 06:34:52 +00:00
function availableCellAddress(cellAddress: CellAddress): boolean {
2024-02-20 11:25:42 +00:00
const safeBounds = availableBounds.value;
return cellAddress.row >= safeBounds.leftTop.row &&
cellAddress.col >= safeBounds.leftTop.col &&
cellAddress.row <= safeBounds.rightBottom.row &&
cellAddress.col <= safeBounds.rightBottom.col;
2024-01-23 06:34:52 +00:00
}
2024-01-22 01:37:44 +00:00
2024-01-26 13:16:17 +00:00
function isColumnHeaderCellAddress(cellAddress: CellAddress): boolean {
return cellAddress.row === -1 && cellAddress.col >= 0;
}
function isRowNumberCellAddress(cellAddress: CellAddress): boolean {
return cellAddress.row >= 0 && cellAddress.col === -1;
}
2024-02-20 11:25:42 +00:00
function getSafeAddressBounds(
bounds: { leftTop: CellAddress, rightBottom: CellAddress },
): { leftTop: CellAddress, rightBottom: CellAddress } {
const available = availableBounds.value;
const safeLeftTop = {
col: Math.max(bounds.leftTop.col, available.leftTop.col),
row: Math.max(bounds.leftTop.row, available.leftTop.row),
};
const safeRightBottom = {
col: Math.min(bounds.rightBottom.col, available.rightBottom.col),
row: Math.min(bounds.rightBottom.row, available.rightBottom.row),
};
return { leftTop: safeLeftTop, rightBottom: safeRightBottom };
}
2024-02-02 00:32:49 +00:00
function registerMouseMove() {
unregisterMouseMove();
addEventListener('mousemove', onMouseMove);
}
function unregisterMouseMove() {
removeEventListener('mousemove', onMouseMove);
}
function registerMouseUp() {
unregisterMouseUp();
addEventListener('mouseup', onMouseUp);
}
function unregisterMouseUp() {
removeEventListener('mouseup', onMouseUp);
}
2024-02-15 22:41:29 +00:00
function createContext(): GridContext {
return {
selectedCell: selectedCell.value,
rangedCells: rangedCells.value,
rangedRows: rangedRows.value,
randedBounds: rangedBounds.value,
availableBounds: availableBounds.value,
state: state.value,
rows: rows.value,
columns: columns.value,
};
}
2024-01-22 01:37:44 +00:00
function refreshData() {
2024-02-01 11:59:30 +00:00
if (_DEV_) {
2024-02-04 00:24:10 +00:00
console.log('[grid][refresh-data][begin]');
2024-02-01 11:59:30 +00:00
}
2024-02-20 12:29:56 +00:00
// データを元に行・列・セルを作成する。
// 行は元データの配列の長さに応じて作成するが、最低限の行数は設定によって決まる。
// 行数が変わるたびに都度レンダリングするとパフォーマンスがイマイチなので、あらかじめ多めにセルを用意しておくための措置。
2024-01-23 06:34:52 +00:00
const _data: DataSource[] = data.value;
2024-02-10 13:31:04 +00:00
const _rows: GridRow[] = (_data.length > rowSetting.minimumDefinitionCount)
2024-02-15 22:41:29 +00:00
? _data.map((_, index) => createRow(index, true, rowSetting))
: Array.from({ length: rowSetting.minimumDefinitionCount }, (_, index) => createRow(index, index < _data.length, rowSetting));
2024-02-04 23:35:26 +00:00
const _cols: GridColumn[] = columns.value;
2024-02-02 00:32:49 +00:00
// 行・列の定義から、元データの配列より値を取得してセルを作成する。
// 行・列の定義はそれぞれインデックスを持っており、そのインデックスは元データの配列番地に対応している。
2024-02-04 23:35:26 +00:00
const _cells: RowHolder[] = _rows.map(row => {
2024-02-25 23:12:37 +00:00
const newCells = row.using
? _cols.map(col => createCell(col, row, _data[row.index][col.setting.bindTo], cellSettings))
2024-02-15 22:41:29 +00:00
: _cols.map(col => createCell(col, row, undefined, cellSettings));
2024-02-04 23:35:26 +00:00
2024-02-25 23:12:37 +00:00
return { row, cells: newCells, origin: _data[row.index] };
2024-02-04 23:35:26 +00:00
});
2024-01-22 01:37:44 +00:00
rows.value = _rows;
2024-01-23 06:34:52 +00:00
cells.value = _cells;
2024-02-04 00:24:10 +00:00
2024-02-25 23:12:37 +00:00
const allCells = _cells.filter(it => it.row.using).flatMap(it => it.cells);
for (const cell of allCells) {
cell.violation = cellValidation(allCells, cell, cell.value);
}
applyRowRules(allCells);
2024-02-10 13:31:04 +00:00
2024-02-04 00:24:10 +00:00
if (_DEV_) {
console.log('[grid][refresh-data][end]');
}
2024-01-22 01:37:44 +00:00
}
2024-02-03 02:17:16 +00:00
/**
* セル値を部分更新するこの関数は外部起因でデータが変更された場合に呼ばれる
*
* 外部起因でデータが変更された場合は{@link data}の値が変更されるが何処の番地がどのように変わったのかまでは検知できない
* セルをすべて作り直せばいいがその手法だと以下のデメリットがある
* - 描画負荷がかかる
* - 各セルが持つ個別の状態選択中状態やバリデーション結果などが失われる
*
2024-02-03 03:59:59 +00:00
* そこで新しい値とセルが持つ値を突き合わせ変更があった場合のみ値を更新しセルそのものは使いまわしつつ値を最新化する
2024-02-03 02:17:16 +00:00
*/
function patchData(newItems: DataSource[]) {
2024-02-04 00:24:10 +00:00
if (_DEV_) {
2024-02-10 13:31:04 +00:00
console.log('[grid][patch-data][begin]');
2024-02-04 00:24:10 +00:00
}
2024-02-03 12:47:07 +00:00
2024-02-04 23:35:26 +00:00
const _cols = columns.value;
2024-02-03 11:10:56 +00:00
2024-02-04 23:35:26 +00:00
if (rows.value.length < newItems.length) {
2024-02-03 12:47:07 +00:00
const newRows = Array.of<GridRow>();
const newCells = Array.of<RowHolder>();
2024-02-04 00:24:10 +00:00
2024-02-20 12:29:56 +00:00
// 未使用の行を含めても足りないので新しい行を追加する
2024-02-04 23:35:26 +00:00
for (let rowIdx = rows.value.length; rowIdx < newItems.length; rowIdx++) {
2024-02-15 22:41:29 +00:00
const newRow = createRow(rowIdx, true, rowSetting);
2024-02-04 23:35:26 +00:00
newRows.push(newRow);
newCells.push({
row: newRow,
2024-02-15 22:41:29 +00:00
cells: _cols.map(col => createCell(col, newRow, newItems[rowIdx][col.setting.bindTo], cellSettings)),
2024-02-04 23:35:26 +00:00
origin: newItems[rowIdx],
});
2024-02-03 03:19:37 +00:00
}
2024-02-03 02:17:16 +00:00
2024-02-04 23:35:26 +00:00
rows.value.push(...newRows);
cells.value.push(...newCells);
2024-02-10 13:31:04 +00:00
applyRowRules(newCells.flatMap(it => it.cells));
2024-02-04 23:35:26 +00:00
}
2024-02-07 10:54:29 +00:00
// 行数の上限が欲しい場合はここに設けてもいいかもしれない
2024-02-10 13:31:04 +00:00
const usingRows = rows.value.filter(it => it.using);
if (usingRows.length > newItems.length) {
2024-02-04 23:35:26 +00:00
// 行数が減っているので古い行をクリアする(再マウント・再レンダリングが重いので要素そのものは消さない)
2024-02-10 13:31:04 +00:00
for (let rowIdx = newItems.length; rowIdx < usingRows.length; rowIdx++) {
2024-02-04 23:35:26 +00:00
resetRow(rows.value[rowIdx]);
for (let colIdx = 0; colIdx < _cols.length; colIdx++) {
const holder = cells.value[rowIdx];
holder.origin = {};
resetCell(holder.cells[colIdx]);
2024-02-03 03:19:37 +00:00
}
}
2024-02-04 04:44:58 +00:00
}
2024-02-20 12:29:56 +00:00
// 新しい値と既に設定されていた値を入れ替える
2024-02-10 13:31:04 +00:00
const changedCells = Array.of<GridCell>();
2024-02-04 23:35:26 +00:00
for (let rowIdx = 0; rowIdx < newItems.length; rowIdx++) {
const holder = cells.value[rowIdx];
holder.row.using = true;
const oldCells = holder.cells;
2024-02-04 04:44:58 +00:00
const newItem = newItems[rowIdx];
for (let colIdx = 0; colIdx < oldCells.length; colIdx++) {
const _col = columns.value[colIdx];
const oldCell = oldCells[colIdx];
const newValue = newItem[_col.setting.bindTo];
if (oldCell.value !== newValue) {
2024-02-17 10:51:44 +00:00
oldCell.value = _col.setting.valueTransformer
? _col.setting.valueTransformer(holder.row, _col, newValue)
: newValue;
2024-02-10 13:31:04 +00:00
changedCells.push(oldCell);
2024-02-03 02:17:16 +00:00
}
}
}
2024-02-04 00:24:10 +00:00
2024-02-10 13:31:04 +00:00
if (changedCells.length > 0) {
2024-02-25 23:12:37 +00:00
const allCells = cells.value.slice(0, newItems.length).flatMap(it => it.cells);
for (const cell of allCells) {
cell.violation = cellValidation(allCells, cell, cell.value);
}
2024-02-10 13:31:04 +00:00
applyRowRules(changedCells);
// セル値が書き換わっており、バリデーションの結果も変わっているので外部に通知する必要がある
emitGridEvent({
type: 'cell-validation',
all: cells.value
.filter(it => it.row.using)
.flatMap(it => it.cells)
.map(it => it.violation)
.filter(it => !it.valid),
});
}
2024-02-04 00:24:10 +00:00
if (_DEV_) {
console.log('[grid][patch-data][end]');
}
2024-02-03 02:17:16 +00:00
}
2024-02-02 00:32:49 +00:00
// endregion
// #endregion
2024-01-26 13:16:17 +00:00
2024-01-30 00:53:47 +00:00
onMounted(() => {
2024-02-17 10:51:44 +00:00
state.value = 'normal';
2024-02-04 23:35:26 +00:00
const bindToList = columnSettings.map(it => it.bindTo);
if (new Set(bindToList).size !== columnSettings.length) {
// 取得元のプロパティ名重複は許容したくない
throw new Error(`Duplicate bindTo setting : [${bindToList.join(',')}]}]`);
}
2024-01-30 00:53:47 +00:00
if (rootEl.value) {
resizeObserver.observe(rootEl.value);
2024-01-22 01:37:44 +00:00
2024-01-30 00:53:47 +00:00
// 初期表示時にコンテンツが表示されていない場合はhidden状態にしておく。
// コンテンツ表示時にresizeイベントが発生するが、そのときにhidden状態にしておかないとサイズの再計算が走らないので
const bounds = rootEl.value.getBoundingClientRect();
if (bounds.width === 0 || bounds.height === 0) {
state.value = 'hidden';
}
}
2024-02-07 10:57:21 +00:00
refreshData();
2024-01-30 00:53:47 +00:00
});
2024-01-22 01:37:44 +00:00
</script>
<style module lang="scss">
.grid {
2024-03-18 07:27:41 +00:00
font-size: 90%;
2024-07-07 08:17:26 +00:00
overflow-x: scroll;
2024-01-29 22:59:21 +00:00
}
2024-07-07 08:10:10 +00:00
</style>
<style lang="scss">
$borderSetting: solid 0.5px var(--divider);
$borderRadius: var(--radius);
2024-01-22 01:37:44 +00:00
2024-07-07 08:10:10 +00:00
// 配下コンポーネントを含めて一括してコントロールするため、scopedもmoduleも使用できない
.mk_grid_border {
2024-01-23 06:34:52 +00:00
border-spacing: 0;
2024-01-29 22:59:21 +00:00
2024-07-07 08:10:10 +00:00
.mk_grid_thead {
.mk_grid_tr {
.mk_grid_th {
2024-01-29 22:59:21 +00:00
border-left: $borderSetting;
border-top: $borderSetting;
&:first-child {
// 左上セル
border-top-left-radius: $borderRadius;
}
&:last-child {
// 右上セル
border-top-right-radius: $borderRadius;
border-right: $borderSetting;
}
}
}
}
2024-07-07 08:10:10 +00:00
.mk_grid_tbody {
.mk_grid_tr {
.mk_grid_td, .mk_grid_th {
2024-01-29 22:59:21 +00:00
border-left: $borderSetting;
border-top: $borderSetting;
&:last-child {
// 一番右端の列
border-right: $borderSetting;
}
}
2024-07-07 08:14:40 +00:00
&:last-child {
2024-07-07 08:10:10 +00:00
.mk_grid_td, .mk_grid_th {
2024-01-29 22:59:21 +00:00
// 一番下の行
border-bottom: $borderSetting;
&:first-child {
// 左下セル
border-bottom-left-radius: $borderRadius;
}
&:last-child {
// 右下セル
border-bottom-right-radius: $borderRadius;
}
}
}
}
}
2024-01-22 01:37:44 +00:00
}
</style>