This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/widgets/quick-access-item.js.es6
Joffrey JAFFEUX e92e06c3a7
FIX: adds data-topic-id to quick-access-item (#8430)
This fix will allow discourse-encrypt to decrypt messages titles of the quick-access-pannel for PMs
2019-11-28 11:27:36 +01:00

73 lines
1.7 KiB
JavaScript

import { h } from "virtual-dom";
import RawHtml from "discourse/widgets/raw-html";
import { createWidget } from "discourse/widgets/widget";
import { emojiUnescape } from "discourse/lib/text";
import { iconNode } from "discourse-common/lib/icon-library";
import { escapeExpression } from "discourse/lib/utilities";
/**
* This helper widget tries to enforce a consistent look and behavior for any
* item under any quick access panels.
*
* It accepts the following attributes:
* action
* actionParam
* content
* escapedContent
* href
* icon
* read
* username
*/
createWidget("quick-access-item", {
tagName: "li",
buildClasses(attrs) {
const result = [];
if (attrs.className) {
result.push(attrs.className);
}
if (attrs.read === undefined || attrs.read) {
result.push("read");
}
return result;
},
html({ href, icon }) {
let content = this._contentHtml();
if (href) {
let topicId = href.match(/\/t\/.*?\/(\d+)/);
if (topicId && topicId[1]) {
topicId = escapeExpression(topicId[1]);
content = `<span data-topic-id="${topicId}">${content}</span>`;
}
}
return h("a", { attributes: { href } }, [
iconNode(icon),
new RawHtml({
html: `<div>${this._usernameHtml()}${content}</div>`
})
]);
},
click(e) {
this.attrs.read = true;
if (this.attrs.action) {
e.preventDefault();
return this.sendWidgetAction(this.attrs.action, this.attrs.actionParam);
}
},
_contentHtml() {
const content =
this.attrs.escapedContent || escapeExpression(this.attrs.content);
return emojiUnescape(content);
},
_usernameHtml() {
return this.attrs.username ? `<span>${this.attrs.username}</span> ` : "";
}
});