-
Notifications
You must be signed in to change notification settings - Fork 970
/
citations.js
265 lines (245 loc) · 9.33 KB
/
citations.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import * as bibtexParse from "libs/bibtexParse";
import baseMVC from "mvc/base-mvc";
import _l from "utils/localization";
import { convertLaTeX } from "latex-to-unicode-converter";
import { stringifyLaTeX } from "latex-parser";
/* global Backbone */
var logNamespace = "citation";
//==============================================================================
/** @class model for tool citations.
* @name Citation
* @augments Backbone.Model
*/
var Citation = Backbone.Model.extend(baseMVC.LoggableMixin).extend({
_logNamespace: logNamespace,
defaults: {
content: ""
},
initialize: function() {
var parsed;
try {
// TODO: to model.parse/.validate
parsed = bibtexParse.toJSON(this.attributes.content);
} catch (err) {
this.log("Error parsing bibtex: " + err);
}
this._fields = {};
this.entry = _.first(parsed);
if (this.entry) {
var rawFields = this.entry.entryTags;
for (var key in rawFields) {
var value = rawFields[key];
var lowerKey = key.toLowerCase();
this._fields[lowerKey] = value;
}
}
},
entryType: function() {
return this.entry ? this.entry.entryType : undefined;
},
fields: function() {
return this._fields;
}
});
//==============================================================================
/** @class Backbone collection of citations.
*/
var BaseCitationCollection = Backbone.Collection.extend(baseMVC.LoggableMixin).extend({
_logNamespace: logNamespace,
/** root api url */
urlRoot: `${Galaxy.root}api`,
partial: true, // Assume some tools in history/workflow may not be properly annotated yet.
model: Citation
});
var HistoryCitationCollection = BaseCitationCollection.extend({
/** complete api url */
url: function() {
return `${this.urlRoot}/histories/${this.history_id}/citations`;
}
});
var ToolCitationCollection = BaseCitationCollection.extend({
/** complete api url */
url: function() {
return `${this.urlRoot}/tools/${this.tool_id}/citations`;
},
partial: false // If a tool has citations, assume they are complete.
});
var CitationView = Backbone.View.extend({
tagName: "div",
className: "citations",
render: function() {
this.$el.append(`<p>${this.formattedReference()}</p>`);
return this;
},
formattedReference: function() {
var model = this.model;
var entryType = model.entryType();
var fields = model.fields();
var ref = "";
// Code inspired by...
// https://github.com/vkaravir/bib-publication-list/blob/master/src/bib-publication-list.js
var authorsAndYear = `${this._asSentence(
(fields.author ? fields.author : "") + (fields.year ? ` (${fields.year})` : "")
)} `;
var title = fields.title || "";
var pages = fields.pages ? `pp. ${fields.pages}` : "";
var address = fields.address;
if (entryType == "article") {
var volume =
(fields.volume ? fields.volume : "") +
(fields.number ? ` (${fields.number})` : "") +
(pages ? `, ${pages}` : "");
ref = `${authorsAndYear +
this._asSentence(title) +
(fields.journal ? `In <em>${fields.journal}, ` : "") +
this._asSentence(volume) +
this._asSentence(fields.address)}</em>`;
} else if (entryType == "inproceedings" || entryType == "proceedings") {
ref = `${authorsAndYear +
this._asSentence(title) +
(fields.booktitle ? `In <em>${fields.booktitle}, ` : "") +
(pages ? pages : "") +
(address ? `, ${address}` : "")}.</em>`;
} else if (entryType == "mastersthesis" || entryType == "phdthesis") {
ref =
authorsAndYear +
this._asSentence(title) +
(fields.howpublished ? `${fields.howpublished}. ` : "") +
(fields.note ? `${fields.note}.` : "");
} else if (entryType == "techreport") {
ref =
authorsAndYear +
this._asSentence(title) +
this._asSentence(fields.institution) +
this._asSentence(fields.number) +
this._asSentence(fields.type);
} else if (entryType == "book" || entryType == "inbook" || entryType == "incollection") {
ref = `${authorsAndYear} ${this._formatBookInfo(fields)}`;
} else {
ref = `${authorsAndYear} ${this._asSentence(title)}${this._asSentence(
fields.howpublished
)}${this._asSentence(fields.note)}`;
}
var doiUrl = "";
if (fields.doi) {
doiUrl = `http://dx.doi.org/${fields.doi}`;
ref += `[<a href="${doiUrl}" target="_blank">doi:${fields.doi}</a>]`;
}
var url = fields.url || doiUrl;
if (url) {
ref += `[<a href="${url}" target="_blank">Link</a>]`;
}
return convertLaTeX({ onError: (error, latex) => `\{${stringifyLaTeX(latex)}\}` }, ref);
},
_formatBookInfo: function(fields) {
var info = "";
if (fields.chapter) {
info += `${fields.chapter} in `;
}
if (fields.title) {
info += `<em>${fields.title}</em>`;
}
if (fields.editor) {
info += `, Edited by ${fields.editor}, `;
}
if (fields.publisher) {
info += `, ${fields.publisher}`;
}
if (fields.pages) {
info += `, pp. ${fields.pages}`;
}
if (fields.series) {
info += `, <em>${fields.series}</em>`;
}
if (fields.volume) {
info += `, Vol.${fields.volume}`;
}
if (fields.issn) {
info += `, ISBN: ${fields.issn}`;
}
return `${info}.`;
},
_asSentence: function(str) {
return str && str.trim() ? `${str}. ` : "";
}
});
var CitationListView = Backbone.View.extend({
/**
* Set up view.
*/
initialize: function() {
this.setElement("<div/>");
this.listenTo(this.collection, "add", this.renderCitation);
this.render();
},
events: {
"click .citations-to-bibtex": "showBibtex",
"click .citations-to-formatted": "showFormatted"
},
renderCitation: function(citation) {
var citationView = new CitationView({ model: citation });
this.$(".citations-formatted").append(citationView.render().el);
var rawTextarea = this.$(".citations-bibtex-text");
rawTextarea.val(`${rawTextarea.val()}\n\r${citation.attributes.content}`);
},
render: function() {
this.$el.html(this.citationsElement());
this.collection.each(function(item) {
this.renderCitation(item);
}, this);
this.showFormatted();
},
showBibtex: function() {
this.$(".citations-to-formatted").show();
this.$(".citations-to-bibtex").hide();
this.$(".citations-bibtex").show();
this.$(".citations-formatted").hide();
this.$(".citations-bibtex-text").select();
},
showFormatted: function() {
this.$(".citations-to-formatted").hide();
this.$(".citations-to-bibtex").show();
this.$(".citations-bibtex").hide();
this.$(".citations-formatted").show();
},
partialWarningElement: function() {
if (this.collection.partial) {
return [
'<div style="padding:5px 10px">',
"<b>Warning: This is a experimental feature.</b> Most Galaxy tools will not annotate",
" citations explicitly at this time. When writing up your analysis, please manually",
" review your histories and find all references",
" that should be cited in order to completely describe your work. Also, please remember to",
' <a href="https://galaxyproject.org/citing-galaxy">cite Galaxy</a>.',
"</div>"
].join("");
} else {
return "";
}
},
citationsElement: function() {
return [
'<div class="toolForm">',
'<div class="toolFormTitle">',
_l("Citations"),
' <button type="button" class="btn btn-xs citations-to-bibtex" title="Show all in BibTeX format."><i class="fa fa-pencil-square-o"></i> Show BibTeX</button>',
' <button type="button" class="btn btn-xs citations-to-formatted" title="Return to formatted citation list."><i class="fa fa-times"></i> Hide BibTeX</button>',
"</div>",
'<div class="toolFormBody" style="padding:5px 10px">',
this.partialWarningElement(),
'<span class="citations-formatted" style="word-wrap: break-word;"></span>',
"</div>",
'<div class="citations-bibtex toolFormBody" style="padding:5px 10px">',
'<textarea style="width: 100%; height: 500px;" class="citations-bibtex-text"></textarea>',
"</div>",
"</div>"
].join("");
}
});
export default {
Citation: Citation,
HistoryCitationCollection: HistoryCitationCollection,
ToolCitationCollection: ToolCitationCollection,
CitationView: CitationView,
CitationListView: CitationListView
};