-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
142 lines (142 loc) · 5.54 KB
/
main.js
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
// main.js
let app = new Vue({
el: '#app',
data: {
lab: config.lab,
idHal: config.idHal,
docTypes: ['ART', 'COUV', 'OUV', 'COMM', 'DOUV'],
docs: null,
selectedDocs: '',
selectedDocType: '',
pubDates: Array(),
nbDocs: {
all: 0,
art: 0,
couv: 0,
ouv: 0,
comm: 0,
douv: 0,
other: 0
},
isSearchInProgress: false
},
components: {
'alert': alertCmpnt,
'author': authorCmpnt,
'navigation': navCmpnt,
'references': refCmpnt
},
mounted: function() {
// Fetches the docs written by the author
this.fetchDocs();
},
methods: {
/*
* Retrieves the docs written by an author.
* If the parameter this.lab is not empty,
* the list is restricted to publications
* affiliated with the lab.
*/
fetchDocs: function() {
// HAL API: bibliographical references
let url = 'https://api.archives-ouvertes.fr/search/';
// Builds the query
let query = `${this.lab}/?q=authIdHal_s:${this.idHal}&fl=halId_s,docType_s,label_s,producedDate_tdate,fileMain_s&facet=true&facet.field=docType_s&rows=500&wt=json&sort=producedDate_tdate+desc`;
// Fetch API to query the HAL API
fetch(url + query)
.then(stream => stream.json())
.then(data => {
this.docs = data.response.docs; // All the docs
this.selectedDocs = this.docs; // Working copy of the docs
this.setPubDates(); // All the publication dates
this.nbDocsByType( // Number of docs for each doctype
data.facet_counts.facet_fields.docType_s,
data.response.numFound
);
})
;
},
/*
* Filters the docs by type
* @param {String} docType: the type of the documents
*/
filterDocs: function(docType = '') {
// Fixes the actual doctype
this.selectedDocType = (docType) ? docType : '';
// If param is empty, displays all the docs
if (!docType) {
this.selectedDocs = this.docs;
}
// If param is one of the docTypes
else if (this.docTypes.indexOf(docType) != -1) {
// Displays the corresponding documents
this.selectedDocs = this.docs.filter(function(doc) {
return doc.docType_s == docType;
});
}
// Otherwise, displays all the docs which typeDoc is not in this.docTypes
else {
this.selectedDocs = this.docs.filter(function(doc) {
return doc.docType_s != 'ART'
&& doc.docType_s != 'COMM'
&& doc.docType_s != 'COUV'
&& doc.docType_s != 'OUV'
&& doc.docType_s != 'DOUV';
});
}
},
/*
* Counts the number of docs for each type of doc
* @param {Array} docTypes: all the possible doctypes
* @param {Number} nbDocs: number of documents found
*/
nbDocsByType: function(docTypes, nbDocs) {
// For each doctype…
for (var i = this.docTypes.length - 1; i >= 0; i--) {
// … captures the index of the listed doctypes in the data
let idx = docTypes.indexOf(this.docTypes[i]);
// The next index is the proper count
this.nbDocs[this.docTypes[i].toLowerCase()] = docTypes[idx + 1];
}
// Counts the other categories of documents : all, other
this.nbDocs.all = nbDocs;
this.nbDocs.other = this.nbDocs.all - (this.nbDocs.art + this.nbDocs.couv + this.nbDocs.ouv + this.nbDocs.comm + this.nbDocs.douv);
},
/*
* Fills the data with the different publication dates
*/
setPubDates: function() {
// For each pubdate…
for (var i = this.docs.length - 1; i >= 0; i--) {
// … captures the year (first four numbers)
let pubdate = this.docs[i].producedDate_tdate.match(/^[0-9]{4}/)[0];
// Appends the result to the data
this.pubDates.push(pubdate);
}
},
/*
* Triggers a search in the selected documents
* @param {String} keyword: the keyword to search in the docs
*/
search: function(keyword) {
// A search is in progress?
this.isSearchInProgress = (keyword.length === 0) ? false : true;
// Limits the search to the documents filtered by type
this.filterDocs(this.selectedDocType);
// Keyword to lowercase
let search = keyword.toLowerCase();
// Dynamic selection of documents depending on the keyword
this.selectedDocs = this.selectedDocs.filter(
function(doc) {
// Field label to lowercase
let label = doc.label_s.toLowerCase();
// If the keyword is in the field label
if (label.indexOf(search) >= 0) {
// Returning the current doc
return doc;
}
}
);
}
}
})