ShareModalCtrl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import angular, { ILocationService } from 'angular';
  2. import config from 'app/core/config';
  3. import { dateTime } from '@grafana/data';
  4. import { appendQueryToUrl, toUrlParams } from 'app/core/utils/url';
  5. import { TimeSrv } from '../../services/TimeSrv';
  6. import { TemplateSrv } from 'app/features/templating/template_srv';
  7. import { LinkSrv } from 'app/features/panel/panellinks/link_srv';
  8. /** @ngInject */
  9. export function ShareModalCtrl(
  10. $scope: any,
  11. $rootScope: any,
  12. $location: ILocationService,
  13. $timeout: any,
  14. timeSrv: TimeSrv,
  15. templateSrv: TemplateSrv,
  16. linkSrv: LinkSrv
  17. ) {
  18. $scope.options = {
  19. forCurrent: true,
  20. includeTemplateVars: true,
  21. theme: 'current',
  22. };
  23. $scope.editor = { index: $scope.tabIndex || 0 };
  24. $scope.init = () => {
  25. $scope.panel = $scope.model && $scope.model.panel ? $scope.model.panel : $scope.panel; // React pass panel and dashboard in the "model" property
  26. $scope.dashboard = $scope.model && $scope.model.dashboard ? $scope.model.dashboard : $scope.dashboard; // ^
  27. $scope.modeSharePanel = $scope.panel ? true : false;
  28. $scope.tabs = [{ title: 'Link', src: 'shareLink.html' }];
  29. if ($scope.modeSharePanel) {
  30. $scope.modalTitle = 'Share Panel';
  31. $scope.tabs.push({ title: 'Embed', src: 'shareEmbed.html' });
  32. } else {
  33. $scope.modalTitle = 'Share';
  34. }
  35. if (!$scope.dashboard.meta.isSnapshot) {
  36. $scope.tabs.push({ title: 'Snapshot', src: 'shareSnapshot.html' });
  37. }
  38. if (!$scope.dashboard.meta.isSnapshot && !$scope.modeSharePanel) {
  39. $scope.tabs.push({ title: 'Export', src: 'shareExport.html' });
  40. }
  41. $scope.buildUrl();
  42. };
  43. $scope.buildUrl = () => {
  44. let baseUrl = $location.absUrl();
  45. const queryStart = baseUrl.indexOf('?');
  46. if (queryStart !== -1) {
  47. baseUrl = baseUrl.substring(0, queryStart);
  48. }
  49. const params = angular.copy($location.search());
  50. const range = timeSrv.timeRange();
  51. params.from = range.from.valueOf();
  52. params.to = range.to.valueOf();
  53. params.orgId = config.bootData.user.orgId;
  54. if ($scope.options.includeTemplateVars) {
  55. templateSrv.fillVariableValuesForUrl(params);
  56. }
  57. if (!$scope.options.forCurrent) {
  58. delete params.from;
  59. delete params.to;
  60. }
  61. if ($scope.options.theme !== 'current') {
  62. params.theme = $scope.options.theme;
  63. }
  64. if ($scope.modeSharePanel) {
  65. params.panelId = $scope.panel.id;
  66. params.fullscreen = true;
  67. } else {
  68. delete params.panelId;
  69. delete params.fullscreen;
  70. }
  71. $scope.shareUrl = appendQueryToUrl(baseUrl, toUrlParams(params));
  72. let soloUrl = baseUrl.replace(config.appSubUrl + '/dashboard/', config.appSubUrl + '/dashboard-solo/');
  73. soloUrl = soloUrl.replace(config.appSubUrl + '/d/', config.appSubUrl + '/d-solo/');
  74. delete params.fullscreen;
  75. delete params.edit;
  76. soloUrl = appendQueryToUrl(soloUrl, toUrlParams(params));
  77. $scope.iframeHtml = '<iframe src="' + soloUrl + '" width="450" height="200" frameborder="0"></iframe>';
  78. $scope.imageUrl = soloUrl.replace(
  79. config.appSubUrl + '/dashboard-solo/',
  80. config.appSubUrl + '/render/dashboard-solo/'
  81. );
  82. $scope.imageUrl = $scope.imageUrl.replace(config.appSubUrl + '/d-solo/', config.appSubUrl + '/render/d-solo/');
  83. $scope.imageUrl += '&width=1000&height=500' + $scope.getLocalTimeZone();
  84. };
  85. // This function will try to return the proper full name of the local timezone
  86. // Chrome does not handle the timezone offset (but phantomjs does)
  87. $scope.getLocalTimeZone = () => {
  88. const utcOffset = '&tz=UTC' + encodeURIComponent(dateTime().format('Z'));
  89. // Older browser does not the internationalization API
  90. if (!(window as any).Intl) {
  91. return utcOffset;
  92. }
  93. const dateFormat = (window as any).Intl.DateTimeFormat();
  94. if (!dateFormat.resolvedOptions) {
  95. return utcOffset;
  96. }
  97. const options = dateFormat.resolvedOptions();
  98. if (!options.timeZone) {
  99. return utcOffset;
  100. }
  101. return '&tz=' + encodeURIComponent(options.timeZone);
  102. };
  103. $scope.getShareUrl = () => {
  104. return $scope.shareUrl;
  105. };
  106. }
  107. angular.module('grafana.controllers').controller('ShareModalCtrl', ShareModalCtrl);