dashboard_ctrl.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.$broadcast('refresh');
  57. };
  58. $scope.addRow = function(dash, row) {
  59. dash.rows.push(row);
  60. };
  61. $scope.addRowDefault = function() {
  62. $scope.resetRow();
  63. $scope.row.title = 'New row';
  64. $scope.addRow($scope.dashboard, $scope.row);
  65. };
  66. $scope.resetRow = function() {
  67. $scope.row = {
  68. title: '',
  69. height: '250px',
  70. editable: true,
  71. };
  72. };
  73. $scope.showJsonEditor = function(evt, options) {
  74. var editScope = $rootScope.$new();
  75. editScope.object = options.object;
  76. editScope.updateHandler = options.updateHandler;
  77. $scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  78. };
  79. $scope.onDrop = function(panelId, row, dropTarget) {
  80. var info = $scope.dashboard.getPanelInfoById(panelId);
  81. if (dropTarget) {
  82. var dropInfo = $scope.dashboard.getPanelInfoById(dropTarget.id);
  83. dropInfo.row.panels[dropInfo.index] = info.panel;
  84. info.row.panels[info.index] = dropTarget;
  85. var dragSpan = info.panel.span;
  86. info.panel.span = dropTarget.span;
  87. dropTarget.span = dragSpan;
  88. } else {
  89. info.row.panels.splice(info.index, 1);
  90. info.panel.span = 12 - $scope.dashboard.rowSpan(row);
  91. row.panels.push(info.panel);
  92. }
  93. $rootScope.$broadcast('render');
  94. };
  95. $scope.registerWindowResizeEvent = function() {
  96. angular.element(window).bind('resize', function() {
  97. $timeout.cancel(resizeEventTimeout);
  98. resizeEventTimeout = $timeout(function() { $scope.$broadcast('render'); }, 200);
  99. });
  100. $scope.$on('$destroy', function() {
  101. angular.element(window).unbind('resize');
  102. });
  103. };
  104. $scope.timezoneChanged = function() {
  105. $rootScope.$broadcast("refresh");
  106. };
  107. }
  108. init(dashboard) {
  109. this.$scope.resetRow();
  110. this.$scope.registerWindowResizeEvent();
  111. this.$scope.onAppEvent('show-json-editor', this.$scope.showJsonEditor);
  112. this.$scope.onAppEvent('template-variable-value-updated', this.$scope.templateVariableUpdated);
  113. this.$scope.setupDashboard(dashboard);
  114. }
  115. }
  116. coreModule.controller('DashboardCtrl', DashboardCtrl);