Skip to content

Commit 24ac749

Browse files
committed
Add LabelBar datagrid formatting mode
Signed-off-by: Andrew Stein <steinlink@gmail.com>
1 parent 41250b2 commit 24ac749

18 files changed

Lines changed: 129 additions & 37 deletions

File tree

packages/viewer-datagrid/src/css/regular_table.css

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ tbody th:last-of-type {
265265
overflow: hidden;
266266
text-overflow: ellipsis;
267267
}
268+
268269
tbody th:empty {
269270
background-image: linear-gradient(
270271
to right,
@@ -316,12 +317,44 @@ regular-table:not(.flat-group-rollup-mode) {
316317
.psp-align-right {
317318
text-align: right;
318319
}
320+
321+
.psp-color-mode-label-bar {
322+
position: relative;
323+
isolation: isolate;
324+
}
325+
326+
.psp-label-bar {
327+
inset: 0;
328+
pointer-events: none;
329+
display: flex;
330+
align-items: center;
331+
justify-content: flex-end;
332+
padding: 0px;
333+
}
334+
335+
.psp-label-bar-fill {
336+
position: absolute;
337+
top: 10%;
338+
height: 80%;
339+
background: var(--psp-label-bar-color);
340+
pointer-events: none;
341+
}
342+
343+
.psp-label-bar-text {
344+
position: relative;
345+
color: var(--psp-label-bar-bg);
346+
mix-blend-mode: difference;
347+
pointer-events: none;
348+
}
349+
319350
.psp-align-left {
320351
text-align: left;
321352
}
353+
322354
.psp-positive:not(:focus) {
323355
color: var(--psp-datagrid--pos-cell--color);
324356
}
357+
325358
.psp-negative:not(:focus) {
326359
color: var(--psp-datagrid--neg-cell--color);
327360
}
@@ -363,6 +396,7 @@ regular-table table {
363396

364397
th.psp-header-leaf {
365398
border-bottom-width: 0px;
399+
366400
span {
367401
height: 23px;
368402
min-height: 23px;
@@ -479,6 +513,7 @@ regular-table table tbody tr:hover + tr:after {
479513
rgba(0, 128, 255, 0.5)
480514
);
481515
}
516+
482517
100% {
483518
background-color: var(
484519
--pulse--background-color-end,
@@ -494,6 +529,7 @@ regular-table table tbody tr:hover + tr:after {
494529
rgba(0, 128, 255, 0.5)
495530
);
496531
}
532+
497533
100% {
498534
background-color: var(
499535
--pulse--background-color-end,
@@ -509,6 +545,7 @@ regular-table table tbody tr:hover + tr:after {
509545
rgba(255, 25, 0, 0.5)
510546
);
511547
}
548+
512549
100% {
513550
background-color: var(
514551
--pulse--background-color-end,
@@ -524,6 +561,7 @@ regular-table table tbody tr:hover + tr:after {
524561
rgba(255, 25, 0, 0.5)
525562
);
526563
}
564+
527565
100% {
528566
background-color: var(
529567
--pulse--background-color-end,

packages/viewer-datagrid/src/ts/custom_elements/datagrid.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,11 @@ export class HTMLPerspectiveViewerDatagridPluginElement
225225
this.regular_table.clear();
226226
}
227227

228-
async save(): Promise<any> {
228+
save(): any {
229229
return save.call(this);
230230
}
231231

232-
async restore(
233-
token: DatagridPluginConfig,
234-
columns_config?: ColumnsConfig,
235-
): Promise<any> {
232+
restore(token: DatagridPluginConfig, columns_config?: ColumnsConfig): void {
236233
return restore.call(this, token, columns_config ?? {});
237234
}
238235

@@ -243,7 +240,7 @@ export class HTMLPerspectiveViewerDatagridPluginElement
243240
}
244241
}
245242

246-
async delete(): Promise<void> {
243+
delete(): void {
247244
this.disconnectedCallback();
248245
this._toolbar = undefined;
249246
if ((this.regular_table as any).table_model) {

packages/viewer-datagrid/src/ts/data_listener/format_cell.ts

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ export function format_cell(
4444
const plugin: ColumnConfig = plugins[title] || {};
4545
const is_numeric = type === "integer" || type === "float";
4646

47-
if (is_numeric && plugin?.number_fg_mode === "bar") {
47+
if (
48+
is_numeric &&
49+
(plugin?.number_fg_mode === "bar" ||
50+
plugin?.number_fg_mode === "label-bar")
51+
) {
4852
const a = Math.max(
4953
0,
5054
Math.min(
@@ -54,15 +58,41 @@ export function format_cell(
5458
),
5559
);
5660

57-
const div = this._div_factory.get();
5861
const anchor = (val as number) >= 0 ? "left" : "right";
5962
const pct = (a * 100).toFixed(2);
60-
div.setAttribute(
61-
"style",
62-
`width:calc(${pct}% - 4px);position:absolute;${anchor}:2px;height:80%;top:10%;pointer-events:none;`,
63-
);
6463

65-
return div;
64+
if (plugin.number_fg_mode === "bar") {
65+
const div = this._div_factory.get();
66+
div.className = "psp-bar";
67+
div.setAttribute(
68+
"style",
69+
`width:calc(${pct}% - 4px);position:absolute;${anchor}:2px;height:80%;top:10%;pointer-events:none;`,
70+
);
71+
72+
return div;
73+
} else {
74+
const wrapper = this._div_factory.get();
75+
wrapper.setAttribute("style", "");
76+
wrapper.className = "psp-label-bar";
77+
while (wrapper.firstChild) {
78+
wrapper.removeChild(wrapper.firstChild);
79+
}
80+
const bar = document.createElement("div");
81+
bar.className = "psp-label-bar-fill";
82+
bar.setAttribute(
83+
"style",
84+
`width:calc(${pct}% - 4px);${anchor}:2px;`,
85+
);
86+
const label = document.createElement("span");
87+
label.className = "psp-label-bar-text";
88+
const formatter = FORMAT_CACHE.get(type, plugin);
89+
label.textContent = formatter
90+
? formatter.format(val)
91+
: (val as string);
92+
wrapper.appendChild(bar);
93+
wrapper.appendChild(label);
94+
return wrapper;
95+
}
6696
} else if (plugin?.format === "link" && type === "string") {
6797
const anchor = document.createElement("a");
6898
anchor.setAttribute("href", val as string);

packages/viewer-datagrid/src/ts/model/column_overrides.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ export function restore_column_size_overrides(
4646
this._cached_column_sizes = old_sizes;
4747
}
4848

49-
const overrides: Record<number, number | undefined> = {};
49+
const regular_table = this.regular_table as RegularTableWithOverrides;
50+
const overrides: Record<number, number | undefined> = {
51+
...regular_table.saveColumnSizes(),
52+
};
5053
const { group_by } = this.model!._config;
5154
const tree_header_offset = group_by?.length > 0 ? group_by.length + 1 : 0;
5255

@@ -57,15 +60,21 @@ export function restore_column_size_overrides(
5760
| undefined;
5861
} else {
5962
const index = this.model!._column_paths.indexOf(key);
63+
// Skip keys that don't resolve to a known column — e.g. on the
64+
// first draw after `activate`, `_column_paths` has not yet been
65+
// populated by the data listener, so we leave any existing
66+
// `regular-table` widths untouched rather than clobbering them
67+
// with garbage indices.
68+
if (index === -1) {
69+
continue;
70+
}
6071
overrides[index + tree_header_offset] = old_sizes[key] as
6172
| number
6273
| undefined;
6374
}
6475
}
6576

66-
(this.regular_table as RegularTableWithOverrides).restoreColumnSizes(
67-
overrides,
68-
);
77+
regular_table.restoreColumnSizes(overrides);
6978
}
7079

7180
/**

packages/viewer-datagrid/src/ts/model/toolbar.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ export function toggle_edit_mode(
4444
if (this._edit_button !== undefined) {
4545
this._edit_button.dataset.editMode = mode;
4646
}
47-
48-
this.dataset.editMode = mode;
4947
}
5048

5149
export function toggle_scroll_lock(

packages/viewer-datagrid/src/ts/plugin/save.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,6 @@ export function save(
4141

4242
return JSON.parse(JSON.stringify(token));
4343
}
44+
4445
return {};
4546
}

packages/viewer-datagrid/src/ts/style_handlers/body.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ export function applyBodyCellStyles(
123123
plugin?.number_fg_mode === "bar" && is_numeric,
124124
);
125125

126+
td.classList.toggle(
127+
"psp-color-mode-label-bar",
128+
plugin?.number_fg_mode === "label-bar" && is_numeric,
129+
);
130+
126131
// Apply row header styling
127132
if (isHeader) {
128133
cell_style_row_header.call(this, regularTable, td, metadata as any);

packages/viewer-datagrid/src/ts/style_handlers/consolidated.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,9 @@ function isEditableMode(
7070
const has_pivots =
7171
model._config.group_by.length === 0 &&
7272
model._config.split_by.length === 0;
73+
7374
const selectable = viewer.hasAttribute("selectable");
74-
const plugin = viewer.children[0] as
75-
| (DatagridPluginElement & { dataset: DOMStringMap })
76-
| undefined;
77-
const editable = allowed || plugin?.dataset?.editMode === "EDIT";
75+
const editable = allowed || model._edit_mode === "EDIT";
7876
return has_pivots && !selectable && editable;
7977
}
8078

packages/viewer-datagrid/src/ts/style_handlers/group_header.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export function applyGroupHeaderStyles(
5454
);
5555

5656
td.classList.toggle("psp-color-mode-bar", false);
57+
td.classList.toggle("psp-color-mode-label-bar", false);
5758
td.classList.toggle("psp-header-sort-asc", false);
5859
td.classList.toggle("psp-header-sort-desc", false);
5960
td.classList.toggle("psp-header-sort-col-asc", false);

packages/viewer-datagrid/src/ts/style_handlers/table_cell/numeric.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ export function cell_style_numeric(
180180
) {
181181
(td.children[0] as HTMLElement).style.background = gradhex;
182182
}
183+
} else if (plugin?.number_fg_mode === "label-bar") {
184+
td.style.color = "";
185+
td.style.setProperty("--psp-label-bar-color", gradhex);
186+
td.style.setProperty("--psp-label-bar-bg", hex);
183187
} else if (plugin?.number_fg_mode === "color" || !plugin?.number_fg_mode) {
184188
td.style.color = hex;
185189
}

0 commit comments

Comments
 (0)