Commit 2dd49e6e by Patrick O'Carroll

converted linkSrv.js to linkSrv.ts

parent c91a1e99
define([ import angular from 'angular';
'angular', import _ from 'lodash';
'lodash', import kbn from 'app/core/utils/kbn';
'app/core/utils/kbn',
],
function (angular, _, kbn) { export class LinkSrv {
'use strict';
/** @ngInject */
kbn = kbn.default; constructor(private templateSrv, private timeSrv) {
}
angular
.module('grafana.services') getLinkUrl(link) {
.service('linkSrv', function(templateSrv, timeSrv) { var url = this.templateSrv.replace(link.url || '');
this.getLinkUrl = function(link) {
var url = templateSrv.replace(link.url || '');
var params = {}; var params = {};
if (link.keepTime) { if (link.keepTime) {
var range = timeSrv.timeRangeForUrl(); var range = this.timeSrv.timeRangeForUrl();
params['from'] = range.from; params['from'] = range.from;
params['to'] = range.to; params['to'] = range.to;
} }
if (link.includeVars) { if (link.includeVars) {
templateSrv.fillVariableValuesForUrl(params); this.templateSrv.fillVariableValuesForUrl(params);
} }
return this.addParamsToUrl(url, params); return this.addParamsToUrl(url, params);
}; }
this.addParamsToUrl = function(url, params) { addParamsToUrl(url, params) {
var paramsArray = []; var paramsArray = [];
_.each(params, function(value, key) { _.each(params, function(value, key) {
if (value === null) { return; } if (value === null) { return; }
if (value === true) { if (value === true) {
paramsArray.push(key); paramsArray.push(key);
} } else if (_.isArray(value)) {
else if (_.isArray(value)) {
_.each(value, function(instance) { _.each(value, function(instance) {
paramsArray.push(key + '=' + encodeURIComponent(instance)); paramsArray.push(key + '=' + encodeURIComponent(instance));
}); });
} } else {
else {
paramsArray.push(key + '=' + encodeURIComponent(value)); paramsArray.push(key + '=' + encodeURIComponent(value));
} }
}); });
...@@ -51,9 +47,9 @@ function (angular, _, kbn) { ...@@ -51,9 +47,9 @@ function (angular, _, kbn) {
} }
return this.appendToQueryString(url, paramsArray.join('&')); return this.appendToQueryString(url, paramsArray.join('&'));
}; }
this.appendToQueryString = function(url, stringToAppend) { appendToQueryString(url, stringToAppend) {
if (!_.isUndefined(stringToAppend) && stringToAppend !== null && stringToAppend !== '') { if (!_.isUndefined(stringToAppend) && stringToAppend !== null && stringToAppend !== '') {
var pos = url.indexOf('?'); var pos = url.indexOf('?');
if (pos !== -1) { if (pos !== -1) {
...@@ -65,54 +61,54 @@ function (angular, _, kbn) { ...@@ -65,54 +61,54 @@ function (angular, _, kbn) {
} }
url += stringToAppend; url += stringToAppend;
} }
return url; return url;
}; }
this.getAnchorInfo = function(link) { getAnchorInfo(link) {
var info = {}; var info = {};
info.href = this.getLinkUrl(link); (<any>info).href = this.getLinkUrl(link);
info.title = templateSrv.replace(link.title || ''); (<any>info).title = this.templateSrv.replace(link.title || '');
return info; return info;
}; }
this.getPanelLinkAnchorInfo = function(link, scopedVars) { getPanelLinkAnchorInfo(link, scopedVars) {
var info = {}; var info = {};
if (link.type === 'absolute') { if (link.type === 'absolute') {
info.target = link.targetBlank ? '_blank' : '_self'; (<any>info).target = link.targetBlank ? '_blank' : '_self';
info.href = templateSrv.replace(link.url || '', scopedVars); (<any>info).href = this.templateSrv.replace(link.url || '', scopedVars);
info.title = templateSrv.replace(link.title || '', scopedVars); (<any>info).title = this.templateSrv.replace(link.title || '', scopedVars);
} } else if (link.dashUri) {
else if (link.dashUri) { (<any>info).href = 'dashboard/' + link.dashUri + '?';
info.href = 'dashboard/' + link.dashUri + '?'; (<any>info).title = this.templateSrv.replace(link.title || '', scopedVars);
info.title = templateSrv.replace(link.title || '', scopedVars); (<any>info).target = link.targetBlank ? '_blank' : '';
info.target = link.targetBlank ? '_blank' : ''; } else {
} (<any>info).title = this.templateSrv.replace(link.title || '', scopedVars);
else {
info.title = templateSrv.replace(link.title || '', scopedVars);
var slug = kbn.slugifyForUrl(link.dashboard || ''); var slug = kbn.slugifyForUrl(link.dashboard || '');
info.href = 'dashboard/db/' + slug + '?'; (<any>info).href = 'dashboard/db/' + slug + '?';
} }
var params = {}; var params = {};
if (link.keepTime) { if (link.keepTime) {
var range = timeSrv.timeRangeForUrl(); var range = this.timeSrv.timeRangeForUrl();
params['from'] = range.from; params['from'] = range.from;
params['to'] = range.to; params['to'] = range.to;
} }
if (link.includeVars) { if (link.includeVars) {
templateSrv.fillVariableValuesForUrl(params, scopedVars); this.templateSrv.fillVariableValuesForUrl(params, scopedVars);
} }
info.href = this.addParamsToUrl(info.href, params); (<any>info).href = this.addParamsToUrl((<any>info).href, params);
if (link.params) { if (link.params) {
info.href = this.appendToQueryString(info.href, templateSrv.replace(link.params, scopedVars)); (<any>info).href = this.appendToQueryString((<any>info).href, this.templateSrv.replace(link.params, scopedVars));
} }
return info; return info;
}; }
}); }
});
angular.module('grafana.services').service('linkSrv', LinkSrv);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment