From 613be01f43e1e181174ace516efde09dbb620fb7 Mon Sep 17 00:00:00 2001 From: John Cowen Date: Wed, 9 Dec 2020 09:22:46 +0000 Subject: [PATCH] ui: Change URI helper to a template based approach (#9344) This moves our uri helper to use a the template renderer we already have for rendering URLs. --- .../app/components/data-form/index.hbs | 24 ++++++++++++-- .../topology-metrics/series/index.hbs | 10 +++++- .../topology-metrics/stats/index.hbs | 11 ++++++- .../consul-ui/app/helpers/render-template.js | 31 ++++++++++--------- ui/packages/consul-ui/app/helpers/uri.js | 12 +++++-- ui/packages/consul-ui/app/services/encoder.js | 22 +++++++++++++ .../dc/services/show/intentions/index.hbs | 11 ++++++- 7 files changed, 99 insertions(+), 22 deletions(-) diff --git a/ui/packages/consul-ui/app/components/data-form/index.hbs b/ui/packages/consul-ui/app/components/data-form/index.hbs index 1e1cb912e8..32c5658a1f 100644 --- a/ui/packages/consul-ui/app/components/data-form/index.hbs +++ b/ui/packages/consul-ui/app/components/data-form/index.hbs @@ -1,8 +1,28 @@ - + diff --git a/ui/packages/consul-ui/app/components/topology-metrics/stats/index.hbs b/ui/packages/consul-ui/app/components/topology-metrics/stats/index.hbs index a5ee66112e..b116a9bf7b 100644 --- a/ui/packages/consul-ui/app/components/topology-metrics/stats/index.hbs +++ b/ui/packages/consul-ui/app/components/topology-metrics/stats/index.hbs @@ -1,6 +1,15 @@ {{#if (not @noMetricsReason)}} diff --git a/ui/packages/consul-ui/app/helpers/render-template.js b/ui/packages/consul-ui/app/helpers/render-template.js index 8c743a8b80..57c006b84c 100644 --- a/ui/packages/consul-ui/app/helpers/render-template.js +++ b/ui/packages/consul-ui/app/helpers/render-template.js @@ -1,18 +1,19 @@ -import { helper } from '@ember/component/helper'; -import { get } from '@ember/object'; +import Helper from '@ember/component/helper'; +import { inject as service } from '@ember/service'; -// Covers alpha-capitalized dot separated API keys such as -// `{{Name}}`, `{{Service.Name}}` etc. but not `{{}}` +// simple mustache regexp `/{{item.Name}}/` const templateRe = /{{([A-Za-z.0-9_-]+)}}/g; -export default helper(function renderTemplate([template, vars]) { - if (typeof vars !== 'undefined' && typeof template !== 'undefined') { - return template.replace(templateRe, function(match, group) { - try { - return encodeURIComponent(get(vars, group) || ''); - } catch (e) { - return ''; - } - }); +let render; +export default class RenderTemplateHelper extends Helper { + @service('encoder') encoder; + constructor() { + super(...arguments); + if (typeof render !== 'function') { + render = this.encoder.createRegExpEncoder(templateRe, encodeURIComponent, false); + } } - return ''; -}); + + compute([template, vars]) { + return render(template, vars); + } +} diff --git a/ui/packages/consul-ui/app/helpers/uri.js b/ui/packages/consul-ui/app/helpers/uri.js index 46dd443d1b..8c3667ea11 100644 --- a/ui/packages/consul-ui/app/helpers/uri.js +++ b/ui/packages/consul-ui/app/helpers/uri.js @@ -1,10 +1,18 @@ import Helper from '@ember/component/helper'; import { inject as service } from '@ember/service'; +const templateRe = /\${([A-Za-z.0-9_-]+)}/g; +let render; export default class UriHelper extends Helper { @service('encoder') encoder; + constructor() { + super(...arguments); + if (typeof render !== 'function') { + render = this.encoder.createRegExpEncoder(templateRe, encodeURIComponent); + } + } - compute(params, hash) { - return this.encoder.uriJoin(params); + compute([template, vars]) { + return render(template, vars); } } diff --git a/ui/packages/consul-ui/app/services/encoder.js b/ui/packages/consul-ui/app/services/encoder.js index c9a6c135b9..12b2f71d97 100644 --- a/ui/packages/consul-ui/app/services/encoder.js +++ b/ui/packages/consul-ui/app/services/encoder.js @@ -1,10 +1,32 @@ import Service from '@ember/service'; +import { get } from '@ember/object'; +import { runInDebug } from '@ember/debug'; import atob from 'consul-ui/utils/atob'; import btoa from 'consul-ui/utils/btoa'; +const createRegExpEncoder = function(re, encoder = str => str, strict = true) { + return (template = '', vars = {}) => { + if (template !== '') { + return template.replace(re, (match, group) => { + const value = get(vars, group); + runInDebug(() => { + if (strict && typeof value === 'undefined') { + console.error(new Error(`${group} is undefined in ${template}`)); + } + }); + return encoder(value || ''); + }); + } + return ''; + }; +}; export default class EncoderService extends Service { uriComponent = encodeURIComponent; + createRegExpEncoder(re, encoder) { + return createRegExpEncoder(re, encoder); + } + atob() { return atob(...arguments); } diff --git a/ui/packages/consul-ui/app/templates/dc/services/show/intentions/index.hbs b/ui/packages/consul-ui/app/templates/dc/services/show/intentions/index.hbs index 4a27e352e1..7456e16c3c 100644 --- a/ui/packages/consul-ui/app/templates/dc/services/show/intentions/index.hbs +++ b/ui/packages/consul-ui/app/templates/dc/services/show/intentions/index.hbs @@ -1,4 +1,13 @@ - +