DEV: allows to decorate username selector (#9869)

DEV: allows to decorate username selector (#9869)

Usage:

api.addUsernameSelectorDecorator(username => {
  return iconHTML("calendar-alt");
});
diff --git a/app/assets/javascripts/discourse/app/helpers/decorate-username-selector.js b/app/assets/javascripts/discourse/app/helpers/decorate-username-selector.js
new file mode 100644
index 0000000..6b2da95
--- /dev/null
+++ b/app/assets/javascripts/discourse/app/helpers/decorate-username-selector.js
@@ -0,0 +1,21 @@
+import { htmlSafe } from "@ember/template";
+import { registerUnbound } from "discourse-common/lib/helpers";
+
+let usernameDecorators = [];
+export function addUsernameSelectorDecorator(decorator) {
+  usernameDecorators.push(decorator);
+}
+
+export function resetUsernameDecorators() {
+  usernameDecorators = [];
+}
+
+export default registerUnbound("decorate-username-selector", username => {
+  const decorations = [];
+
+  usernameDecorators.forEach(decorator => {
+    decorations.push(decorator(username));
+  });
+
+  return decorations.length ? htmlSafe(decorations.join("")) : "";
+});
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index d17fde9..752b2a2 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -40,6 +40,7 @@ import { replaceFormatter } from "discourse/lib/utilities";
 import { modifySelectKit } from "select-kit/mixins/plugin-api";
 import { addGTMPageChangedCallback } from "discourse/lib/page-tracker";
 import { registerCustomAvatarHelper } from "discourse/helpers/user-avatar";
+import { addUsernameSelectorDecorator } from "discourse/helpers/decorate-username-selector";
 import { disableNameSuppression } from "discourse/widgets/poster-name";
 import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 } from "discourse/controllers/topic";
 import Sharing from "discourse/lib/sharing";
@@ -928,6 +929,19 @@ class PluginApi {
   }
 
   /**
+   * Registers a function to decorate each autocomplete usernames.
+   *
+   * Example:
+   *
+   * api.appendUsernameDecorator(username => {
+   *   return `<span class="status">[is_away]</class>`;
+   * })
+   */
+  addUsernameSelectorDecorator(decorator) {
+    addUsernameSelectorDecorator(decorator);
+  }
+
+  /**
    * Registers a "beforeSave" function on the composer. This allows you to
    * implement custom logic that will happen before the user makes a post.
    *
diff --git a/app/assets/javascripts/discourse/app/templates/user-selector-autocomplete.hbr b/app/assets/javascripts/discourse/app/templates/user-selector-autocomplete.hbr
index 22de863..797bd64 100644
--- a/app/assets/javascripts/discourse/app/templates/user-selector-autocomplete.hbr
+++ b/app/assets/javascripts/discourse/app/templates/user-selector-autocomplete.hbr
@@ -6,6 +6,7 @@
           {{avatar user imageSize="tiny"}}
           <span class='username'>{{format-username user.username}}</span>
           <span class='name'>{{user.name}}</span>
+          {{decorate-username-selector user.username}}
         </a>
       </li>
     {{/each}}
diff --git a/test/javascripts/helpers/qunit-helpers.js b/test/javascripts/helpers/qunit-helpers.js
index f196bd0..3a92b29 100644
--- a/test/javascripts/helpers/qunit-helpers.js
+++ b/test/javascripts/helpers/qunit-helpers.js
@@ -19,6 +19,7 @@ import { resetWidgetCleanCallbacks } from "discourse/components/mount-widget";
 import { resetTopicTitleDecorators } from "discourse/components/topic-title";
 import { resetDecorators as resetPostCookedDecorators } from "discourse/widgets/post-cooked";
 import { resetDecorators as resetPluginOutletDecorators } from "discourse/components/plugin-connector";
+import { resetUsernameDecorators } from "discourse/helpers/decorate-username-selector";
 import { resetCache as resetOneboxCache } from "pretty-text/oneboxer";
 import { resetCustomPostMessageCallbacks } from "discourse/controllers/topic";
 import User from "discourse/models/user";
@@ -162,6 +163,7 @@ export function acceptance(name, options) {
       resetPostCookedDecorators();
       resetPluginOutletDecorators();
       resetTopicTitleDecorators();
+      resetUsernameDecorators();
       resetOneboxCache();
       resetCustomPostMessageCallbacks();
       Discourse._runInitializer("instanceInitializers", function(

GitHub sha: 8825395b

1 Like

This commit appears in #9869 which was approved by eviltrout. It was merged by jjaffeux.