Skip to content

Instantly share code, notes, and snippets.

@tdouce
Created September 22, 2011 14:02
Show Gist options
  • Save tdouce/1234841 to your computer and use it in GitHub Desktop.
Save tdouce/1234841 to your computer and use it in GitHub Desktop.
amware_javascript_warnings - final #3
$(".commit").click( function(e){
var warning = '<p class="inline-errors"> Warning, this is blank </p>'
var all_selections = [];
var not_filled_out = [];
$( '.check_value' ).children().each( function(){
var li = $(this);
var li_value = li.val();
var li_div_id = li.parent().parent().parent().parent().attr('id');
all_selections.push( li_div_id );
if ( _.include( not_filled_out, li_div_id ) == false & li_value == '' )
{
not_filled_out.push( li_div_id );
}
$( '#' + li_div_id ).children("p").remove();
});
all_selections = _.uniq(all_selections);
var filled_out = _.difference( all_selections, not_filled_out );
var max_index_filled_out = _(filled_out).chain().map( function(filled) { return _.indexOf( all_selections, filled) }).max().value();
var not_filled_out_indexes = _.map( not_filled_out, function(not_filled) { return _.indexOf( all_selections, not_filled) });
var zipped_not_filled_out = _.zip( not_filled_out, not_filled_out_indexes)
var invoke_confirm_box = [];
for ( zip in zipped_not_filled_out )
{
if ( zipped_not_filled_out[zip][1] < max_index_filled_out )
{
invoke_confirm_box.push( zipped_not_filled_out[zip][0] )
$('#' + zipped_not_filled_out[zip][0] ).append( warning );
}
};
if ( invoke_confirm_box.length > 0 )
{
var continue_popup = confirm("Some date fields are not filled out. 'OK' to submit anyway and 'Cancel' to enter more information.");
if ( continue_popup != true )
{
e.preventDefault();
}
};
});
$(document).ready(function(){
$(".commit").click( function(e){
var warning = 'Warning, this is blank';
var warning_html = '<p class="inline-errors">' + warning + '</p>';
var all_selections = [];
var not_filled_out = [];
// Loop through each element with the specified class and 1) put
// the parent <id> in an array; 2) Put the the parent <id> in an array if
// the datetime selection doesn't have a value and the parent <id> isn't
// already in the array; 3) Remove any previous warning ONLY if they match
// the warning for this check (i.e. var warning)
$( '.check_value' ).children().each( function(){
var li = $(this);
var li_value = li.val();
var li_div_id = li.parent().parent().parent().parent().attr('id');
all_selections.push( li_div_id );
if ( _.include( not_filled_out, li_div_id ) == false & li_value == '' )
{
not_filled_out.push( li_div_id );
}
// Remove warning only if it exactly matches the warning in our variable.
// In-case we happend to add some other validation down the road, we want
// to make sure we only remove the one associated with this validation.
$( '#' + li_div_id ).children("p").each( function(){
if( $(this).text() == warning )
{
$( '#' + li_div_id ).children("p").remove();
};
});
});
all_selections = _.uniq(all_selections);
var filled_out = _.difference( all_selections, not_filled_out );
var max_index_filled_out = _(filled_out).chain().map( function(filled) { return _.indexOf( all_selections, filled) }).max().value();
var not_filled_out_indexes = _.map( not_filled_out, function(not_filled) { return _.indexOf( all_selections, not_filled) });
var zipped_not_filled_out = _.zip( not_filled_out, not_filled_out_indexes);
var invoke_confirm_box = [];
// Loop through the zipped not filled out index and their ids. If their index
// is less than the maximum index that was filled out, then append a warning message to it
// and push the id to the invovke_confirm_box array
_.each( zipped_not_filled_out, function( zipped ){
if (zipped[1] < max_index_filled_out)
{
invoke_confirm_box.push( zipped[0] )
$('#' + zipped[0] ).append( warning_html );
};
});
// If there were any selections that need to be filled out, the stop
// the form submission and display the confirm box
if ( invoke_confirm_box.length > 0 )
{
var continue_popup = confirm("Some date fields are not filled out. 'OK' to submit anyway and 'Cancel' to enter more information.");
if ( continue_popup != true )
{
e.preventDefault();
}
};
});
// Puts a blank value in all the associated datetime selections if the 'clear' button is clicked.
$('.clear').live('click', function() {
$(this).closest('ol').find('select').val("");
});
// When the now button is clicked, fill out all the datetime selections and round the
// minute to the nearest five minutes
$('.now').live('click', function() {
var round_to_nearest = 5;
var all_selects = $('select');
var selects_per_li = $( this ).closest('ol').find( all_selects );
var now = new Date();
var year = now.getFullYear();
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getHours();
var minutes = now.getMinutes();
// Round to the nearest 5 minutes. if minute less than 10 then prepend a '0' to it
if ( minutes < 5 )
{
minutes = '0' + Math.round( minutes / round_to_nearest )* round_to_nearest;
}
else
{
minutes = Math.round( minutes / round_to_nearest )*round_to_nearest;
}
if ( hour < 10 )
{
hour = '0' + hour;
}
var all_possible_date_times = [ year, month, day, hour, minutes ];
var selects_per_li_length = selects_per_li.length;
// Fill out the date time selections
_( all_possible_date_times ).chain()
.select( function( item ){ return _.indexOf( all_possible_date_times, item ) < selects_per_li_length })
.zip( selects_per_li )
.value()
.map( function(zipped){ $(zipped[1]).val( zipped[0] ) } );
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment