sharePanelCtrl.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. define([
  2. 'angular',
  3. 'lodash'
  4. ],
  5. function (angular, _) {
  6. 'use strict';
  7. var module = angular.module('grafana.controllers');
  8. module.controller('SharePanelCtrl', function($scope, $location, $timeout, timeSrv, $element, templateSrv) {
  9. $scope.init = function() {
  10. $scope.editor = { index: 0 };
  11. $scope.forCurrent = true;
  12. $scope.toPanel = true;
  13. $scope.includeTemplateVars = true;
  14. $scope.buildUrl();
  15. };
  16. $scope.buildUrl = function() {
  17. var baseUrl = $location.absUrl();
  18. var queryStart = baseUrl.indexOf('?');
  19. if (queryStart !== -1) {
  20. baseUrl = baseUrl.substring(0, queryStart);
  21. }
  22. var panelId = $scope.panel.id;
  23. var range = timeSrv.timeRange(false);
  24. var params = angular.copy($location.search());
  25. if (_.isString(range.to) && range.to.indexOf('now')) {
  26. range = timeSrv.timeRange();
  27. }
  28. params.from = range.from;
  29. params.to = range.to;
  30. if (_.isDate(params.from)) { params.from = params.from.getTime(); }
  31. if (_.isDate(params.to)) { params.to = params.to.getTime(); }
  32. if ($scope.includeTemplateVars) {
  33. _.each(templateSrv.variables, function(variable) {
  34. params['var-' + variable.name] = variable.current.text;
  35. });
  36. }
  37. else {
  38. _.each(templateSrv.variables, function(variable) {
  39. delete params['var-' + variable.name];
  40. });
  41. }
  42. if (!$scope.forCurrent) {
  43. delete params.from;
  44. delete params.to;
  45. }
  46. if ($scope.toPanel) {
  47. params.panelId = panelId;
  48. params.fullscreen = true;
  49. } else {
  50. delete params.panelId;
  51. delete params.fullscreen;
  52. }
  53. var paramsArray = [];
  54. _.each(params, function(value, key) {
  55. var str = key;
  56. if (value !== true) {
  57. str += '=' + encodeURIComponent(value);
  58. }
  59. paramsArray.push(str);
  60. });
  61. $scope.shareUrl = baseUrl + "?" + paramsArray.join('&') ;
  62. $timeout(function() {
  63. var input = $element.find('[data-share-panel-url]');
  64. input.focus();
  65. input.select();
  66. }, 10);
  67. };
  68. $scope.init();
  69. });
  70. });