forked from astrofrog/acknowledgment-generator
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.js
283 lines (239 loc) · 9.22 KB
/
functions.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// I have not written Javascript before so any comments welcome :)
// Initialize the main function with the data we've loaded
function init(data){
var cite = new Citation(data);
}
// Main function to store the data so we don't have to keep requesting it
function Citation(data){
this.data = data;
this.show_checkboxes().show_acknowledgment();
return this;
}
// Display the checkboxes for the provided JSON data
Citation.prototype.show_checkboxes = function(){
// var checkedBoxes = get_checked_boxes("checkbox_options");
content = ""
this.checkboxes = [];
for (var i = 0; i < this.data.length; i++) {
category = this.data[i];
content += '<h3 class="expandable"><img class="triangle" src="right.png">' + category.title + '</h3>\n';
content += '<div class="options"><ul>';
for (var short_name in category.content) {
content += '<li><input type="checkbox" name="checkbox_' + category.short + '" id="' + category.short + '_' + short_name+ '" value="' + short_name + '">';
// Keep a record of the checkboxes we are adding
this.checkboxes.push({'id':category.short+'_'+short_name,'value':short_name,'name':category.content[short_name].name});
if(typeof(category.content[short_name].url) != 'undefined') {
content += '<a href="' + category.content[short_name].url + '">'
+ category.content[short_name].name + '\n'
+ '</a>\n';
} else {
content += category.content[short_name].name + '\n';
}
}
content += '</ul></div>';
}
// Add the HTML content to the page
document.getElementById("main_check").innerHTML = content;
// Add the change event to all the checkboxes
jQuery('#main_check li input[type=checkbox], #main_options li input[type=checkbox], #delim').on('change',{citation:this},function(e){
e.data.citation.box_checked();
});
jQuery(document).ready(function(){
$('.expandable').click(function() {
$(this).next().toggle(); // add 'slow' to toggle() to animate
title = $(this).html()
if(title.indexOf("right") > -1) {
$(this).html(title.replace("right", "down"));
} else {
$(this).html(title.replace("down", "right"));
}
return false;
}).next().hide();
});
$('#main_check').prepend('<h3><label for="filter">Search:</label> <input type="text" name="filter" id="filter" placeholder="Filter" /></h3>');
this.typeahead('filter');
return this;
}
Citation.prototype.box_checked = function(){
// // Resolve dependencies
// var checkedBoxes = get_checked_boxes("checkbox_" + category.short);
// for (var j = 0; j < checkedBoxes.length; j++) {
// resolve_dependents(checkedBoxes[i])
// }
//
// When a box gets checked, update the acknowledgment
this.show_acknowledgment();
}
Citation.prototype.show_acknowledgment = function() {
// Display the acknowledgment corresponding to the checked boxes
// Get this from latex checkbox
var latex_checkbox = document.getElementById("option_latex");
var facilities_checkbox = document.getElementById("option_facilities");
var main_text = "<h2>Suggested acknowledgment</h2>";
var bibtex_text = "<h2>BibTeX</h2>"
var facilities = [];
var textA = [];
var textB = [];
var madeuseof = "made use of";
var delim = (jQuery('#delim').length == 1) ? jQuery('#delim').val() : ";";
for (var i = 0; i < this.data.length; i++) {
category = this.data[i];
var checkedBoxes = get_checked_boxes("checkbox_" + category.short);
for (var j = 0; j < checkedBoxes.length; j++) {
if (latex_checkbox.checked) {
if (category.content[checkedBoxes[j].value].latex) {
text = category.content[checkedBoxes[j].value].latex
} else {
text = category.content[checkedBoxes[j].value].text
}
if (category.content[checkedBoxes[j].value].bibtex) {
bibtex_text += category.content[checkedBoxes[j].value].bibtex + "<br><br>"
}
} else {
text = category.content[checkedBoxes[j].value].text
}
if (text.indexOf(madeuseof) > 0){
tmp = text.substr(text.indexOf(madeuseof)+madeuseof.length);
if(tmp[tmp.length-1]==".") tmp = tmp.substr(0,tmp.length-1)
textA.push(tmp);
}else textB.push(text);
if (facilities_checkbox.checked) {
if (category.content[checkedBoxes[j].value].facilities) {
facilities.push(category.content[checkedBoxes[j].value].facilities);
}
}
}
}
facilities_text = "<h2>Facilities</h2>"
if(facilities.length > 0) {
facilities_text += "\\textit{Facilities:}" + facilities.join(", ")
}
// Build the first part of the acknowledgements which
// concatenates the entries containing the phrase:
// "made use of".
if(textA.length > 0){
main_text += "This research made use of";
for(var i = 0; i < textA.length; i++){
// If this is not the first item, add a delimiter
if(i > 0) main_text += delim;
// Add a space
if(main_text[main_text.length-1] != " ") main_text += " ";
// Add the shortened text
main_text += textA[i];
}
main_text += ". "
}
if(textB.length > 0){
for(var i = 0; i < textB.length; i++){
// Add a space
if(i > 0 && main_text[main_text.length-1] != " ") main_text += " ";
main_text += textB[i];
}
}
document.getElementById("ack_main").innerHTML = main_text;
if(latex_checkbox.checked) {
document.getElementById("ack_bibtex").innerHTML = bibtex_text;
} else {
document.getElementById("ack_bibtex").innerHTML = "";
}
if(facilities_checkbox.checked) {
document.getElementById("ack_facilities").innerHTML = facilities_text;
} else {
document.getElementById("ack_facilities").innerHTML = "";
}
// Add title text to any sections that need replacement
jQuery('.replace').attr('title','This is a placeholder and should be replaced');
return this;
}
// Build a typeahead search field attached to the element with ID=id
Citation.prototype.typeahead = function(id){
var t = 'typeahead';
// Add the typeahead div and hide it
$('body').append('<div id="'+t+'"></div>');
$('#'+t).hide();
// We want to remove the suggestion box if we lose focus on the
// input text field but not if the user is selecting from the list
this.typeaheadactive = true;
$('#'+t).on('mouseenter',{citation:this},function(e){
e.data.citation.typeaheadactive = true;
}).on('mouseleave',{citation:this},function(e){
e.data.citation.typeaheadactive = false;
});
$('#'+id).on('blur',{citation:this},function(e){
// Lose the suggestion box if we've lost focus
if(!e.data.citation.typeaheadactive) $('#'+t).html('').hide();
}).on('keyup',{citation:this},function(e){
// Once a key has been typed in the search field we process it
var s = $('#'+t+' a.selected');
var list = $('#'+t+' a');
if(e.keyCode==40 || e.keyCode==38){
// Up or down cursor keys
var i = 0;
// If an item is selected we move to the next one
if(s.length > 0){
s.removeClass('selected');
i = parseInt(s.attr('data'))+(e.keyCode==40 ? 1 : -1);
if(i >= list.length) i = 0;
if(i < 0) i = list.length-1;
}
// Select the new item
$(list[i]).addClass('selected');
// Update the search text
$(this).val(e.data.citation.results[i].name)
}else if(e.keyCode==13){
// The user has pressed return
if(s.length > 0) $(list[parseInt(s.attr('data'))]).trigger('click');
else $(list[0]).trigger('click');
}else{
var html = e.data.citation.search($(this).val());
$('#'+t).html(html).css({'position':'absolute','left':$(this).offset().left+'px','top':($(this).offset().top+$(this).outerHeight())+'px','width':$(this).outerWidth()+'px'}).show();
$('#'+t+' a:first').addClass('selected');
$('#'+t+' a').each(function(i){
$(this).on('click',{citation:e.data.citation,s:s,t:t,id:id},function(e){
e.preventDefault();
// Trigger the click event for the item
$('#'+e.data.citation.results[i].id).trigger('click');
// Remove the suggestion list
$('#'+e.data.t).html('').hide();
// Clear the search field
$('#'+e.data.id).val('');
});
});
}
});
}
// Get an HTML list of items which match str
Citation.prototype.search = function(str){
var results = [];
var html = "";
if(str){
for(var i = 0; i < this.checkboxes.length; i++){
if(this.checkboxes[i].name.toLowerCase().indexOf(str.toLowerCase())>=0) results.push(this.checkboxes[i])
}
html = '<ul>';
for(var i = 0; i < results.length; i++){
html += '<li><a href="" data="'+i+'">'+results[i].name+'</a></li>'
}
html += "</ul>";
}
this.results = results;
return html;
}
function get_checked_boxes(checkbox_name) {
// Find all checked checkboxes
var checkboxes = document.getElementsByName(checkbox_name);
var checkboxes_checked = [];
// loop over them all
for (var k = 0; k < checkboxes.length; k++) {
// And stick the checked ones onto an array...
if (checkboxes[k].checked) {
checkboxes_checked.push(checkboxes[k]);
}
}
// Return the array if it is non-empty, or null
return checkboxes_checked;
}
$(document).ready(function(){
// Load JSON database of acknowledgments
$.getJSON("database.json", init);
})