Last active
February 14, 2019 20:34
-
-
Save jmmarco/e59228d890a93a83bee016922b6d6100 to your computer and use it in GitHub Desktop.
Using jQuery to filter movies for Udacity's fresh_tomatoes module: https://github.com/udacity/ud036_StarterCode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>Fresh Tomatoes!</title> | |
<!-- Bootstrap 3 --> | |
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css"> | |
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap-theme.min.css"> | |
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> | |
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.1.0/js/bootstrap.min.js"></script> | |
<style type="text/css" media="screen"> | |
body { | |
padding-top: 80px; | |
} | |
#trailer .modal-dialog { | |
margin-top: 200px; | |
width: 640px; | |
height: 480px; | |
} | |
.hanging-close { | |
position: absolute; | |
top: -12px; | |
right: -12px; | |
z-index: 9001; | |
} | |
#trailer-video { | |
width: 100%; | |
height: 100%; | |
} | |
.movie-tile { | |
margin-bottom: 20px; | |
padding-top: 20px; | |
} | |
.movie-tile:hover { | |
background-color: #EEE; | |
cursor: pointer; | |
} | |
.scale-media { | |
padding-bottom: 56.25%; | |
position: relative; | |
} | |
.scale-media iframe { | |
border: none; | |
height: 100%; | |
position: absolute; | |
width: 100%; | |
left: 0; | |
top: 0; | |
background-color: white; | |
} | |
</style> | |
<script type="text/javascript" charset="utf-8"> | |
// Pause the video when the modal is closed | |
$(document).on('click', '.hanging-close, .modal-backdrop, .modal', function(event) { | |
// Remove the src so the player itself gets removed, as this is the only | |
// reliable way to ensure the video stops playing in IE | |
$("#trailer-video-container").empty(); | |
}); | |
// Start playing the video whenever the trailer modal is opened | |
$(document).on('click', '.movie-tile', function(event) { | |
var trailerYouTubeId = $(this).attr('data-trailer-youtube-id') | |
var sourceUrl = 'http://www.youtube.com/embed/' + trailerYouTubeId + '?autoplay=1&html5=1'; | |
$("#trailer-video-container").empty().append($("<iframe></iframe>", { | |
'id': 'trailer-video', | |
'type': 'text-html', | |
'src': sourceUrl, | |
'frameborder': 0 | |
})); | |
}); | |
// Animate in the movies when the page loads | |
$(document).ready(function() { | |
$('.movie-tile').hide().first().show("fast", function showNext() { | |
$(this).next("div").show("fast", showNext); | |
}); | |
}); | |
</script> | |
</head> | |
<body> | |
<!-- Trailer Video Modal --> | |
<div class="modal" id="trailer"> | |
<div class="modal-dialog"> | |
<div class="modal-content"> | |
<a href="#" class="hanging-close" data-dismiss="modal" aria-hidden="true"> | |
<img src="https://lh5.ggpht.com/v4-628SilF0HtHuHdu5EzxD7WRqOrrTIDi_MhEG6_qkNtUK5Wg7KPkofp_VJoF7RS2LhxwEFCO1ICHZlc-o_=s0#w=24&h=24"/> | |
</a> | |
<div class="scale-media" id="trailer-video-container"> | |
</div> | |
</div> | |
</div> | |
</div> | |
<!-- Main Page Content --> | |
<div class="container"> | |
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> | |
<div class="container"> | |
<div class="navbar-header"> | |
<a class="navbar-brand" href="#">Fresh Tomatoes Movie Trailers</a> | |
</div> | |
<form id="form" class="navbar-form navbar-right"> | |
<div class="form-group"> | |
<input id="search" type="text" class="form-control" placeholder="Search"> | |
</div> | |
<button id="submit" type="submit" class="btn btn-default">Submit</button> | |
<button id="reset" type="reset" class="btn btn-default">Reset</button> | |
</form> | |
</div> | |
</div> | |
</div> | |
<div id="movie-container" class="container"> | |
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="89Kq8SDyvfg" data-toggle="modal" data-target="#trailer"> | |
<img src="https://upload.wikimedia.org/wikipedia/en/8/81/Poster-pursuithappyness.jpg" width="220" height="342"> | |
<h2>The Pursuit of Happyness</h2> | |
</div> | |
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="JfbxcD6biOk" data-toggle="modal" data-target="#trailer"> | |
<img src="https://upload.wikimedia.org/wikipedia/en/0/07/Gully_Boy_poster.jpg" width="220" height="342"> | |
<h2>Gully Boy</h2> | |
</div> | |
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="VVY3do673Zc" data-toggle="modal" data-target="#trailer"> | |
<img src="https://upload.wikimedia.org/wikipedia/en/3/3b/URI_-_New_poster.jpg" width="220" height="342"> | |
<h2>URI</h2> | |
</div> | |
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="coOKvrsmQiI" data-toggle="modal" data-target="#trailer"> | |
<img src="https://upload.wikimedia.org/wikipedia/en/6/6b/Rampage_teaser_film_poster.jpg" width="220" height="342"> | |
<h2>Rampage</h2> | |
</div> | |
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="pKcamCgBvMo" data-toggle="modal" data-target="#trailer"> | |
<img src="https://upload.wikimedia.org/wikipedia/en/4/4f/Ek_Ladki_Ko_Dekha_Toh_Aisa_Laga_poster.jpg" width="220" height="342"> | |
<h2>Ek Ladki Ko Dekha To Aisa Laga</h2> | |
</div> | |
<div class="col-md-6 col-lg-4 movie-tile text-center" data-trailer-youtube-id="fo9EhcwQXcM" data-toggle="modal" data-target="#trailer"> | |
<img src="https://upload.wikimedia.org/wikipedia/en/e/e7/Total_Dhamaal_poster.jpg" width="220" height="342"> | |
<h2>Total Dhamaal</h2> | |
</div> | |
</div> | |
<script> | |
let movies = $('.movie-tile'); | |
// console.log(movies); | |
$('#form').on('reset', function(evt) { | |
evt.preventDefault() | |
$('#movie-container').empty().append(movies); | |
}) | |
$('#form').on('submit', function(evt) { | |
evt.preventDefault(); | |
let query = $('#search').val(); | |
let result = search(query); | |
displayResults(result); | |
}) | |
function search(string) { | |
let result; | |
movies.each(function(index, movie) { | |
if (string.toLowerCase() === movie.children[1].innerText.toLowerCase()) { | |
console.log('found match', movie) | |
result = movie | |
} | |
}) | |
return result | |
} | |
function displayResults(result) { | |
if (result) { | |
$('#movie-container').empty().append(result); | |
} else { | |
$('#movie-container').empty().append('No results for that query'); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment