dashboard_ctrl.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. variableSrv,
  15. alertingSrv,
  16. dashboardSrv,
  17. unsavedChangesSrv,
  18. dynamicDashboardSrv,
  19. dashboardViewStateSrv,
  20. contextSrv,
  21. alertSrv,
  22. $timeout) {
  23. $scope.editor = { index: 0 };
  24. var resizeEventTimeout;
  25. $scope.setupDashboard = function(data) {
  26. try {
  27. $scope.setupDashboardInternal(data);
  28. } catch (err) {
  29. $scope.onInitFailed(err, 'Dashboard init failed', true);
  30. }
  31. };
  32. $scope.setupDashboardInternal = function(data) {
  33. var dashboard = dashboardSrv.create(data.dashboard, data.meta);
  34. dashboardSrv.setCurrent(dashboard);
  35. dashboard.editMode = true;
  36. // init services
  37. timeSrv.init(dashboard);
  38. alertingSrv.init(dashboard, data.alerts);
  39. // template values service needs to initialize completely before
  40. // the rest of the dashboard can load
  41. variableSrv.init(dashboard)
  42. // template values failes are non fatal
  43. .catch($scope.onInitFailed.bind(this, 'Templating init failed', false))
  44. // continue
  45. .finally(function() {
  46. dynamicDashboardSrv.init(dashboard, variableSrv);
  47. dynamicDashboardSrv.process();
  48. unsavedChangesSrv.init(dashboard, $scope);
  49. $scope.dashboard = dashboard;
  50. $scope.dashboardMeta = dashboard.meta;
  51. $scope.dashboardViewState = dashboardViewStateSrv.create($scope);
  52. dashboardKeybindings.shortcuts($scope);
  53. $scope.dashboard.updateSubmenuVisibility();
  54. $scope.setWindowTitleAndTheme();
  55. $scope.appEvent("dashboard-initialized", $scope.dashboard);
  56. })
  57. .catch($scope.onInitFailed.bind(this, 'Dashboard init failed', true));
  58. };
  59. $scope.onInitFailed = function(msg, fatal, err) {
  60. console.log(msg, err);
  61. if (err.data && err.data.message) {
  62. err.message = err.data.message;
  63. } else if (!err.message) {
  64. err = {message: err.toString()};
  65. }
  66. $scope.appEvent("alert-error", [msg, err.message]);
  67. // protect against recursive fallbacks
  68. if (fatal && !$scope.loadedFallbackDashboard) {
  69. $scope.loadedFallbackDashboard = true;
  70. $scope.setupDashboard({dashboard: {title: 'Dashboard Init failed'}});
  71. }
  72. };
  73. $scope.templateVariableUpdated = function() {
  74. dynamicDashboardSrv.process();
  75. };
  76. $scope.setWindowTitleAndTheme = function() {
  77. window.document.title = config.window_title_prefix + $scope.dashboard.title;
  78. };
  79. $scope.broadcastRefresh = function() {
  80. $rootScope.$broadcast('refresh');
  81. };
  82. $scope.addRowDefault = function() {
  83. $scope.dashboard.rows.push({
  84. title: 'New row',
  85. panels: [],
  86. height: '250px',
  87. });
  88. };
  89. $scope.showJsonEditor = function(evt, options) {
  90. var editScope = $rootScope.$new();
  91. editScope.object = options.object;
  92. editScope.updateHandler = options.updateHandler;
  93. $scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  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.registerWindowResizeEvent();
  110. this.$scope.onAppEvent('show-json-editor', this.$scope.showJsonEditor);
  111. this.$scope.onAppEvent('template-variable-value-updated', this.$scope.templateVariableUpdated);
  112. this.$scope.setupDashboard(dashboard);
  113. }
  114. }
  115. coreModule.controller('DashboardCtrl', DashboardCtrl);