mirror of https://github.com/Lissy93/dashy.git
💚 Fix alert, remove event listener when component destroyed
This commit is contained in:
parent
688dece915
commit
0f651b281a
|
@ -52,28 +52,34 @@ export default {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mounted() {
|
mounted() {
|
||||||
window.addEventListener('keydown', (event) => {
|
window.addEventListener('keydown', this.handleKeyPress);
|
||||||
|
},
|
||||||
|
beforeDestroy() {
|
||||||
|
window.removeEventListener('keydown', this.handleKeyPress);
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
/* Call correct function dependending on which key is pressed */
|
||||||
|
handleKeyPress(event) {
|
||||||
const currentElem = document.activeElement.id;
|
const currentElem = document.activeElement.id;
|
||||||
const { key, keyCode } = event;
|
const { key, keyCode } = event;
|
||||||
/* If a modal is open, then do nothing */
|
const notAlreadySearching = currentElem !== 'filter-tiles';
|
||||||
|
// If a modal is open, then do nothing
|
||||||
if (!this.active) return;
|
if (!this.active) return;
|
||||||
if (/^[/:!a-zA-Z]$/.test(key) && currentElem !== 'filter-tiles') {
|
if (/^[/:!a-zA-Z]$/.test(key) && notAlreadySearching) {
|
||||||
/* Letter key pressed - start searching */
|
// Letter or bang key pressed - start searching
|
||||||
if (this.$refs.filter) this.$refs.filter.focus();
|
if (this.$refs.filter) this.$refs.filter.focus();
|
||||||
this.userIsTypingSomething();
|
this.userIsTypingSomething();
|
||||||
} else if (/^[0-9]$/.test(key)) {
|
} else if (/^[0-9]$/.test(key)) {
|
||||||
/* Number key pressed, check if user has a custom binding */
|
// Number key pressed, check if user has a custom binding
|
||||||
this.handleHotKey(key);
|
this.handleHotKey(key);
|
||||||
} else if (keyCode >= 37 && keyCode <= 40) {
|
} else if (keyCode >= 37 && keyCode <= 40) {
|
||||||
/* Arrow key pressed - start navigation */
|
// Arrow key pressed - start navigation
|
||||||
this.akn.arrowNavigation(keyCode);
|
this.akn.arrowNavigation(keyCode);
|
||||||
} else if (keyCode === 27) {
|
} else if (keyCode === 27) {
|
||||||
/* Esc key pressed - reset form */
|
// Esc key pressed - reset form
|
||||||
this.clearFilterInput();
|
this.clearFilterInput();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
},
|
},
|
||||||
methods: {
|
|
||||||
/* Emmits users's search term up to parent */
|
/* Emmits users's search term up to parent */
|
||||||
userIsTypingSomething() {
|
userIsTypingSomething() {
|
||||||
this.$emit('user-is-searchin', this.input);
|
this.$emit('user-is-searchin', this.input);
|
||||||
|
@ -85,6 +91,7 @@ export default {
|
||||||
document.activeElement.blur(); // Remove focus
|
document.activeElement.blur(); // Remove focus
|
||||||
this.akn.resetIndex(); // Reset current element index
|
this.akn.resetIndex(); // Reset current element index
|
||||||
},
|
},
|
||||||
|
/* If configured, launch specific app when hotkey pressed */
|
||||||
handleHotKey(key) {
|
handleHotKey(key) {
|
||||||
const usersHotKeys = this.getCustomKeyShortcuts();
|
const usersHotKeys = this.getCustomKeyShortcuts();
|
||||||
usersHotKeys.forEach((hotkey) => {
|
usersHotKeys.forEach((hotkey) => {
|
||||||
|
@ -93,6 +100,7 @@ export default {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
/* Launch search results, with users desired opening method */
|
||||||
launchWebSearch(url, method) {
|
launchWebSearch(url, method) {
|
||||||
switch (method) {
|
switch (method) {
|
||||||
case 'newtab':
|
case 'newtab':
|
||||||
|
@ -120,8 +128,8 @@ export default {
|
||||||
const searchBang = getSearchEngineFromBang(this.input, bangList);
|
const searchBang = getSearchEngineFromBang(this.input, bangList);
|
||||||
const searchEngine = searchPrefs.searchEngine || defaultSearchEngine;
|
const searchEngine = searchPrefs.searchEngine || defaultSearchEngine;
|
||||||
// Use either search bang, or preffered search engine
|
// Use either search bang, or preffered search engine
|
||||||
let searchUrl = searchBang || searchEngine;
|
const desiredSearchEngine = searchBang || searchEngine;
|
||||||
searchUrl = findUrlForSearchEngine((searchBang || searchEngine), searchEngineUrls);
|
let searchUrl = findUrlForSearchEngine(desiredSearchEngine, searchEngineUrls);
|
||||||
if (searchUrl) { // Append search query to URL, and launch
|
if (searchUrl) { // Append search query to URL, and launch
|
||||||
searchUrl += encodeURIComponent(stripBangs(this.input, bangList));
|
searchUrl += encodeURIComponent(stripBangs(this.input, bangList));
|
||||||
this.launchWebSearch(searchUrl, openingMethod);
|
this.launchWebSearch(searchUrl, openingMethod);
|
||||||
|
|
Loading…
Reference in New Issue