Skip to content

Commit

Permalink
Merge pull request finos#1927 from finos/datagrid-refactor
Browse files Browse the repository at this point in the history
Refactor `@finos/perspective-viewer-datagrid`
  • Loading branch information
texodus authored Aug 2, 2022
2 parents fe5ac9b + 64027b2 commit bb35a4f
Show file tree
Hide file tree
Showing 77 changed files with 3,678 additions and 2,160 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ and/or [Jupyterlab](https://jupyterlab.readthedocs.io/en/stable/).
## Documentation

* [Project Site](https://perspective.finos.org/)
* [Table](https://perspective.finos.org/docs/md/table.html)
* [View](https://perspective.finos.org/docs/md/view.html)
* [Javascript User Guide](https://perspective.finos.org/docs/md/js.html)
* [Python User Guide](https://perspective.finos.org/docs/md/python.html)
* [Data Binding](https://perspective.finos.org/docs/md/table.html)
* [Developer Guide](https://perspective.finos.org/docs/md/development.html)
* [Table](https://perspective.finos.org/docs/table.html)
* [View](https://perspective.finos.org/docs/view.html)
* [Javascript User Guide](https://perspective.finos.org/docs/js.html)
* [Python User Guide](https://perspective.finos.org/docs/python.html)
* [Data Binding](https://perspective.finos.org/docs/table.html)
* [Developer Guide](https://perspective.finos.org/docs/development.html)
* [Perspective API](https://github.com/finos/perspective/blob/master/packages/perspective/README.md)
* [Perspective Viewer API](https://github.com/finos/perspective/blob/master/packages/perspective-viewer/README.md)
* [Perspective Python API](https://perspective.finos.org/docs/obj/perspective-python.html)
Expand Down
4 changes: 2 additions & 2 deletions examples/blocks/src/custom/custom_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class CustomDatagridPlugin extends customElements.get(

async styleListener() {
const viewer = this.parentElement;
const datagrid = this.datagrid;
const datagrid = this.regular_table;
if (this._dirty) {
await this.refresh_cache();
}
Expand Down Expand Up @@ -213,7 +213,7 @@ class CustomDatagridPlugin extends customElements.get(
this._dirty = true;
if (!this._custom_initialized) {
const viewer = this.parentElement;
const datagrid = this.datagrid;
const datagrid = this.regular_table;
this._max = -Infinity;
await this.refresh_cache(view);
const table = await viewer.getTable(true);
Expand Down
8 changes: 4 additions & 4 deletions packages/perspective-jupyterlab/test/results/results.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"__GIT_COMMIT__": "d063eaceb4f0a4465a0453456fc287f47c9e9e78",
"__GIT_COMMIT__": "be50bcdfb1c1160ccc560af452db54446386ffcf",
"resize_Config_should_show_by_default": "f492628efc5ce5c9e1bab910cf2557c9",
"resize_Resize_the_container_causes_the_widget_to_resize": "0f8af36778efdff9b9cff3a3b155d2b3",
"resize_group_by_traitlet_works": "52f47ba2da28aa605a3d55051cf866c1",
"resize_Config_should_be_hidden_by_default": "a77537b1d9156d296a7b1eef59105d80"
"resize_Resize_the_container_causes_the_widget_to_resize": "e3c617dd8acc99d2026d4cd2436d66ab",
"resize_group_by_traitlet_works": "f86409c2ccbdf3613a4aa20fc24cc543",
"resize_Config_should_be_hidden_by_default": "5ca5a07cbb0d81e16939a622c41575c0"
}
46 changes: 23 additions & 23 deletions packages/perspective-viewer-d3fc/test/results/results.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"__GIT_COMMIT__": "f898b5486e7f2c6bf61f8648c8e6686cdee42535",
"__GIT_COMMIT__": "be50bcdfb1c1160ccc560af452db54446386ffcf",
"area_shows_a_grid_without_any_settings_applied": "67bab9ea6654cc7cf7c7b096824e610a",
"area_displays_visible_columns_": "9c195f0f7bf184a4cb9e727f1b37b301",
"area_pivot_by_a_row": "fbae982b73c9cbf91be40a767cbbd3df",
Expand Down Expand Up @@ -121,28 +121,28 @@
"sunburst_pivot_by_two_rows": "740a97d15d12ed2818e4e7404efa1290",
"sunburst_pivot_by_a_row_and_a_column": "a9cc4064d026667a625ef9ca2fb927ff",
"sunburst_pivot_by_two_rows_and_two_columns": "fe25573f98f12bfa788991c219978d35",
"treemap_pivot_by_a_row": "e499b4b4c91d9fdaafaf20d133b4943c",
"treemap_pivot_by_two_rows": "5e32d58d2070756dfa461b4eb1da9b7d",
"treemap_pivot_by_a_row_and_a_column": "e0d9e2504e068c9d6bb4519e0e6f2efe",
"treemap_pivot_by_two_rows_and_two_columns": "b61ebd6eb13fc292a95d5897af6edeb5",
"treemap_shows_a_grid_without_any_settings_applied": "10d1208b485425756fcc932229386b02",
"treemap_displays_visible_columns_": "10d1208b485425756fcc932229386b02",
"treemap_pivot_by_a_column": "10d1208b485425756fcc932229386b02",
"treemap_sort_by_a_hidden_column": "10d1208b485425756fcc932229386b02",
"treemap_sort_by_a_numeric_column": "10d1208b485425756fcc932229386b02",
"treemap_sort_by_an_alpha_column": "10d1208b485425756fcc932229386b02",
"treemap_filters_filters_by_a_numeric_column": "10d1208b485425756fcc932229386b02",
"treemap_filters_filters_by_an_alpha_column": "10d1208b485425756fcc932229386b02",
"treemap_filters_filters_with__in__comparator": "10d1208b485425756fcc932229386b02",
"sunburst_shows_a_grid_without_any_settings_applied": "10d1208b485425756fcc932229386b02",
"sunburst_displays_visible_columns_": "10d1208b485425756fcc932229386b02",
"sunburst_pivot_by_a_column": "10d1208b485425756fcc932229386b02",
"sunburst_sort_by_a_hidden_column": "10d1208b485425756fcc932229386b02",
"sunburst_sort_by_a_numeric_column": "10d1208b485425756fcc932229386b02",
"sunburst_sort_by_an_alpha_column": "10d1208b485425756fcc932229386b02",
"sunburst_filters_filters_by_a_numeric_column": "10d1208b485425756fcc932229386b02",
"sunburst_filters_filters_by_an_alpha_column": "10d1208b485425756fcc932229386b02",
"sunburst_filters_filters_with__in__comparator": "10d1208b485425756fcc932229386b02",
"treemap_pivot_by_a_row": "f39b569dba81f64090d60d767a5337de",
"treemap_pivot_by_two_rows": "646a5eb3ba2790f768f91b9681b59017",
"treemap_pivot_by_a_row_and_a_column": "95fb39573f9d966d7c4d3808d7fbae47",
"treemap_pivot_by_two_rows_and_two_columns": "9d848629342c6b541a3d949451232f05",
"treemap_shows_a_grid_without_any_settings_applied": "80705a191c3675ed34d3058874f09a00",
"treemap_displays_visible_columns_": "80705a191c3675ed34d3058874f09a00",
"treemap_pivot_by_a_column": "80705a191c3675ed34d3058874f09a00",
"treemap_sort_by_a_hidden_column": "80705a191c3675ed34d3058874f09a00",
"treemap_sort_by_a_numeric_column": "80705a191c3675ed34d3058874f09a00",
"treemap_sort_by_an_alpha_column": "80705a191c3675ed34d3058874f09a00",
"treemap_filters_filters_by_a_numeric_column": "80705a191c3675ed34d3058874f09a00",
"treemap_filters_filters_by_an_alpha_column": "80705a191c3675ed34d3058874f09a00",
"treemap_filters_filters_with__in__comparator": "80705a191c3675ed34d3058874f09a00",
"sunburst_shows_a_grid_without_any_settings_applied": "80705a191c3675ed34d3058874f09a00",
"sunburst_displays_visible_columns_": "80705a191c3675ed34d3058874f09a00",
"sunburst_pivot_by_a_column": "80705a191c3675ed34d3058874f09a00",
"sunburst_sort_by_a_hidden_column": "80705a191c3675ed34d3058874f09a00",
"sunburst_sort_by_a_numeric_column": "80705a191c3675ed34d3058874f09a00",
"sunburst_sort_by_an_alpha_column": "80705a191c3675ed34d3058874f09a00",
"sunburst_filters_filters_by_a_numeric_column": "80705a191c3675ed34d3058874f09a00",
"sunburst_filters_filters_by_an_alpha_column": "80705a191c3675ed34d3058874f09a00",
"sunburst_filters_filters_with__in__comparator": "80705a191c3675ed34d3058874f09a00",
"bar_rendering_bugs_correctly_render_when_a_bar_chart_has_non_equidistant_times_on_a_datetime_axis": "96a10ce528c635ca24fa509e899bbb09",
"events_perspective-config-update_event_is_fired_when_series_axis_is_changed": "c0cf24c214ad730393fdd7d58c843a8d",
"events_perspective-config-update_event_is_fired_when_legend_position_is_changed": "9753be00c0d084dfbe592b46f6af6b08"
Expand Down
6 changes: 3 additions & 3 deletions packages/perspective-viewer-datagrid/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const BUILD = [
define: {
global: "window",
},
entryPoints: ["src/js/plugin.js"],
entryPoints: ["src/js/index.js"],
plugins: [InlineCSSPlugin(), NodeModulesExternal()],
format: "esm",
loader: {
Expand All @@ -24,7 +24,7 @@ const BUILD = [
define: {
global: "window",
},
entryPoints: ["src/js/plugin.js"],
entryPoints: ["src/js/index.js"],
globalName: "perspective_datagrid",
plugins: [InlineCSSPlugin(), UMDLoader()],
format: "cjs",
Expand All @@ -37,7 +37,7 @@ const BUILD = [
define: {
global: "window",
},
entryPoints: ["src/js/plugin.js"],
entryPoints: ["src/js/index.js"],
plugins: [InlineCSSPlugin()],
format: "esm",
loader: {
Expand Down
47 changes: 10 additions & 37 deletions packages/perspective-viewer-datagrid/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,20 @@
import {View} from "@finos/perspective";
import {RegularTableElement} from "regular-table";
import type {IPerspectiveViewerPlugin} from "@finos/perspective-viewer";

declare global {
interface CustomElementRegistry {
get(
tagName: "perspective-viewer-datagrid"
): PerspectiveViewerDatagridPluginElement;
): HTMLPerspectiveViewerDatagridPluginElement;

whenDefined(
tagName: "perspective-viewer-datagrid"
): Promise<PerspectiveViewerDatagridPluginElement>;
// TODO is this needed?
whenDefined(tagName: "perspective-viewer-datagrid"): Promise<void>;
}
}

export declare class PerspectiveViewerDatagridPluginElement extends HTMLElement {
public readonly datagrid: RegularTableElement;

// private methods
private _toggle_edit_mode(force?: boolean): void;
private _toggle_scroll_lock(force?: boolean): void;
private _restore_column_size_overrides(old_sizes: any, cache: boolean);
private _save_column_size_overrides(): any;

// getters accessors
public get name(): string;
public get select_mode(): string;
public get min_config_columns(): string;
public get config_column_names(): string;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface HTMLPerspectiveViewerDatagridPluginElement
extends IPerspectiveViewerPlugin {}

// customElements methods
protected connectedCallback(): void;
protected disconnectedCallback(): void;

// view related methods
public activate(view: View): Promise<void>;
public draw(view: View): Promise<void>;
public update(view: View): Promise<void>;
public restyle(view: View): Promise<void>;

// other public methods
public resize(): Promise<void>;
public clear(): Promise<void>;
public delete(): void;
public save(): Promise<void>;
public restore(token: unknown): Promise<void>;
}
export declare class HTMLPerspectiveViewerDatagridPluginElement
extends HTMLElement
implements IPerspectiveViewerPlugin {}
31 changes: 31 additions & 0 deletions packages/perspective-viewer-datagrid/src/js/color_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
*
*/

import chroma from "chroma-js";

export function blend(a, b) {
return chroma.mix(a, `rgb(${b[0]},${b[1]},${b[2]})`, 0.5).hex();
}

// AFAICT `chroma-js` has no alpha-aware blending? So we need a function to get
// the color of a heatmap cell over the background.
export function rgbaToRgb([r, g, b, a], source = [255, 255, 255]) {
Expand All @@ -24,3 +30,28 @@ export function infer_foreground_from_background([r, g, b]) {
? "#161616"
: "#ffffff";
}

function make_gradient(chromahex) {
const [r, g, b] = chromahex.rgb();
const [r1, g1, b1] = chromahex
.set("hsl.h", (chromahex.get("hsl.h") - 15) % 360)
.rgb();
const [r2, g2, b2] = chromahex
.set("hsl.h", (chromahex.get("hsl.h") + 15) % 360)
.rgb();
return `linear-gradient(to right top,rgb(${r1},${g1},${b1}),rgb(${r},${g},${b}) 50%,rgb(${r2},${g2},${b2}))`;
}

export function make_color_record(color) {
const chroma_neg = chroma(color);
const _neg_grad = make_gradient(chroma_neg);
const rgb = chroma_neg.rgb();

return [
color,
...rgb,
_neg_grad,
`rgba(${rgb[0]},${rgb[1]},${rgb[2]},1)`,
`rgba(${rgb[0]},${rgb[1]},${rgb[2]},0)`,
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

import {activate} from "../plugin/activate.js";
import {restore} from "../plugin/restore.js";
import {connectedCallback} from "../plugin/connected";
import {save} from "../plugin/save";
import {draw} from "../plugin/draw";

/**
* The custom element class for this plugin. The interface methods for this
*/
export class HTMLPerspectiveViewerDatagridPluginElement extends HTMLElement {
constructor() {
super();
this.regular_table = document.createElement("regular-table");
this._is_scroll_lock = true;
}

connectedCallback() {
return connectedCallback.call(this);
}

disconnectedCallback() {
this._toolbar.parentElement.removeChild(this._toolbar);
}

async activate(view) {
return await activate.call(this, view);
}

get name() {
return "Datagrid";
}

get category() {
return "Basic";
}

get select_mode() {
return "toggle";
}

get min_config_columns() {
return undefined;
}

get config_column_names() {
return undefined;
}

async draw(view) {
return await draw.call(this, view);
}

async update(view) {
this.model._num_rows = await view.num_rows();
await this.regular_table.draw();
}

async resize() {
if (!this.isConnected || this.offsetParent == null) {
return;
}

if (this._initialized) {
await this.regular_table.draw();
}
}

async clear() {
this.regular_table._resetAutoSize();
this.regular_table.clear();
}

save() {
return save.call(this);
}

restore(token) {
return restore.call(this, token);
}

async restyle(view) {
await this.draw(view);
}

delete() {
if (this.regular_table.table_model) {
this.regular_table._resetAutoSize();
}
this.regular_table.clear();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/******************************************************************************
*
* Copyright (c) 2017, the Perspective Authors.
*
* This file is part of the Perspective library, distributed under the terms of
* the Apache License 2.0. The full license can be found in the LICENSE file.
*
*/

import TOOLBAR_STYLE from "../../less/toolbar.less";
import {toggle_edit_mode, toggle_scroll_lock} from "../model/toolbar";

/**
* The custom element for this plugin's toolbar, a component which displays in
* the host `<perspective-viewer>`'s status bar when this plugin is active.
* In the case of Datagrid, this comprises "Editable" and "Scroll Lock" toggle
* buttons.
*/
export class HTMLPerspectiveViewerDatagridToolbarElement extends HTMLElement {
connectedCallback() {
if (this._initialized) {
return;
}

this._initialized = true;
this.setAttribute("slot", "plugin-settings");
this.attachShadow({mode: "open"});
this.shadowRoot.innerHTML = `
<style>
${TOOLBAR_STYLE}
</style>
<div id="toolbar">
<span id="scroll_lock" class="lock-scroll button">
<span>Align Scroll</span>
</span>
<span id="edit_mode" class="button"><span>Read Only</span></span>
</div>
`;

const viewer = this.parentElement;
const plugin = viewer.querySelector("perspective-viewer-datagrid");
plugin._scroll_lock = this.shadowRoot.querySelector("#scroll_lock");
plugin._scroll_lock.addEventListener("click", () =>
toggle_scroll_lock.call(plugin)
);

plugin._edit_mode = this.shadowRoot.querySelector("#edit_mode");
plugin._edit_mode.addEventListener("click", () => {
toggle_edit_mode.call(plugin);
plugin.regular_table.draw();
viewer.dispatchEvent(new Event("perspective-config-update"));
});
}
}
Loading

0 comments on commit bb35a4f

Please sign in to comment.