BetterDiscordApp-v2/client/src/ui/components/BdSettingsWrapper.vue

108 lines
4.6 KiB
Vue
Raw Normal View History

/**
* BetterDiscord Settings Wrapper Component
* 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.
*/
2018-01-29 18:56:48 +01:00
<template>
2018-08-15 11:42:43 +02:00
<div class="bd-settingsWrapper" :class="[{'bd-active': active}, 'platform-' + this.platform]">
2018-08-15 05:33:42 +02:00
<div class="bd-settingsButton" :class="{'bd-active': active, 'bd-animating': animating, 'bd-hideButton': hideButton}" @click="active = true">
2018-08-15 04:03:56 +02:00
<div v-if="updating === 0" v-tooltip.right="'Checking for updates'" class="bd-settingsButtonBtn bd-loading"></div>
<div v-else-if="updating === 2" v-tooltip.right="'Updates available!'" class="bd-settingsButtonBtn bd-updates"></div>
<div v-else class="bd-settingsButtonBtn" :class="[{'bd-loading': !loaded}]"></div>
</div>
2018-03-30 02:17:02 +02:00
<BdSettings ref="settings" :active="active" @close="active = false" />
</div>
2018-01-29 18:56:48 +01:00
</template>
2018-01-29 18:56:48 +01:00
<script>
// Imports
2018-03-08 03:42:06 +01:00
import { Events, Settings } from 'modules';
import { Modals } from 'ui';
import process from 'process';
2018-01-30 10:28:28 +01:00
import BdSettings from './BdSettings.vue';
export default {
data() {
return {
loaded: false,
2018-02-14 21:02:12 +01:00
updating: false,
2018-01-30 10:28:28 +01:00
active: false,
animating: false,
timeout: null,
platform: process.platform,
eventHandlers: {},
keybindHandler: null,
hideButton: false,
hideButtonToggleHandler: null
2018-03-30 02:17:02 +02:00
};
},
components: {
BdSettings
},
methods: {
keyupListener(e) {
2018-03-08 03:42:06 +01:00
if (Modals.stack.length || !this.active || e.which !== 27) return;
2018-08-10 16:21:33 +02:00
if (this.$refs.settings.item) this.$refs.settings.closeContent();
2018-03-30 02:17:02 +02:00
else this.active = false;
e.stopImmediatePropagation();
},
prevent(e) {
if (this.active && e.which === 27)
e.stopImmediatePropagation();
}
},
watch: {
active(active) {
2018-03-30 02:17:02 +02:00
if (active && !this.loaded)
return this.active = false;
this.animating = true;
if (this.timeout) clearTimeout(this.timeout);
this.timeout = setTimeout(() => {
this.animating = false;
this.timeout = null;
}, 400);
}
},
created() {
Events.on('ready', this.eventHandlers.ready = e => this.loaded = true);
2018-08-15 05:33:42 +02:00
Events.on('bd-open-menu', this.eventHandlers['bd-open-menu'] = item => this.active = true);
Events.on('bd-close-menu', this.eventHandlers['bd-close-menu'] = () => this.active = false);
Events.on('update-check-start', this.eventHandlers['update-check-start'] = e => this.updating = 0);
Events.on('update-check-end', this.eventHandlers['update-check-end'] = e => this.updating = 1);
Events.on('updates-available', this.eventHandlers['updates-available'] = e => this.updating = 2);
2018-01-30 10:28:28 +01:00
window.addEventListener('keyup', this.keyupListener);
window.addEventListener('keydown', this.prevent, true);
2018-03-08 03:42:06 +01:00
const menuKeybind = Settings.getSetting('core', 'default', 'menu-keybind');
menuKeybind.on('keybind-activated', this.keybindHandler = () => this.active = !this.active);
const hideButtonSetting = Settings.getSetting('ui', 'default', 'hide-button');
hideButtonSetting.on('setting-updated', this.hideButtonToggleHandler = event => this.hideButton = event.value);
this.hideButton = hideButtonSetting.value;
},
destroyed() {
for (let event in this.eventHandlers) Events.off(event, this.eventHandlers[event]);
window.removeEventListener('keyup', this.keyupListener);
window.removeEventListener('keydown', this.prevent);
if (this.keybindHandler) {
const menuKeybind = Settings.getSetting('core', 'default', 'menu-keybind');
menuKeybind.removeListener('keybind-activated', this.keybindHandler);
}
if (this.hideButtonToggleHandler) {
const hideButtonSetting = Settings.getSetting('ui', 'default', 'hide-button');
hideButtonSetting.removeListener('setting-updated', this.hideButtonToggleHandler);
}
}
}
</script>