Skip to content

Commit

Permalink
updated panel_state to ts gridData i vs id (#22515)
Browse files Browse the repository at this point in the history
* updated panel state to ts gridData i vs id property

* changed gridData.id to gridData.i
  • Loading branch information
rshen91 authored Aug 30, 2018
1 parent 853d321 commit 11d6e53
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ beforeAll(() => {
y: 1,
w: 1,
h: 1,
id: 'hi',
i: 'hi',
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@
* under the License.
*/

import { DASHBOARD_GRID_COLUMN_COUNT, DEFAULT_PANEL_WIDTH, DEFAULT_PANEL_HEIGHT } from '../dashboard_constants';
import chrome from 'ui/chrome';
import {
DASHBOARD_GRID_COLUMN_COUNT,
DEFAULT_PANEL_HEIGHT,
DEFAULT_PANEL_WIDTH,
} from '../dashboard_constants';
import { PanelState } from '../selectors';

/**
* Represents a panel on a grid. Keeps track of position in the grid and what visualization it
Expand All @@ -40,13 +45,12 @@ import chrome from 'ui/chrome';
*/

// Look for the smallest y and x value where the default panel will fit.
function findTopLeftMostOpenSpace(width, height, currentPanels) {
function findTopLeftMostOpenSpace(width: number, height: number, currentPanels: PanelState[]) {
let maxY = -1;

for (let i = 0; i < currentPanels.length; i++) {
const panel = currentPanels[i];
currentPanels.forEach(panel => {
maxY = Math.max(panel.gridData.y + panel.gridData.h, maxY);
}
});

// Handle case of empty grid.
if (maxY < 0) {
Expand All @@ -58,14 +62,13 @@ function findTopLeftMostOpenSpace(width, height, currentPanels) {
grid[y] = new Array(DASHBOARD_GRID_COLUMN_COUNT).fill(0);
}

for (let i = 0; i < currentPanels.length; i++) {
const panel = currentPanels[i];
currentPanels.forEach(panel => {
for (let x = panel.gridData.x; x < panel.gridData.x + panel.gridData.w; x++) {
for (let y = panel.gridData.y; y < panel.gridData.y + panel.gridData.h; y++) {
grid[y][x] = 1;
}
}
}
});

for (let y = 0; y < maxY; y++) {
for (let x = 0; x < DASHBOARD_GRID_COLUMN_COUNT; x++) {
Expand Down Expand Up @@ -104,21 +107,29 @@ function findTopLeftMostOpenSpace(width, height, currentPanels) {
* @param {Array} currentPanels
* @return {PanelState}
*/
export function createPanelState(id, type, panelIndex, currentPanels) {
const { x, y } = findTopLeftMostOpenSpace(DEFAULT_PANEL_WIDTH, DEFAULT_PANEL_HEIGHT, currentPanels);
export function createPanelState(
id: string,
type: string,
panelIndex: string,
currentPanels: PanelState[]
) {
const { x, y } = findTopLeftMostOpenSpace(
DEFAULT_PANEL_WIDTH,
DEFAULT_PANEL_HEIGHT,
currentPanels
);
return {
gridData: {
w: DEFAULT_PANEL_WIDTH,
h: DEFAULT_PANEL_HEIGHT,
x,
y,
i: panelIndex.toString()
i: panelIndex.toString(),
},
version: chrome.getKibanaVersion(),
panelIndex: panelIndex.toString(),
type: type,
id: id,
type,
id,
embeddableConfig: {},
};
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ beforeAll(() => {
embeddableConfig: {},
gridData: {
h: 0,
id: '0',
i: '0',
w: 0,
x: 0,
y: 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const originalPanelData = {
embeddableConfig: {},
gridData: {
h: 0,
id: '0',
i: '0',
w: 0,
x: 0,
y: 0,
Expand All @@ -47,7 +47,7 @@ describe('UpdatePanel', () => {
...originalPanelData,
gridData: {
h: 1,
id: '1',
i: '1',
w: 10,
x: 1,
y: 5,
Expand All @@ -60,7 +60,7 @@ describe('UpdatePanel', () => {
expect(panel.gridData.y).toBe(5);
expect(panel.gridData.w).toBe(10);
expect(panel.gridData.h).toBe(1);
expect(panel.gridData.id).toBe('1');
expect(panel.gridData.i).toBe('1');
});

test('should allow updating an array that contains fewer values', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export interface GridData {
readonly h: number;
readonly x: number;
readonly y: number;
readonly id: string;
readonly i: string;
}

export type PanelId = string;
Expand Down

0 comments on commit 11d6e53

Please sign in to comment.