Skip to content

Instantly share code, notes, and snippets.

Created November 2, 2015 11:08
Show Gist options
  • Save anonymous/b7f5f03eace4e0392a11 to your computer and use it in GitHub Desktop.
Save anonymous/b7f5f03eace4e0392a11 to your computer and use it in GitHub Desktop.
MovieAdder // source http://jsbin.com/savupe
<!DOCTYPE html>
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0/handlebars.js"></script>
<script src="https://code.jquery.com/jquery-1.11.3.js"></script>
<meta charset="utf-8">
<title>MovieAdder</title>
<style id="jsbin-css">
.module {
margin: 12px;
background: #ccc;
padding: 20px;
border-radius: 7px;
box-sizing: border-box;
text-align: center;
font: 400 14px/1.2 Arial, Verdana;
}
.module h2 {
text-align: center;
padding: 0;
margin: 0;
line-height: 1.5;
}
.module h3 {
margin: 20px 0px 0px;
line-height: 2;
}
.module p {
margin: 0;
}
.form-wrapper input,
.form-wrapper select {
padding: 12px 6px;
width: 100%;
margin: 12px 0 0 0;
box-sizing: border-box;
}
.form-wrapper .primary-btn {
background: #4bbaf7;
border: none;
padding: 12px 17px;
border-radius: 6px;
color: #fff;
margin: 12px 0;
cursor: pointer;
}
#movie-list .entry {
position: relative;
}
.remove {
font-size: 11px;
cursor: pointer;
position: absolute;
top: 12px;
right: 12px;
color: #999;
}
.remove:hover {
color: #555;
}
#movie-activity .entry {
color: #333;
font-size: 11x;
line-height: 1.5;
margin: 5px 0;
}
#movie-list .entry:nth-child(even),
#movie-activity .entry:nth-child(even) {
background: #bbb;
}
</style>
</head>
<body>
<section id="movie-adder" class="module">
<h2>MovieAdder</h2>
<p>Fill in the form and add your movie to the list</p>
<div class="form-wrapper">
<form>
<input type="text" name="title" placeholder="title" required>
<input type="text" name="year" placeholder="year" required>
<input type="text" name="plot" placeholder="plot" required>
<input type="text" name="director" placeholder="director" required>
<select name="genre" required>
<option value="" selected>genre</option>
<option value="action">Action</option>
<option value="biography">Biography</option>
<option value="comedy">Comedy</option>
<option value="crime">Crime</option>
<option value="drama">Drama</option>
<option value="fantasy">Fantasy</option>
<option value="history">History</option>
<option value="horror">Horror</option>
<option value="romance">Romance</option>
<option value="sci-fi">Sci-fi</option>
</select>
<button id="add-movie" class="primary-btn">Add to Database</button>
</form>
</div>
</section>
<section id="movie-list" class="module">
<h2>Movie List</h2>
<p>Here is your private movie list</p>
<div class="list-wrapper"></div>
</section>
<section id="movie-activity" class="module">
<h2>Latest Activity</h2>
<p>Recently on MovieAdder</p>
<div class="list-wrapper"></div>
</section>
<script id="jsbin-javascript">
var movies = [];
var activities = [];
$('#movie-adder').find('form').on('submit', function(event) {
event.preventDefault();
var movie = {
title: $('#movie-adder').find('[name="title"]').val(),
year: $('#movie-adder').find('[name="year"]').val(),
plot: $('#movie-adder').find('[name="plot"]').val(),
director: $('#movie-adder').find('[name="director"]').val(),
genre: $('#movie-adder').find('[name="genre"]').val()
};
var activity = {
title: movie.title,
type: 'add'
};
movies.push(movie);
activities.push(activity);
$('#movie-adder').find('form').trigger("reset");
$('#movie-list').find('.list-wrapper').append('<div class="entry"><h3>' + movie.title + ' ' + '</h3><span>('+ movie.year +')</span><p>Genre: ' + movie.genre + '</p><p>Director: ' + movie.director + '</p><p>Plot: ' + movie.plot + '</p><p><a class="remove">X</a></p></div>');
$('#movie-activity').find('.list-wrapper').append('<p class="entry">User has added <b>' + movie.title + '</b> to the list.</p>');
});
$('#movie-list').find('.list-wrapper').on('click','a.remove', function(event) {
var entry = $(event.target).closest('div.entry');
var index = $('#movie-list').find('.entry').index(entry);
var activity = {
title: movies[index].title,
type: 'remove'
};
activities.push(activity);
$('#movie-activity').find('.list-wrapper').append('<p class="entry">User has removed <b>' + movies[index].title + '</b> from the list.</p>');
entry.remove();
movies.splice(index, 1);
});
</script>
<script id="jsbin-source-css" type="text/css">.module {
margin: 12px;
background: #ccc;
padding: 20px;
border-radius: 7px;
box-sizing: border-box;
text-align: center;
font: 400 14px/1.2 Arial, Verdana;
}
.module h2 {
text-align: center;
padding: 0;
margin: 0;
line-height: 1.5;
}
.module h3 {
margin: 20px 0px 0px;
line-height: 2;
}
.module p {
margin: 0;
}
.form-wrapper input,
.form-wrapper select {
padding: 12px 6px;
width: 100%;
margin: 12px 0 0 0;
box-sizing: border-box;
}
.form-wrapper .primary-btn {
background: #4bbaf7;
border: none;
padding: 12px 17px;
border-radius: 6px;
color: #fff;
margin: 12px 0;
cursor: pointer;
}
#movie-list .entry {
position: relative;
}
.remove {
font-size: 11px;
cursor: pointer;
position: absolute;
top: 12px;
right: 12px;
color: #999;
}
.remove:hover {
color: #555;
}
#movie-activity .entry {
color: #333;
font-size: 11x;
line-height: 1.5;
margin: 5px 0;
}
#movie-list .entry:nth-child(even),
#movie-activity .entry:nth-child(even) {
background: #bbb;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">var movies = [];
var activities = [];
$('#movie-adder').find('form').on('submit', function(event) {
event.preventDefault();
var movie = {
title: $('#movie-adder').find('[name="title"]').val(),
year: $('#movie-adder').find('[name="year"]').val(),
plot: $('#movie-adder').find('[name="plot"]').val(),
director: $('#movie-adder').find('[name="director"]').val(),
genre: $('#movie-adder').find('[name="genre"]').val()
};
var activity = {
title: movie.title,
type: 'add'
};
movies.push(movie);
activities.push(activity);
$('#movie-adder').find('form').trigger("reset");
$('#movie-list').find('.list-wrapper').append('<div class="entry"><h3>' + movie.title + ' ' + '</h3><span>('+ movie.year +')</span><p>Genre: ' + movie.genre + '</p><p>Director: ' + movie.director + '</p><p>Plot: ' + movie.plot + '</p><p><a class="remove">X</a></p></div>');
$('#movie-activity').find('.list-wrapper').append('<p class="entry">User has added <b>' + movie.title + '</b> to the list.</p>');
});
$('#movie-list').find('.list-wrapper').on('click','a.remove', function(event) {
var entry = $(event.target).closest('div.entry');
var index = $('#movie-list').find('.entry').index(entry);
var activity = {
title: movies[index].title,
type: 'remove'
};
activities.push(activity);
$('#movie-activity').find('.list-wrapper').append('<p class="entry">User has removed <b>' + movies[index].title + '</b> from the list.</p>');
entry.remove();
movies.splice(index, 1);
});</script></body>
</html>
.module {
margin: 12px;
background: #ccc;
padding: 20px;
border-radius: 7px;
box-sizing: border-box;
text-align: center;
font: 400 14px/1.2 Arial, Verdana;
}
.module h2 {
text-align: center;
padding: 0;
margin: 0;
line-height: 1.5;
}
.module h3 {
margin: 20px 0px 0px;
line-height: 2;
}
.module p {
margin: 0;
}
.form-wrapper input,
.form-wrapper select {
padding: 12px 6px;
width: 100%;
margin: 12px 0 0 0;
box-sizing: border-box;
}
.form-wrapper .primary-btn {
background: #4bbaf7;
border: none;
padding: 12px 17px;
border-radius: 6px;
color: #fff;
margin: 12px 0;
cursor: pointer;
}
#movie-list .entry {
position: relative;
}
.remove {
font-size: 11px;
cursor: pointer;
position: absolute;
top: 12px;
right: 12px;
color: #999;
}
.remove:hover {
color: #555;
}
#movie-activity .entry {
color: #333;
font-size: 11x;
line-height: 1.5;
margin: 5px 0;
}
#movie-list .entry:nth-child(even),
#movie-activity .entry:nth-child(even) {
background: #bbb;
}
var movies = [];
var activities = [];
$('#movie-adder').find('form').on('submit', function(event) {
event.preventDefault();
var movie = {
title: $('#movie-adder').find('[name="title"]').val(),
year: $('#movie-adder').find('[name="year"]').val(),
plot: $('#movie-adder').find('[name="plot"]').val(),
director: $('#movie-adder').find('[name="director"]').val(),
genre: $('#movie-adder').find('[name="genre"]').val()
};
var activity = {
title: movie.title,
type: 'add'
};
movies.push(movie);
activities.push(activity);
$('#movie-adder').find('form').trigger("reset");
$('#movie-list').find('.list-wrapper').append('<div class="entry"><h3>' + movie.title + ' ' + '</h3><span>('+ movie.year +')</span><p>Genre: ' + movie.genre + '</p><p>Director: ' + movie.director + '</p><p>Plot: ' + movie.plot + '</p><p><a class="remove">X</a></p></div>');
$('#movie-activity').find('.list-wrapper').append('<p class="entry">User has added <b>' + movie.title + '</b> to the list.</p>');
});
$('#movie-list').find('.list-wrapper').on('click','a.remove', function(event) {
var entry = $(event.target).closest('div.entry');
var index = $('#movie-list').find('.entry').index(entry);
var activity = {
title: movies[index].title,
type: 'remove'
};
activities.push(activity);
$('#movie-activity').find('.list-wrapper').append('<p class="entry">User has removed <b>' + movies[index].title + '</b> from the list.</p>');
entry.remove();
movies.splice(index, 1);
});
@pawelqbera
Copy link

This is an example of a simple app with 3 co-working modules written with messy spaghetti jQuery code considered for further refactoring and modularization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment