dashboard_ctrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import coreModule from 'app/core/core_module';
  5. export class DashboardCtrl {
  6. /** @ngInject */
  7. constructor(
  8. private $scope,
  9. $rootScope,
  10. keybindingSrv,
  11. timeSrv,
  12. variableSrv,
  13. alertingSrv,
  14. dashboardSrv,
  15. unsavedChangesSrv,
  16. dynamicDashboardSrv,
  17. dashboardViewStateSrv,
  18. contextSrv,
  19. alertSrv,
  20. $timeout) {
  21. $scope.editor = { index: 0 };
  22. var resizeEventTimeout;
  23. $scope.setupDashboard = function(data) {
  24. try {
  25. $scope.setupDashboardInternal(data);
  26. } catch (err) {
  27. $scope.onInitFailed(err, 'Dashboard init failed', true);
  28. }
  29. };
  30. $scope.setupDashboardInternal = function(data) {
  31. var dashboard = dashboardSrv.create(data.dashboard, data.meta);
  32. dashboardSrv.setCurrent(dashboard);
  33. // init services
  34. timeSrv.init(dashboard);
  35. alertingSrv.init(dashboard, data.alerts);
  36. // template values service needs to initialize completely before
  37. // the rest of the dashboard can load
  38. variableSrv.init(dashboard)
  39. // template values failes are non fatal
  40. .catch($scope.onInitFailed.bind(this, 'Templating init failed', false))
  41. // continue
  42. .finally(function() {
  43. dynamicDashboardSrv.init(dashboard);
  44. dynamicDashboardSrv.process();
  45. unsavedChangesSrv.init(dashboard, $scope);
  46. $scope.dashboard = dashboard;
  47. $scope.dashboardMeta = dashboard.meta;
  48. $scope.dashboardViewState = dashboardViewStateSrv.create($scope);
  49. keybindingSrv.setupDashboardBindings($scope, dashboard);
  50. $scope.dashboard.updateSubmenuVisibility();
  51. $scope.setWindowTitleAndTheme();
  52. $scope.appEvent("dashboard-initialized", $scope.dashboard);
  53. })
  54. .catch($scope.onInitFailed.bind(this, 'Dashboard init failed', true));
  55. };
  56. $scope.onInitFailed = function(msg, fatal, err) {
  57. console.log(msg, err);
  58. if (err.data && err.data.message) {
  59. err.message = err.data.message;
  60. } else if (!err.message) {
  61. err = {message: err.toString()};
  62. }
  63. $scope.appEvent("alert-error", [msg, err.message]);
  64. // protect against recursive fallbacks
  65. if (fatal && !$scope.loadedFallbackDashboard) {
  66. $scope.loadedFallbackDashboard = true;
  67. $scope.setupDashboard({dashboard: {title: 'Dashboard Init failed'}});
  68. }
  69. };
  70. $scope.templateVariableUpdated = function() {
  71. dynamicDashboardSrv.process();
  72. };
  73. $scope.setWindowTitleAndTheme = function() {
  74. window.document.title = config.window_title_prefix + $scope.dashboard.title;
  75. };
  76. $scope.broadcastRefresh = function() {
  77. $rootScope.$broadcast('refresh');
  78. };
  79. $scope.addRowDefault = function() {
  80. $scope.dashboard.addEmptyRow();
  81. };
  82. $scope.showJsonEditor = function(evt, options) {
  83. var editScope = $rootScope.$new();
  84. editScope.object = options.object;
  85. editScope.updateHandler = options.updateHandler;
  86. $scope.appEvent('show-dash-editor', { src: 'public/app/partials/edit_json.html', scope: editScope });
  87. };
  88. $scope.registerWindowResizeEvent = function() {
  89. angular.element(window).bind('resize', function() {
  90. $timeout.cancel(resizeEventTimeout);
  91. resizeEventTimeout = $timeout(function() { $scope.$broadcast('render'); }, 200);
  92. });
  93. $scope.$on('$destroy', function() {
  94. angular.element(window).unbind('resize');
  95. $scope.dashboard.destroy();
  96. });
  97. };
  98. $scope.timezoneChanged = function() {
  99. $rootScope.$broadcast("refresh");
  100. };
  101. }
  102. init(dashboard) {
  103. this.$scope.onAppEvent('show-json-editor', this.$scope.showJsonEditor);
  104. this.$scope.onAppEvent('template-variable-value-updated', this.$scope.templateVariableUpdated);
  105. this.$scope.setupDashboard(dashboard);
  106. this.$scope.registerWindowResizeEvent();
  107. }
  108. }
  109. coreModule.controller('DashboardCtrl', DashboardCtrl);