Skip to content

Instantly share code, notes, and snippets.

Created July 26, 2013 16:36
Show Gist options
  • Save anonymous/6090315 to your computer and use it in GitHub Desktop.
Save anonymous/6090315 to your computer and use it in GitHub Desktop.
Get news
<%@include file="/libs/foundation/global.jsp" %>
<%@ page import="javax.jcr.Repository,
javax.jcr.Session,
javax.jcr.SimpleCredentials,
javax.jcr.Node,
java.net.URLEncoder,
java.util.Calendar,
java.util.Date,
java.util.List,
java.util.Iterator,
java.util.Map,
java.util.HashMap,
javax.jcr.Value,
javax.jcr.RepositoryException,
com.day.cq.wcm.api.Page,
java.util.Iterator,
java.text.DateFormat,
java.text.SimpleDateFormat,
com.day.cq.search.result.Hit,
com.day.cq.wcm.api.PageManager,
com.day.cq.search.PredicateGroup,
com.day.cq.search.QueryBuilder,
com.day.cq.search.Query,
com.day.cq.search.result.SearchResult,
com.day.cq.wcm.api.PageFilter,
com.day.cq.wcm.api.PageManager,
com.day.cq.wcm.api.WCMMode,
com.day.text.Text" %>
<% String contextPath = request.getContextPath() != null ? request.getContextPath() : ""; %>
<cq:includeClientLib js="navfac.endlessscroll"/>
<cq:includeClientLib css="navfac.news"/>
<div id="rollup" class="newsContainer"></div>
<script type="text/javascript" charset="utf-8">
var totalNumberOfNews = 0;
var newsItemsPerPage = 5;
var newsItemsInDOM =0;
var stopScrolling=false;
jQuery(document).ready(function() {
var urlStr = window.location.pathname;
urlStr = urlStr.substring(urlStr.lastIndexOf('/')+1,urlStr.lastIndexOf('.'));
URL = urlStr+"/jcr:content.getSearchHits.json";
jQuery.getJSON(URL,
{
params: "thereAreNone"
},
function(data) {
totalNumberOfNews = data.numberOfHits;
//alert("Total news hits :"+totalNumberOfNews );
if(totalNumberOfNews <= newsItemsPerPage){
newsItemsPerPage = totalNumberOfNews ;
stopScrolling=true;
}
//now we shall get the first page worth of news items
jQuery.getJSON(urlStr+"/jcr:content.rollup.json",
{
limit: newsItemsPerPage,
offset: "0"
},
function(data) {
jQuery.each(data.newsItems, function(i,newsItem){
var htmlData = "";
htmlData += "<div class='indivNews'>";
if(newsItem.image != null && newsItem.image.length > 0) {
htmlData += "<div class='imageContainer'><img src='"+newsItem.image+"'/></div>";
}
else {
htmlData += "<div class='imageContainer'><img src='<%=contextPath%>/etc/designs/navfac/images/navfac_logo_medium.png'/></div>";
}
htmlData += "<div class='textSummary'>";
htmlData += "<h5><a target='_blank' href='"+newsItem.url+"'>"+newsItem.title+"</a></h5>";
htmlData += "<p class='date'>"+newsItem.date+"</p>";
htmlData += "<p class='text'>"+newsItem.lead+"</p>";
htmlData += "<div class='learnMoreLink'><a target='_blank' href='"+newsItem.url+"'>Learn More</a></div>";
htmlData += "</div>";
htmlData += "</div>";
newsItemsInDOM = newsItemsInDOM +1;
if(newsItemsInDOM >=totalNumberOfNews){
stopScrolling=true;
}
jQuery('.newsContainer').append(htmlData);
});
});
});
});
jQuery(function() {
jQuery(window).endlessScroll({
fireOnce: true,
bottomPixels: 100,
insertAfter: "#rollup div:last",
loader: "<div class=\"loading\"></div>",
data: function(i) {
var urlStr = window.location.pathname;
urlStr = urlStr.substring(urlStr.lastIndexOf('/')+1,urlStr.lastIndexOf('.'))+"/jcr:content.rollup.json";
jQuery.getJSON(urlStr,
{
limit: newsItemsPerPage,
offset: newsItemsInDOM
},
function(data) {
jQuery.each(data.newsItems, function(i,newsItem){
var htmlData = "";
htmlData += "<div class='indivNews'>";
if(newsItem.image != null && newsItem.image.length > 0) {
htmlData += "<div class='imageContainer'><img src='"+newsItem.image+"' /></div>";
}
else {
htmlData += "<div class='imageContainer'><img src='/etc/designs/navfac/images/transparent_1x1.png' width='128' height='85' /></div>";
}
htmlData += "<div class='textSummary'>";
htmlData += "<h5><a target='_blank' href='"+newsItem.url+"'>"+newsItem.title+"</a></h5>";
htmlData += "<p class='date'>"+newsItem.date+"</p>";
htmlData += "<p class='text'>"+newsItem.lead+"</p>";
htmlData += "<div class='learnMoreLink'><a target='_blank' href='"+newsItem.url+"'>Learn More</a></div>";
htmlData += "</div>";
htmlData += "</div>";
newsItemsInDOM = newsItemsInDOM +1;
if(newsItemsInDOM >=totalNumberOfNews){
stopScrolling=true;
}
jQuery('.newsContainer').append(htmlData);
newsItemsInDOM = newsItemsInDOM +1;
if(newsItemsInDOM >= totalNumberOfNews){
stopScrolling=true;
}
});
});
},
ceaseFire: function() {
return stopScrolling;
}
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment