discourse/app/assets/javascripts/discourse-common/lib/icon-library.js.es6

126 lines
3.3 KiB
Plaintext
Raw Normal View History

2018-06-15 23:03:24 +08:00
import { h } from "virtual-dom";
let _renderers = [];
const REPLACEMENTS = {
2018-06-15 23:03:24 +08:00
"d-tracking": "circle",
"d-muted": "times-circle",
"d-regular": "circle-o",
2018-06-15 23:03:24 +08:00
"d-watching": "exclamation-circle",
"d-watching-first": "dot-circle-o",
2018-06-15 23:03:24 +08:00
"d-drop-expanded": "caret-down",
"d-drop-collapsed": "caret-right",
"d-unliked": "heart-o",
2018-06-15 23:03:24 +08:00
"d-liked": "heart",
"notification.mentioned": "at",
"notification.group_mentioned": "at",
"notification.quoted": "quote-right",
"notification.replied": "reply",
"notification.posted": "reply",
"notification.edited": "pencil",
2018-06-15 23:03:24 +08:00
"notification.liked": "heart",
"notification.liked_2": "heart",
"notification.liked_many": "heart",
"notification.private_message": "envelope-o",
"notification.invited_to_private_message": "envelope-o",
"notification.invited_to_topic": "hand-o-right",
2018-06-15 23:03:24 +08:00
"notification.invitee_accepted": "user",
"notification.moved_post": "sign-out",
"notification.linked": "link",
"notification.granted_badge": "certificate",
"notification.topic_reminder": "hand-o-right",
"notification.watching_first_post": "dot-circle-o",
2018-06-15 23:03:24 +08:00
"notification.group_message_summary": "group"
};
2017-09-16 01:54:47 +08:00
export function replaceIcon(source, destination) {
REPLACEMENTS[source] = destination;
}
export function renderIcon(renderType, id, params) {
2018-06-15 23:03:24 +08:00
for (let i = 0; i < _renderers.length; i++) {
let renderer = _renderers[i];
let rendererForType = renderer[renderType];
if (rendererForType) {
const icon = { id, replacementId: REPLACEMENTS[id] };
let result = rendererForType(icon, params || {});
if (result) {
return result;
}
}
}
}
export function iconHTML(id, params) {
2018-06-15 23:03:24 +08:00
return renderIcon("string", id, params);
}
export function iconNode(id, params) {
2018-06-15 23:03:24 +08:00
return renderIcon("node", id, params);
}
// TODO: Improve how helpers are registered for vdom compliation
if (typeof Discourse !== "undefined") {
Discourse.__widget_helpers.iconNode = iconNode;
}
export function registerIconRenderer(renderer) {
_renderers.unshift(renderer);
}
// Support for font awesome icons
function faClasses(icon, params) {
let classNames = `fa fa-${icon.replacementId || icon.id} d-icon d-icon-${
icon.id
}`;
if (params) {
if (params.modifier) {
classNames += " fa-" + params.modifier;
}
if (params["class"]) {
classNames += " " + params["class"];
}
}
return classNames;
}
// default resolver is font awesome
registerIconRenderer({
2018-06-15 23:03:24 +08:00
name: "font-awesome",
string(icon, params) {
let tagName = params.tagName || "i";
let html = `<${tagName} class='${faClasses(icon, params)}'`;
if (params.title) {
html += ` title='${I18n.t(params.title).replace(/'/g, "&#39;")}'`;
}
2018-06-15 23:03:24 +08:00
if (params.label) {
html += " aria-hidden='true'";
}
html += `></${tagName}>`;
if (params.label) {
html += `<span class='sr-only'>${params.label}</span>`;
}
return html;
},
node(icon, params) {
let tagName = params.tagName || "i";
const properties = {
className: faClasses(icon, params),
attributes: { "aria-hidden": true }
};
2018-06-15 23:03:24 +08:00
if (params.title) {
properties.attributes.title = params.title;
}
if (params.label) {
return h(tagName, properties, h("span.sr-only", I18n.t(params.label)));
} else {
return h(tagName, properties);
}
}
});