-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
85 lines (79 loc) · 2.83 KB
/
script.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
/**
* Makes request to news feed and builds HTML
*
* @version 1.0
* @author Allar Vendla <allarvendla@gmail.com>
*/
// Get search word
var srchKWrd = window.location.href.match(/search=(.*)/);
if (srchKWrd == null) {srchKWrd = "*%3A*"} else if (srchKWrd[1] == ""){srchKWrd = "*%3A*"} else {srchKWrd = srchKWrd[1]}
var jsonFeed = 'http://uptsearch.cloudapp.net/solr/rss/select?q='+ srchKWrd +'&sort=date+desc&start=0&rows=100&wt=json&json.wrf=?&indent=true';
var feed = [];
var sources = [];
var titleCntPrSrc = 3;
var singleSrcExpand = false;
loadData();
// Load necessary data
function loadData() {
$.getJSON(jsonFeed, function(data){
$.each(data.response.docs, function(index, item) {
feed.push(item);
if ($.inArray(item.source, sources) == -1) {
sources.push(item.source);
}
});
$('<b>')
.append(feed.length)
.appendTo('div [label="stats"]');
loadFeed();
});
}
// Go through each source
function loadFeed (){
$.each(sources, function(index, itemS) {
$('#feed').append('<div id="'+ itemS +'-name" label="'+ itemS +'" class="sourceContainers">');
buildHTML(itemS);
});
}
// Build page items
function buildHTML(itemS){
var sourceTitleCnt = feed.filter(function(item){return item.source == itemS}).length;
var clickable = 'onClick="sourceView(this.id)" class="sources"';
var count = 0;
// Check witch view to show
if (singleSrcExpand == true) {
clickable = 'onClick="fullView(this.id)" class="sources"';
count = -50;
}else if ((singleSrcExpand == false) && (sourceTitleCnt <= titleCntPrSrc)) {
clickable = 'class="sources-nc"';
}
$('<div id="'+ itemS +'" label="'+ itemS +'-info" '+ clickable +'>')
.append(itemS +' <b>'+ sourceTitleCnt)
.appendTo('div [label="' + itemS + '"]');
$.each(feed, function(index, itemF) {
if (itemF.source == itemS && count < titleCntPrSrc){
count++;
// Get image source and check if source is valid
var imgSrc = itemF.description.match(/src="(\S*)"/)[1];
if ((imgSrc.match(/.jpg/) == null) && (imgSrc.match(/.gif/) == null)) {
imgSrc = "";
}
$('<div id="titles">')
.append('<img src="'+ imgSrc +'"/>')
.append('<a href="'+ itemF.link +'" target="_newtab"><h4>'+ itemF.title)
.appendTo('div [label="' + itemF.source + '"]');
}
});
}
// Load all titles for single source
function sourceView(clckdSrc) {
document.getElementById(clckdSrc +"-name").innerHTML = "";
singleSrcExpand = true;
buildHTML(clckdSrc);
}
// Load full view
function fullView(clckdSrc) {
document.getElementById(clckdSrc +"-name").innerHTML = "";
singleSrcExpand = false;
buildHTML(clckdSrc);
}