Used underscore to template list children to utilize their simple escaped interpolation

This commit is contained in:
Luke "Jared" Bennett 2017-04-12 08:11:12 +01:00
parent a62ae94d9c
commit 04eaed8088
No known key found for this signature in database
GPG Key ID: 402ED51FB5D306C2
4 changed files with 12 additions and 10 deletions

View File

@ -2,10 +2,12 @@ const DATA_TRIGGER = 'data-dropdown-trigger';
const DATA_DROPDOWN = 'data-dropdown'; const DATA_DROPDOWN = 'data-dropdown';
const SELECTED_CLASS = 'droplab-item-selected'; const SELECTED_CLASS = 'droplab-item-selected';
const ACTIVE_CLASS = 'droplab-item-active'; const ACTIVE_CLASS = 'droplab-item-active';
const TEMPLATE_REGEX = /\{\{(.+?)\}\}/g;
export { export {
DATA_TRIGGER, DATA_TRIGGER,
DATA_DROPDOWN, DATA_DROPDOWN,
SELECTED_CLASS, SELECTED_CLASS,
ACTIVE_CLASS, ACTIVE_CLASS,
TEMPLATE_REGEX,
}; };

View File

@ -93,7 +93,7 @@ Object.assign(DropDown.prototype, {
}, },
renderChildren: function(data) { renderChildren: function(data) {
var html = utils.t(this.templateString, data); var html = utils.template(this.templateString, data);
var template = document.createElement('div'); var template = document.createElement('div');
template.innerHTML = html; template.innerHTML = html;

View File

@ -1,19 +1,19 @@
/* eslint-disable */ /* eslint-disable */
import { DATA_TRIGGER, DATA_DROPDOWN } from './constants'; import { template as _template } from 'underscore';
import { DATA_TRIGGER, DATA_DROPDOWN, TEMPLATE_REGEX } from './constants';
const utils = { const utils = {
toCamelCase(attr) { toCamelCase(attr) {
return this.camelize(attr.split('-').slice(1).join(' ')); return this.camelize(attr.split('-').slice(1).join(' '));
}, },
t(s, d) { template(templateString, data) {
for (const p in d) { const template = _template(templateString, {
if (Object.prototype.hasOwnProperty.call(d, p)) { escape: TEMPLATE_REGEX,
s = s.replace(new RegExp(`{{${p}}}`, 'g'), d[p]); });
}
} return template(data);
return s;
}, },
camelize(str) { camelize(str) {

View File

@ -63,7 +63,7 @@ require('./filtered_search_dropdown');
Object.assign({ Object.assign({
icon: `fa-${icon}`, icon: `fa-${icon}`,
hint, hint,
tag: `&lt;${tag}&gt;`, tag: `<${tag}>`,
}, type && { type }), }, type && { type }),
); );
} }