ZoteroQueryComponent.vue 4.16 KB
Newer Older
Jerome Chauveau's avatar
Jerome Chauveau committed
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<template>
    <div class="zotero-query-component">
        <h2>Zotero Client <a role="button" @click="isConfigVisible=!isConfigVisible"></a></h2>
        <div class="zotero-config-pane" v-if="isConfigVisible">
            <div>
                <label>Type de collection</label>
                <select v-model="type">
                    <option value="group">Group</option>
                    <option value="user">User</option>
                </select>
            </div>

            <div>
                <label>Identifiant de "{{type}}"</label>
                <input v-model="groupOrUserID"/>
            </div>

            <div>
                <label>Résultats par page</label>
                <input type="number" v-model="pageLength"/>
            </div>

        </div>
        <div>
            <input v-model="textQuery"/>
            <button class="zotero-query-btn" @click="doQuery()" :disabled="!canQuery">
                <span>&#128269;</span>
            </button>
        </div>
        <progress v-if="isFetching"/>

        <template v-if="entries">
            <h3>Résultats <span class="results-count">{{nbResults}}</span></h3>
            <div class="zotero-pager">
                <a role="button" class="next-prev-btn" @click="previousPage()">&#5176;</a>
                <span>{{currentPage}}/{{getTotalPages}}</span>
                <a role="button" class="next-prev-btn" @click="nextPage()">&#5171;</a>
            </div>
            <div class="zotero-entries">
                <ul>
                    <li v-for="entry in entries" :key="'entry-' + entry.key">
                        {{entry.key}}
                    </li>
                </ul>
            </div>
        </template>

    </div>
</template>

<script>
    const Zotero = require('libzotero');

    export default {
        name: 'ZoteroQueryComponent',
        props: {},
        data() {
            return {
                type: 'group',
                groupOrUserID: '427575',
                entries: null,
                isFetching: false,
                isConfigVisible: true,
                start:0,
                currentPage:1,
                pageLength: 10,
                nbResults: 0,
                textQuery : ""

            }
        },
        computed: {
            getTotalPages : function(){
                return Math.ceil(this.nbResults / this.pageLength)
            },

            canQuery : function(){
                return this.textQuery && this.textQuery.trim() !== ''
            }
        },
        methods: {
            doQuery() {
                this.isFetching = true;
                this.entries = null;
                let library = new Zotero.Library(this.type, this.groupOrUserID);
                library.loadItems({start: this.start, limit: this.pageLength, q: this.textQuery}).then((response)=>{
                    this.entries = response.data;
                    this.nbResults = response.totalResults;
                    this.isFetching = false;
                })
            },

            previousPage(){
                this.start -= this.pageLength;
                this.currentPage --;
                this.doQuery();
            },

            nextPage(){
                this.start += this.pageLength;
                this.currentPage ++;
                this.doQuery();
            }
        }


    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
    * {
        --input-height: 34px;
    }

    input, button, select {
        height: var(--input-height);
    }

    h1 {}
    a[role=button], button{
        cursor: pointer;
    }

    label{
        display: inline-block;
        width: 200px;
    }

    .next-prev-btn{
        display: inline-block;
        margin-left: 15px;
        margin-right: 15px;
    }

    .zotero-entries ul{
        list-style-type: square;
    }

    .results-count{
        font-size: 0.8em;
    }
    .results-count:before{
        content: ' (';
    }
    .results-count:after{
        content: ') ';
    }

    .zotero-config-pane{
        background-color: gainsboro;
        padding:15px;
        margin-bottom: 15px;
    }

    .zotero-query-btn{
        margin-left: 15px;
    }


</style>