From cf08ed90b3d3c35b21d5b19610ae12d5e3eeb69b Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Mon, 23 Oct 2017 19:30:34 -0700 Subject: [PATCH 1/3] Added my name --- js/app.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/js/app.js b/js/app.js index a6502cd..c9b7208 100755 --- a/js/app.js +++ b/js/app.js @@ -1,3 +1,6 @@ +// GA JS-SF-8 Matt Jones + + /* Please add all Javascript code to this file. */ From f733a5ba71fd4975daa6f626353d659b857d6030 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Mon, 30 Oct 2017 17:39:15 -0700 Subject: [PATCH 2/3] Project v1.0 on 10.30 --- index.html | 13 ++-- js/app.js | 206 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 212 insertions(+), 7 deletions(-) diff --git a/index.html b/index.html index 3ba0ab0..a95459f 100755 --- a/index.html +++ b/index.html @@ -20,11 +20,11 @@
-

Feedr

+

Feedr

+ + + + +
diff --git a/js/app.js b/js/app.js index c9b7208..04a82e8 100755 --- a/js/app.js +++ b/js/app.js @@ -1,6 +1,206 @@ // GA JS-SF-8 Matt Jones +// LOADING THE PAGE +// While the page is loading... +$(window).load(function () { + // show the #popUp and hide the .closePopUp button + $('#popUp').show().removeClass('hidden'); + $('.closePopUp').hide(); -/* - Please add all Javascript code to this file. -*/ + //Create an empty array for all the articles + let newsArray = []; + + // Create a function to churn out articles from the array + function articleCreation(array) { + // Clear whatever was in #main + $('#main').html(''); + // For every article in the array passed through, make the article html + for (let i = 0; i < array.length; i++) { + let newArticle = $(` + + `); + // Add that html to #main + $('#main').append(newArticle); + + // ARTICLE PREVIEW UI + // Show #popUp when you click on an a element of one of the articles + $(newArticle).on('click', function() { + $('#popUp').show().removeClass('loader hidden'); + $('.closePopUp').show(); + // Replace the information in #popUp with the information from each article + $('#popUp .container h1').text(array[i].title) + $('#popUp .container p').text(array[i].description); + $('#popUp .container a').attr("href", array[i].url); + // Change the text of the "Read more" button based on the news source + if (array[i].url.startsWith("http://www.breitbart.com")) { + $('#popUp .container a').text("Read more from Breitbart"); + } else if (array[i].url.startsWith("https://www.buzzfeed.com")) { + $('#popUp .container a').text("Read more from Buzzfeed"); + } else { + $('#popUp .container a').text("Read more from The New York Times"); + }; + }); + + // Hide #popUp when you click on .closePopUp + $('.closePopUp').on('click', function() { + $('#popUp').hide(); + }); + }; + }; + + // Create a function to sort the newsArray chronologically + function sortedArray(response) { + if (newsArray.length === 30) { + newsArray.sort(function(a,b) { + if (a.publishedAt > b.publishedAt) { + return -1; + } else { + return 1; + }; + }); + console.log(newsArray); + // Run the above articleCreation function with the articles in the correct order + articleCreation(newsArray); + $('#popUp').hide(); + }; + }; + + // Create a function to filter the page by news source + function filterPage(x,y) { + let filteredArray = newsArray.filter(function(a) { + return a.url.startsWith(x); + }) + $('#main').html(''); + articleCreation(filteredArray); + $('span').text(y); + } + + // Create a function to search the page based on user input + function searchPage(string) { + let searchedArray = newsArray.filter(function(a) { + return a.title.includes(string); + }) + $('#main').html(''); + articleCreation(searchedArray); + } + + // When finished loading, get rid of the old #main example articles + $('#main').html(''); + + // GET REQUESTS + // Get request to Breitbart News + $.get('https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=breitbart-news&sortBy=top&apiKey=0727657a13f44aa09df55289aef50bc6', function(response) { + if (response.status === "ok") { + console.log(response); + $.merge(newsArray, response.articles); + sortedArray(response); + } else { + alert("Oh no! You cannot access data from Breitbart") + }; + + }); + + // Get request to Buzzfeed + $.get('https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=buzzfeed&sortBy=top&apiKey=0727657a13f44aa09df55289aef50bc6', function(response) { + if (response.status === "ok") { + console.log(response); + $.merge(newsArray, response.articles); + sortedArray(response); + } else { + alert("Oh no! You cannot access data from Buzzfeed") + }; + }); + + // Get request to The New York Times + $.get('https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=the-new-york-times&sortBy=top&apiKey=0727657a13f44aa09df55289aef50bc6', function(response) { + if (response.status === "ok") { + console.log(response); + $.merge(newsArray, response.articles); + sortedArray(response); + } else { + alert("Oh no! You cannot access data from The New York Times") + }; + }); + + + // SOURCES DROP DOWN UI + // Change the text in the drop down to the three news sources + $('.otherList li:first-child a').text("Breitbart News"); + $('.otherList li:nth-child(2) a').text("Buzzfeed"); + $('.otherList li:nth-child(3) a').text("The New York Times"); + + // Run the filterPage function when the user clicks on the news sources + $('.otherList li:first-child a').on('click', function() { + filterPage("http://www.breitbart.com", "Breitbart"); + + }); + + $('.otherList li:nth-child(2) a').on('click', function() { + filterPage("https://www.buzzfeed.com", "Buzzfeed"); + }); + + $('.otherList li:nth-child(3) a').on('click', function() { + filterPage("https://www.nytimes.com", "The New York Times"); + }); + + + // Show all articles from all the sources in #main when the user clicks on #home + $('#home').on('click', function() { + articleCreation(newsArray); + $('span').text("All"); + }); + + + + // SEARCH BUTTON UI + // When the user clicks on the search icon (#search a) + $('#search a').on('click', function() { + // Show/toggle the search input field + $('#search').toggleClass('active'); + // Hide/toggle the sources dropdown menu + $('.bigList').toggle(); + // Clear the input field + $('#search input').val(''); + // Create a boolean variable for below + let $currentClass = $('#search').hasClass('active'); + console.log($currentClass); + + // If the search field is .active + if ($currentClass) { + // When the user presses Enter + $(document).keypress(function(e) { + if (e.which === 13) { + e.preventDefault(); + // Hide the search input field + $('#search').removeClass('active'); + // Show the sources dropdown menu + $('.bigList').show(); + }; + }); + }; + + // Run the searchPage function when the user presses any key + $(document).keypress(function() { + let userInput = $('input').val(); + searchPage(userInput); + }); + }); + + +// Only show the first 20 articles + +// Add infinite scrolling + +}) \ No newline at end of file From f442afca3c9dbe691edc801c2cea3016740bf0d7 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Mon, 30 Oct 2017 18:36:56 -0700 Subject: [PATCH 3/3] Took out API key --- js/app.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/js/app.js b/js/app.js index 04a82e8..5264ccc 100755 --- a/js/app.js +++ b/js/app.js @@ -10,6 +10,8 @@ $(window).load(function () { //Create an empty array for all the articles let newsArray = []; + let apiKey = "xxx"; + // Create a function to churn out articles from the array function articleCreation(array) { // Clear whatever was in #main @@ -101,7 +103,7 @@ $(window).load(function () { // GET REQUESTS // Get request to Breitbart News - $.get('https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=breitbart-news&sortBy=top&apiKey=0727657a13f44aa09df55289aef50bc6', function(response) { + $.get(`https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=breitbart-news&sortBy=top&apiKey=${apiKey}`, function(response) { if (response.status === "ok") { console.log(response); $.merge(newsArray, response.articles); @@ -113,7 +115,7 @@ $(window).load(function () { }); // Get request to Buzzfeed - $.get('https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=buzzfeed&sortBy=top&apiKey=0727657a13f44aa09df55289aef50bc6', function(response) { + $.get(`https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=buzzfeed&sortBy=top&apiKey=${apiKey}`, function(response) { if (response.status === "ok") { console.log(response); $.merge(newsArray, response.articles); @@ -124,7 +126,7 @@ $(window).load(function () { }); // Get request to The New York Times - $.get('https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=the-new-york-times&sortBy=top&apiKey=0727657a13f44aa09df55289aef50bc6', function(response) { + $.get(`https://accesscontrolalloworiginall.herokuapp.com/https://newsapi.org/v1/articles?source=the-new-york-times&sortBy=top&apiKey=${apiKey}`, function(response) { if (response.status === "ok") { console.log(response); $.merge(newsArray, response.articles);