shareModalCtrl.ts 3.5 KB

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