shareModalCtrl.ts 3.7 KB

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