Add a keybinding that toggles all legends in a dashboard

This commit is contained in:
Jon Ferreira 2019-03-04 10:57:22 -05:00
parent c36047674a
commit 46fa09fdad
5 changed files with 49 additions and 0 deletions

View File

@ -27,6 +27,7 @@ export class HelpCtrl {
{ keys: ['d', 'C'], description: 'Collapse all rows' },
{ keys: ['d', 'a'], description: 'Toggle auto fit panels (experimental feature)' },
{ keys: ['mod+o'], description: 'Toggle shared graph crosshair' },
{ keys: ['d', 'l'], description: 'Toggle all panel legends' },
],
'Focused Panel': [
{ keys: ['e'], description: 'Toggle panel edit view' },

View File

@ -256,6 +256,11 @@ export class KeybindingSrv {
}
});
// toggle all panel legends
this.bind('d l', () => {
dashboard.toggleLegendsForAll();
});
// collapse all rows
this.bind('d shift+c', () => {
dashboard.collapseRows();

View File

@ -635,4 +635,32 @@ describe('DashboardModel', () => {
expect(saveModel.templating.list[0].filters[0].value).toBe('server 1');
});
});
describe('Given a dashboard with one panel legend on and two off', () => {
let model;
beforeEach(() => {
const data = {
panels: [
{ id: 1, type: 'graph', gridPos: { x: 0, y: 0, w: 24, h: 2 }, legend: { show: true } },
{ id: 3, type: 'graph', gridPos: { x: 0, y: 4, w: 12, h: 2 }, legend: { show: false } },
{ id: 4, type: 'graph', gridPos: { x: 12, y: 4, w: 12, h: 2 }, legend: { show: false } },
],
};
model = new DashboardModel(data);
});
it('toggleLegendsForAll should toggle all legends on on first execution', () => {
model.toggleLegendsForAll();
const legendsOn = model.panels.filter(panel => panel.legend.show === true);
expect(legendsOn.length).toBe(3);
});
it('toggleLegendsForAll should toggle all legends off on second execution', () => {
model.toggleLegendsForAll();
model.toggleLegendsForAll();
const legendsOn = model.panels.filter(panel => panel.legend.show === true);
expect(legendsOn.length).toBe(0);
});
});
});

View File

@ -917,4 +917,18 @@ export class DashboardModel {
}
}
}
toggleLegendsForAll() {
const panels = this.panels.filter(panel => {
return panel.legend !== undefined && panel.legend !== null;
});
// determine if more panels are displaying legends or not
const onCount = panels.filter(panel => panel.legend.show).length;
const offCount = panels.length - onCount;
const panelLegendsOn = onCount >= offCount;
panels.forEach(panel => {
panel.legend.show = !panelLegendsOn;
panel.render();
});
}
}

View File

@ -106,6 +106,7 @@ export class PanelModel {
events: Emitter;
cacheTimeout?: any;
cachedPluginOptions?: any;
legend?: { show: boolean };
constructor(model) {
this.events = new Emitter();