Commit 8593ca00 by Alexander Zobnin

repeat row: refactor

parent 88760983
...@@ -305,8 +305,6 @@ export class DashboardModel { ...@@ -305,8 +305,6 @@ export class DashboardModel {
} }
let clone = new PanelModel(sourcePanel.getSaveModel()); let clone = new PanelModel(sourcePanel.getSaveModel());
clone.id = this.getNextPanelId();
// for row clones we need to figure out panels under row to clone and where to insert clone // for row clones we need to figure out panels under row to clone and where to insert clone
let rowPanels, insertPos; let rowPanels, insertPos;
if (sourcePanel.collapsed) { if (sourcePanel.collapsed) {
...@@ -322,9 +320,7 @@ export class DashboardModel { ...@@ -322,9 +320,7 @@ export class DashboardModel {
} }
this.panels.splice(insertPos, 0, clone); this.panels.splice(insertPos, 0, clone);
clone.repeatIteration = this.iteration; this.updateRepeatedPanelIds(clone);
clone.repeatPanelId = sourcePanel.id;
clone.repeat = null;
return clone; return clone;
} }
...@@ -337,101 +333,117 @@ export class DashboardModel { ...@@ -337,101 +333,117 @@ export class DashboardModel {
return; return;
} }
let selected; if (panel.type === 'row') {
if (variable.current.text === 'All') { this.repeatRow(panel, panelIndex, variable);
selected = variable.options.slice(1, variable.options.length); return;
} else {
selected = _.filter(variable.options, {selected: true});
} }
let selectedOptions = this.getSelectedVariableOptions(variable);
let minWidth = panel.minSpan || 6; let minWidth = panel.minSpan || 6;
let xPos = 0; let xPos = 0;
let yPos = panel.gridPos.y; let yPos = panel.gridPos.y;
for (let index = 0; index < selected.length; index++) { for (let index = 0; index < selectedOptions.length; index++) {
let option = selected[index]; let option = selectedOptions[index];
let copy; let copy;
if (panel.type === 'row') { copy = this.getPanelRepeatClone(panel, index, panelIndex);
copy = this.getRowRepeatClone(panel, index, panelIndex);
copy.scopedVars = {}; copy.scopedVars = {};
copy.scopedVars[variable.name] = option; copy.scopedVars[variable.name] = option;
let rowHeight = this.getRowHeight(copy); if (panel.repeatDirection === REPEAT_DIR_VERTICAL) {
// if (rowHeight) { copy.gridPos.y = yPos;
let panelsBelowIndex; yPos += copy.gridPos.h;
let rowPanels = copy.panels || []; } else {
// insert after 'row' panel // set width based on how many are selected
let insertPos = panelIndex + ((rowPanels.length + 1) * index) + 1; // assumed the repeated panels should take up full row width
if (copy.collapsed) { copy.gridPos.w = Math.max(GRID_COLUMN_COUNT / selectedOptions.length, minWidth);
copy.gridPos.y += index; copy.gridPos.x = xPos;
yPos += index; copy.gridPos.y = yPos;
panelsBelowIndex = panelIndex + index + 1;
xPos += copy.gridPos.w;
// handle overflow by pushing down one row
if (xPos + copy.gridPos.w > GRID_COLUMN_COUNT) {
xPos = 0;
yPos += copy.gridPos.h;
}
}
}
}
_.each(copy.panels, (panel, i) => { repeatRow(panel: PanelModel, panelIndex: number, variable) {
let selectedOptions = this.getSelectedVariableOptions(variable);
let yPos = panel.gridPos.y;
function setScopedVars(panel, variableOption) {
panel.scopedVars = {}; panel.scopedVars = {};
panel.scopedVars[variable.name] = option; panel.scopedVars[variable.name] = variableOption;
}
if (index > 0) { for (let optionIndex = 0; optionIndex < selectedOptions.length; optionIndex++) {
panel.id = this.getNextPanelId(); let option = selectedOptions[optionIndex];
panel.repeatIteration = this.iteration; let rowCopy = this.getRowRepeatClone(panel, optionIndex, panelIndex);
panel.repeatPanelId = rowPanels[i].id; setScopedVars(rowCopy, option);
panel.repeat = null;
copy.panels[i] = panel; let rowHeight = this.getRowHeight(rowCopy);
let rowPanels = rowCopy.panels || [];
let panelBelowIndex;
if (panel.collapsed) {
// For collapsed row just copy its panels and set scoped vars and proper IDs
_.each(rowPanels, (rowPanel, i) => {
setScopedVars(rowPanel, option);
if (optionIndex > 0) {
this.updateRepeatedPanelIds(rowPanel);
} }
}); });
rowCopy.gridPos.y += optionIndex;
yPos += optionIndex;
panelBelowIndex = panelIndex + optionIndex + 1;
} else { } else {
// insert after 'row' panel
let insertPos = panelIndex + ((rowPanels.length + 1) * optionIndex) + 1;
_.each(rowPanels, (rowPanel, i) => { _.each(rowPanels, (rowPanel, i) => {
rowPanel.scopedVars = {}; setScopedVars(rowPanel, option);
rowPanel.scopedVars[variable.name] = option; if (optionIndex > 0) {
if (index > 0) {
let cloneRowPanel = new PanelModel(rowPanel); let cloneRowPanel = new PanelModel(rowPanel);
cloneRowPanel.id = this.getNextPanelId(); this.updateRepeatedPanelIds(cloneRowPanel);
cloneRowPanel.repeatIteration = this.iteration; // For exposed row additionally set proper Y grid position and add it to dashboard panels
cloneRowPanel.repeatPanelId = rowPanel.id; cloneRowPanel.gridPos.y += rowHeight * optionIndex;
cloneRowPanel.repeat = null;
cloneRowPanel.gridPos.y += rowHeight * index;
this.panels.splice(insertPos+i, 0, cloneRowPanel); this.panels.splice(insertPos+i, 0, cloneRowPanel);
} }
}); });
copy.panels = []; rowCopy.panels = [];
copy.gridPos.y += rowHeight * index; rowCopy.gridPos.y += rowHeight * optionIndex;
yPos += rowHeight; yPos += rowHeight;
panelsBelowIndex = insertPos+rowPanels.length; panelBelowIndex = insertPos+rowPanels.length;
} }
// Update gridPos for panels below // Update gridPos for panels below
for (let i = panelsBelowIndex; i< this.panels.length; i++) { for (let i = panelBelowIndex; i< this.panels.length; i++) {
this.panels[i].gridPos.y += yPos; this.panels[i].gridPos.y += yPos;
} }
} else {
copy = this.getPanelRepeatClone(panel, index, panelIndex);
copy.scopedVars = {};
copy.scopedVars[variable.name] = option;
if (panel.repeatDirection === REPEAT_DIR_VERTICAL) {
copy.gridPos.y = yPos;
yPos += copy.gridPos.h;
} else {
// set width based on how many are selected
// assumed the repeated panels should take up full row width
copy.gridPos.w = Math.max(GRID_COLUMN_COUNT / selected.length, minWidth);
copy.gridPos.x = xPos;
copy.gridPos.y = yPos;
xPos += copy.gridPos.w;
// handle overflow by pushing down one row
if (xPos + copy.gridPos.w > GRID_COLUMN_COUNT) {
xPos = 0;
yPos += copy.gridPos.h;
} }
} }
updateRepeatedPanelIds(panel: PanelModel) {
panel.repeatPanelId = panel.id;
panel.id = this.getNextPanelId();
panel.repeatIteration = this.iteration;
panel.repeat = null;
return panel;
} }
getSelectedVariableOptions(variable) {
let selectedOptions;
if (variable.current.text === 'All') {
selectedOptions = variable.options.slice(1, variable.options.length);
} else {
selectedOptions = _.filter(variable.options, {selected: true});
} }
return selectedOptions;
} }
getRowHeight(rowPanel: PanelModel): number { getRowHeight(rowPanel: PanelModel): number {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment