DEV: Add data-topic-id to more elements containing titles.

DEV: Add data-topic-id to more elements containing titles.

From 1f9f00a629f2f013cf358bd6a9683dd701f2acb1 Mon Sep 17 00:00:00 2001
From: Dan Ungureanu <dan@ungureanu.me>
Date: Sun, 4 Nov 2018 11:23:45 +0200
Subject: [PATCH] DEV: Add data-topic-id to more elements containing titles.


diff --git a/app/assets/javascripts/discourse/components/composer-action-title.js.es6 b/app/assets/javascripts/discourse/components/composer-action-title.js.es6
index 55a437f..25fc7f4 100644
--- a/app/assets/javascripts/discourse/components/composer-action-title.js.es6
+++ b/app/assets/javascripts/discourse/components/composer-action-title.js.es6
@@ -66,9 +66,9 @@ export default Ember.Component.extend({
   },
 
   _formatReplyToTopic(link) {
-    return `<a class="topic-link" href="${link.href}">${
-      link.anchor
-    }</a>`.htmlSafe();
+    return `<a class="topic-link" href="${link.href}" data-topic-id="${this.get(
+      "model.topic.id"
+    )}">${link.anchor}</a>`.htmlSafe();
   },
 
   _formatReplyToUserPost(avatar, link) {
diff --git a/app/assets/javascripts/discourse/helpers/topic-link.js.es6 b/app/assets/javascripts/discourse/helpers/topic-link.js.es6
index 32a55cf..31e6a6a 100644
--- a/app/assets/javascripts/discourse/helpers/topic-link.js.es6
+++ b/app/assets/javascripts/discourse/helpers/topic-link.js.es6
@@ -11,6 +11,6 @@ registerUnbound("topic-link", (topic, args) => {
     args.class.split(" ").forEach(c => classes.push(c));
   }
 
-  const result = `<a href='${url}' class='${classes.join(" ")}'>${title}</a>`;
+  const result = `<a href='${url}' class='${classes.join(" ")}' data-topic-id='${topic.id}'>${title}</a>`;
   return new Handlebars.SafeString(result);
 });
diff --git a/app/assets/javascripts/discourse/templates/topic.hbs b/app/assets/javascripts/discourse/templates/topic.hbs
index 400a7ef..b87bf86 100644
--- a/app/assets/javascripts/discourse/templates/topic.hbs
+++ b/app/assets/javascripts/discourse/templates/topic.hbs
@@ -44,7 +44,7 @@
           </div>
 
         {{else}}
-          <h1>
+          <h1 data-topic-id="{{unbound model.id}}">
             {{#unless model.is_warning}}
               {{#if siteSettings.enable_personal_messages}}
                 <a href={{pmPath}}>
diff --git a/app/assets/javascripts/discourse/widgets/header-topic-info.js.es6 b/app/assets/javascripts/discourse/widgets/header-topic-info.js.es6
index 6f79883..def3708 100644
--- a/app/assets/javascripts/discourse/widgets/header-topic-info.js.es6
+++ b/app/assets/javascripts/discourse/widgets/header-topic-info.js.es6
@@ -40,6 +40,7 @@ export default createWidget("header-topic-info", {
           className: "topic-link",
           action: "jumpToTopPost",
           href,
+          attributes: { "data-topic-id": topic.get("id") },
           contents: () => titleHTML
         })
       );
diff --git a/app/assets/javascripts/discourse/widgets/link.js.es6 b/app/assets/javascripts/discourse/widgets/link.js.es6
index 24d62c3..841fda0 100644
--- a/app/assets/javascripts/discourse/widgets/link.js.es6
+++ b/app/assets/javascripts/discourse/widgets/link.js.es6
@@ -35,12 +35,18 @@ export default createWidget("link", {
   },
 
   buildAttributes(attrs) {
-    return {
+    const ret = {
       href: this.href(attrs),
       title: attrs.title
         ? I18n.t(attrs.title, attrs.titleOptions)
         : this.label(attrs)
     };
+    if (attrs.attributes) {
+      Object.keys(attrs.attributes).forEach(
+        k => (ret[k] = attrs.attributes[k])
+      );
+    }
+    return ret;
   },
 
   label(attrs) {
diff --git a/app/assets/javascripts/discourse/widgets/post.js.es6 b/app/assets/javascripts/discourse/widgets/post.js.es6
index 0c64696..51a46c9 100644
--- a/app/assets/javascripts/discourse/widgets/post.js.es6
+++ b/app/assets/javascripts/discourse/widgets/post.js.es6
@@ -464,7 +464,11 @@ createWidget("post-article", {
   },
 
   buildAttributes(attrs) {
-    return { "data-post-id": attrs.id, "data-user-id": attrs.user_id };
+    return {
+      "data-post-id": attrs.id,
+      "data-topic-id": attrs.topicId,
+      "data-user-id": attrs.user_id
+    };
   },
 
   html(attrs, state) {

GitHub

1 Like