link_srv.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import angular from 'angular';
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. export class LinkSrv {
  5. /** @ngInject */
  6. constructor(private templateSrv, private timeSrv) {
  7. }
  8. getLinkUrl(link) {
  9. var url = this.templateSrv.replace(link.url || '');
  10. var params = {};
  11. if (link.keepTime) {
  12. var range = this.timeSrv.timeRangeForUrl();
  13. params['from'] = range.from;
  14. params['to'] = range.to;
  15. }
  16. if (link.includeVars) {
  17. this.templateSrv.fillVariableValuesForUrl(params);
  18. }
  19. return this.addParamsToUrl(url, params);
  20. }
  21. addParamsToUrl(url, params) {
  22. var paramsArray = [];
  23. _.each(params, function(value, key) {
  24. if (value === null) { return; }
  25. if (value === true) {
  26. paramsArray.push(key);
  27. } else if (_.isArray(value)) {
  28. _.each(value, function(instance) {
  29. paramsArray.push(key + '=' + encodeURIComponent(instance));
  30. });
  31. } else {
  32. paramsArray.push(key + '=' + encodeURIComponent(value));
  33. }
  34. });
  35. if (paramsArray.length === 0) {
  36. return url;
  37. }
  38. return this.appendToQueryString(url, paramsArray.join('&'));
  39. }
  40. appendToQueryString(url, stringToAppend) {
  41. if (!_.isUndefined(stringToAppend) && stringToAppend !== null && stringToAppend !== '') {
  42. var pos = url.indexOf('?');
  43. if (pos !== -1) {
  44. if (url.length - pos > 1) {
  45. url += '&';
  46. }
  47. } else {
  48. url += '?';
  49. }
  50. url += stringToAppend;
  51. }
  52. return url;
  53. }
  54. getAnchorInfo(link) {
  55. var info: any = {};
  56. info.href = this.getLinkUrl(link);
  57. info.title = this.templateSrv.replace(link.title || '');
  58. return info;
  59. }
  60. getPanelLinkAnchorInfo(link, scopedVars) {
  61. var info: any = {};
  62. if (link.type === 'absolute') {
  63. info.target = link.targetBlank ? '_blank' : '_self';
  64. info.href = this.templateSrv.replace(link.url || '', scopedVars);
  65. info.title = this.templateSrv.replace(link.title || '', scopedVars);
  66. } else if (link.dashUri) {
  67. info.href = 'dashboard/' + link.dashUri + '?';
  68. info.title = this.templateSrv.replace(link.title || '', scopedVars);
  69. info.target = link.targetBlank ? '_blank' : '';
  70. } else {
  71. info.title = this.templateSrv.replace(link.title || '', scopedVars);
  72. var slug = kbn.slugifyForUrl(link.dashboard || '');
  73. info.href = 'dashboard/db/' + slug + '?';
  74. }
  75. var params = {};
  76. if (link.keepTime) {
  77. var range = this.timeSrv.timeRangeForUrl();
  78. params['from'] = range.from;
  79. params['to'] = range.to;
  80. }
  81. if (link.includeVars) {
  82. this.templateSrv.fillVariableValuesForUrl(params, scopedVars);
  83. }
  84. info.href = this.addParamsToUrl(info.href, params);
  85. if (link.params) {
  86. info.href = this.appendToQueryString(info.href, this.templateSrv.replace(link.params, scopedVars));
  87. }
  88. return info;
  89. }
  90. }
  91. angular.module('grafana.services').service('linkSrv', LinkSrv);