link_srv.ts 3.1 KB

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