This repository has been archived by the owner on May 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
embed.js
222 lines (203 loc) · 6.97 KB
/
embed.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
/*
These two variables get replaced with the respective values during the
entrypoint script of the Docker container and depend on the environment variables
*/
var MC_BASE_HOST = ':base_host';
var MC_BASE_PORT = ':base_port';
/*
Documents API
*/
function DocumentsApi(Converter) {
if(MC_BASE_HOST.charAt(0) == ':') {
MC_BASE_HOST='http:https://mc-server.kohn.io';
}
if(MC_BASE_PORT.charAt(0) == ':') {
MC_BASE_PORT='80';
}
this.baseUrl = MC_BASE_HOST + ':' + MC_BASE_PORT;
};
DocumentsApi.prototype.queryDocumentsAsync = function(profileIds, fieldIds, orderAttr, orderDir, offset, limit, onlyCount) {
var self = this;
var url = this.baseUrl;
url += '/documents/';
var arguments = []
if(profileIds) {
arguments.push('profile-ids=' + profileIds.join(','));
}
if(fieldIds) {
arguments.push('field-ids=' + fieldIds.join(','));
}
if(orderAttr) {
if(orderAttr == 'pub_year') {
orderAttr = 'year';
}
arguments.push('order-attr=' + orderAttr);
}
if(orderDir) {
arguments.push('order-dir=' + orderDir);
}
if(offset) {
arguments.push('offset='+offset);
}
if(limit) {
arguments.push('limit='+limit);
}
if(onlyCount) {
arguments.push('only-count='+onlyCount);
}
var argumentString = '?' + arguments.join('&');
url += argumentString;
// Log url request
window.console&&console.log('GET ' + url);
// Return ajax promise
return jQuery.getJSON(url);
}
/*
Profiles Api
*/
function ProfilesApi() {
if(MC_BASE_HOST.charAt(0) == ':') {
MC_BASE_HOST='http:https://mc-server.kohn.io';
}
if(MC_BASE_PORT.charAt(0) == ':') {
MC_BASE_PORT='80';
}
this.baseUrl = MC_BASE_HOST + ':' + MC_BASE_PORT;
}
ProfilesApi.prototype.querySlimProfilesAsync = function() {
var url = this.baseUrl;
url += "/profiles/?slim=true";
// Log url request
window.console&&console.log('GET ' + url);
// Return ajax promise
return jQuery.getJSON(url);
}
var documentsApi = new DocumentsApi();
var profilesApi = new ProfilesApi();
// Object that will be used later to store the fetched slim profiles with their names
var profilePages = {};
/*
For each .mc-documents tag, query the documents and add them to the page
*/
function queryDocuments() {
// Loop through mc-documents
jQuery('.mc-documents').each(function() {
// Prepare target div && parent
var target = jQuery(this);
target.addClass('mc-loading');
// Fetch all the data variables
var profileIds = null;
if(typeof jQuery(this).data('profileids') !== 'undefined'){
var data = jQuery(this).data('profileids');
if(data) {
// Kill whitespace
profileIds = data.replace(/[ \n\t]+/, '');
// Split by comma
profileIds = profileIds.split(',');
}
}
var fieldIds = null;
if(typeof jQuery(this).data('fieldids') !== 'undefined'){
var data = jQuery(this).data('fieldids');
if(data) {
// Kill whitespace
fieldIds = data.replace(/[ \n\t]+/, '');
// Split by comma
fieldIds = fieldIds.split(',');
}
}
var orderAttr = 'year';
if(typeof jQuery(this).data('orderattr') !== 'undefined'){
var data = jQuery(this).data('orderattr');
if(data) {
orderAttr = data;
}
}
var orderDir = 'desc';
if(typeof jQuery(this).data('orderdir') !== 'undefined'){
var data = jQuery(this).data('orderdir');
if(data) {
orderDir = data;
}
}
var limit = '20';
if(typeof jQuery(this).data('limit') !== 'undefined'){
var data = jQuery(this).data('limit');
if(data) {
limit = data;
}
}
// Trigger async fetch
documentsApi.queryDocumentsAsync(profileIds, fieldIds, orderAttr, orderDir, 0, limit)
.done(function(data) {
var docs = '<ul>';
// loop through docs
for (var i = 0; i < data.length; i++) {
docs += '<li>';
if(typeof data[i].title !== 'undefined') {
docs += '<b>' + data[i].title + '</b><br />';
}
if(typeof data[i].authors !== 'undefined') {
// Now try to find the profile pages for the different authors
// First prepare the authors string
var authors = data[i].authors.split(',');
authors = authors.map(function(author) {
return author.replace(/(^[,\s]+)|([,\s]+$)/g, '');
});
// Then probe with the previously fetched profilePages dict
for(var j = 0; j < authors.length; j++) {
var name = authors[j];
if(j > 0) {
docs += ', ';
}
if(name in profilePages) {
docs += '<a href="' + profilePages[name] + '">' + name + '</a>';
} else {
docs += name;
}
}
}
if(typeof data[i].source !== "undefined" &&
typeof data[i].pub_year !== "undefined" &&
data[i].source != "" &&
data[i].pub_year != "") {
docs += '<br />' + data[i].source + ', ' + data[i].pub_year + '.</li>';
} else {
docs += '.</li>';
}
}
docs += '</ul>';
// update target
target.removeClass('mc-loading');
target.addClass('mc-loaded');
setTimeout(function() { target.empty(); target.append(docs);}, 1100);
})
.fail(function(jqXHR, textStatus) {
window.console&&console.log(textStatus);
});
});
}
// Function that is executed as soon as jquery is loaded
jQuery(document).ready(function() {
// Dont use the cache in Ajax calls
jQuery.ajaxSetup({ cache: false });
// Check if there is a single mc-documents tag in the page
if(jQuery('.mc-documents').length) {
// Fetch the slim profiles for the page links
profilesApi.querySlimProfilesAsync()
.done(function(data) {
for(var i=0; i<data.length; i++) {
var slimProfile = data[i]
profilePages[slimProfile["name"]] = slimProfile["page"];
}
})
.fail(function(jqXHR, textStatus) {
window.console&&console.log("Failed to fetch slim profiles!");
window.console&&console.log("Proceeding with document download.");
window.console&&console.log(textStatus);
})
.always(function() {
queryDocuments();
})
}
});