summaryrefslogtreecommitdiff
path: root/src/client/app/mobile/views/pages/search.vue
blob: 9850fbcfb42460d5d8a0e0e8a61488327226a4c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<template>
<mk-ui>
	<span slot="header">%fa:search% {{ q }}</span>
	<main v-if="!fetching">
		<mk-notes :class="$style.notes" :notes="notes">
			<span v-if="notes.length == 0">{{ '%i18n:@empty%'.replace('{}', q) }}</span>
			<button v-if="existMore" @click="more" :disabled="fetching" slot="tail">
				<span v-if="!fetching">%i18n:@load-more%</span>
				<span v-if="fetching">%i18n:common.loading%<mk-ellipsis/></span>
			</button>
		</mk-notes>
	</main>
</mk-ui>
</template>

<script lang="ts">
import Vue from 'vue';
import Progress from '../../../common/scripts/loading';
import parse from '../../../common/scripts/parse-search-query';

const limit = 20;

export default Vue.extend({
	data() {
		return {
			fetching: true,
			existMore: false,
			notes: [],
			offset: 0
		};
	},
	watch: {
		$route: 'fetch'
	},
	computed: {
		q(): string {
			return this.$route.query.q;
		}
	},
	mounted() {
		document.title = `%i18n:@search%: ${this.q} | Misskey`;

		this.fetch();
	},
	methods: {
		fetch() {
			this.fetching = true;
			Progress.start();

			(this as any).api('notes/search', Object.assign({
				limit: limit + 1
			}, parse(this.q))).then(notes => {
				if (notes.length == limit + 1) {
					notes.pop();
					this.existMore = true;
				}
				this.notes = notes;
				this.fetching = false;
				Progress.done();
			});
		},
		more() {
			this.offset += limit;
			(this as any).api('notes/search', Object.assign({
				limit: limit + 1,
				offset: this.offset
			}, parse(this.q))).then(notes => {
				if (notes.length == limit + 1) {
					notes.pop();
				} else {
					this.existMore = false;
				}
				this.notes = this.notes.concat(notes);
			});
		}
	}
});
</script>

<style lang="stylus" module>
.notes
	margin 8px auto
	max-width 500px
	width calc(100% - 16px)
	background #fff
	border-radius 8px
	box-shadow 0 0 0 1px rgba(#000, 0.2)

	@media (min-width 500px)
		margin 16px auto
		width calc(100% - 32px)
</style>