|
var i = 0; |
|
var row = 0; |
|
var blank_num = 0; |
|
var column = 0; |
|
|
|
$("#grey-area").hide(); |
|
$("#modal").hide(); |
|
|
|
$.getJSON('http://www.freecodecamp.com/news/hot', function(data) { |
|
|
|
function content() { |
|
$('<div/>', { |
|
'class': 'hexagon hexagon' + (column%4), |
|
'id': 'newsWrapDiv' + i |
|
}).appendTo('#row' + row); |
|
|
|
//--------- |
|
|
|
/* $('<a/>', { |
|
'class': 'newsLink', |
|
'id': 'Link' + i, |
|
'href': data[i].link, |
|
'target': '_blank' |
|
}).appendTo("#" + i);*/ |
|
|
|
$('<div/>', { |
|
'class': 'hexagon-in1', |
|
'id': 'newsHexOne' + i |
|
}).appendTo('#newsWrapDiv' + i); |
|
|
|
$('<div/>', { |
|
'class': 'hexagon-in2', |
|
'id': 'newsHexTwo' + i |
|
}).appendTo('#newsHexOne' + i); |
|
|
|
if (data[i].image !== "") { |
|
$('#newsHexTwo' + i).css('background-image', 'url(' + data[i].image + ')'); |
|
} else { |
|
$('#newsHexTwo' + i).css('background-image', 'url(' + data[i].author.picture + ')'); |
|
} |
|
$('#newsHexTwo' + i).css('background-color', 'rgb(0,0,0,1)'); |
|
|
|
$('<h1/>', { |
|
'class': 'teaser', |
|
'id': i, |
|
text: data[i].headline |
|
}).appendTo('#newsHexTwo' + i); |
|
|
|
i++; |
|
if (! (column++ % 4)){ |
|
row++; |
|
} |
|
|
|
//modal window |
|
|
|
$('.hexagon').unbind("click").click(function() { |
|
//$('#test').html('WORKS') |
|
var cellNum = parseInt(event.target.id); |
|
|
|
function dateCreate(){ |
|
var nowTime = Date.now(); |
|
var postTime = data[cellNum].timePosted; |
|
var timeElapsed = nowTime - postTime; |
|
if(timeElapsed < 60000){ |
|
$('#date').html("Posted " + Math.round(timeElapsed/1000) + " seconds ago"); |
|
} |
|
else if(timeElapsed >= 60000 && timeElapsed < 120000){ |
|
$('#date').html("Posted 1 minute ago"); |
|
} |
|
else if(timeElapsed >= 120000 && timeElapsed < 3600000){ |
|
$('#date').html("Posted " + Math.round((timeElapsed/1000)/60) + " minutes ago"); |
|
} |
|
else if(timeElapsed >= 3600000 && timeElapsed < 7200000){ |
|
$('#date').html("Posted 1 hour ago"); |
|
} |
|
else if(timeElapsed >= 7200000 && timeElapsed < 86400000){ |
|
$('#date').html("Posted " + Math.round(((timeElapsed/1000)/60)/60) + " hours ago"); |
|
} |
|
else if(timeElapsed >= 86400000 && timeElapsed < 172800000){ |
|
$('#date').html("Posted 1 day ago"); |
|
} |
|
else if(timeElapsed >= 172800000 && timeElapsed < 604800000){ |
|
$('#date').html("Posted " + Math.round((((timeElapsed/1000)/60)/60)/24) + " days ago"); |
|
} |
|
else if(timeElapsed >= 604800000 && timeElapsed < 1209600000){ |
|
$('#date').html("Posted 1 week ago"); |
|
} |
|
else if(timeElapsed >= 1209600000 && timeElapsed < 2419200000){ |
|
$('#date').html("Posted " + Math.round(((((timeElapsed/1000)/60)/60)/24)/7) + " weeks ago"); |
|
} |
|
else if(timeElapsed >= 2419200000 && timeElapsed < 5184000000){ |
|
$('#date').html("Posted 1 month ago"); |
|
} |
|
else if(timeElapsed >= 5184000000 && timeElapsed < 31557600000){ |
|
$('#date').html("Posted " + Math.round(((((timeElapsed/1000)/60)/60)/24)/30.42) + " months ago"); |
|
} |
|
else if(timeElapsed >= 31557600000 && timeElapsed < 63115200000){ |
|
$('#date').html("Posted 1 year ago"); |
|
} |
|
else if(timeElapsed >= 63115200000){ |
|
$('#date').html("Posted " + Math.round(((((timeElapsed/1000)/60)/60)/24)/365.25) + " years ago"); |
|
} |
|
|
|
} |
|
|
|
dateCreate(); |
|
//alert(event.target.id); |
|
$("#avatar").attr('src', data[cellNum].author.picture); |
|
$("#author").html(data[cellNum].author.username); |
|
if(data[cellNum].image !== ""){$("#mainPic").css("background-image", "url('" + data[cellNum].image + "')");} |
|
else{ |
|
$("#mainPic").css("background-image", "url('https://camo.githubusercontent.com/edde83bbe3872711faf6085b514bf2c21f3d0ea0/68747470733a2f2f692e76696d656f63646e2e636f6d2f766964656f2f3532303132393337375f31323830783732302e6a7067')"); |
|
} |
|
$("#articleLink").attr('href', data[cellNum].link); |
|
$('#title').html(data[cellNum].headline); |
|
if(data[cellNum].rank < 1000){ |
|
$('#likes').append(data[cellNum].rank); |
|
} |
|
else if(data[cellNum].rank >= 1000 && data[cellNum].rank < 1000000){ |
|
var rankK = data[cellNum].rank/1000; |
|
$('#likes').append(rankK.toFixed(1) +"K"); |
|
} |
|
else{ |
|
var rankM = data[cellNum].rank/1000000; |
|
$('#likes').append(rankM.toFixed(1) +"M"); |
|
} |
|
$("#grey-area").fadeIn('slow'); |
|
$("#modal").fadeIn('slow'); |
|
}); |
|
|
|
$('#grey-area').unbind("click").click(function() { |
|
$("#grey-area").fadeOut('slow'); |
|
$("#modal").fadeOut('slow', function() { |
|
$("#likes").html('<i class="fa fa-thumbs-up"></i>'); |
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
function blank() { |
|
|
|
$('<div/>', { |
|
'class': 'hexagon hexagon' + column, |
|
'id': 'blankImgWrap' + blank_num |
|
}).appendTo('#row' + row); |
|
|
|
$('<div/>', { |
|
'class': 'hexagon-in1', |
|
'id': 'blankHexOne' + blank_num |
|
}).appendTo('#blankImgWrap' + blank_num); |
|
|
|
$('<div/>', { |
|
'class': 'blank hexagon-in2', |
|
'id': 'blankHexTwo' + blank_num |
|
}).appendTo('#blankHexOne' + blank_num); |
|
blank_num++; |
|
/*if (column < 4) { |
|
column++; |
|
} else { |
|
column = 1; |
|
row++; |
|
}*/ |
|
if (!(column++ % 4)) { row++ } |
|
} |
|
|
|
for (var j = 0; j < 10; j++) { |
|
var rand = Math.floor((Math.random() * 100) + 1); |
|
if (rand >= 1 && rand <= 50) { |
|
content(); |
|
content(); |
|
content(); |
|
content(); |
|
} else if (rand > 50 && rand <= 75) { |
|
if (rand > 50 && rand <= 56) { |
|
blank(); |
|
content(); |
|
content(); |
|
content(); |
|
} else if (rand > 56 && rand <= 62) { |
|
content(); |
|
blank(); |
|
content(); |
|
content(); |
|
} else if (rand > 62 && rand <= 68) { |
|
content(); |
|
content(); |
|
blank(); |
|
content(); |
|
} else if (rand > 68 && rand <= 75) { |
|
content(); |
|
content(); |
|
content(); |
|
blank(); |
|
} |
|
} else if (rand > 75 && rand <= 95) { |
|
if (rand > 75 && rand <= 78) { |
|
blank(); |
|
blank(); |
|
content(); |
|
content(); |
|
} else if (rand > 78 && rand <= 81) { |
|
blank(); |
|
content(); |
|
blank(); |
|
content(); |
|
} else if (rand > 81 && rand <= 84) { |
|
blank(); |
|
content(); |
|
content(); |
|
blank(); |
|
} else if (rand > 84 && rand <= 87) { |
|
content(); |
|
blank(); |
|
blank(); |
|
content(); |
|
} else if (rand > 87 && rand <= 91) { |
|
content(); |
|
blank(); |
|
content(); |
|
blank(); |
|
} else if (rand > 91 && rand <= 95) { |
|
content(); |
|
content(); |
|
blank(); |
|
blank(); |
|
} |
|
} else if (rand > 95 && rand < 100) { |
|
if (rand == 96) { |
|
blank(); |
|
blank(); |
|
blank(); |
|
content(); |
|
} else if (rand == 97) { |
|
blank(); |
|
blank(); |
|
content(); |
|
blank(); |
|
} else if (rand == 98) { |
|
blank(); |
|
content(); |
|
blank(); |
|
blank(); |
|
} else if (rand == 99) { |
|
content(); |
|
blank(); |
|
blank(); |
|
blank(); |
|
} |
|
} else if (rand == 100) { |
|
blank(); |
|
blank(); |
|
blank(); |
|
blank(); |
|
} |
|
|
|
} |
|
|
|
}); |