feat: Add hiding to recommendations as a prop

fix: change some of the regexes to remove , and ; as separator support because the query parser doesn't understad them and I don't feel like dealing with all the edge cases introduces by it
This commit is contained in:
Zephyrrus 2020-07-23 04:08:31 +03:00
parent 279234a081
commit 39e9941ded
4 changed files with 50 additions and 22 deletions

View File

@ -12,7 +12,8 @@
placeholder="Search"
type="search"
open-on-focus
@typing="handleTyping">
@typing="handleTyping"
@keydown.native.enter="onSubmit">
<template slot-scope="props">
<b>{{ props.option.name }}:</b>
<small>
@ -37,6 +38,12 @@ export default {
components: {
SearchInput,
},
props: {
hiddenHints: {
type: Array,
default: () => [],
},
},
data() {
return {
query: '',
@ -66,11 +73,17 @@ export default {
'valueFormat': 'specific date',
'hint': '',
},
{
'name': 'file',
'valueFormat': 'generated name',
'hint': '',
},
],
filteredHints: [],
};
},
created() {
this.hints = this.hints.filter(({ name }) => this.hiddenHints.indexOf(name) === -1);
this.filteredHints = this.hints; // fixes the issue where on pageload, suggestions wont load
},
methods: {
@ -83,15 +96,19 @@ export default {
handleTyping(qry) {
qry = qry || '';
// get the last word or group of words
const lastWord = (qry.match(/("[^"]*")|[^;, ]+/g) || ['']).pop().toLowerCase();
let lastWord = (qry.match(/("[^"]*")|[^\s]+/g) || ['']).pop().toLowerCase();
// if there's an open/unbalanced quote, don't autosuggest
if (/^[^"]*("[^"]*"[^"]*)*(")[^"]*$/.test(qry)) { this.filteredHints = []; return; }
// don't autosuggest if we have an open query but no text yet
if (/:[\s|;|,]?$/gi.test(qry)) { this.filteredHints = []; return; }
if (/:\s+$/gi.test(qry)) { this.filteredHints = []; return; }
// if the above query didn't match (all quotes are balanced
// and the previous tag has value
// check if we're about to start a new tag
if (/[\s|;|,]+$/gi.test(qry)) { this.filteredHints = this.hints; return; }
if (/\s+$/gi.test(qry)) { this.filteredHints = this.hints; return; }
// ignore starting `-` from lastword, because - is used to
// exclude something, so -alb should autosuggest album
lastWord = lastWord.replace(/^-/, '');
// if we got here, then we handled all special cases
// now take last word, and check if we can autosuggest a tag
@ -100,11 +117,11 @@ export default {
.toLowerCase()
.indexOf(lastWord) === 0);
},
sanitizeQuery(qry) {
// \w+:\s+? to transform 'tag: 123' into 'tag:123'
},
onSubmit() {
this.$emit('search', this.query);
onSubmit(event) {
if (event.key === 'Enter') {
if (/:$/gi.test(this.query)) { return; }
}
this.$emit('search', this.query, event);
},
},
};

View File

@ -25,17 +25,7 @@
</div>
<div class="level-right">
<div class="level-item">
<b-field>
<b-input
class="lolisafe-input"
placeholder="Search"
type="search" />
<p class="control">
<b-button type="is-lolisafe">
Search
</b-button>
</p>
</b-field>
<Search :hidden-hints="['album']" />
</div>
</div>
</nav>
@ -43,7 +33,8 @@
<hr>
<Grid
v-if="totalFiles"
v-if="
totalFiles"
:files="images.files"
:total="totalFiles">
<template v-slot:pagination>
@ -64,8 +55,12 @@
aria-current-label="Current page" />
</template>
</Grid>
</search>
</div>
</div>
</nav>
</div>
</div>
</div>
</section>
</template>
@ -75,11 +70,13 @@ import { mapState, mapGetters, mapActions } from 'vuex';
import Sidebar from '~/components/sidebar/Sidebar.vue';
import Grid from '~/components/grid/Grid.vue';
import Search from '~/components/search/Search.vue';
export default {
components: {
Sidebar,
Grid,
Search,
},
middleware: ['auth', ({ route, store }) => {
store.commit('images/resetState');

View File

@ -103,8 +103,16 @@ export default {
await this.fetch(this.current);
this.isLoading = false;
},
sanitizeQuery(qry) {
// remove spaces between a search type selector `album:`
// and the value (ex `tag: 123` -> `tag:123`)
return (qry || '').replace(/(\w+):\s+/gi, '$1:');
},
onSearch(query) {
this.searc = query;
this.search = query;
this.$handler.executeAction('images/search', {
q: this.sanitizeQuery(query),
});
},
},
};

View File

@ -106,6 +106,12 @@ export const actions = {
commit('removeTagFromFile', response.data);
return response;
},
async search({ commit }, { q, albumId }) {
const optionalAlbum = albumId ? `&albumId=${albumId}` : '';
const response = await this.$axios.$get(`search/?q=${encodeURI(q)}${optionalAlbum}`);
return response;
},
};