- Base.vue: the browser tab shows the phrase searched (vue-meta), « Base » without a search. - terms of an old or edited URL parsed without throwing: malformed JSON is ignored. - Production build. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01ShDspHaZGwHoRZCsPBJzsM
153 lines
4.6 KiB
Vue
153 lines
4.6 KiB
Vue
<template>
|
|
<div id="Base">
|
|
<div class="loading" v-if="!searchinfos">
|
|
<span>{{ $t('default.Loading…') }}</span>
|
|
</div>
|
|
<div class="cards-list" v-else>
|
|
<!-- aria-live: screen readers announce the result message of each search (F1). -->
|
|
<aside class="search-info" aria-live="polite">
|
|
{{ searchinfos }}
|
|
</aside>
|
|
<div class="loading" v-if="!items.length & !noresults">
|
|
<span>{{ $t('default.Loading…') }}</span>
|
|
</div>
|
|
<ul v-else>
|
|
<li v-for="item in displayedItems" v-bind:key="item.id">
|
|
<Card v-if="item.bundle == 'materiau'" :item="item"/>
|
|
<CardThematique v-if="item.bundle == 'thematique'" :item="item"/>
|
|
</li>
|
|
</ul>
|
|
<infinite-loading
|
|
v-if="count > limit && displayedItems.length >= limit"
|
|
:identifier="searchId"
|
|
@infinite="nextPage"
|
|
>
|
|
<div slot="no-more">No more results</div>
|
|
</infinite-loading>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
|
|
<script>
|
|
|
|
import Card from 'vuejs/components/Content/Card'
|
|
import CardThematique from 'vuejs/components/Content/CardThematique'
|
|
|
|
import { mapState, mapActions } from 'vuex'
|
|
|
|
export default {
|
|
name: "Base",
|
|
data() {
|
|
return {
|
|
pagetitle:"Base",
|
|
// searchinfos: null
|
|
}
|
|
},
|
|
computed: {
|
|
...mapState({
|
|
items: state => state.Search.items,
|
|
searchinfos: state => state.Search.infos,
|
|
count: state => state.Search.count,
|
|
noresults: state => state.Search.noresults,
|
|
limit: state => state.Search.limit,
|
|
searchId: state => state.Search.searchId,
|
|
keys: state => state.Search.keys
|
|
}),
|
|
// items is a sparse array (pages placed by offset); drop the holes left by
|
|
// pages that returned fewer results than the limit before rendering.
|
|
displayedItems () {
|
|
return this.items.filter(Boolean)
|
|
}
|
|
},
|
|
// Title of the browser tab: the phrase searched (F3).
|
|
metaInfo () {
|
|
return {
|
|
title: this.keys && this.keys.length ? this.keys.join(', ') : 'Base'
|
|
}
|
|
},
|
|
methods: {
|
|
// terms of an URL: a JSON list, possibly malformed in an old or edited
|
|
// link; never break the page for it (F3).
|
|
parseTerms (value) {
|
|
try {
|
|
const terms = JSON.parse(value)
|
|
return Array.isArray(terms) ? terms : []
|
|
} catch (e) {
|
|
return []
|
|
}
|
|
},
|
|
...mapActions({
|
|
newSearch: 'Search/newSearch',
|
|
nextPage: 'Search/nextPage'
|
|
}),
|
|
// infiniteHandler($state){
|
|
// console.log('inifiniteHandler', $state)
|
|
// this.nextPage()
|
|
// }
|
|
},
|
|
created() {
|
|
// at first page load or first route entering launch a search if params exists in url query
|
|
console.log('Base created() location',window.location)
|
|
let params = new URLSearchParams(window.location.search)
|
|
if (params.has('keys')) {
|
|
const r = /,\s?$/
|
|
let keys = params.get('keys').replace(r,'').split(', ')
|
|
console.log('Base created, keys', keys)
|
|
this.$store.commit('Search/setKeys', keys)
|
|
this.pagetitle = keys.join(', ') //params.get('keys')
|
|
} else {
|
|
this.$store.commit('Search/reSetKeys')
|
|
this.pagetitle = 'Base'
|
|
}
|
|
|
|
if (params.has('terms')) {
|
|
console.log('Base created, has terms', params.get('terms'))
|
|
// this.$store.commit('Search/setTerms', params.get('terms').split(','))
|
|
this.$store.commit('Search/setTerms', this.parseTerms(params.get('terms')))
|
|
} else {
|
|
this.$store.commit('Search/reSetTerms')
|
|
}
|
|
|
|
// The filters parameter of former URLs is ignored (F2).
|
|
|
|
this.newSearch()
|
|
},
|
|
beforeRouteUpdate (to, from, next) {
|
|
// when query change launch a new search
|
|
console.log('Base beforeRouteUpdate', to, from, next)
|
|
// todo text field of search form is not emptying on clicking on base after a search
|
|
// this.$store.commit('Search/setKeys', to.query.keys)
|
|
if (to.query.hasOwnProperty('terms')) {
|
|
// to.query.terms is a raw JSON string (see SearchForm.submit) and must be
|
|
// parsed like in created(), otherwise getResults() double-encodes it and
|
|
// the backend silently drops the terms (less relevant results).
|
|
this.$store.commit('Search/setTerms', this.parseTerms(to.query.terms))
|
|
}else{
|
|
this.$store.commit('Search/reSetTerms')
|
|
}
|
|
if (to.query.hasOwnProperty('keys')) {
|
|
const r = /,\s?$/
|
|
let keys = to.query.keys.replace(r,'').split(', ')
|
|
console.log('Base created, keys', keys)
|
|
this.$store.commit('Search/setKeys', keys)
|
|
this.pagetitle = to.query.keys
|
|
}else{
|
|
this.$store.commit('Search/reSetKeys')
|
|
}
|
|
this.newSearch()
|
|
next()
|
|
},
|
|
components: {
|
|
Card,
|
|
CardThematique
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
</style>
|