$(function(){
form = $('#');
input = form.find('#deselected_ids');
list = $('#relation-list');
var checkboxes = list.find( ".checkbox-item" ).not( ":disabled" );
var checkboxAll = list.find( ".checkbox-all" );
var screen = $('#screen');
var has_one = ;
if( checkboxes.length && !has_one ){
if( checkboxes.length === checkboxes.filter( ":checked" ).length ){
checkboxAll.prop( "checked", true );
}else{
checkboxAll.prop( "checked", false );
}
}
else{
checkboxAll.prop( "disabled", true );
}
checkboxes.on( "change", function(e){
var $this = $( this );
var checked = $this.prop( "checked" );
if( !has_one ){
if( checkboxes.length === checkboxes.filter( ":checked" ).length ){
checkboxAll.prop( "checked", true );
}else{
checkboxAll.prop( "checked", false );
}
}
if( checked ){
$this.closest( "tr" ).addClass( "selected" );
}else{
$this.closest( "tr" ).removeClass( "selected" );
}
if( !checked ){
input.val( $this.val() );
}
screen.css( "display", "block" );
form.submit();
})
checkboxAll.on( "change", function(e){
var $this = $( this );
var checked = $this.prop( "checked" );
checkboxes.prop( "checked", checked );
if( !checked ){
var ids = [];
checkboxes.each(function(rowIndex){
ids.push($(this).val());
});
ids = ids.join(", ");
input.val( ids );
}
screen.css( "display", "block" );
form.submit();
});
list.on( "click", "td", function( e ) {
if( e.target === this || /LI|DIV|STRONG/.test( e.target.nodeName ) ){
$( this ).closest( "tr" ).find( ".checkbox-item" ).not( ":disabled" ).prop( "checked", function( i, val ){
return !val;
}).trigger( "change" );
}
});
});