dashboard_ctrl.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import _ from 'lodash';
  6. import coreModule from 'app/core/core_module';
  7. export class DashboardCtrl {
  8. /** @ngInject */
  9. constructor(
  10. private $scope,
  11. private $rootScope,
  12. dashboardKeybindings,
  13. timeSrv,
  14. templateValuesSrv,
  15. dashboardSrv,
  16. unsavedChangesSrv,
  17. dynamicDashboardSrv,
  18. dashboardViewStateSrv,
  19. contextSrv,
  20. $timeout) {
  21. $scope.editor = { index: 0 };
  22. $scope.panels = config.panels;
  23. var resizeEventTimeout;
  24. $scope.setupDashboard = function(data) {
  25. var dashboard = dashboardSrv.create(data.dashboard, data.meta);
  26. dashboardSrv.setCurrent(dashboard);
  27. // init services
  28. timeSrv.init(dashboard);
  29. // template values service needs to initialize completely before
  30. // the rest of the dashboard can load
  31. templateValuesSrv.init(dashboard).finally(function() {
  32. dynamicDashboardSrv.init(dashboard);
  33. unsavedChangesSrv.init(dashboard, $scope);
  34. $scope.dashboard = dashboard;
  35. $scope.dashboardMeta = dashboard.meta;
  36. $scope.dashboardViewState = dashboardViewStateSrv.create($scope);
  37. dashboardKeybindings.shortcuts($scope);
  38. $scope.updateSubmenuVisibility();
  39. $scope.setWindowTitleAndTheme();
  40. $scope.appEvent("dashboard-initialized", $scope.dashboard);
  41. }).catch(function(err) {
  42. if (err.data && err.data.message) { err.message = err.data.message; }
  43. $scope.appEvent("alert-error", ['Dashboard init failed', 'Template variables could not be initialized: ' + err.message]);
  44. });
  45. };
  46. $scope.templateVariableUpdated = function() {
  47. dynamicDashboardSrv.update($scope.dashboard);
  48. };
  49. $scope.updateSubmenuVisibility = function() {
  50. $scope.submenuEnabled = $scope.dashboard.isSubmenuFeaturesEnabled();
  51. };
  52. $scope.setWindowTitleAndTheme = function() {
  53. window.document.title = config.window_title_prefix + $scope.dashboard.title;
  54. };
  55. $scope.broadcastRefresh = function() {
  56. $rootScope.performance.panelsRendered = 0;
  57. $rootScope.$broadcast('refresh');
  58. };
  59. $scope.addRow = function(dash, row) {
  60. dash.rows.push(row);
  61. };
  62. $scope.addRowDefault = function() {
  63. $scope.resetRow();
  64. $scope.row.title = 'New row';
  65. $scope.addRow($scope.dashboard, $scope.row);
  66. };
  67. $scope.resetRow = function() {
  68. $scope.row = {
  69. title: '',
  70. height: '250px',
  71. editable: true,
  72. };
  73. };
  74. $scope.showJsonEditor = function(evt, options) {
  75. var editScope = $rootScope.$new();
  76. editScope.object = options.object;
  77. editScope.updateHandler = options.updateHandler;
  78. $scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  79. };
  80. $scope.onDrop = function(panelId, row, dropTarget) {
  81. var info = $scope.dashboard.getPanelInfoById(panelId);
  82. if (dropTarget) {
  83. var dropInfo = $scope.dashboard.getPanelInfoById(dropTarget.id);
  84. dropInfo.row.panels[dropInfo.index] = info.panel;
  85. info.row.panels[info.index] = dropTarget;
  86. var dragSpan = info.panel.span;
  87. info.panel.span = dropTarget.span;
  88. dropTarget.span = dragSpan;
  89. } else {
  90. info.row.panels.splice(info.index, 1);
  91. info.panel.span = 12 - $scope.dashboard.rowSpan(row);
  92. row.panels.push(info.panel);
  93. }
  94. $rootScope.$broadcast('render');
  95. };
  96. $scope.registerWindowResizeEvent = function() {
  97. angular.element(window).bind('resize', function() {
  98. $timeout.cancel(resizeEventTimeout);
  99. resizeEventTimeout = $timeout(function() { $scope.$broadcast('render'); }, 200);
  100. });
  101. $scope.$on('$destroy', function() {
  102. angular.element(window).unbind('resize');
  103. });
  104. };
  105. $scope.timezoneChanged = function() {
  106. $rootScope.$broadcast("refresh");
  107. };
  108. }
  109. init(dashboard) {
  110. this.$scope.resetRow();
  111. this.$scope.registerWindowResizeEvent();
  112. this.$scope.onAppEvent('show-json-editor', this.$scope.showJsonEditor);
  113. this.$scope.onAppEvent('template-variable-value-updated', this.$scope.templateVariableUpdated);
  114. this.$scope.setupDashboard(dashboard);
  115. }
  116. }
  117. coreModule.controller('DashboardCtrl', DashboardCtrl);