-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathImpactStory.js
408 lines (365 loc) · 14.9 KB
/
ImpactStory.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Set up the global impact story object
var ImpactStory = new impactStory();
// impactStory class.
// If you don't want to use the global object, you can create a new impactStory object by using `var myIS = new impactStory()`.
function impactStory() {
var self = this;
// This should be overriden. An error will be thrown if a key is not set.
self.key = '';
// Template directory. This needs to be overriden and set.
// @@TODO: Once we have this properly hosted somewhere, we can default this to the hosted template path.
self.templates = '';
// This may optionally be overriden if you want to use a different URL
self.url = 'http://api.impactstory.org/v1/';
/**
* Add an item.
* ImpactStory.addItem(item, callback, error);
*
* @item: key-value pairs. For example: ['pmid','12345'] or ['doi','10.1371/journal.pbio.1000056']
* You may also pass a hash object such as {pmid: 12345} or {doi: '10.1371/journal.pbio.1000056'}
* @callback: Callback to be called when call finishes. function(data)
* @error: callback function to be called on error: functon(error)
*/
self.addItem = function(item, callback, error) {
if (!self._checkKey()) return false;
// Transform hash objects into arrays
item = self._arrayify(item);
jQuery.ajax({
url: self.url + "item/" + item[0] + "/" + item[1] + "?key=" + self.key,
type: 'POST',
}).done(function () {
callback();
}).error(function (err) {
if (error) {
error(err);
}
});
};
/**
* Get an item with ALM data
* ImpactStory.getItem(item, callback, error);
*
* @item: key-value pair. For example: ['pmid','12345'] or ['doi','10.1371/journal.pbio.1000056']
* You may also pass a hash object such as {pmid: 12345} or {doi: '10.1371/journal.pbio.1000056'}
* @callback: Callback to be called when call finishes. function(data)
* @error: callback function to be called on error: functon(error)
*/
self.getItem = function(item, callback, error) {
if (!self._checkKey()) return false;
// Transform hash objects into arrays
item = self._arrayify(item);
jQuery.ajax({
url: self.url + "item/" + item[0] + "/" + item[1] + "?key=" + self.key,
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
statusCode: {
210: function(data){
//@@TODO: run partial callback stuff and deal with errors
//@@TODO: Respect conf.retry and conf.interval
setTimeout(function(){
self.getItem(item, callback, error, conf)
}, 1000)
},
200: function(data) {
callback(data)
}
}
});
};
/**
* Add and then get ALM for an item
* ImpactStory.addAndGetItem(item, callback, error, conf);
*
* See documentation on impactStory.getCollection for more information on how polling works
*
* @item: key-value pair. For example: ['pmid','12345'] or ['doi','10.1371/journal.pbio.1000056']
* @callback: Callback to be called when the item gotten. function(data)
* @error callback function to be called on error: functon(error)
* @conf Configuration object. For example:
* {
* retry: 10, // Number of times to poll before giving up
* interval: 1000, // Number of milliseconds between polls
* }
*/
self.addAndGetItem = function(item, callback, error, conf) {
if (!self._checkKey()) return false;
self.addItem(item, function() {
self.getItem(item, callback, error, conf);
}, error);
};
/**
* Get collection information (including all ALM data)
* ImpactStory.getCollection(collection, callback, error, conf);
*
* Note that for a newly created collection, this can time some time to return the full set of data
* as total-impact can take a while to generate it. To get around this problem we poll the total-impact API
* in intervals and only return when we have the full set of data. This generally takes about 5 seconds.
* To get back partial (incomplete) data returned from each poll, defind a partial-callback function
* using conf.partial = partialCallbackFunc.
*
* @collection: Can be either the collection ID (string), a "create-collection" meta-object, or a collection object.
* @callback: Callback to be called when the collection is done loading and we have all data. function(data)
* @error: Callback function to be called on error: functon(error)
* @conf Configuration object. For example:
* {
* includeItems: true, // set to false to only return meta-information, not ALM data
* retry: 10, // Number of times to poll before giving up
* interval: 1000, // Number of milliseconds between polls
* partial: function(data) // Partial callback function. Call this on each poll, even if we have only partial data.
* }
*/
self.getCollection = function(collection, callback, error, conf) {
if (!self._checkKey()) return false;
// Get the collection ID. Can pass either a string, a "create-collection" meta-object, or a collection object
var collectionId;
if (typeof collection == 'string') {
collectionId = collection;
}
if (typeof collection == 'object') {
if (collection.hasOwnProperty('collection')) {
collectionId = collection.collection._id;
}
else {
collectionId = collection._id;
}
}
jQuery.ajax({
url: self.url + "collection/" + collectionId + '?key=' + self.key,
type: "GET",
dataType: "json",
contentType: "application/json; charset=utf-8",
statusCode: {
210: function(data){
//@@TODO: run partial callback stuff and deal with errors
//@@TODO: Respect conf.retry and conf.interval
setTimeout(function(){
self.getCollection(collection, callback, error, conf)
}, 1000)
},
200: function(data) {
callback(data)
}
}
});
};
/**
* Create a collection
* ImpactStory.createCollection(aliases, title, callback, error);
*
* @aliases: list of key-value pairs. For example: [['pmid','12345'],['doi','10.1371/journal.pbio.1000056']]
* You may also pass an array of object hashes, for example [{pmid: 12345} , {doi: '10.1371/journal.pbio.1000056'}]
* @title: Title of collection
* @callback: Callback to be called when the collection is done loading. function(data)
* @error: callback function to be called on error: functon(error)
*/
self.createCollection = function(aliases, title, callback, error) {
if (!self._checkKey()) return false;
// Transform all hash objects into arrays for POSTing
for (var i in aliases) {
aliases[i] = self._arrayify(aliases[i]);
}
var postData = {
'aliases' : aliases,
'title': title
};
jQuery.ajax({
url: self.url + "collection?key=" + self.key,
type: 'POST',
dataType: 'json',
contentType:"application/json; charset=utf-8",
data: JSON.stringify(postData)
}).done(function (returnedData) {
callback(returnedData);
}).error(function (err) {
if (error) {
error(err);
}
});
};
/**
* Create and then get ALM for a collection
* ImpactStory.createAndGetCollection(aliases, title, callback, error, conf);
*
* See documentation on impactStory.getCollection for more information on how polling works
*
* @aliases: list of key-value pairs. For example: [['pmid','12345'],['doi','10.1371/journal.pbio.1000056']]
* @title: Title of collection
* @callback: Callback to be called when the collection is done loading. function(data)
* @error callback function to be called on error: functon(error)
* @conf Configuration object. For example:
* {
* includeItems: true, // set to false to only return meta-information, not ALM data
* retry: 10, // Number of times to poll before giving up
* interval: 1000, // Number of milliseconds between polls
* partial: function(data) // Partial callback function. Call this on each poll, even if we have only partial data.
* }
*/
self.createAndGetCollection = function(aliases, title, callback, error, conf) {
if (!self._checkKey()) return false;
self.createCollection(aliases, title, function(collection) {
self.getCollection(collection, callback, error, conf);
}, error);
};
//@@TODO
self.getProviderInfo = function() {
// http://api.impactstory.org/v1/provider?key=YOURKEY
};
self.renderTemplate = function(template, item, callback, error) {
jQuery.ajax({
url: self.templates + "/" + template + ".html",
type: "GET"
//cache: false
}).done(function (returnedData) {
// First we need to transform metrics
callback(Mustache.render(returnedData, self.templatizeItem(item)));
}).error(function (err) {
if (error) {
error(err);
}
});
};
// Get an item ready for templatization
// This basically just transforms the metrics object into an array and adds other useful data required by the moustache template engine
self.templatizeItem = function(item) {
var metricsArray = [];
for (var name in item.metrics) {
// Add the name to the metrics
item.metrics[name].name = name;
// Mark wether it's values are a "simple" string or a "complex" array of objects
if (Array.isArray(item.metrics[name].values.raw)) {
item.metrics[name].values.simple = false;
}
else {
item.metrics[name].values.simple = true;
}
// When rendering, we need to know how much to offset the upper-limit WoS.
if (self._propExists(item.metrics[name], 'values.WoS.CI95_upper')) {
item.metrics[name].values.WoS.CI95_upper_offset = 100 - item.metrics[name].values.WoS.CI95_upper;
}
if (self._propExists(item.metrics[name], 'values.WoS.estimate_upper')) {
item.metrics[name].values.WoS.estimate_upper_offset = 100 - item.metrics[name].values.WoS.estimate_upper;
}
metricsArray.push(item.metrics[name])
}
item.metrics = metricsArray;
return item;
};
/**
* Private function that checks for a valid key and alerts an error if there isn't one set.
*/
self._checkKey = function() {
if (self.key === "") {
alert('Please specify an Impact Story key by running `impactStory.key = "YOURKEY";` before calling any impactStory methods.');
return false;
}
else {
return true;
}
};
/**
* Private utlity function that transforms a hash object like {pmid: 12345} into an array like ['pmid','12345']
*/
self._arrayify = function(item) {
// cross-browser method to determine if a variable is an array or not
if (Object.prototype.toString.call(item) === '[object Array]') return item;
if (typeof item === 'object') {
for (var key in item) {
return [key, item[key]];
}
}
else {
throw "impactStory error: items must be in the form of an array (eg. ['pmid','12345'] ) or a hash object (eg. {doi: '10.1371/journal.pbio.1000056'} .)";
}
};
/**
* Private utlity function that checks if a sub-property exists
* See: http://stackoverflow.com/questions/4676223/check-if-object-member-exists-in-nested-object
*/
self._propExists = function(obj, prop) {
var parts = prop.split('.');
for(var i = 0, l = parts.length; i < l; i++) {
var part = parts[i];
if(obj !== null && typeof obj === "object" && part in obj) {
obj = obj[part];
}
else {
return false;
}
}
return true;
};
}
/**
* ImpactStoryEmbed is a jQuery plugin that will embed an ImpactStory template
*/
(function($){
$.fn.extend({
ImpactStoryEmbed: function(template, options) {
var defaults = {
'api-key' : ImpactStory.key,
'url' : ImpactStory.url,
'templates': ImpactStory.templates,
'preloaded': false, // Set to true if you are sure ImpactStory already has your item indexed. Setting this to true should make things faster.
'doi' : false, // Can set a DOI
'pmid' : false, // can set a pmid
'item' : false, // can set an item in the form of {pmid: 12345} or ['pmid','12345']
'id-type': "doi", // Can set the type for the "id" field
'id': false, // Can set an id
}
var options = $.extend(defaults, options);
return this.each(function() {
var item;
var $container = $(this);
var IS = new impactStory();
// Data options in the element override the options passed or defauled
for (var i in options) {
if ($container.data(i)) {
options[i] = $(this).data(i);
}
}
// Lots of ways to specify the id-type and id-value. Start by checking the "doi" and "pmid" values
if (options.pmid) {
item = ['pmid', options.pmid];
}
else if (options.doi) {
item = ['doi', options.doi];
}
else if (options.item) {
item = options.item;
}
else {
item = [options['id-type'], options['id']];
}
// Set the key, url and templates
IS.key = options['api-key'];
IS.url = options['url'];
IS.templates = options['templates'];
if (options.preloaded) {
IS.getItem(item, function(data) {
IS.renderTemplate(template, data, function(markup) {
$container.html(markup);
});
});
}
else {
IS.addAndGetItem(item, function(data) {
IS.renderTemplate(template, data, function(markup) {
$container.html(markup);
});
});
}
});
}
});
})(jQuery);
/**
* Automatically process all items with impactstory-embed-report and impactstory-embed-badges
*/
(function($){
$(document).ready(function() {
$('.impactstory-embed-report').ImpactStoryEmbed('report');
//@@TODO
//$('.impactstory-embed-report').ImpactStoryEmbed('badges');
});
})(jQuery);