From 2dd49e6e5022663545dbd5e591b3f7816e267d86 Mon Sep 17 00:00:00 2001 From: Patrick O'Carroll Date: Wed, 25 Oct 2017 15:29:13 +0200 Subject: [PATCH 1/6] converted linkSrv.js to linkSrv.ts --- public/app/features/panellinks/linkSrv.js | 118 ---------------------- public/app/features/panellinks/linkSrv.ts | 114 +++++++++++++++++++++ 2 files changed, 114 insertions(+), 118 deletions(-) delete mode 100644 public/app/features/panellinks/linkSrv.js create mode 100644 public/app/features/panellinks/linkSrv.ts diff --git a/public/app/features/panellinks/linkSrv.js b/public/app/features/panellinks/linkSrv.js deleted file mode 100644 index 89d89487c51..00000000000 --- a/public/app/features/panellinks/linkSrv.js +++ /dev/null @@ -1,118 +0,0 @@ -define([ - 'angular', - 'lodash', - 'app/core/utils/kbn', -], -function (angular, _, kbn) { - 'use strict'; - - kbn = kbn.default; - - angular - .module('grafana.services') - .service('linkSrv', function(templateSrv, timeSrv) { - - this.getLinkUrl = function(link) { - var url = templateSrv.replace(link.url || ''); - var params = {}; - - if (link.keepTime) { - var range = timeSrv.timeRangeForUrl(); - params['from'] = range.from; - params['to'] = range.to; - } - - if (link.includeVars) { - templateSrv.fillVariableValuesForUrl(params); - } - - return this.addParamsToUrl(url, params); - }; - - this.addParamsToUrl = function(url, params) { - var paramsArray = []; - _.each(params, function(value, key) { - if (value === null) { return; } - if (value === true) { - paramsArray.push(key); - } - else if (_.isArray(value)) { - _.each(value, function(instance) { - paramsArray.push(key + '=' + encodeURIComponent(instance)); - }); - } - else { - paramsArray.push(key + '=' + encodeURIComponent(value)); - } - }); - - if (paramsArray.length === 0) { - return url; - } - - return this.appendToQueryString(url, paramsArray.join('&')); - }; - - this.appendToQueryString = function(url, stringToAppend) { - if (!_.isUndefined(stringToAppend) && stringToAppend !== null && stringToAppend !== '') { - var pos = url.indexOf('?'); - if (pos !== -1) { - if (url.length - pos > 1) { - url += '&'; - } - } else { - url += '?'; - } - url += stringToAppend; - } - return url; - }; - - this.getAnchorInfo = function(link) { - var info = {}; - info.href = this.getLinkUrl(link); - info.title = templateSrv.replace(link.title || ''); - return info; - }; - - this.getPanelLinkAnchorInfo = function(link, scopedVars) { - var info = {}; - if (link.type === 'absolute') { - info.target = link.targetBlank ? '_blank' : '_self'; - info.href = templateSrv.replace(link.url || '', scopedVars); - info.title = templateSrv.replace(link.title || '', scopedVars); - } - else if (link.dashUri) { - info.href = 'dashboard/' + link.dashUri + '?'; - info.title = templateSrv.replace(link.title || '', scopedVars); - info.target = link.targetBlank ? '_blank' : ''; - } - else { - info.title = templateSrv.replace(link.title || '', scopedVars); - var slug = kbn.slugifyForUrl(link.dashboard || ''); - info.href = 'dashboard/db/' + slug + '?'; - } - - var params = {}; - - if (link.keepTime) { - var range = timeSrv.timeRangeForUrl(); - params['from'] = range.from; - params['to'] = range.to; - } - - if (link.includeVars) { - templateSrv.fillVariableValuesForUrl(params, scopedVars); - } - - info.href = this.addParamsToUrl(info.href, params); - - if (link.params) { - info.href = this.appendToQueryString(info.href, templateSrv.replace(link.params, scopedVars)); - } - - return info; - }; - - }); -}); diff --git a/public/app/features/panellinks/linkSrv.ts b/public/app/features/panellinks/linkSrv.ts new file mode 100644 index 00000000000..5847d0b7b46 --- /dev/null +++ b/public/app/features/panellinks/linkSrv.ts @@ -0,0 +1,114 @@ +import angular from 'angular'; +import _ from 'lodash'; +import kbn from 'app/core/utils/kbn'; + + +export class LinkSrv { + + /** @ngInject */ + constructor(private templateSrv, private timeSrv) { + } + + getLinkUrl(link) { + var url = this.templateSrv.replace(link.url || ''); + var params = {}; + + if (link.keepTime) { + var range = this.timeSrv.timeRangeForUrl(); + params['from'] = range.from; + params['to'] = range.to; + } + + if (link.includeVars) { + this.templateSrv.fillVariableValuesForUrl(params); + } + + return this.addParamsToUrl(url, params); + } + + addParamsToUrl(url, params) { + var paramsArray = []; + + _.each(params, function(value, key) { + if (value === null) { return; } + if (value === true) { + paramsArray.push(key); + } else if (_.isArray(value)) { + _.each(value, function(instance) { + paramsArray.push(key + '=' + encodeURIComponent(instance)); + }); + } else { + paramsArray.push(key + '=' + encodeURIComponent(value)); + } + }); + + if (paramsArray.length === 0) { + return url; + } + + return this.appendToQueryString(url, paramsArray.join('&')); + } + + appendToQueryString(url, stringToAppend) { + if (!_.isUndefined(stringToAppend) && stringToAppend !== null && stringToAppend !== '') { + var pos = url.indexOf('?'); + if (pos !== -1) { + if (url.length - pos > 1) { + url += '&'; + } + } else { + url += '?'; + } + url += stringToAppend; + } + + return url; + } + + getAnchorInfo(link) { + var info = {}; + (info).href = this.getLinkUrl(link); + (info).title = this.templateSrv.replace(link.title || ''); + return info; + } + + getPanelLinkAnchorInfo(link, scopedVars) { + var info = {}; + 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.dashUri) { + (info).href = 'dashboard/' + link.dashUri + '?'; + (info).title = this.templateSrv.replace(link.title || '', scopedVars); + (info).target = link.targetBlank ? '_blank' : ''; + } else { + (info).title = this.templateSrv.replace(link.title || '', scopedVars); + var slug = kbn.slugifyForUrl(link.dashboard || ''); + (info).href = 'dashboard/db/' + slug + '?'; + } + + var params = {}; + + if (link.keepTime) { + var range = this.timeSrv.timeRangeForUrl(); + params['from'] = range.from; + params['to'] = range.to; + } + + if (link.includeVars) { + this.templateSrv.fillVariableValuesForUrl(params, scopedVars); + } + + (info).href = this.addParamsToUrl((info).href, params); + + if (link.params) { + (info).href = this.appendToQueryString((info).href, this.templateSrv.replace(link.params, scopedVars)); + } + + return info; + } + +} + +angular.module('grafana.services').service('linkSrv', LinkSrv); From 2f4744ca7127ba923cc8e5565f6b78cea2a0daa7 Mon Sep 17 00:00:00 2001 From: Patrick O'Carroll Date: Thu, 26 Oct 2017 13:25:47 +0200 Subject: [PATCH 2/6] declared any to info in declaration --- public/app/features/panellinks/linkSrv.ts | 28 +++++++++++------------ 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/public/app/features/panellinks/linkSrv.ts b/public/app/features/panellinks/linkSrv.ts index 5847d0b7b46..b167fb813ac 100644 --- a/public/app/features/panellinks/linkSrv.ts +++ b/public/app/features/panellinks/linkSrv.ts @@ -66,26 +66,26 @@ export class LinkSrv { } getAnchorInfo(link) { - var info = {}; - (info).href = this.getLinkUrl(link); - (info).title = this.templateSrv.replace(link.title || ''); + var info: any = {}; + info.href = this.getLinkUrl(link); + info.title = this.templateSrv.replace(link.title || ''); return info; } getPanelLinkAnchorInfo(link, scopedVars) { - var info = {}; + var info: any = {}; 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); + 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.dashUri) { - (info).href = 'dashboard/' + link.dashUri + '?'; - (info).title = this.templateSrv.replace(link.title || '', scopedVars); - (info).target = link.targetBlank ? '_blank' : ''; + info.href = 'dashboard/' + link.dashUri + '?'; + info.title = this.templateSrv.replace(link.title || '', scopedVars); + info.target = link.targetBlank ? '_blank' : ''; } else { - (info).title = this.templateSrv.replace(link.title || '', scopedVars); + info.title = this.templateSrv.replace(link.title || '', scopedVars); var slug = kbn.slugifyForUrl(link.dashboard || ''); - (info).href = 'dashboard/db/' + slug + '?'; + info.href = 'dashboard/db/' + slug + '?'; } var params = {}; @@ -100,10 +100,10 @@ export class LinkSrv { this.templateSrv.fillVariableValuesForUrl(params, scopedVars); } - (info).href = this.addParamsToUrl((info).href, params); + info.href = this.addParamsToUrl(info.href, params); if (link.params) { - (info).href = this.appendToQueryString((info).href, this.templateSrv.replace(link.params, scopedVars)); + info.href = this.appendToQueryString(info.href, this.templateSrv.replace(link.params, scopedVars)); } return info; From a228bb2308f32b74a68e27cab13ae122594f5299 Mon Sep 17 00:00:00 2001 From: Patrick O'Carroll Date: Thu, 26 Oct 2017 13:34:41 +0200 Subject: [PATCH 3/6] renamed file --- public/app/features/panellinks/{linkSrv.ts => link_srv.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename public/app/features/panellinks/{linkSrv.ts => link_srv.ts} (100%) diff --git a/public/app/features/panellinks/linkSrv.ts b/public/app/features/panellinks/link_srv.ts similarity index 100% rename from public/app/features/panellinks/linkSrv.ts rename to public/app/features/panellinks/link_srv.ts From e654f80e4b92032bf4e8532f55a19392b7c2226d Mon Sep 17 00:00:00 2001 From: Patrick O'Carroll Date: Thu, 26 Oct 2017 14:25:43 +0200 Subject: [PATCH 4/6] fixed link issues --- public/app/features/panellinks/link_srv.ts | 1 - public/app/features/panellinks/module.js | 2 +- public/app/plugins/panel/singlestat/module.ts | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/public/app/features/panellinks/link_srv.ts b/public/app/features/panellinks/link_srv.ts index b167fb813ac..873e0233e41 100644 --- a/public/app/features/panellinks/link_srv.ts +++ b/public/app/features/panellinks/link_srv.ts @@ -2,7 +2,6 @@ import angular from 'angular'; import _ from 'lodash'; import kbn from 'app/core/utils/kbn'; - export class LinkSrv { /** @ngInject */ diff --git a/public/app/features/panellinks/module.js b/public/app/features/panellinks/module.js index 351b38f27c4..a36317dc2b3 100644 --- a/public/app/features/panellinks/module.js +++ b/public/app/features/panellinks/module.js @@ -1,7 +1,7 @@ define([ 'angular', 'lodash', - './linkSrv', + './link_srv', ], function (angular, _) { 'use strict'; diff --git a/public/app/plugins/panel/singlestat/module.ts b/public/app/plugins/panel/singlestat/module.ts index 5879b2d720a..134a703a0fb 100644 --- a/public/app/plugins/panel/singlestat/module.ts +++ b/public/app/plugins/panel/singlestat/module.ts @@ -2,7 +2,7 @@ import _ from 'lodash'; import $ from 'jquery'; import 'vendor/flot/jquery.flot'; import 'vendor/flot/jquery.flot.gauge'; -import 'app/features/panellinks/linkSrv'; +import 'app/features/panellinks/link_srv'; import kbn from 'app/core/utils/kbn'; import config from 'app/core/config'; From f097bce565ad5adb689c048fb31baadfd8535715 Mon Sep 17 00:00:00 2001 From: Patrick O'Carroll Date: Thu, 26 Oct 2017 14:47:07 +0200 Subject: [PATCH 5/6] more link fixes --- public/app/features/dashboard/specs/share_modal_ctrl_specs.ts | 2 +- public/app/features/panellinks/specs/link_srv_specs.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public/app/features/dashboard/specs/share_modal_ctrl_specs.ts b/public/app/features/dashboard/specs/share_modal_ctrl_specs.ts index 7a04f5f7579..c39342a6f40 100644 --- a/public/app/features/dashboard/specs/share_modal_ctrl_specs.ts +++ b/public/app/features/dashboard/specs/share_modal_ctrl_specs.ts @@ -2,7 +2,7 @@ import {describe, beforeEach, it, expect, sinon, angularMocks} from 'test/lib/co import helpers from 'test/specs/helpers'; import '../shareModalCtrl'; import config from 'app/core/config'; -import 'app/features/panellinks/linkSrv'; +import 'app/features/panellinks/link_srv'; describe('ShareModalCtrl', function() { var ctx = new helpers.ControllerTestContext(); diff --git a/public/app/features/panellinks/specs/link_srv_specs.ts b/public/app/features/panellinks/specs/link_srv_specs.ts index 77bb0a36c1f..397e97ca758 100644 --- a/public/app/features/panellinks/specs/link_srv_specs.ts +++ b/public/app/features/panellinks/specs/link_srv_specs.ts @@ -1,5 +1,5 @@ import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common'; -import 'app/features/panellinks/linkSrv'; +import 'app/features/panellinks/link_srv'; import _ from 'lodash'; describe('linkSrv', function() { From ec94dfa8906ebbb63bc3c1dc1acf1c09561ae9fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Tue, 31 Oct 2017 14:29:25 +0100 Subject: [PATCH 6/6] tests: migrated tests for link_srv to jest, #9666 --- public/app/features/panellinks/link_srv.ts | 8 ++-- .../panellinks/specs/link_srv_specs.ts | 46 ------------------- 2 files changed, 4 insertions(+), 50 deletions(-) delete mode 100644 public/app/features/panellinks/specs/link_srv_specs.ts diff --git a/public/app/features/panellinks/link_srv.ts b/public/app/features/panellinks/link_srv.ts index 873e0233e41..71192a86487 100644 --- a/public/app/features/panellinks/link_srv.ts +++ b/public/app/features/panellinks/link_srv.ts @@ -5,8 +5,7 @@ import kbn from 'app/core/utils/kbn'; export class LinkSrv { /** @ngInject */ - constructor(private templateSrv, private timeSrv) { - } + constructor(private templateSrv, private timeSrv) {} getLinkUrl(link) { var url = this.templateSrv.replace(link.url || ''); @@ -29,7 +28,9 @@ export class LinkSrv { var paramsArray = []; _.each(params, function(value, key) { - if (value === null) { return; } + if (value === null) { + return; + } if (value === true) { paramsArray.push(key); } else if (_.isArray(value)) { @@ -107,7 +108,6 @@ export class LinkSrv { return info; } - } angular.module('grafana.services').service('linkSrv', LinkSrv); diff --git a/public/app/features/panellinks/specs/link_srv_specs.ts b/public/app/features/panellinks/specs/link_srv_specs.ts deleted file mode 100644 index 397e97ca758..00000000000 --- a/public/app/features/panellinks/specs/link_srv_specs.ts +++ /dev/null @@ -1,46 +0,0 @@ -import {describe, beforeEach, it, expect, angularMocks} from 'test/lib/common'; -import 'app/features/panellinks/link_srv'; -import _ from 'lodash'; - -describe('linkSrv', function() { - var _linkSrv; - - beforeEach(angularMocks.module('grafana.core')); - beforeEach(angularMocks.module('grafana.services')); - - beforeEach(angularMocks.inject(function(linkSrv) { - _linkSrv = linkSrv; - })); - - describe('when appending query strings', function() { - - it('add ? to URL if not present', function() { - var url = _linkSrv.appendToQueryString('http://example.com', 'foo=bar'); - expect(url).to.be('http://example.com?foo=bar'); - }); - - it('do not add & to URL if ? is present but query string is empty', function() { - var url = _linkSrv.appendToQueryString('http://example.com?', 'foo=bar'); - expect(url).to.be('http://example.com?foo=bar'); - }); - - it('add & to URL if query string is present', function() { - var url = _linkSrv.appendToQueryString('http://example.com?foo=bar', 'hello=world'); - expect(url).to.be('http://example.com?foo=bar&hello=world'); - }); - - it('do not change the URL if there is nothing to append', function() { - _.each(['', undefined, null], function(toAppend) { - var url1 = _linkSrv.appendToQueryString('http://example.com', toAppend); - expect(url1).to.be('http://example.com'); - - var url2 = _linkSrv.appendToQueryString('http://example.com?', toAppend); - expect(url2).to.be('http://example.com?'); - - var url3 = _linkSrv.appendToQueryString('http://example.com?foo=bar', toAppend); - expect(url3).to.be('http://example.com?foo=bar'); - }); - }); - - }); -});