2017-07-27 00:13:49 +08:00
|
|
|
import { h } from 'virtual-dom';
|
|
|
|
let _renderers = [];
|
|
|
|
|
|
|
|
export function renderIcon(renderType, id, params) {
|
|
|
|
for (let i=0; i<_renderers.length; i++) {
|
|
|
|
let renderer = _renderers[i];
|
|
|
|
let rendererForType = renderer[renderType];
|
|
|
|
|
|
|
|
if (rendererForType) {
|
|
|
|
let result = rendererForType(id, params || {});
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function iconHTML(id, params) {
|
|
|
|
return renderIcon('string', id, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function iconNode(id, params) {
|
|
|
|
return renderIcon('node', id, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function registerIconRenderer(renderer) {
|
|
|
|
_renderers.unshift(renderer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Support for font awesome icons
|
|
|
|
function faClasses(id, params) {
|
2017-07-27 04:25:09 +08:00
|
|
|
let classNames = `fa fa-${id} d-icon d-icon-${id}`;
|
2017-07-27 00:13:49 +08:00
|
|
|
if (params) {
|
|
|
|
if (params.modifier) { classNames += " fa-" + params.modifier; }
|
|
|
|
if (params['class']) { classNames += ' ' + params['class']; }
|
|
|
|
}
|
|
|
|
return classNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default resolver is font awesome
|
|
|
|
registerIconRenderer({
|
|
|
|
name: 'font-awesome',
|
|
|
|
|
|
|
|
string(id, params) {
|
2017-07-27 04:33:17 +08:00
|
|
|
let tagName = params.tagName || 'i';
|
|
|
|
let html = `<${tagName} class='${faClasses(id, params)}'`;
|
2017-07-27 00:13:49 +08:00
|
|
|
if (params.title) { html += ` title='${I18n.t(params.title)}'`; }
|
|
|
|
if (params.label) { html += " aria-hidden='true'"; }
|
2017-07-27 04:33:17 +08:00
|
|
|
html += `></${tagName}>`;
|
2017-07-27 00:13:49 +08:00
|
|
|
if (params.label) {
|
|
|
|
html += "<span class='sr-only'>" + I18n.t(params.label) + "</span>";
|
|
|
|
}
|
|
|
|
return html;
|
|
|
|
},
|
|
|
|
|
|
|
|
node(id, params) {
|
|
|
|
const properties = {
|
|
|
|
className: faClasses(id, params),
|
|
|
|
attributes: { "aria-hidden": true }
|
|
|
|
};
|
|
|
|
|
|
|
|
if (params.title) { properties.attributes.title = params.title; }
|
|
|
|
if (params.label) {
|
|
|
|
return h('i', properties, h('span.sr-only', I18n.t(params.label)));
|
|
|
|
} else {
|
|
|
|
return h('i', properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|