@trismegisto/primitives 4.1.0 → 4.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/MIGRATION.md +14 -0
- package/dist/calendar-selection.d.ts +46 -0
- package/dist/calendar-selection.js +78 -0
- package/dist/calendar-selection.js.map +1 -0
- package/dist/command-palette.d.ts +2 -19
- package/dist/command-palette.js +4 -80
- package/dist/command-palette.js.map +1 -1
- package/dist/data-grid-aggregate.d.ts +46 -1
- package/dist/data-grid-aggregate.js +87 -22
- package/dist/data-grid-aggregate.js.map +1 -1
- package/dist/data-grid-cell-dom.d.ts +136 -0
- package/dist/data-grid-cell-dom.js +1054 -0
- package/dist/data-grid-cell-dom.js.map +1 -0
- package/dist/data-grid-cell-nav.d.ts +260 -0
- package/dist/data-grid-cell-nav.js +369 -0
- package/dist/data-grid-cell-nav.js.map +1 -0
- package/dist/data-grid-clipboard.d.ts +35 -1
- package/dist/data-grid-clipboard.js +155 -0
- package/dist/data-grid-clipboard.js.map +1 -1
- package/dist/data-grid-edit-history-controller.d.ts +96 -0
- package/dist/data-grid-edit-history-controller.js +257 -0
- package/dist/data-grid-edit-history-controller.js.map +1 -0
- package/dist/data-grid-edit-history.d.ts +77 -0
- package/dist/data-grid-edit-history.js +152 -0
- package/dist/data-grid-edit-history.js.map +1 -0
- package/dist/data-grid-group-levels.d.ts +234 -0
- package/dist/data-grid-group-levels.js +426 -0
- package/dist/data-grid-group-levels.js.map +1 -0
- package/dist/data-grid-live-controller.d.ts +96 -0
- package/dist/data-grid-live-controller.js +290 -0
- package/dist/data-grid-live-controller.js.map +1 -0
- package/dist/data-grid-live-dom.d.ts +38 -0
- package/dist/data-grid-live-dom.js +125 -0
- package/dist/data-grid-live-dom.js.map +1 -0
- package/dist/data-grid-live.d.ts +181 -0
- package/dist/data-grid-live.js +377 -0
- package/dist/data-grid-live.js.map +1 -0
- package/dist/data-grid-measure.d.ts +4 -1
- package/dist/data-grid-measure.js +5 -2
- package/dist/data-grid-measure.js.map +1 -1
- package/dist/data-grid-paste.d.ts +86 -0
- package/dist/data-grid-paste.js +97 -0
- package/dist/data-grid-paste.js.map +1 -0
- package/dist/data-grid-persist.d.ts +2 -0
- package/dist/data-grid-persist.js +10 -1
- package/dist/data-grid-persist.js.map +1 -1
- package/dist/data-grid-spreadsheet-job.d.ts +60 -0
- package/dist/data-grid-spreadsheet-job.js +73 -0
- package/dist/data-grid-spreadsheet-job.js.map +1 -0
- package/dist/data-grid-spreadsheet.d.ts +141 -0
- package/dist/data-grid-spreadsheet.js +546 -0
- package/dist/data-grid-spreadsheet.js.map +1 -0
- package/dist/data-grid.d.ts +11 -0
- package/dist/data-grid.js.map +1 -1
- package/dist/date.d.ts +6 -0
- package/dist/date.js +24 -14
- package/dist/date.js.map +1 -1
- package/dist/expandable-text.d.ts +93 -0
- package/dist/expandable-text.js +224 -0
- package/dist/expandable-text.js.map +1 -0
- package/dist/highlight.d.ts +140 -0
- package/dist/highlight.js +578 -0
- package/dist/highlight.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -1
- package/dist/inline-edit.d.ts +138 -0
- package/dist/inline-edit.js +271 -0
- package/dist/inline-edit.js.map +1 -0
- package/dist/messages-calendar-selection.d.ts +37 -0
- package/dist/messages-calendar-selection.js +60 -0
- package/dist/messages-calendar-selection.js.map +1 -0
- package/dist/messages-data-grid.d.ts +119 -1
- package/dist/messages-data-grid.js +225 -0
- package/dist/messages-data-grid.js.map +1 -1
- package/dist/messages-expandable-text.d.ts +23 -0
- package/dist/messages-expandable-text.js +23 -0
- package/dist/messages-expandable-text.js.map +1 -0
- package/dist/messages-highlight.d.ts +17 -0
- package/dist/messages-highlight.js +16 -0
- package/dist/messages-highlight.js.map +1 -0
- package/dist/messages-inline-edit.d.ts +32 -0
- package/dist/messages-inline-edit.js +37 -0
- package/dist/messages-inline-edit.js.map +1 -0
- package/dist/messages-multi-date-picker.d.ts +26 -0
- package/dist/messages-multi-date-picker.js +42 -0
- package/dist/messages-multi-date-picker.js.map +1 -0
- package/dist/messages-phone-field.d.ts +45 -0
- package/dist/messages-phone-field.js +82 -0
- package/dist/messages-phone-field.js.map +1 -0
- package/dist/messages-week-picker.d.ts +22 -0
- package/dist/messages-week-picker.js +34 -0
- package/dist/messages-week-picker.js.map +1 -0
- package/dist/messages-year-picker.d.ts +15 -0
- package/dist/messages-year-picker.js +25 -0
- package/dist/messages-year-picker.js.map +1 -0
- package/dist/multi-date.d.ts +52 -0
- package/dist/multi-date.js +81 -0
- package/dist/multi-date.js.map +1 -0
- package/dist/phone-country-index.generated.d.ts +11 -0
- package/dist/phone-country-index.generated.js +313 -0
- package/dist/phone-country-index.generated.js.map +1 -0
- package/dist/phone-number.d.ts +227 -0
- package/dist/phone-number.js +551 -0
- package/dist/phone-number.js.map +1 -0
- package/dist/phone-plan-br.generated.d.ts +2 -0
- package/dist/phone-plan-br.generated.js +66 -0
- package/dist/phone-plan-br.generated.js.map +1 -0
- package/dist/phone-plan-types.d.ts +61 -0
- package/dist/phone-plan-types.js +10 -0
- package/dist/phone-plan-types.js.map +1 -0
- package/dist/phone-plans/africa.d.ts +2 -0
- package/dist/phone-plans/africa.js +712 -0
- package/dist/phone-plans/africa.js.map +1 -0
- package/dist/phone-plans/americas.d.ts +2 -0
- package/dist/phone-plans/americas.js +1724 -0
- package/dist/phone-plans/americas.js.map +1 -0
- package/dist/phone-plans/asia.d.ts +2 -0
- package/dist/phone-plans/asia.js +664 -0
- package/dist/phone-plans/asia.js.map +1 -0
- package/dist/phone-plans/europe.d.ts +2 -0
- package/dist/phone-plans/europe.js +1020 -0
- package/dist/phone-plans/europe.js.map +1 -0
- package/dist/phone-plans/oceania.d.ts +2 -0
- package/dist/phone-plans/oceania.js +274 -0
- package/dist/phone-plans/oceania.js.map +1 -0
- package/dist/phone-plans.d.ts +5 -0
- package/dist/phone-plans.js +42 -0
- package/dist/phone-plans.js.map +1 -0
- package/dist/secret.d.ts +5 -1
- package/dist/secret.js +7 -2
- package/dist/secret.js.map +1 -1
- package/dist/week.d.ts +102 -0
- package/dist/week.js +244 -0
- package/dist/week.js.map +1 -0
- package/dist/year-picker.d.ts +43 -0
- package/dist/year-picker.js +88 -0
- package/dist/year-picker.js.map +1 -0
- package/dist/zip-writer.d.ts +53 -0
- package/dist/zip-writer.js +227 -0
- package/dist/zip-writer.js.map +1 -0
- package/package.json +5 -4
|
@@ -0,0 +1,1054 @@
|
|
|
1
|
+
import { gridMatrixToHtml, writeGridClipboard } from './data-grid-clipboard';
|
|
2
|
+
import { GRID_GROUP_ROW_PREFIX, GRID_HEADER_ROW_ID, GRID_RANGE_COPY_SLICE_CELLS, GRID_RANGE_SETTLE_MS, GRID_RANGE_SUMMARY_SLICE_CELLS, clampGridRangePoint, gridActivePoint, gridAutoScrollDelta, gridCellIndex, gridCellInRect, gridCellKeyAction, gridCellRect, gridCellRefOf, gridRangeMatrix, gridRangePoints, gridRangeRefs, gridRangeToTsv, gridRangeToTsvSliced, gridRectSize, gridTouchRangeStep, isGridMultiRange, reduceGridCellNav, resolveGridRange, sameGridCellRange, sameGridCellRef, summarizeGridRange, summarizeGridRangeSliced, } from './data-grid-cell-nav';
|
|
3
|
+
import { formatMessage, pluralMessage } from './i18n';
|
|
4
|
+
/** O que recebe foco dentro de uma célula (os controles que saem da ordem de Tab fora do modo de interação). */
|
|
5
|
+
const CELL_FOCUSABLE = 'a[href], button, input, select, textarea, summary, iframe, [tabindex], [contenteditable=""], [contenteditable="true"]';
|
|
6
|
+
/** Atributo com a posição da célula na linha (marca as células decoradas). */
|
|
7
|
+
const CELL_ATTR = 'data-hm-cell';
|
|
8
|
+
/** Tabindex original de um controle interno (vazio = não tinha). */
|
|
9
|
+
const SAVED_TABINDEX = 'data-hm-tabindex';
|
|
10
|
+
/** Linhas de dados levam o id neste atributo (só no modo grade). */
|
|
11
|
+
export const GRID_ROW_ID_ATTR = 'data-hm-row-id';
|
|
12
|
+
/** Linhas de cabeçalho de grupo levam o id (`GRID_GROUP_ROW_PREFIX` + caminho) neste atributo (agrupamento no modo grade). */
|
|
13
|
+
export const GRID_GROUP_ROW_ATTR = 'data-hm-group-id';
|
|
14
|
+
const CLASS = {
|
|
15
|
+
active: 'hm-data-grid__cell--active',
|
|
16
|
+
inRange: 'hm-data-grid__cell--in-range',
|
|
17
|
+
top: 'hm-data-grid__cell--range-top',
|
|
18
|
+
bottom: 'hm-data-grid__cell--range-bottom',
|
|
19
|
+
start: 'hm-data-grid__cell--range-start',
|
|
20
|
+
end: 'hm-data-grid__cell--range-end',
|
|
21
|
+
dragging: 'hm-data-grid--range-dragging',
|
|
22
|
+
};
|
|
23
|
+
const CELL_CLASSES = [CLASS.active, CLASS.inRange, CLASS.top, CLASS.bottom, CLASS.start, CLASS.end];
|
|
24
|
+
/** Pressão longa (ms) que liga o modo "Selecionar intervalo" no toque, e o deslocamento (px) que a cancela. */
|
|
25
|
+
export const GRID_LONG_PRESS_MS = 500;
|
|
26
|
+
const LONG_PRESS_SLOP = 10;
|
|
27
|
+
export const EMPTY_GRID_CELL_VIEW = { summary: null, preparing: false, touchRange: false, hasRange: false };
|
|
28
|
+
// ---------------------------------------------------------------- DOM: localizar células
|
|
29
|
+
/** A linha de cabeçalho das colunas (a última do `<thead>`; a de grupos de colunas vem antes). */
|
|
30
|
+
function headerRow(table) {
|
|
31
|
+
const rows = table.tHead?.rows;
|
|
32
|
+
return rows && rows.length > 0 ? rows[rows.length - 1] : null;
|
|
33
|
+
}
|
|
34
|
+
function dataRows(table) {
|
|
35
|
+
return Array.from(table.querySelectorAll(`tbody > tr[${GRID_ROW_ID_ATTR}]`));
|
|
36
|
+
}
|
|
37
|
+
/** Seletor de atributo com o valor escapado (aspas e barra invertida). */
|
|
38
|
+
function attrSelector(name, value) {
|
|
39
|
+
return `[${name}="${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"]`;
|
|
40
|
+
}
|
|
41
|
+
/** A célula da grade que contém `target` (cabeçalho das colunas ou linha de dados). */
|
|
42
|
+
function hitCell(table, target) {
|
|
43
|
+
const node = target instanceof Element ? target : null;
|
|
44
|
+
if (!node || !table.contains(node))
|
|
45
|
+
return null;
|
|
46
|
+
const cell = node.closest('td, th');
|
|
47
|
+
const tr = cell?.parentElement;
|
|
48
|
+
if (!cell || !tr || !table.contains(tr))
|
|
49
|
+
return null;
|
|
50
|
+
const col = Array.prototype.indexOf.call(tr.children, cell);
|
|
51
|
+
if (tr === headerRow(table))
|
|
52
|
+
return { element: cell, rowId: GRID_HEADER_ROW_ID, col, inside: node !== cell };
|
|
53
|
+
const groupId = tr.getAttribute(GRID_GROUP_ROW_ATTR);
|
|
54
|
+
if (groupId !== null && tr.parentElement?.tagName === 'TBODY') {
|
|
55
|
+
// A linha de grupo é uma célula só para a navegação: o botão de recolher dentro dela não é modo de interação.
|
|
56
|
+
return { element: tr.children[0] ?? cell, rowId: groupId, col: 0, inside: false, group: true };
|
|
57
|
+
}
|
|
58
|
+
const rowId = tr.getAttribute(GRID_ROW_ID_ATTR);
|
|
59
|
+
if (rowId === null || tr.parentElement?.tagName !== 'TBODY')
|
|
60
|
+
return null;
|
|
61
|
+
return { element: cell, rowId, col, inside: node !== cell };
|
|
62
|
+
}
|
|
63
|
+
/** O elemento da célula `rowId`/`col`, se estiver desenhado. */
|
|
64
|
+
function findCell(table, rowId, col) {
|
|
65
|
+
if (rowId.startsWith(GRID_GROUP_ROW_PREFIX)) {
|
|
66
|
+
const group = table.querySelector(`tbody > tr${attrSelector(GRID_GROUP_ROW_ATTR, rowId)}`);
|
|
67
|
+
return group?.children[0] ?? null;
|
|
68
|
+
}
|
|
69
|
+
const tr = rowId === GRID_HEADER_ROW_ID ? headerRow(table) : table.querySelector(`tbody > tr${attrSelector(GRID_ROW_ID_ATTR, rowId)}`);
|
|
70
|
+
return tr?.children[col] ?? null;
|
|
71
|
+
}
|
|
72
|
+
function controlsOf(cell) {
|
|
73
|
+
return Array.from(cell.querySelectorAll(CELL_FOCUSABLE));
|
|
74
|
+
}
|
|
75
|
+
function firstControl(cell) {
|
|
76
|
+
return (controlsOf(cell).find((element) => {
|
|
77
|
+
const disabled = element.disabled === true;
|
|
78
|
+
const hidden = element.closest('[hidden], [inert]') !== null || element.getAttribute('aria-hidden') === 'true';
|
|
79
|
+
return !disabled && !hidden;
|
|
80
|
+
}) ?? null);
|
|
81
|
+
}
|
|
82
|
+
/** Tira o controle da ordem de Tab, guardando o tabindex original. */
|
|
83
|
+
function suppress(element) {
|
|
84
|
+
if (!element.hasAttribute(SAVED_TABINDEX))
|
|
85
|
+
element.setAttribute(SAVED_TABINDEX, element.getAttribute('tabindex') ?? '');
|
|
86
|
+
if (element.getAttribute('tabindex') !== '-1')
|
|
87
|
+
element.setAttribute('tabindex', '-1');
|
|
88
|
+
}
|
|
89
|
+
function restore(element) {
|
|
90
|
+
const saved = element.getAttribute(SAVED_TABINDEX);
|
|
91
|
+
if (saved === null)
|
|
92
|
+
return;
|
|
93
|
+
element.removeAttribute(SAVED_TABINDEX);
|
|
94
|
+
if (saved === '')
|
|
95
|
+
element.removeAttribute('tabindex');
|
|
96
|
+
else
|
|
97
|
+
element.setAttribute('tabindex', saved);
|
|
98
|
+
}
|
|
99
|
+
function setAttr(element, name, value) {
|
|
100
|
+
if (value === null) {
|
|
101
|
+
if (element.hasAttribute(name))
|
|
102
|
+
element.removeAttribute(name);
|
|
103
|
+
}
|
|
104
|
+
else if (element.getAttribute(name) !== value)
|
|
105
|
+
element.setAttribute(name, value);
|
|
106
|
+
}
|
|
107
|
+
/** Desfaz tudo o que `decorate` aplicou (o modo grade foi desligado). */
|
|
108
|
+
function undecorate(table, scroller, virtual) {
|
|
109
|
+
for (const cell of Array.from(table.querySelectorAll(`[${CELL_ATTR}]`))) {
|
|
110
|
+
cell.removeAttribute(CELL_ATTR);
|
|
111
|
+
for (const name of ['role', 'tabindex', 'aria-selected', 'aria-colindex'])
|
|
112
|
+
cell.removeAttribute(name);
|
|
113
|
+
cell.classList.remove(...CELL_CLASSES);
|
|
114
|
+
if (cell.getAttribute('class') === '')
|
|
115
|
+
cell.removeAttribute('class');
|
|
116
|
+
}
|
|
117
|
+
for (const control of Array.from(table.querySelectorAll(`[${SAVED_TABINDEX}]`)))
|
|
118
|
+
restore(control);
|
|
119
|
+
for (const tr of Array.from(table.querySelectorAll('tr[aria-level]'))) {
|
|
120
|
+
for (const name of ['aria-level', 'aria-expanded', 'aria-setsize', 'aria-posinset'])
|
|
121
|
+
tr.removeAttribute(name);
|
|
122
|
+
}
|
|
123
|
+
for (const name of ['role', 'aria-multiselectable', 'aria-colcount', 'aria-describedby'])
|
|
124
|
+
table.removeAttribute(name);
|
|
125
|
+
table.classList.remove(CLASS.dragging);
|
|
126
|
+
if (scroller && virtual)
|
|
127
|
+
scroller.setAttribute('tabindex', '0');
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Modo grade: depois de fechar a edição, o foco volta para a CÉLULA (não para o botão de editar dentro dela), para as
|
|
131
|
+
* setas continuarem navegando. O campo aberto (dentro de `.hm-data-grid__editor`) continua recebendo o foco.
|
|
132
|
+
*/
|
|
133
|
+
export function gridCellFocusTarget(target) {
|
|
134
|
+
if (target.closest('.hm-data-grid__editor'))
|
|
135
|
+
return target;
|
|
136
|
+
return target.closest('td, th') ?? target;
|
|
137
|
+
}
|
|
138
|
+
// ---------------------------------------------------------------- controlador
|
|
139
|
+
export function createGridCellController(options) {
|
|
140
|
+
const on = () => options.mode() !== 'none';
|
|
141
|
+
const rangeMode = () => options.mode() === 'range';
|
|
142
|
+
let interacting = false;
|
|
143
|
+
let decorated = false;
|
|
144
|
+
let view = EMPTY_GRID_CELL_VIEW;
|
|
145
|
+
let settleTimer = null;
|
|
146
|
+
let summaryToken = 0;
|
|
147
|
+
let copyAbort = null;
|
|
148
|
+
let touchPending = null;
|
|
149
|
+
let longPress = null;
|
|
150
|
+
let drag = null;
|
|
151
|
+
let bound = null;
|
|
152
|
+
// Índices da vista: refeitos só quando as linhas, as colunas ou as de sistema mudam (não a cada tecla).
|
|
153
|
+
let cache = null;
|
|
154
|
+
const index = () => {
|
|
155
|
+
const rows = options.rows();
|
|
156
|
+
const columns = options.columns();
|
|
157
|
+
const system = options.systemKeys();
|
|
158
|
+
const rowId = options.rowId();
|
|
159
|
+
if (cache && cache.rows === rows && cache.columns === columns && cache.rowId === rowId && sameKeys(cache.system, system))
|
|
160
|
+
return cache.index;
|
|
161
|
+
const next = gridCellIndex(rows.map((row) => rowId(row)), system, columns.map((column) => column.key));
|
|
162
|
+
cache = { rows, columns, system, rowId, index: next };
|
|
163
|
+
return next;
|
|
164
|
+
};
|
|
165
|
+
const dims = (ix) => ({ rows: ix.rowIds.length, cols: ix.keys.length, firstCol: ix.firstCol, page: pageSize() });
|
|
166
|
+
const state = (ix) => ({
|
|
167
|
+
active: gridActivePoint(options.getActive(), ix),
|
|
168
|
+
range: rangeMode() ? gridRangePoints(options.getRange(), ix) : null,
|
|
169
|
+
});
|
|
170
|
+
const setView = (next) => {
|
|
171
|
+
view = { ...view, ...next };
|
|
172
|
+
options.setView(view);
|
|
173
|
+
};
|
|
174
|
+
// ---------------------------------------------------------------- linhas de grupo
|
|
175
|
+
const groupsNav = () => (options.groups ? options.groups() : null);
|
|
176
|
+
/** O grupo da célula ativa, se ela estiver numa linha de grupo desenhada. */
|
|
177
|
+
const activeGroup = (g) => {
|
|
178
|
+
const id = options.getActive()?.rowId;
|
|
179
|
+
return g && id ? (g.groups.get(id) ?? null) : null;
|
|
180
|
+
};
|
|
181
|
+
/** Foca a célula `col` da linha `id` (rolando até ela na rolagem virtual; tenta de novo depois da pintura). */
|
|
182
|
+
function focusElement(id, col) {
|
|
183
|
+
const table = options.table();
|
|
184
|
+
if (!table)
|
|
185
|
+
return;
|
|
186
|
+
const target = () => findCell(table, id, col);
|
|
187
|
+
if (id !== GRID_HEADER_ROW_ID && options.virtual())
|
|
188
|
+
options.reveal(id);
|
|
189
|
+
const now = target();
|
|
190
|
+
if (now) {
|
|
191
|
+
now.focus();
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
let attempts = 3;
|
|
195
|
+
const retry = () => {
|
|
196
|
+
const element = target();
|
|
197
|
+
if (element) {
|
|
198
|
+
decorate();
|
|
199
|
+
element.focus();
|
|
200
|
+
}
|
|
201
|
+
else if (--attempts > 0)
|
|
202
|
+
options.afterRender(retry);
|
|
203
|
+
};
|
|
204
|
+
options.afterRender(retry);
|
|
205
|
+
}
|
|
206
|
+
/** Leva a célula ativa à linha `at` da navegação (grupo ou dados), mantendo a coluna para as linhas de dados. */
|
|
207
|
+
function goLine(g, at, ix) {
|
|
208
|
+
const id = g.lines[Math.max(0, Math.min(g.lines.length - 1, at))];
|
|
209
|
+
if (id === undefined)
|
|
210
|
+
return;
|
|
211
|
+
const key = options.getActive()?.key ?? ix.keys[ix.firstCol] ?? ix.keys[0];
|
|
212
|
+
if (g.groups.has(id)) {
|
|
213
|
+
interacting = false;
|
|
214
|
+
if (key !== undefined && !sameGridCellRef({ rowId: id, key }, options.getActive()))
|
|
215
|
+
options.setActive({ rowId: id, key });
|
|
216
|
+
if (options.getRange())
|
|
217
|
+
options.setRange(null);
|
|
218
|
+
clearSettle();
|
|
219
|
+
if (view.summary || view.hasRange)
|
|
220
|
+
setView({ summary: null, hasRange: false });
|
|
221
|
+
decorate();
|
|
222
|
+
focusElement(id, 0);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const row = ix.rowOf.get(id);
|
|
226
|
+
if (row === undefined)
|
|
227
|
+
return;
|
|
228
|
+
const col = (key !== undefined ? ix.colOf.get(key) : undefined) ?? ix.firstCol;
|
|
229
|
+
const point = { row, col: Math.min(col, Math.max(0, ix.keys.length - 1)) };
|
|
230
|
+
commit({ active: point, range: null }, ix);
|
|
231
|
+
focusPoint(point, ix);
|
|
232
|
+
}
|
|
233
|
+
/** Teclado com grupos: as teclas do padrão *treegrid* nas linhas de grupo e a passagem entre grupos e dados. */
|
|
234
|
+
function onGroupKeydown(event, g, ix) {
|
|
235
|
+
const ref = options.getActive();
|
|
236
|
+
const group = activeGroup(g);
|
|
237
|
+
const plain = !event.ctrlKey && !event.metaKey && !event.altKey;
|
|
238
|
+
if (group) {
|
|
239
|
+
const at = g.lineOf.get(group.id) ?? 0;
|
|
240
|
+
if (event.key === '*' && plain) {
|
|
241
|
+
event.preventDefault();
|
|
242
|
+
g.expandSiblings(group.siblings);
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
const action = gridCellKeyAction(event, options.rtl());
|
|
246
|
+
if (!action)
|
|
247
|
+
return false;
|
|
248
|
+
switch (action.type) {
|
|
249
|
+
case 'move':
|
|
250
|
+
event.preventDefault();
|
|
251
|
+
switch (action.move) {
|
|
252
|
+
case 'right':
|
|
253
|
+
if (!group.expanded)
|
|
254
|
+
g.toggle(group.path, true);
|
|
255
|
+
else
|
|
256
|
+
goLine(g, at + 1, ix);
|
|
257
|
+
return true;
|
|
258
|
+
case 'left':
|
|
259
|
+
if (group.expanded)
|
|
260
|
+
g.toggle(group.path, false);
|
|
261
|
+
else if (group.parent)
|
|
262
|
+
goLine(g, g.lineOf.get(group.parent) ?? at, ix);
|
|
263
|
+
return true;
|
|
264
|
+
case 'up':
|
|
265
|
+
if (at === 0) {
|
|
266
|
+
const col = (ref ? ix.colOf.get(ref.key) : undefined) ?? ix.firstCol;
|
|
267
|
+
const point = { row: -1, col };
|
|
268
|
+
commit({ active: point, range: null }, ix);
|
|
269
|
+
focusPoint(point, ix);
|
|
270
|
+
}
|
|
271
|
+
else
|
|
272
|
+
goLine(g, at - 1, ix);
|
|
273
|
+
return true;
|
|
274
|
+
case 'down':
|
|
275
|
+
goLine(g, at + 1, ix);
|
|
276
|
+
return true;
|
|
277
|
+
case 'pageUp':
|
|
278
|
+
goLine(g, at - pageSize(), ix);
|
|
279
|
+
return true;
|
|
280
|
+
case 'pageDown':
|
|
281
|
+
goLine(g, at + pageSize(), ix);
|
|
282
|
+
return true;
|
|
283
|
+
case 'gridStart':
|
|
284
|
+
goLine(g, 0, ix);
|
|
285
|
+
return true;
|
|
286
|
+
case 'gridEnd':
|
|
287
|
+
goLine(g, g.lines.length - 1, ix);
|
|
288
|
+
return true;
|
|
289
|
+
default:
|
|
290
|
+
return true;
|
|
291
|
+
}
|
|
292
|
+
case 'activate': {
|
|
293
|
+
event.preventDefault();
|
|
294
|
+
if (action.key === 'Enter')
|
|
295
|
+
g.toggle(group.path, !group.expanded);
|
|
296
|
+
else {
|
|
297
|
+
const table = options.table();
|
|
298
|
+
const cell = table ? findCell(table, group.id, 0) : null;
|
|
299
|
+
if (cell)
|
|
300
|
+
interact(cell);
|
|
301
|
+
}
|
|
302
|
+
return true;
|
|
303
|
+
}
|
|
304
|
+
case 'selectAll':
|
|
305
|
+
case 'escape':
|
|
306
|
+
return false;
|
|
307
|
+
case 'copy':
|
|
308
|
+
return !options.getRange();
|
|
309
|
+
default:
|
|
310
|
+
return true;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
if (!ref)
|
|
314
|
+
return false;
|
|
315
|
+
const action = gridCellKeyAction(event, options.rtl());
|
|
316
|
+
if (!action || action.type !== 'move' || action.extend)
|
|
317
|
+
return false;
|
|
318
|
+
if (ref.rowId === GRID_HEADER_ROW_ID) {
|
|
319
|
+
if (action.move === 'down' && g.lines.length > 0 && g.groups.has(g.lines[0])) {
|
|
320
|
+
event.preventDefault();
|
|
321
|
+
goLine(g, 0, ix);
|
|
322
|
+
return true;
|
|
323
|
+
}
|
|
324
|
+
return false;
|
|
325
|
+
}
|
|
326
|
+
const at = g.lineOf.get(ref.rowId);
|
|
327
|
+
if (at === undefined)
|
|
328
|
+
return false;
|
|
329
|
+
let target = -1;
|
|
330
|
+
if (action.move === 'up' && at > 0 && g.groups.has(g.lines[at - 1]))
|
|
331
|
+
target = at - 1;
|
|
332
|
+
else if (action.move === 'down' && at < g.lines.length - 1 && g.groups.has(g.lines[at + 1]))
|
|
333
|
+
target = at + 1;
|
|
334
|
+
else if (action.move === 'left' && ix.colOf.get(ref.key) === 0 && g.rowGroup.has(ref.rowId))
|
|
335
|
+
target = g.lineOf.get(g.rowGroup.get(ref.rowId)) ?? -1;
|
|
336
|
+
if (target < 0)
|
|
337
|
+
return false;
|
|
338
|
+
event.preventDefault();
|
|
339
|
+
goLine(g, target, ix);
|
|
340
|
+
return true;
|
|
341
|
+
}
|
|
342
|
+
/** Linhas por página da área rolável: a altura visível (limitada à janela) ÷ a altura de uma linha desenhada. */
|
|
343
|
+
function pageSize() {
|
|
344
|
+
const scroller = options.scroller();
|
|
345
|
+
const table = options.table();
|
|
346
|
+
if (!scroller || !table)
|
|
347
|
+
return 10;
|
|
348
|
+
const row = table.querySelector(`tbody > tr[${GRID_ROW_ID_ATTR}]`);
|
|
349
|
+
const rowHeight = row?.getBoundingClientRect().height || 0;
|
|
350
|
+
const header = table.tHead?.getBoundingClientRect().height ?? 0;
|
|
351
|
+
const viewport = scroller.ownerDocument.defaultView?.innerHeight ?? scroller.clientHeight;
|
|
352
|
+
const visible = Math.min(scroller.clientHeight || viewport, viewport) - header;
|
|
353
|
+
if (rowHeight <= 0 || visible <= 0)
|
|
354
|
+
return 10;
|
|
355
|
+
return Math.max(1, Math.floor(visible / rowHeight));
|
|
356
|
+
}
|
|
357
|
+
/** Escreve o estado nos modelos (só o que mudou) e reaplica nas células. */
|
|
358
|
+
function commit(next, ix) {
|
|
359
|
+
const active = gridCellRefOf(next.active, ix);
|
|
360
|
+
const range = isGridMultiRange(next.range) ? gridRangeRefs(next.range, ix) : null;
|
|
361
|
+
if (!sameGridCellRef(active, options.getActive()))
|
|
362
|
+
options.setActive(active);
|
|
363
|
+
if (!sameGridCellRange(range, options.getRange()))
|
|
364
|
+
options.setRange(range);
|
|
365
|
+
if (range) {
|
|
366
|
+
setView({ hasRange: true });
|
|
367
|
+
settle();
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
clearSettle();
|
|
371
|
+
if (view.summary || view.hasRange)
|
|
372
|
+
setView({ summary: null, hasRange: false });
|
|
373
|
+
}
|
|
374
|
+
decorate();
|
|
375
|
+
}
|
|
376
|
+
// ---------------------------------------------------------------- foco
|
|
377
|
+
function focusPoint(point, ix) {
|
|
378
|
+
const ref = gridCellRefOf(point, ix);
|
|
379
|
+
const table = options.table();
|
|
380
|
+
if (!ref || !table)
|
|
381
|
+
return;
|
|
382
|
+
interacting = false;
|
|
383
|
+
const target = () => findCell(table, ref.rowId, point.col);
|
|
384
|
+
if (ref.rowId !== GRID_HEADER_ROW_ID && options.virtual())
|
|
385
|
+
options.reveal(ref.rowId);
|
|
386
|
+
const now = target();
|
|
387
|
+
if (now) {
|
|
388
|
+
now.focus();
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
// A linha ainda não está na janela virtual: foca depois da pintura (algumas tentativas).
|
|
392
|
+
let attempts = 3;
|
|
393
|
+
const retry = () => {
|
|
394
|
+
const element = target();
|
|
395
|
+
if (element) {
|
|
396
|
+
decorate();
|
|
397
|
+
element.focus();
|
|
398
|
+
}
|
|
399
|
+
else if (--attempts > 0)
|
|
400
|
+
options.afterRender(retry);
|
|
401
|
+
};
|
|
402
|
+
options.afterRender(retry);
|
|
403
|
+
}
|
|
404
|
+
/** Modo de interação: devolve os controles da célula à ordem de Tab e foca o primeiro. */
|
|
405
|
+
function interact(cell) {
|
|
406
|
+
const control = firstControl(cell);
|
|
407
|
+
if (!control)
|
|
408
|
+
return false;
|
|
409
|
+
interacting = true;
|
|
410
|
+
for (const element of controlsOf(cell))
|
|
411
|
+
restore(element);
|
|
412
|
+
control.focus();
|
|
413
|
+
return true;
|
|
414
|
+
}
|
|
415
|
+
function leaveInteraction(cell) {
|
|
416
|
+
interacting = false;
|
|
417
|
+
decorate();
|
|
418
|
+
cell.focus();
|
|
419
|
+
}
|
|
420
|
+
// ---------------------------------------------------------------- resumo e anúncio (com atraso)
|
|
421
|
+
function clearSettle() {
|
|
422
|
+
if (settleTimer)
|
|
423
|
+
clearTimeout(settleTimer);
|
|
424
|
+
settleTimer = null;
|
|
425
|
+
summaryToken += 1;
|
|
426
|
+
}
|
|
427
|
+
function settle() {
|
|
428
|
+
clearSettle();
|
|
429
|
+
settleTimer = setTimeout(() => {
|
|
430
|
+
settleTimer = null;
|
|
431
|
+
void summarize();
|
|
432
|
+
}, GRID_RANGE_SETTLE_MS);
|
|
433
|
+
}
|
|
434
|
+
function number(value, fraction = 2) {
|
|
435
|
+
try {
|
|
436
|
+
return new Intl.NumberFormat(options.locale(), { maximumFractionDigits: fraction }).format(value);
|
|
437
|
+
}
|
|
438
|
+
catch {
|
|
439
|
+
return String(value);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
function summaryView(summary, numericKey, calculating) {
|
|
443
|
+
const text = options.text();
|
|
444
|
+
const formatter = options.formatter();
|
|
445
|
+
const rows = number(summary.rows, 0);
|
|
446
|
+
const columns = number(summary.columns, 0);
|
|
447
|
+
const items = [];
|
|
448
|
+
const stats = summary.stats;
|
|
449
|
+
if (stats && !calculating) {
|
|
450
|
+
const fmt = (value, fn) => (formatter ? formatter(value, fn, numericKey) : number(value));
|
|
451
|
+
items.push({ key: 'count', label: text.rangeCount, value: number(stats.count, 0) }, { key: 'sum', label: text.rangeSum, value: fmt(stats.sum, 'sum') }, { key: 'average', label: text.rangeAverage, value: fmt(stats.average, 'avg') }, { key: 'min', label: text.rangeMin, value: fmt(stats.min, 'min') }, { key: 'max', label: text.rangeMax, value: fmt(stats.max, 'max') });
|
|
452
|
+
}
|
|
453
|
+
return {
|
|
454
|
+
size: formatMessage(text.rangeSize, { rows, columns }),
|
|
455
|
+
spoken: formatMessage(text.rangeSelected, { rows, columns }),
|
|
456
|
+
items,
|
|
457
|
+
calculating,
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
async function summarize() {
|
|
461
|
+
const ix = index();
|
|
462
|
+
const points = rangeMode() ? gridRangePoints(options.getRange(), ix) : null;
|
|
463
|
+
if (!points || !isGridMultiRange(points))
|
|
464
|
+
return;
|
|
465
|
+
const resolved = resolveGridRange(gridCellRect(points.anchor, points.focus), options.rows(), options.columns(), ix);
|
|
466
|
+
if (!resolved)
|
|
467
|
+
return;
|
|
468
|
+
const token = ++summaryToken;
|
|
469
|
+
const numeric = resolved.columns.filter((column) => column.type === 'number' && typeof column.raw === 'function');
|
|
470
|
+
const numericKey = numeric.length === 1 ? numeric[0].key : '';
|
|
471
|
+
const size = gridRectSize(resolved.rect);
|
|
472
|
+
let summary;
|
|
473
|
+
if (size.cells > GRID_RANGE_SUMMARY_SLICE_CELLS && numeric.length > 0) {
|
|
474
|
+
setView({ summary: summaryView({ rows: size.rows, columns: size.columns, cells: size.cells, stats: null }, numericKey, true) });
|
|
475
|
+
summary = await summarizeGridRangeSliced(resolved);
|
|
476
|
+
if (token !== summaryToken)
|
|
477
|
+
return;
|
|
478
|
+
}
|
|
479
|
+
else {
|
|
480
|
+
summary = summarizeGridRange(resolved);
|
|
481
|
+
}
|
|
482
|
+
const next = summaryView(summary, numericKey, false);
|
|
483
|
+
if (options.rangeSummary())
|
|
484
|
+
setView({ summary: next });
|
|
485
|
+
else
|
|
486
|
+
options.announce(next.spoken);
|
|
487
|
+
}
|
|
488
|
+
// ---------------------------------------------------------------- cópia
|
|
489
|
+
async function copy(header = false) {
|
|
490
|
+
if (!on() || view.preparing)
|
|
491
|
+
return false;
|
|
492
|
+
const ix = index();
|
|
493
|
+
const current = state(ix);
|
|
494
|
+
if (!current.range && activeGroup(groupsNav()))
|
|
495
|
+
return false; // linha de grupo: nada a copiar
|
|
496
|
+
const rect = current.range
|
|
497
|
+
? gridCellRect(current.range.anchor, current.range.focus)
|
|
498
|
+
: current.active.row >= 0 && current.active.col >= ix.firstCol
|
|
499
|
+
? gridCellRect(current.active, current.active)
|
|
500
|
+
: null;
|
|
501
|
+
if (!rect)
|
|
502
|
+
return false;
|
|
503
|
+
const resolved = resolveGridRange(rect, options.rows(), options.columns(), ix);
|
|
504
|
+
const doc = options.document();
|
|
505
|
+
if (!resolved)
|
|
506
|
+
return false;
|
|
507
|
+
const text = options.text();
|
|
508
|
+
const cells = gridRectSize(rect).cells;
|
|
509
|
+
let tsv;
|
|
510
|
+
let html;
|
|
511
|
+
if (cells > GRID_RANGE_COPY_SLICE_CELLS) {
|
|
512
|
+
copyAbort = new AbortController();
|
|
513
|
+
setView({ preparing: true });
|
|
514
|
+
options.announce(text.preparingCopy);
|
|
515
|
+
try {
|
|
516
|
+
tsv = await gridRangeToTsvSliced(resolved, header, { signal: copyAbort.signal });
|
|
517
|
+
}
|
|
518
|
+
catch (error) {
|
|
519
|
+
if (error?.name === 'AbortError') {
|
|
520
|
+
options.announce(text.copyCanceled);
|
|
521
|
+
return false;
|
|
522
|
+
}
|
|
523
|
+
throw error;
|
|
524
|
+
}
|
|
525
|
+
finally {
|
|
526
|
+
copyAbort = null;
|
|
527
|
+
setView({ preparing: false });
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
else {
|
|
531
|
+
tsv = gridRangeToTsv(resolved, header);
|
|
532
|
+
html = gridMatrixToHtml(gridRangeMatrix(resolved, header), header);
|
|
533
|
+
}
|
|
534
|
+
const ok = doc ? await writeGridClipboard(doc, tsv, html) : false;
|
|
535
|
+
if (!ok) {
|
|
536
|
+
options.announce(text.copyFailed);
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
options.announce(pluralMessage(text.copiedCells, cells, options.locale(), { cells: number(cells, 0) }));
|
|
540
|
+
options.copied({ kind: 'range', text: tsv, rows: resolved.rows, keys: resolved.columns.map((column) => column.key) });
|
|
541
|
+
return true;
|
|
542
|
+
}
|
|
543
|
+
// ---------------------------------------------------------------- teclado
|
|
544
|
+
function activate(action, cell, point, ix) {
|
|
545
|
+
const ref = gridCellRefOf(point, ix);
|
|
546
|
+
if (!ref)
|
|
547
|
+
return false;
|
|
548
|
+
if (point.row < 0 && action.key === 'Enter') {
|
|
549
|
+
const sort = cell.querySelector('.hm-data-grid__sort');
|
|
550
|
+
if (sort && !sort.disabled) {
|
|
551
|
+
sort.click();
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
if (point.row >= 0 && point.col >= ix.firstCol && options.editable(ref.rowId, ref.key)) {
|
|
556
|
+
interacting = true;
|
|
557
|
+
options.beginEdit(ref.rowId, ref.key);
|
|
558
|
+
return true;
|
|
559
|
+
}
|
|
560
|
+
return interact(cell);
|
|
561
|
+
}
|
|
562
|
+
function onKeydown(event) {
|
|
563
|
+
if (!on() || event.defaultPrevented)
|
|
564
|
+
return;
|
|
565
|
+
const table = options.table();
|
|
566
|
+
const scroller = options.scroller();
|
|
567
|
+
if (!table)
|
|
568
|
+
return;
|
|
569
|
+
if (view.preparing && event.key === 'Escape') {
|
|
570
|
+
event.preventDefault();
|
|
571
|
+
copyAbort?.abort();
|
|
572
|
+
return;
|
|
573
|
+
}
|
|
574
|
+
const hit = hitCell(table, event.target);
|
|
575
|
+
if (!hit && event.target !== scroller)
|
|
576
|
+
return;
|
|
577
|
+
if (hit?.inside) {
|
|
578
|
+
// Modo de interação: as teclas são do controle; Esc e F2 devolvem o foco à célula.
|
|
579
|
+
if ((event.key === 'Escape' || event.key === 'F2') && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
|
580
|
+
event.preventDefault();
|
|
581
|
+
leaveInteraction(hit.element);
|
|
582
|
+
}
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
const groups = groupsNav();
|
|
586
|
+
if (groups && onGroupKeydown(event, groups, index()))
|
|
587
|
+
return;
|
|
588
|
+
const action = gridCellKeyAction(event, options.rtl());
|
|
589
|
+
if (!action)
|
|
590
|
+
return;
|
|
591
|
+
const ix = index();
|
|
592
|
+
const current = state(ix);
|
|
593
|
+
const dimensions = dims(ix);
|
|
594
|
+
if (action.type === 'copy') {
|
|
595
|
+
if (action.header && !options.copyRows())
|
|
596
|
+
return;
|
|
597
|
+
const copyable = current.range || (current.active.row >= 0 && current.active.col >= ix.firstCol);
|
|
598
|
+
if (!copyable)
|
|
599
|
+
return;
|
|
600
|
+
event.preventDefault();
|
|
601
|
+
void copy(action.header);
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
if (action.type === 'activate') {
|
|
605
|
+
const cell = findCell(table, gridCellRefOf(current.active, ix)?.rowId ?? '', current.active.col);
|
|
606
|
+
if (cell && activate(action, cell, current.active, ix))
|
|
607
|
+
event.preventDefault();
|
|
608
|
+
return;
|
|
609
|
+
}
|
|
610
|
+
if (action.type === 'selectRow' && options.rowSelection()) {
|
|
611
|
+
const ref = gridCellRefOf(current.active, ix);
|
|
612
|
+
if (current.active.row < 0 || !ref)
|
|
613
|
+
return;
|
|
614
|
+
event.preventDefault();
|
|
615
|
+
options.toggleRowSelection(ref.rowId);
|
|
616
|
+
return;
|
|
617
|
+
}
|
|
618
|
+
if (action.type === 'move' && action.move === 'down' && !action.extend && current.active.row === dimensions.rows - 1 && dimensions.rows > 0) {
|
|
619
|
+
// Fim das linhas carregadas com "carregar mais": a seta pede mais; ao chegar, a casca leva a célula à linha nova.
|
|
620
|
+
if (options.requestMore()) {
|
|
621
|
+
event.preventDefault();
|
|
622
|
+
return;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
const next = reduceGridCellNav(current, action, dimensions, options.mode());
|
|
626
|
+
if (!next)
|
|
627
|
+
return;
|
|
628
|
+
event.preventDefault();
|
|
629
|
+
const moved = next.active.row !== current.active.row || next.active.col !== current.active.col;
|
|
630
|
+
commit(next, ix);
|
|
631
|
+
if (moved || hit === null)
|
|
632
|
+
focusPoint(next.active, ix);
|
|
633
|
+
}
|
|
634
|
+
// ---------------------------------------------------------------- foco e ponteiro
|
|
635
|
+
function onFocusIn(event) {
|
|
636
|
+
if (!on())
|
|
637
|
+
return;
|
|
638
|
+
const table = options.table();
|
|
639
|
+
const scroller = options.scroller();
|
|
640
|
+
if (!table)
|
|
641
|
+
return;
|
|
642
|
+
const hit = hitCell(table, event.target);
|
|
643
|
+
if (!hit) {
|
|
644
|
+
// Tab de fora com a célula ativa fora da janela virtual: a área rolável recebe o foco e o passa para a célula.
|
|
645
|
+
const from = event.relatedTarget;
|
|
646
|
+
if (event.target === scroller && scroller && from && !scroller.contains(from)) {
|
|
647
|
+
const group = activeGroup(groupsNav());
|
|
648
|
+
if (group) {
|
|
649
|
+
focusElement(group.id, 0);
|
|
650
|
+
return;
|
|
651
|
+
}
|
|
652
|
+
const ix = index();
|
|
653
|
+
focusPoint(state(ix).active, ix);
|
|
654
|
+
}
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
if (hit.group) {
|
|
658
|
+
interacting = false;
|
|
659
|
+
selectGroupHit(hit);
|
|
660
|
+
decorate();
|
|
661
|
+
return;
|
|
662
|
+
}
|
|
663
|
+
interacting = hit.inside;
|
|
664
|
+
const ix = index();
|
|
665
|
+
const point = pointOfHit(hit, ix);
|
|
666
|
+
const ref = point ? gridCellRefOf(point, ix) : null;
|
|
667
|
+
if (ref && !sameGridCellRef(ref, options.getActive()))
|
|
668
|
+
options.setActive(ref);
|
|
669
|
+
decorate();
|
|
670
|
+
}
|
|
671
|
+
function onFocusOut(event) {
|
|
672
|
+
if (!on() || !interacting)
|
|
673
|
+
return;
|
|
674
|
+
const next = event.relatedTarget;
|
|
675
|
+
const table = options.table();
|
|
676
|
+
if (table && next && table.contains(next))
|
|
677
|
+
return;
|
|
678
|
+
interacting = false;
|
|
679
|
+
decorate();
|
|
680
|
+
}
|
|
681
|
+
/** A célula ativa vai para a linha de grupo tocada (mantendo a coluna para quando voltar aos dados). */
|
|
682
|
+
function selectGroupHit(hit) {
|
|
683
|
+
const ix = index();
|
|
684
|
+
const key = options.getActive()?.key ?? ix.keys[ix.firstCol] ?? ix.keys[0];
|
|
685
|
+
if (key === undefined)
|
|
686
|
+
return;
|
|
687
|
+
const ref = { rowId: hit.rowId, key };
|
|
688
|
+
if (!sameGridCellRef(ref, options.getActive()))
|
|
689
|
+
options.setActive(ref);
|
|
690
|
+
if (options.getRange())
|
|
691
|
+
options.setRange(null);
|
|
692
|
+
}
|
|
693
|
+
function pointOfHit(hit, ix) {
|
|
694
|
+
if (hit.rowId === GRID_HEADER_ROW_ID)
|
|
695
|
+
return { row: -1, col: hit.col };
|
|
696
|
+
const row = ix.rowOf.get(hit.rowId);
|
|
697
|
+
return row === undefined ? null : { row, col: hit.col };
|
|
698
|
+
}
|
|
699
|
+
function cancelLongPress() {
|
|
700
|
+
if (longPress)
|
|
701
|
+
clearTimeout(longPress.timer);
|
|
702
|
+
longPress = null;
|
|
703
|
+
}
|
|
704
|
+
function onPointerDown(event) {
|
|
705
|
+
if (!on() || event.button !== 0)
|
|
706
|
+
return;
|
|
707
|
+
const table = options.table();
|
|
708
|
+
if (!table)
|
|
709
|
+
return;
|
|
710
|
+
const hit = hitCell(table, event.target);
|
|
711
|
+
if (!hit || hit.inside)
|
|
712
|
+
return; // o gesto que começa num controle é do controle
|
|
713
|
+
if (hit.group) {
|
|
714
|
+
selectGroupHit(hit);
|
|
715
|
+
decorate();
|
|
716
|
+
return;
|
|
717
|
+
}
|
|
718
|
+
const ix = index();
|
|
719
|
+
const point = pointOfHit(hit, ix);
|
|
720
|
+
if (!point)
|
|
721
|
+
return;
|
|
722
|
+
const dimensions = dims(ix);
|
|
723
|
+
const current = state(ix);
|
|
724
|
+
if (event.pointerType === 'touch') {
|
|
725
|
+
if (rangeMode() && view.touchRange) {
|
|
726
|
+
const step = gridTouchRangeStep(touchPending, point, dimensions);
|
|
727
|
+
touchPending = step.pending;
|
|
728
|
+
commit({ active: step.active, range: step.range }, ix);
|
|
729
|
+
return;
|
|
730
|
+
}
|
|
731
|
+
commit({ active: point, range: null }, ix);
|
|
732
|
+
if (rangeMode() && point.row >= 0 && point.col >= ix.firstCol) {
|
|
733
|
+
cancelLongPress();
|
|
734
|
+
longPress = {
|
|
735
|
+
x: event.clientX,
|
|
736
|
+
y: event.clientY,
|
|
737
|
+
point,
|
|
738
|
+
timer: setTimeout(() => {
|
|
739
|
+
const at = longPress?.point;
|
|
740
|
+
longPress = null;
|
|
741
|
+
if (!at)
|
|
742
|
+
return;
|
|
743
|
+
// Pressão longa: atalho do "Selecionar intervalo"; esta célula vira a âncora.
|
|
744
|
+
touchPending = clampGridRangePoint(at, dims(index()));
|
|
745
|
+
setView({ touchRange: true });
|
|
746
|
+
}, GRID_LONG_PRESS_MS),
|
|
747
|
+
};
|
|
748
|
+
}
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
751
|
+
if (event.shiftKey && rangeMode() && point.row >= 0 && dimensions.cols > ix.firstCol) {
|
|
752
|
+
// Shift+clique estende a faixa a partir da âncora; sem seleção de texto nem foco nativo.
|
|
753
|
+
event.preventDefault();
|
|
754
|
+
const anchor = current.range?.anchor ?? clampGridRangePoint(current.active, dimensions);
|
|
755
|
+
const focus = clampGridRangePoint(point, dimensions);
|
|
756
|
+
commit({ active: focus, range: { anchor, focus } }, ix);
|
|
757
|
+
hit.element.focus({ preventScroll: true });
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
commit({ active: point, range: null }, ix);
|
|
761
|
+
if (rangeMode() && point.row >= 0 && point.col >= ix.firstCol) {
|
|
762
|
+
drag = { anchor: point, pointerId: event.pointerId, started: false, x: event.clientX, y: event.clientY, frame: 0 };
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
function dragTo(x, y) {
|
|
766
|
+
const table = options.table();
|
|
767
|
+
const doc = options.document();
|
|
768
|
+
if (!drag || !table || !doc || typeof doc.elementFromPoint !== 'function')
|
|
769
|
+
return;
|
|
770
|
+
const hit = hitCell(table, doc.elementFromPoint(x, y));
|
|
771
|
+
if (!hit)
|
|
772
|
+
return;
|
|
773
|
+
const ix = index();
|
|
774
|
+
const point = pointOfHit(hit, ix);
|
|
775
|
+
if (!point || point.row < 0)
|
|
776
|
+
return;
|
|
777
|
+
const dimensions = dims(ix);
|
|
778
|
+
const focus = clampGridRangePoint(point, dimensions);
|
|
779
|
+
const anchor = clampGridRangePoint(drag.anchor, dimensions);
|
|
780
|
+
if (!drag.started && focus.row === anchor.row && focus.col === anchor.col)
|
|
781
|
+
return;
|
|
782
|
+
if (!drag.started) {
|
|
783
|
+
drag.started = true;
|
|
784
|
+
table.classList.add(CLASS.dragging);
|
|
785
|
+
doc.getSelection?.()?.removeAllRanges();
|
|
786
|
+
try {
|
|
787
|
+
bound?.setPointerCapture?.(drag.pointerId);
|
|
788
|
+
}
|
|
789
|
+
catch {
|
|
790
|
+
// Sem captura (ponteiro já solto): segue pelos movimentos que chegarem.
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
commit({ active: focus, range: { anchor, focus } }, ix);
|
|
794
|
+
}
|
|
795
|
+
function autoScroll() {
|
|
796
|
+
if (!drag?.started || !bound)
|
|
797
|
+
return;
|
|
798
|
+
const win = bound.ownerDocument.defaultView;
|
|
799
|
+
const rect = bound.getBoundingClientRect();
|
|
800
|
+
const header = options.table()?.tHead?.getBoundingClientRect().height ?? 0;
|
|
801
|
+
const delta = gridAutoScrollDelta(drag.x, drag.y, { left: rect.left, right: rect.right, top: rect.top + header, bottom: rect.bottom });
|
|
802
|
+
if (delta.dx || delta.dy) {
|
|
803
|
+
bound.scrollLeft += delta.dx;
|
|
804
|
+
bound.scrollTop += delta.dy;
|
|
805
|
+
dragTo(drag.x, drag.y);
|
|
806
|
+
}
|
|
807
|
+
drag.frame = win?.requestAnimationFrame ? win.requestAnimationFrame(autoScroll) : 0;
|
|
808
|
+
}
|
|
809
|
+
function onPointerMove(event) {
|
|
810
|
+
if (longPress && Math.hypot(event.clientX - longPress.x, event.clientY - longPress.y) > LONG_PRESS_SLOP)
|
|
811
|
+
cancelLongPress();
|
|
812
|
+
if (!drag || event.pointerId !== drag.pointerId)
|
|
813
|
+
return;
|
|
814
|
+
drag.x = event.clientX;
|
|
815
|
+
drag.y = event.clientY;
|
|
816
|
+
const wasStarted = drag.started;
|
|
817
|
+
dragTo(event.clientX, event.clientY);
|
|
818
|
+
if (drag.started && !wasStarted)
|
|
819
|
+
autoScroll();
|
|
820
|
+
}
|
|
821
|
+
function onPointerUp(event) {
|
|
822
|
+
cancelLongPress();
|
|
823
|
+
if (!drag || event.pointerId !== drag.pointerId)
|
|
824
|
+
return;
|
|
825
|
+
const finished = drag;
|
|
826
|
+
drag = null;
|
|
827
|
+
const win = bound?.ownerDocument.defaultView;
|
|
828
|
+
if (finished.frame && win?.cancelAnimationFrame)
|
|
829
|
+
win.cancelAnimationFrame(finished.frame);
|
|
830
|
+
options.table()?.classList.remove(CLASS.dragging);
|
|
831
|
+
try {
|
|
832
|
+
bound?.releasePointerCapture?.(finished.pointerId);
|
|
833
|
+
}
|
|
834
|
+
catch {
|
|
835
|
+
// Já liberado.
|
|
836
|
+
}
|
|
837
|
+
if (finished.started) {
|
|
838
|
+
const ix = index();
|
|
839
|
+
focusPoint(state(ix).active, ix);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
const listeners = [
|
|
843
|
+
['focusin', onFocusIn],
|
|
844
|
+
['focusout', onFocusOut],
|
|
845
|
+
['pointerdown', onPointerDown],
|
|
846
|
+
['pointermove', onPointerMove],
|
|
847
|
+
['pointerup', onPointerUp],
|
|
848
|
+
['pointercancel', onPointerUp],
|
|
849
|
+
];
|
|
850
|
+
function bind(scroller) {
|
|
851
|
+
if (scroller === bound)
|
|
852
|
+
return;
|
|
853
|
+
for (const [type, fn] of listeners)
|
|
854
|
+
bound?.removeEventListener(type, fn);
|
|
855
|
+
bound = scroller;
|
|
856
|
+
if (!scroller)
|
|
857
|
+
return;
|
|
858
|
+
for (const [type, fn] of listeners)
|
|
859
|
+
scroller.addEventListener(type, fn);
|
|
860
|
+
}
|
|
861
|
+
// ---------------------------------------------------------------- decoração
|
|
862
|
+
function decorate() {
|
|
863
|
+
const table = options.table();
|
|
864
|
+
if (!table)
|
|
865
|
+
return;
|
|
866
|
+
const scroller = options.scroller();
|
|
867
|
+
if (!on()) {
|
|
868
|
+
if (decorated)
|
|
869
|
+
undecorate(table, scroller, options.virtual());
|
|
870
|
+
decorated = false;
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
decorated = true;
|
|
874
|
+
const ix = index();
|
|
875
|
+
const current = state(ix);
|
|
876
|
+
const range = current.range && isGridMultiRange(current.range) ? current.range : null;
|
|
877
|
+
const rect = range ? gridCellRect(range.anchor, range.focus) : null;
|
|
878
|
+
const multi = rangeMode();
|
|
879
|
+
const colIndexes = options.colIndexes();
|
|
880
|
+
const columns = options.columns();
|
|
881
|
+
const groups = groupsNav();
|
|
882
|
+
const treegrid = options.tree() || !!groups;
|
|
883
|
+
const groupActive = activeGroup(groups);
|
|
884
|
+
setAttr(table, 'role', treegrid ? 'treegrid' : 'grid');
|
|
885
|
+
setAttr(table, 'aria-multiselectable', multi ? 'true' : null);
|
|
886
|
+
setAttr(table, 'aria-colcount', String(ix.keys.length));
|
|
887
|
+
setAttr(table, 'aria-describedby', options.instructionsId() || null);
|
|
888
|
+
let activeRendered = false;
|
|
889
|
+
const paint = (tr, row) => {
|
|
890
|
+
const cells = tr.children;
|
|
891
|
+
for (let col = 0; col < cells.length; col += 1) {
|
|
892
|
+
const cell = cells[col];
|
|
893
|
+
const active = !groupActive && row === current.active.row && col === current.active.col;
|
|
894
|
+
if (active)
|
|
895
|
+
activeRendered = true;
|
|
896
|
+
const data = col >= ix.firstCol;
|
|
897
|
+
const inRange = data && row >= 0 && gridCellInRect(rect, row, col);
|
|
898
|
+
setAttr(cell, CELL_ATTR, String(col));
|
|
899
|
+
const role = row < 0 ? 'columnheader' : data && columns[col - ix.firstCol]?.rowHeader ? 'rowheader' : 'gridcell';
|
|
900
|
+
setAttr(cell, 'role', role);
|
|
901
|
+
setAttr(cell, 'tabindex', active ? '0' : '-1');
|
|
902
|
+
setAttr(cell, 'aria-selected', multi && row >= 0 && data ? String(inRange) : null);
|
|
903
|
+
setAttr(cell, 'aria-colindex', colIndexes ? String(col + 1) : null);
|
|
904
|
+
cell.classList.toggle(CLASS.active, active);
|
|
905
|
+
cell.classList.toggle(CLASS.inRange, inRange);
|
|
906
|
+
cell.classList.toggle(CLASS.top, inRange && row === rect.top);
|
|
907
|
+
cell.classList.toggle(CLASS.bottom, inRange && row === rect.bottom);
|
|
908
|
+
cell.classList.toggle(CLASS.start, inRange && col === rect.left);
|
|
909
|
+
cell.classList.toggle(CLASS.end, inRange && col === rect.right);
|
|
910
|
+
const open = active && interacting;
|
|
911
|
+
for (const control of controlsOf(cell)) {
|
|
912
|
+
if (open)
|
|
913
|
+
restore(control);
|
|
914
|
+
else
|
|
915
|
+
suppress(control);
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
};
|
|
919
|
+
const head = headerRow(table);
|
|
920
|
+
if (head)
|
|
921
|
+
paint(head, -1);
|
|
922
|
+
for (const tr of dataRows(table)) {
|
|
923
|
+
const row = ix.rowOf.get(tr.getAttribute(GRID_ROW_ID_ATTR) ?? '');
|
|
924
|
+
if (row === undefined)
|
|
925
|
+
continue;
|
|
926
|
+
paint(tr, row);
|
|
927
|
+
if (treegrid) {
|
|
928
|
+
setAttr(tr, 'aria-level', tr.getAttribute('data-hm-tree-level') ?? (groups ? String(groups.rowLevel) : null));
|
|
929
|
+
const toggle = tr.querySelector('[data-hm-tree-id]');
|
|
930
|
+
setAttr(tr, 'aria-expanded', toggle ? toggle.getAttribute('aria-expanded') : null);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (groups) {
|
|
934
|
+
for (const tr of Array.from(table.querySelectorAll(`tbody > tr[${GRID_GROUP_ROW_ATTR}]`))) {
|
|
935
|
+
const id = tr.getAttribute(GRID_GROUP_ROW_ATTR) ?? '';
|
|
936
|
+
const info = groups.groups.get(id);
|
|
937
|
+
setAttr(tr, 'aria-level', info ? String(info.level) : tr.getAttribute('data-hm-group-level'));
|
|
938
|
+
setAttr(tr, 'aria-expanded', info ? String(info.expanded) : null);
|
|
939
|
+
setAttr(tr, 'aria-setsize', tr.getAttribute('data-hm-group-setsize'));
|
|
940
|
+
setAttr(tr, 'aria-posinset', tr.getAttribute('data-hm-group-posinset'));
|
|
941
|
+
const cells = tr.children;
|
|
942
|
+
let colIndex = 1;
|
|
943
|
+
for (let col = 0; col < cells.length; col += 1) {
|
|
944
|
+
const cell = cells[col];
|
|
945
|
+
const active = col === 0 && groupActive?.id === id;
|
|
946
|
+
if (active)
|
|
947
|
+
activeRendered = true;
|
|
948
|
+
setAttr(cell, CELL_ATTR, String(col));
|
|
949
|
+
setAttr(cell, 'role', col === 0 ? 'rowheader' : 'gridcell');
|
|
950
|
+
setAttr(cell, 'tabindex', active ? '0' : '-1');
|
|
951
|
+
setAttr(cell, 'aria-colindex', colIndexes ? String(colIndex) : null);
|
|
952
|
+
colIndex += Math.max(1, cell.colSpan || 1);
|
|
953
|
+
cell.classList.toggle(CLASS.active, active);
|
|
954
|
+
const open = active && interacting;
|
|
955
|
+
for (const control of controlsOf(cell)) {
|
|
956
|
+
if (open)
|
|
957
|
+
restore(control);
|
|
958
|
+
else
|
|
959
|
+
suppress(control);
|
|
960
|
+
}
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
// Rolagem virtual: a área rolável é o ponto de Tab só enquanto a célula ativa não está desenhada.
|
|
965
|
+
if (scroller && options.virtual())
|
|
966
|
+
setAttr(scroller, 'tabindex', activeRendered ? '-1' : '0');
|
|
967
|
+
}
|
|
968
|
+
// ---------------------------------------------------------------- API
|
|
969
|
+
function viewChanged() {
|
|
970
|
+
if (!on())
|
|
971
|
+
return;
|
|
972
|
+
const range = options.getRange();
|
|
973
|
+
touchPending = null;
|
|
974
|
+
clearSettle();
|
|
975
|
+
if (view.summary || view.hasRange)
|
|
976
|
+
setView({ summary: null, hasRange: false });
|
|
977
|
+
if (!range)
|
|
978
|
+
return;
|
|
979
|
+
options.setActive(range.anchor);
|
|
980
|
+
options.setRange(null);
|
|
981
|
+
decorate();
|
|
982
|
+
}
|
|
983
|
+
return {
|
|
984
|
+
onKeydown,
|
|
985
|
+
bind,
|
|
986
|
+
decorate,
|
|
987
|
+
viewChanged,
|
|
988
|
+
copy,
|
|
989
|
+
cancelCopy() {
|
|
990
|
+
copyAbort?.abort();
|
|
991
|
+
},
|
|
992
|
+
toggleTouchRange() {
|
|
993
|
+
touchPending = null;
|
|
994
|
+
setView({ touchRange: !view.touchRange });
|
|
995
|
+
},
|
|
996
|
+
focusRow(rowId) {
|
|
997
|
+
const ix = index();
|
|
998
|
+
const row = ix.rowOf.get(rowId);
|
|
999
|
+
if (row === undefined)
|
|
1000
|
+
return;
|
|
1001
|
+
const next = { active: { row, col: state(ix).active.col }, range: null };
|
|
1002
|
+
commit(next, ix);
|
|
1003
|
+
focusPoint(next.active, ix);
|
|
1004
|
+
},
|
|
1005
|
+
focusCell(ref) {
|
|
1006
|
+
const ix = index();
|
|
1007
|
+
const point = gridRangePoints({ anchor: ref, focus: ref }, ix)?.anchor ?? (ref.rowId === GRID_HEADER_ROW_ID ? gridActivePoint(ref, ix) : null);
|
|
1008
|
+
if (!point)
|
|
1009
|
+
return false;
|
|
1010
|
+
commit({ active: point, range: null }, ix);
|
|
1011
|
+
focusPoint(point, ix);
|
|
1012
|
+
return true;
|
|
1013
|
+
},
|
|
1014
|
+
selectRange(range) {
|
|
1015
|
+
const ix = index();
|
|
1016
|
+
if (!range) {
|
|
1017
|
+
commit({ active: state(ix).active, range: null }, ix);
|
|
1018
|
+
return true;
|
|
1019
|
+
}
|
|
1020
|
+
const points = gridRangePoints(range, ix);
|
|
1021
|
+
if (!points || !rangeMode())
|
|
1022
|
+
return false;
|
|
1023
|
+
commit({ active: points.focus, range: points }, ix);
|
|
1024
|
+
return true;
|
|
1025
|
+
},
|
|
1026
|
+
activeRange() {
|
|
1027
|
+
if (!on())
|
|
1028
|
+
return null;
|
|
1029
|
+
const ix = index();
|
|
1030
|
+
const current = state(ix);
|
|
1031
|
+
if (!current.range && activeGroup(groupsNav()))
|
|
1032
|
+
return null;
|
|
1033
|
+
const rect = current.range
|
|
1034
|
+
? gridCellRect(current.range.anchor, current.range.focus)
|
|
1035
|
+
: current.active.row >= 0 && current.active.col >= ix.firstCol
|
|
1036
|
+
? gridCellRect(current.active, current.active)
|
|
1037
|
+
: null;
|
|
1038
|
+
return rect ? resolveGridRange(rect, options.rows(), options.columns(), ix) : null;
|
|
1039
|
+
},
|
|
1040
|
+
dispose() {
|
|
1041
|
+
clearSettle();
|
|
1042
|
+
cancelLongPress();
|
|
1043
|
+
copyAbort?.abort();
|
|
1044
|
+
if (drag?.frame)
|
|
1045
|
+
bound?.ownerDocument.defaultView?.cancelAnimationFrame?.(drag.frame);
|
|
1046
|
+
drag = null;
|
|
1047
|
+
bind(null);
|
|
1048
|
+
},
|
|
1049
|
+
};
|
|
1050
|
+
}
|
|
1051
|
+
function sameKeys(a, b) {
|
|
1052
|
+
return a === b || (a.length === b.length && a.every((key, i) => key === b[i]));
|
|
1053
|
+
}
|
|
1054
|
+
//# sourceMappingURL=data-grid-cell-dom.js.map
|