Skip to content

Commit

Permalink
Fix #151 and make minor feed UI adjustments (#157)
Browse files Browse the repository at this point in the history
* Fix #151 and make minor feed UI adjustments

* Reformat script using 2 spaces

* Bug fixes

* Fixed header sizes
  • Loading branch information
Caleb Ely authored and Charlie Lee committed May 15, 2016
1 parent 376073e commit 842f50b
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 140 deletions.
27 changes: 17 additions & 10 deletions app/css/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,22 @@ a {
width: 19em;
}

#sidebar a { color: #d3d3d3; }
#sidebar a:hover { border-bottom: 1.5px solid #d3d3d3; }
.sidebar-header h2 { font-size: 1.2rem; }

.sidebar-header i {
margin-right: 0.4em;
font-size: 0.9em;
}

#sidebar-content {
padding-left: 1em;
padding-bottom: 1em;
}

#sidebar-content h2 { font-size: 1.2em; }

#sidebar-content ul {
list-style: none;
line-height: 1.4;
padding-left: 35px;
line-height: 1.5;
padding-left: 1.5em;
}

#sidebar-content li { margin-left: -0.7em; }
Expand Down Expand Up @@ -80,7 +82,7 @@ a {
}

.content h2 {
font-size: 1.2em;
font-size: 1.2rem;
margin: 0;
padding: 0.83em 0;
}
Expand Down Expand Up @@ -143,17 +145,22 @@ a {
color: #fff;
text-decoration: none;
}
.post-link:hover { color: #fff; }
.post-link:hover {
color: #fff;
border-bottom: 1px solid #fff;
}

.post-title {
font-weight: 500;
margin: 0;
margin-bottom: -0.4em;
font-size: 1.2em;
font-weight: 500;
color: #fff;
}
.post-date {
margin: 0.2em 0 1.2em 0;
font-size: 0.85em;
color: #d3d3d3;
margin: 0.75em 0 0.25em 0;
}
.post-content p:first-of-type { margin: 0; }
.post-content p { margin-top: 0; }
Expand Down
6 changes: 2 additions & 4 deletions app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
</head>

<body>
<!--Sidebar content-->
<!-- News feed -->
<div id="sidebar">
<div id="sidebar-content">

<!--Capture Options-->
<div class="sidebar-item">
<div class="sidebar-header">
<h2><i class="fa fa-newspaper-o"></i> Recent News</h2>
<h2><i class="fa fa-newspaper-o"></i>Recent News</h2>
</div>
<ul>
<li><div class="container-news"></div></li>
Expand Down
254 changes: 128 additions & 126 deletions app/js/newsfeed.js
Original file line number Diff line number Diff line change
@@ -1,151 +1,153 @@
module.exports = {};

(function() {
"use strict";
let qNewsContainer = document.querySelector(".container-news");
qNewsContainer.addEventListener("click", openNews);
"use strict";
let qNewsContainer = document.querySelector(".container-news");
qNewsContainer.addEventListener("click", openNews);

/**
* Open the desired news post in the user's browser.
*
* @param {Object} e - An MouseEvent object.
*/
function openNews(e) {
if (e.target.classList.contains("post-title")) {
nw.Shell.openExternal(e.target.parentElement.dataset.url);
}
/**
* Open the desired news post in the user's browser.
*
* @param {Object} e - An MouseEvent object.
*/
function openNews(e) {
if (e.target.classList.contains("post-link")) {
utils.openURL(e.target.dataset.url);
}
}

/**
* Cache the latest news.
*
* @param {Array} posts - An array containing raw new objects.
* @returns {Boolean} True if new was cached, false otherwise.
*/
function _cacheNews(posts) {
// Store the news in localStorage in case we cannot fetch it anew
localStorage.setItem("ba-news", JSON.stringify(posts));
/**
* Cache the latest news.
*
* @param {Array} posts - An array containing raw new objects.
* @returns {Boolean} True if new was cached, false otherwise.
*/
function _cacheNews(posts) {
// Store the news in localStorage in case we cannot fetch it anew
localStorage.setItem("ba-news", JSON.stringify(posts));

// Because setItem() does not have a return value upon successful
// storage, this kludge does that for us.
if (localStorage.getItem("ba-news")) {
console.info("Latest news sucessfully cached");
return true;
} else {
console.info("Latest news was not sucessfully cached");
return false;
}
// Because setItem() does not have a return value upon successful
// storage, this kludge does that for us.
if (localStorage.getItem("ba-news")) {
console.info("Latest news sucessfully cached");
return true;
} else {
console.info("Latest news was not sucessfully cached");
return false;
}
}

/**
* Retrieve the cached news.
*
* @returns {Array} An array containing raw news objects.
* If not available, an empty array.
*/
function _getNewsCache() {
console.info("Fetching cached news");
let cached = localStorage.getItem("ba-news");
return cached ? JSON.parse(cached) : [];
}
/**
* Retrieve the cached news.
*
* @returns {Array} An array containing raw news objects.
* If not available, an empty array.
*/
function _getNewsCache() {
console.info("Fetching cached news");
let cached = localStorage.getItem("ba-news");
return cached ? JSON.parse(cached) : [];
}

/**
* Clear the cached news.
*
* @returns {Boolean} Always returns true.
*/
function _clearNewsCache() {
localStorage.removeItem("ba-news");
return true;
}
/**
* Clear the cached news.
*
* @returns {Boolean} Always returns true.
*/
function _clearNewsCache() {
localStorage.removeItem("ba-news");
return true;
}

/**
* Construct the HTML structure for the news.
*
* @param {Object} post - A news article object.
* @returns {String}
*/
function _compilePostHTML(post) {
return `<div class="post" id="post-${post.id}">
/**
* Construct the HTML structure for the news.
*
* @param {Object} post - A news article object.
* @returns {String}
*/
function _compilePostHTML(post) {
return `<div class="post" id="post-${post.id}">
<header>
<a class="post-link" href="#" data-url="${post.url}"><h2 class="post-title">${post.title}</h2></a>
<h2 class="post-title"><a class="post-link" href="#" data-url="${post.url}">${post.title}</a></h2>
<p class="post-date">${post.date}</p>
</header>
<div class="post-content"><p>${post.excerpt}</p></div>
</div>`;
}

/**
* Compile the latest news.
*
* @param {JSON} data - The JSON response data.
* @returns {Array} The latest news, in the form of news objects.
*/
function getPosts(data) {
let posts = [],
numOfPosts = 3;
}

// Get the post up to the limit of posts we want to display
for (var i = 0; i < numOfPosts; i++) {
var curPost = data.posts[i];
posts.push({
id: curPost.id,
url: curPost.url,
date: new Date(curPost.date).toLocaleDateString(),
title: curPost.title_plain,
excerpt: curPost.excerpt
});
}
/**
* Compile the latest news.
*
* @param {JSON} data - The JSON response data.
* @returns {Array} The latest news, in the form of news objects.
*/
function getPosts(data) {
let posts = [],
numOfPosts = 3;

// Cache the news
_clearNewsCache();
_cacheNews(posts);
return posts;
// Get the post up to the limit of posts we want to display
for (var i = 0; i < numOfPosts; i++) {
var curPost = data.posts[i];
posts.push({
id: curPost.id,
url: curPost.url,
date: new Date(curPost.date).toLocaleDateString(),
title: curPost.title_plain,
excerpt: curPost.excerpt
});
}

/**
* Display an error message if the news cannot be loaded.
*/
function erroring(err) {
qNewsContainer.innerHTML = "<h3>News could not be loaded at this time.</h3>";
console.error(err);
}
// Cache the news
_clearNewsCache();
_cacheNews(posts);
return posts;
}

/**
* Display the news.
*
* @param {Array} posts - An array containing the news to display.
* @returns {Boolean} Always returns true.
*/
function displayNews(posts) {
posts.forEach(function(post) {
qNewsContainer.insertAdjacentHTML("beforeend", _compilePostHTML(post));
});
return true;
}
/**
* Display an error message if the news cannot be loaded.
*/
function erroring(err) {
qNewsContainer.innerHTML = "<h3>News could not be loaded at this time.</h3>";
console.error(err);
}

/**
* Fetch the latest news.
*
* @param {String} url - The URL to the JSON news feed.
*/
function load(url) {
window.fetch(url, {method: "get"})
.then(r => r.json())
.then(r => displayNews(getPosts(r)))
.catch(err => {
// First try to load the cached news, if possible
var cachedNews = _getNewsCache();
if (cachedNews) {
displayNews(cachedNews);
/**
* Display the news.
*
* @param {Array} posts - An array containing the news to display.
* @returns {Boolean} Always returns true.
*/
function displayNews(posts) {
posts.forEach(function(post) {
qNewsContainer.insertAdjacentHTML("beforeend", _compilePostHTML(post));
});
return true;
}

// No cache available, error
} else {
erroring(err);
}
});
}
/**
* Fetch the latest news.
*
* @param {String} url - The URL to the JSON news feed.
*/
function load(url) {
window.fetch(url, {
method: "get"
})
.then(r => r.json())
.then(r => displayNews(getPosts(r)))
.catch(err => {
// First try to load the cached news, if possible
var cachedNews = _getNewsCache();
if (cachedNews) {
displayNews(cachedNews);

// No cache available, error
} else {
erroring(err);
}
});
}

// Public exports
module.exports.load = load;
// Public exports
module.exports.load = load;
}());

0 comments on commit 842f50b

Please sign in to comment.