refactor: moving code around a bit, refactoring PR #12796

This commit is contained in:
Torkel Ödegaard
2018-08-05 11:04:12 +02:00
parent f00b5eee83
commit 013f8cd8ea
5 changed files with 29 additions and 47 deletions

View File

@@ -4,7 +4,6 @@ import coreModule from 'app/core/core_module';
import { PanelContainer } from './dashgrid/PanelContainer';
import { DashboardModel } from './dashboard_model';
import { PanelModel } from './panel_model';
import { GRID_CELL_HEIGHT, GRID_CELL_VMARGIN } from 'app/core/constants';
export class DashboardCtrl implements PanelContainer {
dashboard: DashboardModel;
@@ -24,8 +23,7 @@ export class DashboardCtrl implements PanelContainer {
private unsavedChangesSrv,
private dashboardViewStateSrv,
public playlistSrv,
private panelLoader,
private $location
private panelLoader
) {
// temp hack due to way dashboards are loaded
// can't use controllerAs on route yet
@@ -64,8 +62,8 @@ export class DashboardCtrl implements PanelContainer {
.finally(() => {
this.dashboard = dashboard;
this.dashboard.processRepeats();
this.dashboard.autoFitPanels(window.innerHeight);
this.autofitPanels();
this.unsavedChangesSrv.init(dashboard, this.$scope);
// TODO refactor ViewStateSrv
@@ -82,28 +80,6 @@ export class DashboardCtrl implements PanelContainer {
.catch(this.onInitFailed.bind(this, 'Dashboard init failed', true));
}
autofitPanels() {
if (this.$location.search().autofitpanels) {
let maxRows = Math.max(
...this.dashboard.panels.map(panel => {
return panel.gridPos.h + panel.gridPos.y;
})
);
//Consider navbar and submenu controls, padding and margin
let availableHeight = window.innerHeight - 40;
let availableRows = Math.floor(availableHeight / (GRID_CELL_HEIGHT + GRID_CELL_VMARGIN));
let scaleFactor = maxRows / availableRows;
this.dashboard.panels.forEach((panel, i) => {
panel.gridPos.y = Math.round(panel.gridPos.y / scaleFactor) || 1;
panel.gridPos.h = Math.round(panel.gridPos.h / scaleFactor) || 1;
});
this.dashboard.meta.autofitpanels = true;
console.log(this.dashboard);
}
}
onInitFailed(msg, fatal, err) {
console.log(msg, err);

View File

@@ -1,7 +1,7 @@
import moment from 'moment';
import _ from 'lodash';
import { GRID_COLUMN_COUNT, REPEAT_DIR_VERTICAL } from 'app/core/constants';
import { GRID_COLUMN_COUNT, REPEAT_DIR_VERTICAL, GRID_CELL_HEIGHT, GRID_CELL_VMARGIN } from 'app/core/constants';
import { DEFAULT_ANNOTATION_COLOR } from 'app/core/utils/colors';
import { Emitter } from 'app/core/utils/emitter';
import { contextSrv } from 'app/core/services/context_srv';
@@ -591,10 +591,6 @@ export class DashboardModel {
updateSubmenuVisibility() {
this.meta.submenuEnabled = (() => {
if (this.meta.autofitpanels) {
return false;
}
if (this.links.length > 0) {
return true;
}
@@ -834,4 +830,26 @@ export class DashboardModel {
return !_.isEqual(updated, this.originalTemplating);
}
autoFitPanels(viewHeight: number) {
if (!this.meta.autofitpanels) {
return;
}
let maxRows = Math.max(
...this.panels.map(panel => {
return panel.gridPos.h + panel.gridPos.y;
})
);
//Consider navbar and submenu controls, padding and margin
let availableHeight = window.innerHeight - 55 - 20;
let availableRows = Math.floor(availableHeight / (GRID_CELL_HEIGHT + GRID_CELL_VMARGIN));
let scaleFactor = maxRows / availableRows;
this.panels.forEach((panel, i) => {
panel.gridPos.y = Math.round(panel.gridPos.y / scaleFactor) || 1;
panel.gridPos.h = Math.round(panel.gridPos.h / scaleFactor) || 1;
});
}
}

View File

@@ -305,19 +305,6 @@ describe('DashboardModel', function() {
});
});
describe('updateSubmenuVisibility with autofitpanels enabled', function() {
var model;
beforeEach(function() {
model = new DashboardModel({}, { autofitpanels: true });
model.updateSubmenuVisibility();
});
it('should not enable submmenu', function() {
expect(model.meta.submenuEnabled).toBe(false);
});
});
describe('updateSubmenuVisibility with hidden annotation toggle', function() {
var dashboard;

View File

@@ -38,9 +38,10 @@ export class LoadDashboardCtrl {
}
}
if ($routeParams.keepRows) {
result.meta.keepRows = true;
if ($routeParams.autofitpanels) {
result.meta.autofitpanels = true;
}
$scope.initDashboard(result, $scope);
});
}

View File

@@ -1,5 +1,5 @@
.dashboard-container {
padding: $dashboard-padding;
padding: $dashboard-padding $dashboard-padding 0 $dashboard-padding;
width: 100%;
min-height: 100%;
}