fixed dashboard sorting

This commit is contained in:
Torkel Ödegaard 2017-10-12 19:55:52 +02:00
parent 770a21b368
commit 83e496606e
3 changed files with 29 additions and 25 deletions

View File

@ -40,6 +40,13 @@ export class DashboardModel {
folderId: number;
panels: PanelModel[];
static nonPersistedProperties: {[str: string]: boolean} = {
"events": true,
"meta": true,
"panels": true, // needs special handling
"templating": true, // needs special handling
};
constructor(data, meta?) {
if (!data) {
data = {};
@ -118,30 +125,26 @@ export class DashboardModel {
// cleans meta data and other non peristent state
getSaveModelClone() {
// temp remove stuff
var events = this.events;
var meta = this.meta;
var variables = this.templating.list;
var panels = this.panels;
delete this.events;
delete this.meta;
delete this.panels;
// prepare save model
this.templating.list = _.map(variables, variable => variable.getSaveModel ? variable.getSaveModel() : variable);
this.panels = _.map(panels, panel => panel.getSaveModel());
// make clone
var copy = _.cloneDeep(this);
// sort clone
copy = sortByKeys(copy);
var copy: any = {};
for (var property in this) {
if (DashboardModel.nonPersistedProperties[property] || !this.hasOwnProperty(property)) {
continue;
}
// restore properties
this.events = events;
this.meta = meta;
this.templating.list = variables;
this.panels = panels;
copy[property] = _.cloneDeep(this[property]);
}
// get variable save models
copy.templating = {
list: _.map(this.templating.list, variable => variable.getSaveModel ? variable.getSaveModel() : variable),
};
// get panel save models
copy.panels = _.map(this.panels, panel => panel.getSaveModel());
// sort by keys
copy = sortByKeys(copy);
return copy;
}
@ -188,7 +191,7 @@ export class DashboardModel {
addPanel(panel) {
panel.id = this.getNextPanelId();
this.panels.push(new PanelModel(panel));
this.panels.unshift(new PanelModel(panel));
// make sure it's sorted by pos
this.panels.sort(function(panelA, panelB) {

View File

@ -47,8 +47,8 @@ describe('DashboardModel', function() {
var saveModel = model.getSaveModelClone();
var keys = _.keys(saveModel);
expect(keys[0]).to.be('autoUpdate');
expect(keys[1]).to.be('revision');
expect(keys[0]).to.be('annotations');
expect(keys[1]).to.be('autoUpdate');
});
});

View File

@ -103,6 +103,7 @@ describe('given dashboard with repeated panels', function() {
};
dash = new DashboardModel(dash, {});
dash.getSaveModelClone();
var exporter = new DashboardExporter(datasourceSrvStub);
exporter.makeExportable(dash).then(clean => {
exported = clean;