Skip to content

Commit

Permalink
Feature/cldn 1968 (Display JSON data inline) (#268)
Browse files Browse the repository at this point in the history
* Add buttons to expand and minimize JSON data as well as ability to expand and/or collapse all rows in a certain column

* [CLDN-1968] Added expand button for full row

* [CLDN-1968] Resize JSON columns

* [CLDN-1968] Added new array which tracks JSON cell state

* Revert "[CLDN-1968] Added new array which tracks JSON cell state"

This reverts commit dabc3da.

* [CLDN-1968] Added ability for row level expand all button to track if cells are expanded or not

* [CLDN-1968] Ran pre-commit hook

* [CLDN-1968] Improved UI

* [CLDN-1968] Update image tag for testing

* [CLDN-1968] Revert image tag for testing

* [CLDN-1968] Added multiple UI/UX changes based on QA feedback

* [CLDN-1968] Added more UI/UX changes based on QA feedback

* [CLDN-1968] Temp change to image

* Revert "[CLDN-1968] Temp change to image"

This reverts commit 57490bd.

* Update superset-frontend/src/cccs-viz/plugins/plugin-chart-cccs-grid/src/CccsGrid.tsx

Co-authored-by: cccs-rc <62034438+cccs-rc@users.noreply.github.com>

* [CLDN-1968] Remove 'TODO's as they are no longer needed

* [CLDN-1968] Changed a variable name, and condensed a few lines

* [CLDN-1968] Modified a setState so that only one is needed instead of 2

---------

Co-authored-by: cccs-rc <62034438+cccs-rc@users.noreply.github.com>
  • Loading branch information
2 people authored and cccs-RyanK committed Apr 20, 2023
1 parent 701a7e7 commit 17c2182
Show file tree
Hide file tree
Showing 11 changed files with 591 additions and 257 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
.Button {
background-color: transparent;
border: solid rgb(137, 137, 137);
border-width: 0 2px 2px 0;
display: inline-block;
padding-right: 3px;
padding-left: 3px;
padding-top: 3px;
padding-bottom: 3px;
vertical-align: middle;
margin-right: 10px;
margin-left: 5px;
}

.Expand {
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
}

.Collapse {
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
}

.Row-Expand {
background-color: transparent;
text-decoration: underline;
vertical-align: middle;
border: none;
color: rgb(31, 167, 201);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import Ipv4ValueRenderer from './Ipv4ValueRenderer';
import Ipv6ValueRenderer from './Ipv6ValueRenderer';
import DomainValueRenderer from './DomainValueRenderer';
import JsonValueRenderer from './JsonValueRenderer';
import ExpandAllValueRenderer from './ExpandAllValueRenderer';
import CustomTooltip from './CustomTooltip';

/// / jcc
Expand Down Expand Up @@ -222,7 +223,8 @@ export default function CccsGrid({
'separator',
{
name: 'Emit Filter(s)',
disabled: params.value === null,
disabled:
params.value === null || 'undefined' in selectedDataByColumnName,
action: () => handleChange(selectedDataByColumnName),
// eslint-disable-next-line theme-colors/no-literal-colors
icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" class=""><path fill-rule="evenodd" clip-rule="evenodd" d="M18.1573 17.864C21.2763 14.745 21.2763 9.66935 18.1573 6.5503C15.0382 3.43125 9.96264 3.43125 6.84359 6.5503L5.42938 5.13609C9.32836 1.2371 15.6725 1.2371 19.5715 5.13609C23.4705 9.03507 23.4705 15.3792 19.5715 19.2782C15.6725 23.1772 9.32836 23.1772 5.42938 19.2782L6.84359 17.864C9.96264 20.9831 15.0375 20.9838 18.1573 17.864ZM2.00035 11.5C2.00035 11.2239 2.2242 11 2.50035 11H5.00035L5.00035 10C5.00035 9.58798 5.47073 9.35279 5.80035 9.60001L9.00035 12C9.17125 12.1032 6.98685 13.637 5.77613 14.4703C5.44613 14.6975 5.00035 14.4601 5.00035 14.0595V13L2.50035 13C2.22421 13 2.00035 12.7761 2.00035 12.5L2.00035 11.5ZM9.67202 9.37873C11.2319 7.81885 13.7697 7.81956 15.3289 9.37873C16.888 10.9379 16.8887 13.4757 15.3289 15.0356C13.769 16.5955 11.2312 16.5948 9.67202 15.0356L8.2578 16.4498C10.5976 18.7896 14.4033 18.7896 16.7431 16.4498C19.0829 14.11 19.0829 10.3043 16.7431 7.96451C14.4033 5.6247 10.5976 5.6247 8.2578 7.96451L9.67202 9.37873Z" fill="#20A7C9"></path></svg>',
Expand Down Expand Up @@ -269,12 +271,67 @@ export default function CccsGrid({
],
);

const getMainMenuItems = useCallback(params => {
// If the column currently selected has either JSON data, or the
// expand all button for the row, add extra options
if (
params.column.colDef.cellRenderer === 'jsonValueRenderer' ||
params.column.colDef.cellRenderer === 'expandAllValueRenderer'
) {
// Get the default menu items so we can add to them
const jsonMenuItems = params.defaultItems.slice(0);

// Get all of the cell renderers and the current column ID
const instances = params.api.getCellRendererInstances();
const columnID = params.column.colId;

// Only keep cells which belong to the current column
const newInstances = instances.filter(
(instance: any) => instance.params.column.colId === columnID,
);

// Add an expand all button which will send an update to each cell renderer
jsonMenuItems.push({
name:
params.column.colDef.cellRenderer === 'jsonValueRenderer'
? 'Expand Column'
: 'Expand All',
action: () => {
newInstances.map((instance: any) =>
instance.componentInstance.updateState(true),
);
},
});

// Add a collapse all button which will send an update to each cell renderer
jsonMenuItems.push({
name:
params.column.colDef.cellRenderer === 'jsonValueRenderer'
? 'Collapse Column'
: 'Collapse All',
action: () => {
newInstances.map((instance: any) =>
instance.componentInstance.updateState(false),
);
},
});

// Return the default menu with added options (expand/collapse)
return jsonMenuItems;
}

// If the column currently selected has neither JSON data, nor the
// expand all button for the row, use the regular menu
return params.defaultItems;
}, []);

const frameworkComponents = {
countryValueRenderer: CountryValueRenderer,
ipv4ValueRenderer: Ipv4ValueRenderer,
ipv6ValueRenderer: Ipv6ValueRenderer,
domainValueRenderer: DomainValueRenderer,
jsonValueRenderer: JsonValueRenderer,
expandAllValueRenderer: ExpandAllValueRenderer,
customTooltip: CustomTooltip,
};

Expand All @@ -291,6 +348,21 @@ export default function CccsGrid({
}
};

const onFirstDataRendered = (params: any) => {
// Get all of the columns in the grid
const instances = params.columnApi.getAllColumns();

// Filter on columns that have JSON data
const newInstances = instances.filter(
(instance: any) => instance.colDef?.cellRenderer === 'jsonValueRenderer',
);

// Set the columns which have JSON data to be 350 pixels wide
newInstances.map((instance: any) =>
params.columnApi.setColumnWidth(instance.colId, 350),
);
};

const onSelectionChanged = (params: any) => {
const gridApi = params.api;
const selectedRows = gridApi.getSelectedRows();
Expand Down Expand Up @@ -496,7 +568,9 @@ export default function CccsGrid({
gridOptions={gridOptions}
onGridColumnsChanged={autoSizeFirst100Columns}
getContextMenuItems={getContextMenuItems}
getMainMenuItems={getMainMenuItems}
onGridReady={onGridReady}
onFirstDataRendered={onFirstDataRendered}
onRangeSelectionChanged={onRangeSelectionChanged}
onSelectionChanged={onSelectionChanged}
rowData={rowData}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { GroupCellRenderer } from '@ag-grid-enterprise/all-modules';
import React, { Component } from 'react';
import './Buttons.css';

// Show a button to collapse all of the JSON blobs in the row
function collapseJSON(this: any, reverseState: any) {
return (
<button className="Row-Expand" type="button" onClick={reverseState}>
Collapse Row
</button>
);
}

// Show a button to expand all of the JSON blobs in the row
function expandJSON(this: any, reverseState: any) {
return (
<>
<button className="Row-Expand" type="button" onClick={reverseState}>
Expand Row
</button>
</>
);
}

export default class ExpandAllValueRenderer extends Component<
{},
{ api: any; expanded: boolean; rowIndex: number }
> {
constructor(props: any) {
super(props);

this.state = {
api: props.api,
expanded: false,
rowIndex: props.rowIndex,
};
}

// Get all of the cells in the AG Grid and only keep the ones that
// are in the same row as the current expand all button, and make
// sure that they have a JSON blob
getJSONCells = () => {
const instances = this.state.api.getCellRendererInstances();

// Make sure row grouping is not enabled, but if it is, don't
// try to find all of the JSON blobs in the row
if (
instances.filter((instance: any) => instance instanceof GroupCellRenderer)
.length === 0
) {
const newInstances = instances.filter(
(instance: any) =>
instance.params.rowIndex === this.state.rowIndex &&
instance.params.column.colDef.cellRenderer === 'jsonValueRenderer',
);

return newInstances;
}
return [];
};

// Set the current `expanded` field to the opposite of what it currently is
// as well as go through each cell renderer and if it's in the same row &
// it's a cell with a JSON blob, update whether it is expanded or not
reverseState = () => {
this.setState(prevState => ({
...prevState,
expanded: !prevState.expanded,
}));

const newInstances = this.getJSONCells();

newInstances.map((instance: any) =>
instance.componentInstance.updateState(!this.state.expanded),
);
};

// Set the current `expanded` field to be equal to the boolean being passed in
// as well as go through each cell renderer and if it's in the same row &
// it's a cell with a JSON blob, update whether it is expanded or not
updateState = (newFlag: any) => {
this.setState(prevState => ({ ...prevState, expanded: newFlag }));

const newInstances = this.getJSONCells();

newInstances.map((instance: any) =>
instance.componentInstance.updateState(newFlag),
);
};

// Get all of the cells in the AG Grid and only keep the ones
// that are in the same row as the current expand all button and
// make sure that they have a JSON blob (and see whether they are
// expanded or not)
checkState = () => {
const newInstances = this.getJSONCells();

const jsonCellExpandedValues = newInstances.map((instance: any) =>
instance.componentInstance.getExpandedValue(),
);

// If there is at least one cell that can expand, the expand all
// button for the row should show 'Expand'
this.setState(prevState => ({
...prevState,
expanded: !jsonCellExpandedValues.includes(false),
}));
};

// Show either the expand or collapse button dependent
// on the value of the `expanded` field
render() {
if (this.state.expanded === false) {
return expandJSON(this.reverseState);
}
return collapseJSON(this.reverseState);
}
}
Loading

0 comments on commit 17c2182

Please sign in to comment.