Skip to content

Instantly share code, notes, and snippets.

@walemust
Created September 11, 2020 18:49
Show Gist options
  • Save walemust/0d527201fa1de2529bfb3999bfeea0be to your computer and use it in GitHub Desktop.
Save walemust/0d527201fa1de2529bfb3999bfeea0be to your computer and use it in GitHub Desktop.
Fork Me! FCC: Quote Machine
<div id="wrapper">
<div id="quote-box">
<div class="quote-text">
<i class="fa fa-quote-left"> </i><span id="text"></span>
</div>
<div class="quote-author">
- <span id="author"></span>
</div>
<div class="buttons">
<a class="button" id="tweet-quote" title="Tweet this quote!" target="_blank">
<i class="fa fa-twitter"></i>
</a>
<a class="button" id="tumblr-quote" title="Post this quote on tumblr!" target="_blank">
<i class="fa fa-tumblr"></i>
</a>
<button class="button" id="new-quote">New quote</button>
</div>
</div>
<div class="footer"> by <a href="https://codepen.io/hezag/">hezag</a></div>
</div>
<!--
Hello Camper!
For now, the test suite only works in Chrome! Please read the README below in the JS Editor before beginning. Feel free to delete this message once you have read it. Good luck and Happy Coding!
- The freeCodeCamp Team
-->
const projectName = "random-quote-machine";
localStorage.setItem('example_project', 'Randowm Quote Machine');
let quotesData;
/*
Code by Gabriel Nunes
Modified by Todd Chaffee to use Camper gist for JSON Quote data.
*/
function inIframe () { try { return window.self !== window.top; } catch (e) { return true; } }
var colors = ['#16a085', '#27ae60', '#2c3e50', '#f39c12', '#e74c3c', '#9b59b6', '#FB6964', '#342224', "#472E32", "#BDBB99", "#77B1A9", "#73A857"];
var currentQuote = '', currentAuthor = '';
function openURL(url){
window.open(url, 'Share', 'width=550, height=400, toolbar=0, scrollbars=1 ,location=0 ,statusbar=0,menubar=0, resizable=0');
}
function getQuotes() {
return $.ajax({
headers: {
Accept: "application/json"
},
url: 'https://gist.githubusercontent.com/camperbot/5a022b72e96c4c9585c32bf6a75f62d9/raw/e3c6895ce42069f0ee7e991229064f167fe8ccdc/quotes.json',
success: function(jsonQuotes) {
if (typeof jsonQuotes === 'string') {
quotesData = JSON.parse(jsonQuotes);
console.log('quotesData');
console.log(quotesData);
}
}
});
}
function getRandomQuote() {
return quotesData.quotes[Math.floor(Math.random() * quotesData.quotes.length)];
}
function getQuote() {
let randomQuote = getRandomQuote();
currentQuote = randomQuote.quote;
currentAuthor = randomQuote.author;
if(inIframe())
{
$('#tweet-quote').attr('href', 'https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" ' + currentAuthor));
$('#tumblr-quote').attr('href', 'https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption='+encodeURIComponent(currentAuthor)+'&content=' + encodeURIComponent(currentQuote)+'&canonicalUrl=https%3A%2F%2Fwww.tumblr.com%2Fbuttons&shareSource=tumblr_share_button');
}
$(".quote-text").animate(
{ opacity: 0 },
500,
function() {
$(this).animate({ opacity: 1}, 500);
$('#text').text(randomQuote.quote);
}
);
$(".quote-author").animate(
{ opacity: 0 },
500,
function() {
$(this).animate({ opacity: 1}, 500);
$('#author').html(randomQuote.author);
}
);
var color = Math.floor(Math.random() * colors.length);
$("html body").animate(
{
backgroundColor: colors[color],
color: colors[color]
},
1000
);
$(".button").animate(
{
backgroundColor: colors[color]
},
1000
);
}
$(document).ready(function() {
getQuotes().then(() => {
getQuote();
});
$('#new-quote').on('click', getQuote);
$('#tweet-quote').on('click', function() {
if(!inIframe()) {
openURL('https://twitter.com/intent/tweet?hashtags=quotes&related=freecodecamp&text=' + encodeURIComponent('"' + currentQuote + '" ' + currentAuthor));
}
});
$('#tumblr-quote').on('click', function() {
if(!inIframe()) {
openURL('https://www.tumblr.com/widgets/share/tool?posttype=quote&tags=quotes,freecodecamp&caption='+encodeURIComponent(currentAuthor)+'&content=' + encodeURIComponent(currentQuote)+'&canonicalUrl=https%3A%2F%2Fwww.tumblr.com%2Fbuttons&shareSource=tumblr_share_button');
}
});
});
// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place.
/***********
INSTRUCTIONS:
- Select the project you would
like to complete from the dropdown
menu.
- Click the "RUN TESTS" button to
run the tests against the blank
pen.
- Click the "TESTS" button to see
the individual test cases.
(should all be failing at first)
- Start coding! As you fulfill each
test case, you will see them go
from red to green.
- As you start to build out your
project, when tests are failing,
you should get helpful errors
along the way!
************/
// PLEASE NOTE: Adding global style rules using the * selector, or by adding rules to body {..} or html {..}, or to all elements within body or html, i.e. h1 {..}, has the potential to pollute the test suite's CSS. Try adding: * { color: red }, for a quick example!
// Once you have read the above messages, you can delete all comments.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
@import url(https://fonts.googleapis.com/css?family=Raleway:400,500);
* {
margin: 0;
padding: 0;
list-style: none;
vertical-align: baseline;
}
div {
position:relative;
z-index: 2;
}
body {
background-color: #333;
color: #333;
font-family: 'Raleway', sans-serif;
font-weight:400;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.footer {
width:450px;
text-align:center;
display:block;
margin:15px auto 30px auto;
font-size:0.8em;
color:#fff;
a {
font-weight:500;
text-decoration:none;
color:#fff;
}
}
#quote-box {
border-radius:3px;
position:relative;
//margin:8% auto auto auto;
width:450px;
padding:40px 50px;
display:table;
background-color:#fff;
.quote-text {
i{
font-size:1.0em;
margin-right: 0.4em;
}
text-align:center;
width:450px;
height:auto;
clear:both;
font-weight:500;
font-size:1.75em;
}
.quote-author {
width:450px;
height:auto;
clear:both;
padding-top:20px;
font-size:1em;
text-align:right;
}
.buttons {
width:450px;
margin:auto;
display:block;
.button {
height:38px;
border:none;
border-radius:3px;
color:#fff;
background-color:#333;
outline:none;
font-size:0.85em;
padding: 8px 18px 6px 18px;
margin-top:30px;
opacity:1;
cursor:pointer;
&:hover {
opacity:0.9;
}
&#tweet-quote, &#tumblr-quote {
float:left;
padding:0px;
padding-top:8px;
text-align:center;
font-size:1.2em;
margin-right:5px;
height:30px;
width:40px;
}
&#new-quote {
float:right;
}
}
}
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment