2014-08-13 05:16:37 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'lodash',
|
2014-08-13 07:22:21 -05:00
|
|
|
'jquery',
|
2017-02-23 04:03:37 -06:00
|
|
|
'app/core/config'
|
2014-08-13 05:16:37 -05:00
|
|
|
],
|
2017-02-23 04:03:37 -06:00
|
|
|
function (angular, _, $, config) {
|
2014-08-13 05:16:37 -05:00
|
|
|
'use strict';
|
|
|
|
|
2017-10-24 03:45:46 -05:00
|
|
|
config = config.default;
|
2014-08-13 05:16:37 -05:00
|
|
|
var module = angular.module('grafana.services');
|
|
|
|
|
2017-01-13 10:37:53 -06:00
|
|
|
module.factory('dashboardViewStateSrv', function($location, $timeout) {
|
2014-08-13 05:16:37 -05:00
|
|
|
|
|
|
|
// represents the transient view state
|
|
|
|
// like fullscreen panel & edit
|
|
|
|
function DashboardViewState($scope) {
|
|
|
|
var self = this;
|
2014-09-08 04:03:14 -05:00
|
|
|
self.state = {};
|
|
|
|
self.panelScopes = [];
|
|
|
|
self.$scope = $scope;
|
2016-01-25 14:09:37 -06:00
|
|
|
self.dashboard = $scope.dashboard;
|
2014-08-13 05:16:37 -05:00
|
|
|
|
|
|
|
$scope.onAppEvent('$routeUpdate', function() {
|
2014-08-13 07:22:21 -05:00
|
|
|
var urlState = self.getQueryStringState();
|
|
|
|
if (self.needsSync(urlState)) {
|
|
|
|
self.update(urlState, true);
|
2014-08-13 05:16:37 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-01-22 18:44:38 -06:00
|
|
|
$scope.onAppEvent('panel-change-view', function(evt, payload) {
|
|
|
|
self.update(payload);
|
|
|
|
});
|
|
|
|
|
2016-04-22 04:41:56 -05:00
|
|
|
$scope.onAppEvent('panel-initialized', function(evt, payload) {
|
2016-01-22 18:44:38 -06:00
|
|
|
self.registerPanel(payload.scope);
|
|
|
|
});
|
|
|
|
|
2017-06-02 07:00:42 -05:00
|
|
|
// this marks changes to location during this digest cycle as not to add history item
|
|
|
|
// dont want url changes like adding orgId to add browser history
|
|
|
|
$location.replace();
|
2016-05-26 01:49:55 -05:00
|
|
|
this.update(this.getQueryStringState());
|
2014-10-29 03:51:44 -05:00
|
|
|
this.expandRowForPanel();
|
2014-08-13 07:22:21 -05:00
|
|
|
}
|
|
|
|
|
2014-10-29 03:51:44 -05:00
|
|
|
DashboardViewState.prototype.expandRowForPanel = function() {
|
|
|
|
if (!this.state.panelId) { return; }
|
|
|
|
|
|
|
|
var panelInfo = this.$scope.dashboard.getPanelInfoById(this.state.panelId);
|
2014-11-06 05:30:42 -06:00
|
|
|
if (panelInfo) {
|
|
|
|
panelInfo.row.collapse = false;
|
|
|
|
}
|
2014-10-29 03:51:44 -05:00
|
|
|
};
|
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
DashboardViewState.prototype.needsSync = function(urlState) {
|
2014-09-08 04:03:14 -05:00
|
|
|
return _.isEqual(this.state, urlState) === false;
|
2014-08-13 07:22:21 -05:00
|
|
|
};
|
2014-08-13 05:16:37 -05:00
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
DashboardViewState.prototype.getQueryStringState = function() {
|
2014-11-02 04:56:51 -06:00
|
|
|
var state = $location.search();
|
|
|
|
state.panelId = parseInt(state.panelId) || null;
|
2014-11-10 09:18:59 -06:00
|
|
|
state.fullscreen = state.fullscreen ? true : null;
|
|
|
|
state.edit = (state.edit === "true" || state.edit === true) || null;
|
2015-01-21 12:45:23 -06:00
|
|
|
state.editview = state.editview || null;
|
2017-02-23 04:03:37 -06:00
|
|
|
state.orgId = config.bootData.user.orgId;
|
2014-11-02 04:56:51 -06:00
|
|
|
return state;
|
2014-09-08 04:03:14 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
DashboardViewState.prototype.serializeToUrl = function() {
|
|
|
|
var urlState = _.clone(this.state);
|
2015-01-21 12:45:23 -06:00
|
|
|
urlState.fullscreen = this.state.fullscreen ? true : null;
|
2014-09-08 04:03:14 -05:00
|
|
|
urlState.edit = this.state.edit ? true : null;
|
|
|
|
return urlState;
|
2014-08-13 07:22:21 -05:00
|
|
|
};
|
|
|
|
|
2017-01-13 10:37:53 -06:00
|
|
|
DashboardViewState.prototype.update = function(state, fromRouteUpdated) {
|
2016-11-02 11:03:14 -05:00
|
|
|
// implement toggle logic
|
2016-11-02 15:45:46 -05:00
|
|
|
if (state.toggle) {
|
|
|
|
delete state.toggle;
|
|
|
|
if (this.state.fullscreen && state.fullscreen) {
|
|
|
|
if (this.state.edit === state.edit) {
|
|
|
|
state.fullscreen = !state.fullscreen;
|
|
|
|
}
|
2016-11-02 11:03:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-11 02:51:43 -05:00
|
|
|
// remember if editStateChanged
|
|
|
|
this.editStateChanged = state.edit !== this.state.edit;
|
|
|
|
|
2014-09-08 04:03:14 -05:00
|
|
|
_.extend(this.state, state);
|
2016-01-25 14:09:37 -06:00
|
|
|
this.dashboard.meta.fullscreen = this.state.fullscreen;
|
2014-08-13 08:02:57 -05:00
|
|
|
|
2014-09-08 04:03:14 -05:00
|
|
|
if (!this.state.fullscreen) {
|
2014-11-11 01:07:05 -06:00
|
|
|
this.state.fullscreen = null;
|
|
|
|
this.state.edit = null;
|
2016-07-18 08:41:06 -05:00
|
|
|
// clear panel id unless in solo mode
|
|
|
|
if (!this.dashboard.meta.soloMode) {
|
|
|
|
this.state.panelId = null;
|
|
|
|
}
|
2014-08-13 07:22:21 -05:00
|
|
|
}
|
|
|
|
|
2016-07-26 05:29:52 -05:00
|
|
|
// if no edit state cleanup tab parm
|
|
|
|
if (!this.state.edit) {
|
|
|
|
delete this.state.tab;
|
|
|
|
}
|
|
|
|
|
2017-01-13 10:37:53 -06:00
|
|
|
// do not update url params if we are here
|
|
|
|
// from routeUpdated event
|
|
|
|
if (fromRouteUpdated !== true) {
|
|
|
|
$location.search(this.serializeToUrl());
|
|
|
|
}
|
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
this.syncState();
|
2014-08-13 05:16:37 -05:00
|
|
|
};
|
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
DashboardViewState.prototype.syncState = function() {
|
|
|
|
if (this.panelScopes.length === 0) { return; }
|
|
|
|
|
2016-01-25 14:09:37 -06:00
|
|
|
if (this.dashboard.meta.fullscreen) {
|
2014-09-08 04:03:14 -05:00
|
|
|
var panelScope = this.getPanelScope(this.state.panelId);
|
2015-11-12 05:01:44 -06:00
|
|
|
if (!panelScope) {
|
|
|
|
return;
|
|
|
|
}
|
2016-02-03 11:49:36 -06:00
|
|
|
|
2016-06-11 15:33:02 -05:00
|
|
|
if (this.fullscreenPanel) {
|
|
|
|
// if already fullscreen
|
2016-10-11 02:51:43 -05:00
|
|
|
if (this.fullscreenPanel === panelScope && this.editStateChanged === false) {
|
2016-06-11 15:33:02 -05:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
this.leaveFullscreen(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-03 11:49:36 -06:00
|
|
|
if (!panelScope.ctrl.editModeInitiated) {
|
|
|
|
panelScope.ctrl.initEditMode();
|
|
|
|
}
|
|
|
|
|
2016-06-11 15:33:02 -05:00
|
|
|
if (!panelScope.ctrl.fullscreen) {
|
|
|
|
this.enterFullscreen(panelScope);
|
|
|
|
}
|
|
|
|
} else if (this.fullscreenPanel) {
|
2014-08-13 07:22:21 -05:00
|
|
|
this.leaveFullscreen(true);
|
|
|
|
}
|
|
|
|
};
|
2014-08-13 05:16:37 -05:00
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
DashboardViewState.prototype.getPanelScope = function(id) {
|
|
|
|
return _.find(this.panelScopes, function(panelScope) {
|
2016-01-22 18:44:38 -06:00
|
|
|
return panelScope.ctrl.panel.id === id;
|
2014-08-13 07:22:21 -05:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
DashboardViewState.prototype.leaveFullscreen = function(render) {
|
|
|
|
var self = this;
|
2016-01-22 18:44:38 -06:00
|
|
|
var ctrl = self.fullscreenPanel.ctrl;
|
2014-08-13 07:22:21 -05:00
|
|
|
|
2016-01-22 18:44:38 -06:00
|
|
|
ctrl.editMode = false;
|
|
|
|
ctrl.fullscreen = false;
|
2016-10-24 06:50:38 -05:00
|
|
|
ctrl.dashboard.editMode = this.oldDashboardEditMode;
|
2014-08-13 07:22:21 -05:00
|
|
|
|
2016-01-22 18:44:38 -06:00
|
|
|
this.$scope.appEvent('panel-fullscreen-exit', {panelId: ctrl.panel.id});
|
2015-09-19 04:40:51 -05:00
|
|
|
|
2014-08-13 07:22:21 -05:00
|
|
|
if (!render) { return false;}
|
|
|
|
|
|
|
|
$timeout(function() {
|
2016-11-02 15:45:46 -05:00
|
|
|
if (self.oldTimeRange !== ctrl.range) {
|
2015-02-22 01:09:58 -06:00
|
|
|
self.$scope.broadcastRefresh();
|
2016-10-22 02:51:18 -05:00
|
|
|
} else {
|
|
|
|
self.$scope.$broadcast('render');
|
2014-08-13 07:22:21 -05:00
|
|
|
}
|
|
|
|
delete self.fullscreenPanel;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
DashboardViewState.prototype.enterFullscreen = function(panelScope) {
|
2016-01-22 18:44:38 -06:00
|
|
|
var ctrl = panelScope.ctrl;
|
2014-08-13 07:22:21 -05:00
|
|
|
|
2016-09-19 04:34:08 -05:00
|
|
|
ctrl.editMode = this.state.edit && this.dashboard.meta.canEdit;
|
2016-01-25 14:09:37 -06:00
|
|
|
ctrl.fullscreen = true;
|
2015-06-01 09:49:14 -05:00
|
|
|
|
2016-10-24 06:50:38 -05:00
|
|
|
this.oldDashboardEditMode = this.dashboard.editMode;
|
2016-01-22 18:44:38 -06:00
|
|
|
this.oldTimeRange = ctrl.range;
|
2014-08-13 07:22:21 -05:00
|
|
|
this.fullscreenPanel = panelScope;
|
2016-10-24 06:50:38 -05:00
|
|
|
this.dashboard.editMode = false;
|
2014-08-13 07:22:21 -05:00
|
|
|
|
|
|
|
$(window).scrollTop(0);
|
|
|
|
|
2016-01-22 18:44:38 -06:00
|
|
|
this.$scope.appEvent('panel-fullscreen-enter', {panelId: ctrl.panel.id});
|
2014-08-13 07:22:21 -05:00
|
|
|
|
|
|
|
$timeout(function() {
|
2016-03-23 06:50:56 -05:00
|
|
|
ctrl.render();
|
2014-08-13 07:22:21 -05:00
|
|
|
});
|
2014-08-13 05:16:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
DashboardViewState.prototype.registerPanel = function(panelScope) {
|
2014-08-13 07:22:21 -05:00
|
|
|
var self = this;
|
|
|
|
self.panelScopes.push(panelScope);
|
|
|
|
|
2016-07-18 08:41:06 -05:00
|
|
|
if (!self.dashboard.meta.soloMode) {
|
|
|
|
if (self.state.panelId === panelScope.ctrl.panel.id) {
|
|
|
|
if (self.state.edit) {
|
|
|
|
panelScope.ctrl.editPanel();
|
|
|
|
} else {
|
|
|
|
panelScope.ctrl.viewPanel();
|
|
|
|
}
|
2016-01-24 16:30:29 -06:00
|
|
|
}
|
2014-08-13 07:22:21 -05:00
|
|
|
}
|
|
|
|
|
2016-10-30 09:14:18 -05:00
|
|
|
var unbind = panelScope.$on('$destroy', function() {
|
2014-08-13 07:22:21 -05:00
|
|
|
self.panelScopes = _.without(self.panelScopes, panelScope);
|
2016-10-30 09:14:18 -05:00
|
|
|
unbind();
|
2014-08-13 07:22:21 -05:00
|
|
|
});
|
2014-08-13 05:16:37 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
create: function($scope) {
|
|
|
|
return new DashboardViewState($scope);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|