forked from karthik/almviz
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathembed.1.0.js
73 lines (67 loc) · 4.92 KB
/
embed.1.0.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
define(["jquery"], function($) {
main();
});
/******** Get identifier from data attribute ********/
function getIdentifier(tag) {
var value = tag.data('doi');
var type = "doi";
if (! value) {
var value = tag.data('pmid');
var type = "pmid";
} else if (! value) {
var value = tag.data('pmcid');
var type = "pmcid";
} else if (! value) {
var value = tag.data('mendeley');
var type = "mendeley";
}
return { 'value': value, 'type': type }
}
/******** Create HTML for large widget ********/
function setSmallWidget(tag, data) {
$('.alm-signpost').popover();
if (data["views"] > 0 || tag.data('show-zero')) tag.append('<span class="alm-signpost alm-views label label-info" title="Twitter Bootstrap Popover" data-views="' + data["views"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["views"] + ' Views</span>');
if (data["shares"] > 0 || tag.data('show-zero')) tag.append(' <span class="alm-signpost alm-shares label label-info" title="Twitter Bootstrap Popover" data-shares="' + data["shares"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["shares"] + ' Shares</span>');
if (data["bookmarks"] > 0 || tag.data('show-zero')) tag.append(' <span class="alm-signpost alm-bookmarks label label-info" title="Twitter Bootstrap Popover" data-bookmarks="' + data["bookmarks"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["bookmarks"] + ' Bookmarks</span>');
if (data["citations"] > 0 || tag.data('show-zero')) tag.append(' <span class="alm-signpost alm-citations label label-info" title="Twitter Bootstrap Popover" data-citations="' + data["citations"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["citations"] + ' Citations</span>');
if (tag.data('coins')) tag.append(' <span class="Z3988" title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft_id=info:doi/' + data["doi"] + '&rft.genre=article&rft.atitle=' + encodeURIComponent(data["title"]).replace(/%20/g, '+') + '&rft_date=' + data["publication_date"] + '"></span>');
}
/******** Create HTML for large widget ********/
function setLargeWidget(tag, data) {
tag.width(100);
//$('.alm-signpost').popover();
if (data["views"] > 0 || tag.data('show-zero')) tag.append('<div class="alm-signpost alm-views label label-info" title="Twitter Bootstrap Popover" data-views="' + data["views"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["views"] + ' Views</div>');
if (data["shares"] > 0 || tag.data('show-zero')) tag.append(' <div class="alm-signpost alm-shares label label-info" title="Twitter Bootstrap Popover" data-shares="' + data["shares"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["shares"] + ' Shares</div>');
if (data["bookmarks"] > 0 || tag.data('show-zero')) tag.append(' <div class="alm-signpost alm-bookmarks label label-info" title="Twitter Bootstrap Popover" data-bookmarks="' + data["bookmarks"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["bookmarks"] + ' Bookmarks</div>');
if (data["citations"] > 0 || tag.data('show-zero')) tag.append(' <div class="alm-signpost alm-citations label label-info" title="Twitter Bootstrap Popover" data-citations="' + data["citations"] + '" data-content="It\'s so simple to create a tooltop for my website!">' + data["citations"] + ' Citations</div>');
if (tag.data('coins')) tag.append(' <span class="Z3988" title="ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft_id=info:doi/' + data["doi"] + '&rft.genre=article&rft.atitle=' + encodeURIComponent(data["title"]).replace(/%20/g, '+') + '&rft_date=' + data["publication_date"] + '"></span>');
}
/******** Create error message ********/
function setErrorMessage(tag, message) {
tag.html('<div class="alert"><button type="button" class="close" data-dismiss="alert">×</button>Error: ' + message + '</div')
}
/******** Our main function ********/
function main() {
// Loop through all embed snippets
$('.alm-embed').each(function (i) {
var tag = $(this);
var id = getIdentifier(tag);
// Proceed if calling API for data only if we have an identifier
if (! id.value) {
setErrorMessage(tag, 'no identifier found.');
return;
}
var jsonp_url = "http://alm-plos.local/api/v3/articles?callback=?";
$.getJSON(jsonp_url, { ids: id.value, type: id.type, info: "summary" }, function(data) {
if ($.isArray(data)) {
if (tag.prop('tagName') == "DIV") {
setLargeWidget(tag, data[0]);
} else {
setSmallWidget(tag, data[0]);
}
} else {
setErrorMessage(tag, 'an error occured while fetching the data.');
}
});
});
}