Merge pull request #62 from JsSucks/events

Merge
This commit is contained in:
Alexei Stukov 2018-02-01 05:06:51 +02:00 committed by GitHub
commit 1dae0a40b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 77 additions and 17 deletions

View File

@ -8,15 +8,14 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import { DOM, BdUI, ProfileBadges } from 'ui'; import { DOM, BdUI } from 'ui';
import BdCss from './styles/index.scss'; import BdCss from './styles/index.scss';
import { Events, CssEditor, Globals, PluginManager, ThemeManager } from 'modules'; import { Events, CssEditor, Globals, PluginManager, ThemeManager, ModuleManager } from 'modules';
import { ClientLogger as Logger } from 'common'; import { ClientLogger as Logger } from 'common';
class BetterDiscord { class BetterDiscord {
constructor() { constructor() {
ProfileBadges.init(); // Not final way to do it
DOM.injectStyle(BdCss, 'bdmain'); DOM.injectStyle(BdCss, 'bdmain');
Events.on('global-ready', this.globalReady.bind(this)); Events.on('global-ready', this.globalReady.bind(this));
} }
@ -24,6 +23,7 @@ class BetterDiscord {
async init() { async init() {
await PluginManager.loadAllPlugins(); await PluginManager.loadAllPlugins();
await ThemeManager.loadAllThemes(); await ThemeManager.loadAllThemes();
ModuleManager.initModules();
Events.emit('ready'); Events.emit('ready');
} }

View File

@ -0,0 +1,22 @@
/**
* BetterDiscord Event Listener Module
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import Module from './module';
import Events from './events';
export default class extends Module {
events() {
for (let event of this.eventBindings) {
Events.on(event.id, event.callback);
}
}
}

View File

@ -23,4 +23,5 @@ export default class {
static emit(...args) { static emit(...args) {
emitter.emit(...args); emitter.emit(...args);
} }
} }

View File

@ -20,10 +20,10 @@ export default class Module {
args args
} }
this.setState = this.setState.bind(this); this.setState = this.setState.bind(this);
this.init(); this.initialize();
} }
init() { initialize() {
if (this.bindings) this.bindings(); if (this.bindings) this.bindings();
if (this.setInitialState) this.setInitialState(this.state); if (this.setInitialState) this.setInitialState(this.state);
if (this.events) this.events(); if (this.events) this.events();

View File

@ -0,0 +1,34 @@
/**
* BetterDiscord Module Manager
* Copyright (c) 2015-present Jiiks/JsSucks - https://github.com/Jiiks / https://github.com/JsSucks
* All rights reserved.
* https://betterdiscord.net
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
/*Module Manager initializes all modules when everything is ready*/
import { Events } from 'modules';
import { ProfileBadges } from 'ui';
export default class {
static get modules() {
return this._modules ? this._modules : (this._modules = [
new ProfileBadges()
]);
}
static initModules() {
for (let module of this.modules) {
try {
if (module.init && module.init instanceof Function) module.init();
} catch (err) {
console.log(`Failed to initialize module: ${err}`);
}
}
}
}

View File

@ -6,3 +6,5 @@ export { default as ThemeManager } from './thememanager';
export { default as Globals } from './globals'; export { default as Globals } from './globals';
export { default as Vendor } from './vendor'; export { default as Vendor } from './vendor';
export { default as WebpackModules } from './webpackmodules'; export { default as WebpackModules } from './webpackmodules';
export { default as ModuleManager } from './modulemanager';
export { default as EventListener } from './eventlistener';

View File

@ -17,7 +17,6 @@ import { Utils } from 'common';
export default class { export default class {
static initUiEvents() { static initUiEvents() {
//this.profilePopupModule.open
const defer = setInterval(() => { const defer = setInterval(() => {
if (!this.profilePopupModule) return; if (!this.profilePopupModule) return;
clearInterval(defer); clearInterval(defer);

View File

@ -8,18 +8,24 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import { EventListener } from 'modules';
import DOM from './dom'; import DOM from './dom';
import { BdBadge } from './components/bd'; import { BdBadge } from './components/bd';
import VueInjector from './vueinjector'; import VueInjector from './vueinjector';
import { Events } from 'modules';
export default class { export default class extends EventListener {
static init() { bindings() {
Events.on('ui-event', this.uiEvent.bind(this)); this.uiEvent = this.uiEvent.bind(this);
} }
static uiEvent(e) { get eventBindings() {
return [
{ id: 'ui-event', callback: this.uiEvent }
];
}
uiEvent(e) {
const { event, data } = e; const { event, data } = e;
if (event !== 'profile-popup-open') return; if (event !== 'profile-popup-open') return;
const { userid } = data; const { userid } = data;
@ -28,7 +34,7 @@ export default class {
this.inject(userid); this.inject(userid);
} }
static inject(userid) { inject(userid) {
const c = this.contributors.find(c => c.id === userid); const c = this.contributors.find(c => c.id === userid);
if (!c) return; if (!c) return;
@ -52,11 +58,7 @@ export default class {
}, 400); }, 400);
} }
static filter(mutation) { get contributors() {
return mutation.target.firstChild && mutation.target.className.includes('modal');
}
static get contributors() {
return [ return [
{ 'id': '81388395867156480', 'webdev': true, 'developer': true, 'contributor': true }, // Jiiks { 'id': '81388395867156480', 'webdev': true, 'developer': true, 'contributor': true }, // Jiiks
{ 'id': '98003542823944192', 'webdev': false, 'developer': true, 'contributor': true }, // Pohky { 'id': '98003542823944192', 'webdev': false, 'developer': true, 'contributor': true }, // Pohky