2018-06-15 23:03:24 +08:00
|
|
|
import { h } from "virtual-dom";
|
2017-07-27 00:13:49 +08:00
|
|
|
let _renderers = [];
|
|
|
|
|
2017-09-02 00:14:16 +08:00
|
|
|
const REPLACEMENTS = {
|
2018-06-15 23:03:24 +08:00
|
|
|
"d-tracking": "circle",
|
|
|
|
"d-muted": "times-circle",
|
2018-11-08 13:12:18 +08:00
|
|
|
"d-regular": "circle-o",
|
2018-06-15 23:03:24 +08:00
|
|
|
"d-watching": "exclamation-circle",
|
2018-11-08 13:12:18 +08:00
|
|
|
"d-watching-first": "dot-circle-o",
|
2018-06-15 23:03:24 +08:00
|
|
|
"d-drop-expanded": "caret-down",
|
|
|
|
"d-drop-collapsed": "caret-right",
|
2018-11-08 13:12:18 +08:00
|
|
|
"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",
|
2018-11-08 13:12:18 +08:00
|
|
|
"notification.edited": "pencil",
|
2018-06-15 23:03:24 +08:00
|
|
|
"notification.liked": "heart",
|
|
|
|
"notification.liked_2": "heart",
|
|
|
|
"notification.liked_many": "heart",
|
2018-11-08 13:12:18 +08:00
|
|
|
"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",
|
2018-11-08 13:12:18 +08:00
|
|
|
"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-02 00:14:16 +08:00
|
|
|
};
|
|
|
|
|
2017-09-16 01:54:47 +08:00
|
|
|
export function replaceIcon(source, destination) {
|
|
|
|
REPLACEMENTS[source] = destination;
|
|
|
|
}
|
2017-09-14 23:14:43 +08:00
|
|
|
|
2017-07-27 00:13:49 +08:00
|
|
|
export function renderIcon(renderType, id, params) {
|
2018-06-15 23:03:24 +08:00
|
|
|
for (let i = 0; i < _renderers.length; i++) {
|
2017-07-27 00:13:49 +08:00
|
|
|
let renderer = _renderers[i];
|
|
|
|
let rendererForType = renderer[renderType];
|
|
|
|
|
|
|
|
if (rendererForType) {
|
2017-11-21 18:53:09 +08:00
|
|
|
const icon = { id, replacementId: REPLACEMENTS[id] };
|
|
|
|
let result = rendererForType(icon, params || {});
|
2017-07-27 00:13:49 +08:00
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function iconHTML(id, params) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return renderIcon("string", id, params);
|
2017-07-27 00:13:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function iconNode(id, params) {
|
2018-06-15 23:03:24 +08:00
|
|
|
return renderIcon("node", id, params);
|
2017-07-27 00:13:49 +08:00
|
|
|
}
|
|
|
|
|
2017-09-02 00:26:42 +08:00
|
|
|
// TODO: Improve how helpers are registered for vdom compliation
|
|
|
|
if (typeof Discourse !== "undefined") {
|
|
|
|
Discourse.__widget_helpers.iconNode = iconNode;
|
|
|
|
}
|
2017-09-01 23:20:14 +08:00
|
|
|
|
2017-07-27 00:13:49 +08:00
|
|
|
export function registerIconRenderer(renderer) {
|
|
|
|
_renderers.unshift(renderer);
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:12:18 +08:00
|
|
|
// Support for font awesome icons
|
|
|
|
function faClasses(icon, params) {
|
|
|
|
let classNames = `fa fa-${icon.replacementId || icon.id} d-icon d-icon-${
|
|
|
|
icon.id
|
|
|
|
}`;
|
2018-11-08 02:05:43 +08:00
|
|
|
|
2018-11-08 13:12:18 +08:00
|
|
|
if (params) {
|
|
|
|
if (params.modifier) {
|
|
|
|
classNames += " fa-" + params.modifier;
|
|
|
|
}
|
|
|
|
if (params["class"]) {
|
|
|
|
classNames += " " + params["class"];
|
|
|
|
}
|
2017-07-27 00:13:49 +08:00
|
|
|
}
|
|
|
|
return classNames;
|
|
|
|
}
|
|
|
|
|
|
|
|
// default resolver is font awesome
|
|
|
|
registerIconRenderer({
|
2018-06-15 23:03:24 +08:00
|
|
|
name: "font-awesome",
|
2017-07-27 00:13:49 +08:00
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
string(icon, params) {
|
2018-11-08 13:12:18 +08:00
|
|
|
let tagName = params.tagName || "i";
|
|
|
|
let html = `<${tagName} class='${faClasses(icon, params)}'`;
|
|
|
|
if (params.title) {
|
|
|
|
html += ` title='${I18n.t(params.title).replace(/'/g, "'")}'`;
|
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
if (params.label) {
|
|
|
|
html += " aria-hidden='true'";
|
|
|
|
}
|
2018-11-08 13:12:18 +08:00
|
|
|
html += `></${tagName}>`;
|
2017-07-27 00:13:49 +08:00
|
|
|
if (params.label) {
|
2018-02-14 19:41:24 +08:00
|
|
|
html += `<span class='sr-only'>${params.label}</span>`;
|
2017-07-27 00:13:49 +08:00
|
|
|
}
|
|
|
|
return html;
|
|
|
|
},
|
|
|
|
|
2017-11-21 18:53:09 +08:00
|
|
|
node(icon, params) {
|
2018-11-08 13:12:18 +08:00
|
|
|
let tagName = params.tagName || "i";
|
2017-07-28 07:22:19 +08:00
|
|
|
|
2018-11-08 13:12:18 +08:00
|
|
|
const properties = {
|
|
|
|
className: faClasses(icon, params),
|
|
|
|
attributes: { "aria-hidden": true }
|
|
|
|
};
|
2017-07-27 00:13:49 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
if (params.title) {
|
2018-11-08 13:12:18 +08:00
|
|
|
properties.attributes.title = params.title;
|
|
|
|
}
|
|
|
|
if (params.label) {
|
|
|
|
return h(tagName, properties, h("span.sr-only", I18n.t(params.label)));
|
2017-07-27 00:13:49 +08:00
|
|
|
} else {
|
2018-11-08 13:12:18 +08:00
|
|
|
return h(tagName, properties);
|
2017-07-27 00:13:49 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|