mirror of https://github.com/hashicorp/consul
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.pull/9357/head
parent
e0a4646768
commit
613be01f43
@ -1,18 +1,19 @@
|
|||||||
import { helper } from '@ember/component/helper';
|
import Helper from '@ember/component/helper';
|
||||||
import { get } from '@ember/object';
|
import { inject as service } from '@ember/service';
|
||||||
|
|
||||||
// Covers alpha-capitalized dot separated API keys such as
|
// simple mustache regexp `/{{item.Name}}/`
|
||||||
// `{{Name}}`, `{{Service.Name}}` etc. but not `{{}}`
|
|
||||||
const templateRe = /{{([A-Za-z.0-9_-]+)}}/g;
|
const templateRe = /{{([A-Za-z.0-9_-]+)}}/g;
|
||||||
export default helper(function renderTemplate([template, vars]) {
|
let render;
|
||||||
if (typeof vars !== 'undefined' && typeof template !== 'undefined') {
|
export default class RenderTemplateHelper extends Helper {
|
||||||
return template.replace(templateRe, function(match, group) {
|
@service('encoder') encoder;
|
||||||
try {
|
constructor() {
|
||||||
return encodeURIComponent(get(vars, group) || '');
|
super(...arguments);
|
||||||
} catch (e) {
|
if (typeof render !== 'function') {
|
||||||
return '';
|
render = this.encoder.createRegExpEncoder(templateRe, encodeURIComponent, false);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
return '';
|
|
||||||
});
|
compute([template, vars]) {
|
||||||
|
return render(template, vars);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,10 +1,18 @@
|
|||||||
import Helper from '@ember/component/helper';
|
import Helper from '@ember/component/helper';
|
||||||
import { inject as service } from '@ember/service';
|
import { inject as service } from '@ember/service';
|
||||||
|
|
||||||
|
const templateRe = /\${([A-Za-z.0-9_-]+)}/g;
|
||||||
|
let render;
|
||||||
export default class UriHelper extends Helper {
|
export default class UriHelper extends Helper {
|
||||||
@service('encoder') encoder;
|
@service('encoder') encoder;
|
||||||
|
constructor() {
|
||||||
|
super(...arguments);
|
||||||
|
if (typeof render !== 'function') {
|
||||||
|
render = this.encoder.createRegExpEncoder(templateRe, encodeURIComponent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
compute(params, hash) {
|
compute([template, vars]) {
|
||||||
return this.encoder.uriJoin(params);
|
return render(template, vars);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue