grafana/public/app/features/panel/panellinks/link_srv.ts

119 lines
3.4 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import angular from 'angular';
import _ from 'lodash';
import kbn from 'app/core/utils/kbn';
import { TemplateSrv } from 'app/features/templating/template_srv';
import { TimeSrv } from 'app/features/dashboard/services/TimeSrv';
import { ScopedVars } from '@grafana/ui/src/types/datasource';
2017-10-25 08:29:13 -05:00
export class LinkSrv {
/** @ngInject */
constructor(private templateSrv: TemplateSrv, private timeSrv: TimeSrv) {}
2017-10-25 08:29:13 -05:00
getLinkUrl(link: any) {
const url = this.templateSrv.replace(link.url || '');
const params: { [key: string]: any } = {};
2017-10-25 08:29:13 -05:00
if (link.keepTime) {
const range = this.timeSrv.timeRangeForUrl();
2017-12-20 05:33:33 -06:00
params['from'] = range.from;
params['to'] = range.to;
2017-10-25 08:29:13 -05:00
}
if (link.includeVars) {
this.templateSrv.fillVariableValuesForUrl(params);
}
return this.addParamsToUrl(url, params);
}
addParamsToUrl(url: string, params: any) {
const paramsArray: Array<string | number> = [];
2017-10-25 08:29:13 -05:00
_.each(params, (value, key) => {
if (value === null) {
return;
}
2017-10-25 08:29:13 -05:00
if (value === true) {
paramsArray.push(key);
} else if (_.isArray(value)) {
_.each(value, instance => {
2017-12-20 05:33:33 -06:00
paramsArray.push(key + '=' + encodeURIComponent(instance));
2017-10-25 08:29:13 -05:00
});
} else {
2017-12-20 05:33:33 -06:00
paramsArray.push(key + '=' + encodeURIComponent(value));
2017-10-25 08:29:13 -05:00
}
});
if (paramsArray.length === 0) {
return url;
}
2017-12-20 05:33:33 -06:00
return this.appendToQueryString(url, paramsArray.join('&'));
2017-10-25 08:29:13 -05:00
}
appendToQueryString(url: string, stringToAppend: string) {
if (!_.isUndefined(stringToAppend) && stringToAppend !== null && stringToAppend !== '') {
const pos = url.indexOf('?');
2017-10-25 08:29:13 -05:00
if (pos !== -1) {
if (url.length - pos > 1) {
2017-12-20 05:33:33 -06:00
url += '&';
2017-10-25 08:29:13 -05:00
}
} else {
2017-12-20 05:33:33 -06:00
url += '?';
2017-10-25 08:29:13 -05:00
}
url += stringToAppend;
}
return url;
}
getAnchorInfo(link: any) {
const info: any = {};
2017-10-26 06:25:47 -05:00
info.href = this.getLinkUrl(link);
2017-12-20 05:33:33 -06:00
info.title = this.templateSrv.replace(link.title || '');
2017-10-25 08:29:13 -05:00
return info;
}
getPanelLinkAnchorInfo(link: any, scopedVars: ScopedVars) {
const info: any = {};
info.target = link.targetBlank ? '_blank' : '';
2017-12-20 05:33:33 -06:00
if (link.type === 'absolute') {
info.target = link.targetBlank ? '_blank' : '_self';
info.href = this.templateSrv.replace(link.url || '', scopedVars);
info.title = this.templateSrv.replace(link.title || '', scopedVars);
} else if (link.url) {
info.href = link.url;
info.title = this.templateSrv.replace(link.title || '', scopedVars);
2017-10-25 08:29:13 -05:00
} else if (link.dashUri) {
2017-12-20 05:33:33 -06:00
info.href = 'dashboard/' + link.dashUri + '?';
info.title = this.templateSrv.replace(link.title || '', scopedVars);
2017-10-25 08:29:13 -05:00
} else {
2017-12-20 05:33:33 -06:00
info.title = this.templateSrv.replace(link.title || '', scopedVars);
const slug = kbn.slugifyForUrl(link.dashboard || '');
2017-12-20 05:33:33 -06:00
info.href = 'dashboard/db/' + slug + '?';
2017-10-25 08:29:13 -05:00
}
const params: any = {};
2017-10-25 08:29:13 -05:00
if (link.keepTime) {
const range = this.timeSrv.timeRangeForUrl();
2017-12-20 05:33:33 -06:00
params['from'] = range.from;
params['to'] = range.to;
2017-10-25 08:29:13 -05:00
}
if (link.includeVars) {
this.templateSrv.fillVariableValuesForUrl(params, scopedVars);
}
2017-10-26 06:25:47 -05:00
info.href = this.addParamsToUrl(info.href, params);
2017-10-25 08:29:13 -05:00
if (link.params) {
info.href = this.appendToQueryString(info.href, this.templateSrv.replace(link.params, scopedVars));
2017-10-25 08:29:13 -05:00
}
return info;
}
}
2017-12-20 05:33:33 -06:00
angular.module('grafana.services').service('linkSrv', LinkSrv);