Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated panel_state to ts gridData i vs id #22515

Merged
merged 3 commits into from
Aug 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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