Adds web search to Minimal View

This commit is contained in:
Alicia Sykes 2021-08-28 16:19:51 +01:00
parent 411357c5b6
commit 438693c8db
1 changed files with 81 additions and 10 deletions

View File

@ -1,13 +1,19 @@
<template> <template>
<form> <form @submit.prevent="searchSubmitted">
<input <div class="minimal-search-wrap">
id="filter-tiles" <input
v-model="input" id="filter-tiles"
ref="filter" v-model="input"
class="minimal-search" ref="filter"
:placeholder="$t('search.search-placeholder')" class="minimal-search"
v-on:input="userIsTypingSomething" :placeholder="$t('search.search-placeholder')"
@keydown.esc="clearFilterInput" /> v-on:input="userIsTypingSomething"
@keydown.esc="clearFilterInput"
/>
<p v-if="webSearchEnabled && input.length > 0" class="web-search-note">
{{ $t('search.enter-to-search-web') }}
</p>
</div>
<i v-if="input.length > 0" <i v-if="input.length > 0"
class="clear-search" class="clear-search"
:title="$t('search.clear-search-tooltip')" :title="$t('search.clear-search-tooltip')"
@ -17,11 +23,19 @@
<script> <script>
import router from '@/router';
import ArrowKeyNavigation from '@/utils/ArrowKeyNavigation'; import ArrowKeyNavigation from '@/utils/ArrowKeyNavigation';
import ErrorHandler from '@/utils/ErrorHandler';
import { getCustomKeyShortcuts } from '@/utils/ConfigHelpers'; import { getCustomKeyShortcuts } from '@/utils/ConfigHelpers';
import {
searchEngineUrls,
defaultSearchEngine,
defaultSearchOpeningMethod,
} from '@/utils/defaults';
export default { export default {
name: 'MinimalSearch', name: 'MinimalSearch',
inject: ['config'],
props: { props: {
active: Boolean, active: Boolean,
}, },
@ -32,6 +46,15 @@ export default {
getCustomKeyShortcuts, getCustomKeyShortcuts,
}; };
}, },
computed: {
webSearchEnabled() {
const { appConfig } = this.config;
if (appConfig && appConfig.webSearch) {
return !appConfig.webSearch.disableWebSearch;
}
return true;
},
},
methods: { methods: {
/* Emmits users's search term up to parent */ /* Emmits users's search term up to parent */
userIsTypingSomething() { userIsTypingSomething() {
@ -44,6 +67,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
}, },
/* Launches a given app when hotkey pressed */
handleHotKey(key) { handleHotKey(key) {
const usersHotKeys = this.getCustomKeyShortcuts(); const usersHotKeys = this.getCustomKeyShortcuts();
usersHotKeys.forEach((hotkey) => { usersHotKeys.forEach((hotkey) => {
@ -52,6 +76,7 @@ export default {
} }
}); });
}, },
/* Filter results as user types */
startFiltering(event) { startFiltering(event) {
const currentElem = document.activeElement.id; const currentElem = document.activeElement.id;
const { key, keyCode } = event; const { key, keyCode } = event;
@ -72,6 +97,42 @@ export default {
this.clearFilterInput(); this.clearFilterInput();
} }
}, },
/* Open web search results in users desired method */
launchWebSearch(url, method) {
switch (method) {
case 'newtab':
window.open(url, '_blank');
break;
case 'sametab':
window.open(url, '_self');
break;
case 'workspace':
router.push({ name: 'workspace', query: { url } });
break;
default:
ErrorHandler(`Unknown opening method: ${method}`);
window.open(url, '_blank');
}
},
/* If web search enabled, then launch search results when enter is pressed */
searchSubmitted() {
// Get search preferences from appConfig
const { appConfig } = this.config;
const searchPrefs = appConfig.webSearch || {};
if (this.webSearchEnabled) { // Only proceed if user hasn't disabled web search
const openingMethod = searchPrefs.openingMethod || defaultSearchOpeningMethod;
// Get search engine, and make URL
const searchEngine = searchPrefs.searchEngine || defaultSearchEngine;
let searchUrl = searchEngineUrls[searchEngine];
if (!searchUrl) ErrorHandler(`Search engine not found - ${searchEngine}`);
if (searchEngine === 'custom' && searchPrefs.customSearchEngine) {
searchUrl = searchPrefs.customSearchEngine;
}
// Append users encoded query onto search URL, and launch
searchUrl += encodeURIComponent(this.input);
this.launchWebSearch(searchUrl, openingMethod);
}
},
}, },
mounted() { mounted() {
window.addEventListener('keydown', this.startFiltering); window.addEventListener('keydown', this.startFiltering);
@ -89,6 +150,17 @@ export default {
form { form {
display: flex; display: flex;
align-items: center; align-items: center;
.minimal-search-wrap {
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
p.web-search-note {
margin: 0;
color: var(--minimal-view-search-color);
opacity: var(--dimming-factor);
}
}
input { input {
display: inline-block; display: inline-block;
width: 80%; width: 80%;
@ -107,7 +179,6 @@ export default {
} }
} }
.clear-search { .clear-search {
//position: absolute;
color: var(--minimal-view-search-color); color: var(--minimal-view-search-color);
padding: 0.15rem 0.5rem 0.2rem 0.5rem; padding: 0.15rem 0.5rem 0.2rem 0.5rem;
font-style: normal; font-style: normal;