Created
March 17, 2017 13:50
-
-
Save freekrai/99dde42abc6faa417ee562a6f2eec8b5 to your computer and use it in GitHub Desktop.
flybase popular posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#popular { | |
float:left; | |
clear: both; | |
margin: 0 0 30px; | |
background: #fff; | |
width: 100%; | |
max-width:100%; | |
-moz-border-radius: 6px; | |
-webkit-border-radius: 6px; | |
border-radius: 6px; | |
border: solid 1px #ddd; | |
font-size: 81%; | |
} | |
#popular .header { | |
background: #f3f3f3; | |
border-bottom: 1px solid #ddd; | |
-moz-border-radius: 5px; | |
-webkit-border-radius: 5px; | |
border-radius: 5px; | |
padding: 6px 0px 0px 10px; | |
cursor: s-resize; | |
} | |
#popular .header h1 { | |
font-size: 1em; | |
color: #666; | |
font-family: "Open Sans", "Helvetica Neue", Arial, sans-serif; | |
padding-bottom: 6px; | |
margin: 0; | |
} | |
#popular .header.loading span { | |
-webkit-animation-name: pulse; | |
-webkit-animation-duration: 1s; | |
-webkit-animation-iteration-count: 100; | |
color: #bbb; | |
} | |
#popular .header.loaded { | |
-moz-border-radius-bottomleft: 0; | |
-webkit-border-bottom-left-radius: 0; | |
border-bottom-left-radius: 0; | |
-moz-border-radius-bottomright: 0; | |
-webkit-border-bottom-right-radius: 0; | |
border-bottom-right-radius: 0; | |
cursor: default; | |
} | |
#popular ul { | |
list-style-type: none; | |
padding: 0; | |
margin: 10px; | |
color: #007892; | |
} | |
#popular ul li { | |
float: left; | |
width: 49%; | |
margin: 0.25em 0; | |
} | |
#popular ul li a { | |
display: block; | |
margin: 0; | |
padding: 2px; | |
width: 91%; | |
color: #0689a6; | |
height: 1.5em; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
white-space: nowrap; | |
font-size: .85em; | |
} | |
/* | |
#popular ul li a { | |
display: block; | |
margin: 0; | |
padding: 2px; | |
width: 91%; | |
color: #0689a6; | |
height: 1.5em; | |
overflow: hidden; | |
text-overflow: ellipsis; | |
white-space: nowrap; | |
font-size: 15px; | |
} | |
*/ | |
@media only screen and (max-width: 790px) { | |
#popular ul li { | |
float: none; | |
width: 100%} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$document.ready(function () { | |
var url = document.location.pathname; | |
var title = document.title; | |
if ( url === "/" ){ | |
var page = getParameterByName("page"); | |
if( page === "" || page === "1" ){ | |
var popular = new mostPopular().getPages( $("#popularpart") ); | |
} | |
}else { | |
// not home page so let's record the url count. | |
var popular = new mostPopular().updatePage(url, title); | |
} | |
}); | |
var mostPopular = function(){ | |
this.api_key = "FLYBASE-API-KEY"; | |
this.db = "FLYBASE-APP"; | |
this.collection = "mostpopular"; | |
this.pages = []; | |
this.flybaseRef = new Flybase( this.api_key, this.db, this.collection ); | |
this.populate(); | |
return this; | |
}; | |
mostPopular.prototype.populate = function(){ | |
var _this = this; | |
// let's get a list of pages for use later... | |
this.flybaseRef.orderBy( {"views":-1} ).on('value', function( data ){ | |
if( data.count() ){ | |
data.forEach( function(snapshot) { | |
var item = snapshot.value(); | |
_this.pages[ item.key ] = item; | |
}); | |
} | |
}); | |
}; | |
mostPopular.prototype.getPages = function( div_id ){ | |
var _this = this; | |
var r={ | |
headline:"Recently popular posts…", | |
clickhere:"(click to load)", | |
loading:"(loading…)" | |
}; | |
$('<aside id="popular"><div class="header"><h1>'+r.headline+"</h1></div></aside>").prependTo( div_id ); | |
this.flybaseRef.orderBy({"views":-1}).limit(6).on('value',function( data ){ | |
if( data.count() ){ | |
var pages = []; | |
data.forEach( function(snapshot) { | |
var item = snapshot.value(); | |
pages[item._id] = item; | |
}); | |
var aside = $("#popular"); | |
var header = $("header",aside); | |
var ul = $("<ul />").attr("style","display:none"); | |
for( var i in pages ){ | |
var item = pages[i]; | |
$('<li/>').attr("id",item._id).prepend( | |
$("<a>") | |
.attr("href",item.url) | |
.attr("title",item.title.replace("'","'") ) | |
.attr("data-count",item.views) | |
.text(item.title.replace("'","'") ) | |
).appendTo( ul ); | |
_this.pages[ item.key ] = item; | |
} | |
aside.append( ul ); | |
ul.slideDown(400); | |
header.removeClass("loading").addClass("loaded"); | |
header.find("h1").html(r.headline); | |
} | |
}); | |
return this; | |
}; | |
mostPopular.prototype.updatePage = function(url, title){ | |
var key = url.replace(/[\/-]/g,''); | |
var _this = this; | |
var cnt = 0; | |
_this.flybaseRef.where({"url": url}).orderBy( {"views":-1} ).once('value').then( function( data ){ | |
data.forEach( function(snapshot) { | |
var item = snapshot.value(); | |
item.views = item.views + 1; | |
_this.flybaseRef.update(item._id,item, function(resp) { | |
console.log( key + " updated" ); | |
}); | |
}); | |
},function(){ | |
// no count, so never added before.. | |
_this.flybaseRef.push({ | |
"key": key, | |
"url": url, | |
"title": title, | |
"views": 1 | |
}, function(resp) { | |
console.log( "URL added" ); | |
}); | |
}); | |
return this; | |
}; | |
function hashCode( str ){ | |
if (Array.prototype.reduce){ | |
return str.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0); | |
} | |
var hash = 0; | |
if (str.length === 0) return hash; | |
for (var i = 0; i < str.length; i++) { | |
var character = str.charCodeAt(i); | |
hash = ((hash<<5)-hash)+character; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return hash; | |
} | |
function getParameterByName(name) { | |
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); | |
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),results = regex.exec(location.search); | |
return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Sample Page</title> | |
<link rel="stylesheet" type="text/css" href="popular.css"> | |
</head> | |
<body> | |
<!-- placeholder for popular posts --> | |
<div id="popularpart"></div> | |
<script src="https://cdn.flybase.io/flybase.js"></script> | |
<script type="text/javascript" src="popular.js"></script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment