sharkey/src/client/app/desktop/views/home/search.vue

131 lines
2.8 KiB
Vue
Raw Normal View History

2018-02-16 17:35:15 +09:00
<template>
<div class="oxgbmvii">
<div class="notes">
<header>
<span><fa icon="search"/> {{ q }}</span>
</header>
<p v-if="!fetching && notAvailable">{{ $t('not-available') }}</p>
<p v-if="!fetching && empty"><fa icon="search"/> {{ $t('not-found', { q }) }}</p>
<mk-notes ref="timeline" :more="existMore ? more : null"/>
</div>
</div>
2018-02-16 17:35:15 +09:00
</template>
<script lang="ts">
import Vue from 'vue';
import i18n from '../../../i18n';
2018-02-16 17:35:15 +09:00
import Progress from '../../../common/scripts/loading';
2018-02-26 00:39:05 +09:00
const limit = 20;
2018-02-16 17:35:15 +09:00
export default Vue.extend({
i18n: i18n('desktop/views/pages/search.vue'),
2018-02-16 17:35:15 +09:00
data() {
return {
fetching: true,
moreFetching: false,
2018-02-26 00:39:05 +09:00
existMore: false,
2018-02-16 17:35:15 +09:00
offset: 0,
empty: false,
notAvailable: false
2018-02-16 17:35:15 +09:00
};
},
2018-02-26 00:39:05 +09:00
watch: {
$route: 'fetch'
},
2018-02-16 17:35:15 +09:00
computed: {
2018-02-26 00:39:05 +09:00
q(): string {
return this.$route.query.q;
2018-02-16 17:35:15 +09:00
}
},
mounted() {
document.addEventListener('keydown', this.onDocumentKeydown);
2018-06-07 01:52:03 +09:00
window.addEventListener('scroll', this.onScroll, { passive: true });
2018-02-16 17:35:15 +09:00
2018-02-26 00:39:05 +09:00
this.fetch();
2018-02-16 17:35:15 +09:00
},
beforeDestroy() {
document.removeEventListener('keydown', this.onDocumentKeydown);
window.removeEventListener('scroll', this.onScroll);
},
methods: {
onDocumentKeydown(e) {
if (e.target.tagName != 'INPUT' && e.target.tagName != 'TEXTAREA') {
if (e.which == 84) { // t
(this.$refs.timeline as any).focus();
}
}
},
2018-02-26 00:39:05 +09:00
fetch() {
this.fetching = true;
Progress.start();
2018-07-04 21:23:50 +09:00
(this.$refs.timeline as any).init(() => new Promise((res, rej) => {
2018-11-09 08:13:34 +09:00
this.$root.api('notes/search', {
2018-07-04 21:23:50 +09:00
limit: limit + 1,
offset: this.offset,
query: this.q
}).then(notes => {
if (notes.length == 0) this.empty = true;
if (notes.length == limit + 1) {
notes.pop();
this.existMore = true;
}
res(notes);
this.fetching = false;
Progress.done();
}, (e: string) => {
this.fetching = false;
Progress.done();
if (e === 'searching not available') this.notAvailable = true;
});
2018-07-04 21:23:50 +09:00
}));
2018-02-26 00:39:05 +09:00
},
2018-02-16 17:35:15 +09:00
more() {
this.offset += limit;
2018-07-04 21:23:50 +09:00
2018-11-09 08:13:34 +09:00
const promise = this.$root.api('notes/search', {
2018-02-26 00:39:05 +09:00
limit: limit + 1,
2018-07-04 20:44:13 +09:00
offset: this.offset,
query: this.q
2018-07-04 21:23:50 +09:00
});
promise.then(notes => {
2018-04-08 02:30:37 +09:00
if (notes.length == limit + 1) {
notes.pop();
2018-02-26 00:39:05 +09:00
} else {
this.existMore = false;
}
for (const n of notes) {
(this.$refs.timeline as any).append(n);
}
2018-02-26 00:39:05 +09:00
this.moreFetching = false;
2018-02-16 17:35:15 +09:00
});
2018-07-04 21:23:50 +09:00
return promise;
2018-02-16 17:35:15 +09:00
}
}
});
</script>
<style lang="stylus" scoped>
.oxgbmvii
> .notes
background var(--face)
box-shadow var(--shadow)
border-radius var(--round)
overflow hidden
2018-02-16 17:35:15 +09:00
> header
padding 0 8px
z-index 10
background var(--faceHeader)
box-shadow 0 var(--lineWidth) var(--desktopTimelineHeaderShadow)
2018-02-16 17:35:15 +09:00
> span
padding 0 8px
font-size 0.9em
line-height 42px
color var(--text)
2018-02-16 17:35:15 +09:00
</style>