discourse/app/assets/javascripts/select-kit/components/topic-notifications-button.js
Joffrey JAFFEUX a947b7b839
FIX: prevents registering multiple topic-notifications-button:changed (#9356)
A large topic page will always have the bottom tracking button, and will also have the timeline, meaning we already had 2 tracking events.

But it gets even worse when you know that the timeline button is a component connector which will trigger `didInsertElement` very frequently, meaning we were constantly adding more and more appEvents handlers.
2020-04-06 15:06:26 +02:00

57 lines
1.4 KiB
JavaScript

import Component from "@ember/component";
export default Component.extend({
layoutName: "select-kit/templates/components/topic-notifications-button",
classNames: ["topic-notifications-button"],
appendReason: true,
showFullTitle: true,
placement: "bottom-start",
didInsertElement() {
this._super(...arguments);
if (!this.mountedAsWidget) {
this.appEvents.on(
"topic-notifications-button:changed",
this,
"_changeTopicNotificationLevel"
);
}
},
willDestroyElement() {
this._super(...arguments);
if (!this.mountedAsWidget) {
this.appEvents.off(
"topic-notifications-button:changed",
this,
"_changeTopicNotificationLevel"
);
}
},
_changeTopicNotificationLevel(level) {
// this change is coming from a keyboard event
if (level.event) {
const topicSectionNode = level.event.target.querySelector("#topic");
if (topicSectionNode && topicSectionNode.dataset.topicId) {
const topicId = parseInt(topicSectionNode.dataset.topicId, 10);
if (topicId && topicId !== this.topic.id) {
return;
}
}
}
if (level.id !== this.notificationLevel) {
this.topic.details.updateNotifications(level.id);
}
},
actions: {
changeTopicNotificationLevel(level, notification) {
this._changeTopicNotificationLevel(notification);
}
}
});