diff --git a/index.html b/index.html index 3ba0ab0..b8628cf 100755 --- a/index.html +++ b/index.html @@ -1,112 +1,64 @@ - - - - Feedr @ GA - - - - - + + + + Feedr @ GA + + + + + + - + + +
+
+

Feedr

+ +
+
+
- - + -
-
-

Feedr

- -
-
-
- -
-
-
- -
-
-

Test article title

-
Lifestyle
-
-
- 526 -
-
-
-
-
- -
-
-

Test article title

-
Lifestyle
-
-
- 526 -
-
-
-
-
- -
-
-

Test article title

-
Lifestyle
-
-
- 526 -
-
-
-
-
- -
-
-

Test article title

-
Lifestyle
-
-
- 526 -
-
-
-
+
+
+
- - - + + + + - - + \ No newline at end of file diff --git a/js/app.js b/js/app.js index a6502cd..08cdf81 100755 --- a/js/app.js +++ b/js/app.js @@ -1,3 +1,188 @@ /* Please add all Javascript code to this file. + GA JS-SF-8 Ian */ + +const API_KEY_NYT = "49384f9a1a4f4a30803f51d2ff1a26f0"; +const API_KEY_NPI = "fb5d233e64d94275b9021245ac114ed0"; +const ARTICLES_NYT = {}; +const ARTICLES_WPO = {}; +const ARTICLES_BB = {}; + +function nytSaveData(array) { + for (var article in array) { + var key = array[article].slug_name; + ARTICLES_NYT[key] = array[article]; + } +} + +function npiSaveData(array, source) { + for (var article in array) { + var key = array[article].publishedAt; + source[key] = array[article]; + } +} + +function initSearch() { + $("#search").on("click", function() { + $(this).toggleClass("active"); + }); +} + +function initPopup() { + $(".closePopUp").on("click", function() { + $("#popUp").addClass("hidden"); + }); +} + +function initPopupLinks(source) { + $("article a").on("click", function(event) { + event.preventDefault(); + showPopup($(this).attr("href"), source); + }); +} + +function showLoaderMain() { + $("#main").html("
"); +} + +function initHome() { + $("#home").on("click", function(event) { + event.preventDefault(); + showLoaderMain(); + getNYT(); + }); +} + +function initNYT() { + $("[data-filter=nyt]").on("click", function() { + showLoaderMain(); + getNYT(); + }); +} + +function initWPO() { + $("[data-filter=wpo]").on("click", function() { + showLoaderMain(); + getWPO(); + }); +} + +function initBB() { + $("[data-filter=bb]").on("click", function() { + showLoaderMain(); + getBB(); + }); +} + +function initPage() { + initSearch(); + initPopup(); + initHome(); + initNYT(); + initWPO(); + initBB(); +} + +function showPopup(slug, source) { + var article = source[slug]; + $("#popUp").removeClass("loader hidden"); + $("#popUp h1").html(article.title); + if (source == ARTICLES_NYT) { + $("#popUp p").html(article.abstract); + } else { + $("#popUp p").html(article.description); + } + $("#popUp .popUpAction").attr("href", article.url); +} + +function nytDisplayArticles(articles) { + var html = `${articles.map(article => + `
+
+ +
+
+

${article.title}

+
${article.section}
+
+
+
` + ).join("")}`; + $("#main").empty().append(html); + initPopupLinks(ARTICLES_NYT); +} + +function npiDisplayArticles(articles, source) { + var html = `${articles.map(article => + `
+
+ +
+
+

${article.title}

+
+
+
+
` + ).join("")}`; + $("#main").empty().append(html); + initPopupLinks(source); +} + +function getNYT(numArticles) { + $.ajax({ + async: true, + crossDomain: true, + url: `http://api.nytimes.com/svc/news/v3/content/all/all.json?limit=${numArticles}&api-key=${API_KEY_NYT}`, + method: "GET", + // success: function(msg) {}, + error: function(msg) { + alert("Oops. That did not go as planned. Refresh the page and try again?"); + }, + // complete: function(msg) {} + }).done(function (response) { + var articles = response.results; + nytSaveData(articles); + nytDisplayArticles(articles); + }); +} + +function getWPO() { + $.ajax({ + async: true, + crossDomain: true, + url: `https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=the-washington-post&apiKey=${API_KEY_NPI}`, + method: "GET", + // success: function(msg) {}, + error: function(msg) { + alert("Oops. That did not go as planned. Refresh the page and try again?"); + }, + // complete: function(msg) {} + }).done(function (response) { + var articles = response.articles; + npiSaveData(articles, ARTICLES_WPO); + npiDisplayArticles(articles, ARTICLES_WPO); + }); +} + +function getBB() { + $.ajax({ + async: true, + crossDomain: true, + url: `https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=bloomberg&apiKey=${API_KEY_NPI}`, + method: "GET", + // success: function(msg) {}, + error: function(msg) { + alert("Oops. That did not go as planned. Refresh the page and try again?"); + }, + // complete: function(msg) {} + }).done(function (response) { + var articles = response.articles; + npiSaveData(articles, ARTICLES_BB); + npiDisplayArticles(articles, ARTICLES_BB); + }); +} + +initPage(); +getNYT(); diff --git a/styles/style.css b/styles/style.css index fcfbf38..e4b865e 100755 --- a/styles/style.css +++ b/styles/style.css @@ -4,6 +4,15 @@ margin: 0 auto; } +.loader { + width: 100%; + height: 100vh; + max-height: 100%; + background-image: url(../images/ajax_loader.gif); + background-repeat: no-repeat; + background-position: center center; +} + #popUp { opacity: 0.95; position: absolute; @@ -23,12 +32,6 @@ text-decoration: none; } -#popUp.loader { - background-image: url(../images/ajax_loader.gif); - background-repeat: no-repeat; - background-position: center center; -} - #popUp.loader .container { display: none; }