Extend EventListener and add badges on other events

This commit is contained in:
Jiiks 2018-03-08 21:40:43 +02:00
parent dfa7987756
commit 1c63b8a05d
2 changed files with 47 additions and 38 deletions

View File

@ -8,7 +8,7 @@
* LICENSE file in the root directory of this source tree.
*/
import { Events, WebpackModules } from 'modules';
import { Events, WebpackModules, EventListener } from 'modules';
import Reflection from './reflection';
import DOM from './dom';
import VueInjector from './vueinjector';
@ -38,43 +38,10 @@ class TempApi {
}
}
export default class {
export default class extends EventListener {
constructor() {
window.Reflection = Reflection;
Events.on('server-switch', e => {
try {
this.appMount.setAttribute('guild-id', TempApi.currentGuildId);
this.appMount.setAttribute('channel-id', TempApi.currentChannelId);
this.setIds();
this.makeMutable();
} catch (err) {
console.log(err);
}
});
Events.on('channel-switch', e => {
try {
this.appMount.setAttribute('guild-id', TempApi.currentGuildId);
this.appMount.setAttribute('channel-id', TempApi.currentChannelId);
this.setIds();
this.makeMutable();
} catch (err) {
console.log(err);
}
});
Events.on('discord:MESSAGE_CREATE', e => {
if (!e.element) return;
this.setId(e.element);
const markup = e.element.querySelector('.markup:not(.mutable)');
if (markup) this.injectMarkup(markup, this.cloneMarkup(markup), false);
});
Events.on('discord:MESSAGE_UPDATE', e => {
if (!e.element) return;
this.setId(e.element);
const markup = e.element.querySelector('.markup:not(.mutable)');
if (markup) this.injectMarkup(markup, this.cloneMarkup(markup), false);
});
super();
const filter = function (mutation) {
return mutation.removedNodes && mutation.removedNodes.length && mutation.removedNodes[0].className && mutation.removedNodes[0].className.includes('loading-more');
}
@ -86,6 +53,38 @@ export default class {
});
}
bindings() {
this.manipAll = this.manipAll.bind(this);
this.markupInjector = this.markupInjector.bind(this);
}
get eventBindings() {
return [
{ id: 'server-switch', callback: this.manipAll },
{ id: 'channel-switch', callback: this.manipAll },
{ id: 'discord:MESSAGE_CREATE', callback: this.markupInjector },
{ id: 'discord:MESSAGE_UPDATE', callback: this.markupInjector }
];
}
manipAll() {
try {
this.appMount.setAttribute('guild-id', TempApi.currentGuildId);
this.appMount.setAttribute('channel-id', TempApi.currentChannelId);
this.setIds();
this.makeMutable();
} catch (err) {
console.log(err);
}
}
markupInjector(e) {
if (!e.element) return;
this.setId(e.element);
const markup = e.element.querySelector('.markup:not(.mutable)');
if (markup) this.injectMarkup(markup, this.cloneMarkup(markup), false);
}
getEts(node) {
try {
const reh = Object.keys(node).find(k => k.startsWith('__reactInternalInstance'));

View File

@ -17,12 +17,16 @@ export default class extends EventListener {
bindings() {
this.uiEvent = this.uiEvent.bind(this);
this.messageBadge = this.messageBadge.bind(this);
this.messageBadges = this.messageBadges.bind(this);
}
get eventBindings() {
return [
{ id: 'discord:MESSAGE_CREATE', callback: this.messageBadges },
{ id: 'discord:MESSAGE_CREATE', callback: this.messageBadge },
{ id: 'discord:MESSAGE_UPDATE', callback: this.messageBadge },
{ id: 'server-switch', callback: this.messageBadges },
{ id: 'channel-switch', callback: this.messageBadges },
{ id: 'ui-event', callback: this.uiEvent }
];
}
@ -36,7 +40,13 @@ export default class extends EventListener {
this.inject(userid);
}
messageBadges(e) {
messageBadges() {
for (const messageGroup of document.querySelectorAll('.message-group')) {
this.messageBadge({ element: messageGroup });
}
}
messageBadge(e) {
if (!e.element) return;
const msgGroup = e.element.closest('.message-group');
if (msgGroup.dataset.hasBadges) return;