Skip to content

Commit

Permalink
implement input checking and submission as described in #137 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
benlk committed Jun 5, 2020
1 parent 48616b8 commit 8bdbbfe
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion wp-content/themes/currentorg/js/current-ltw-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,56 @@ $(document).ready(function(){
});

$('#tax_input\\[project-org-type\\]\\[\\]').on('change', function( event ) {
console.log( event );
$(this).closest('form').find('\[type=submit\]').click();
});

// this keeps track of the array of events
var $checkboxes = $('details.project-category input');
var checkboxValues = Array();
var initialCheckboxValues = Array();

function maybeToggleDisabledInputs() {
if ( checkboxValues.length >= 3 ) {
// disable unchecked checkboxes
$checkboxes.filter( ':not(:checked)' ).prop( 'disabled', true );
} else {
$checkboxes.prop( 'disabled', false );
}
}

// setup
$checkboxes.each( function( index ) {
if ( this.checked ) {
checkboxValues.push( this.value );
initialCheckboxValues.push( this.value );
}
});
maybeToggleDisabledInputs();

$checkboxes.on('change', function( event ) {
if ( event.target.checked ) {
// add it to the array and perform cleanup
checkboxValues.push( event.target.value );
maybeToggleDisabledInputs();
} else {
// remove it from the array and perform cleanup
let index = checkboxValues.indexOf( event.target.value );
if ( index > -1 ) {
checkboxValues.splice( index, 1 );
}
maybeToggleDisabledInputs();
}
});

$('details.project-category').on('toggle', function( event ) {
console.log( event, checkboxValues, initialCheckboxValues );
if ( ! this.open ) {
console.log( checkboxValues.toString(), initialCheckboxValues.toString(), checkboxValues.sort().toString() != initialCheckboxValues.sort().toString() );
if ( checkboxValues.sort().toString() != initialCheckboxValues.sort().toString() ) {
$(this).closest('form').find('\[type=submit\]').click();
}
}
});
});

/**
Expand Down

0 comments on commit 8bdbbfe

Please sign in to comment.