| 1 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 |
// Checkbox Selection functions for Listing |
| 3 |
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 4 |
/** |
| 5 |
* Usual DOM Listing structure: |
| 6 |
<div class="wpbc_listing_container wpbc_selectable_table wpbc_NAME_listing_container"> |
| 7 |
<div class="wpbc_listing_usual_row wpbc_list_header wpbc_selectable_head"> |
| 8 |
<div class="wpbc_listing_col wpbc_col_id check-column"><div class="content_text"><input type="checkbox" /></div></div> |
| 9 |
<div class="wpbc_listing_col wpbc_col_labels"><div class="content_text"><?php echo esc_js( __( 'Actions', 'email-reminders' ) ); ?></div></div> |
| 10 |
<div class="wpbc_listing_col wpbc_col_data"><div class="content_text"><?php echo esc_js( __( 'Data', 'email-reminders' ) ); ?></div></div> |
| 11 |
</div> |
| 12 |
<div id="row_id_{{{data.rules_id}}}" class="wpbc_listing_usual_row wpbc_list_row wpbc_row"> |
| 13 |
<div class="wpbc_listing_col wpbc_col_id check-column"><div class="content_text"><input type="checkbox" /></div></div> |
| 14 |
<div class="wpbc_listing_col wpbc_col_labels"> |
| 15 |
<div class="content_text"><span class="wpbc_label"><?php _e('Email', 'email-reminders'); ?>: {{{data['rule']['email_template']}}}</span></div> |
| 16 |
</div> |
| 17 |
... |
| 18 |
</div> |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* Selections of several checkboxes like in gMail with shift :) |
| 23 |
* Need to have this structure: |
| 24 |
* .wpbc_selectable_table |
| 25 |
* .wpbc_selectable_head |
| 26 |
* .check-column |
| 27 |
* :checkbox |
| 28 |
* .wpbc_selectable_body |
| 29 |
* .wpbc_row |
| 30 |
* .check-column |
| 31 |
* :checkbox |
| 32 |
* .wpbc_selectable_foot |
| 33 |
* .check-column |
| 34 |
* :checkbox |
| 35 |
*/ |
| 36 |
function wpbc_define_gmail_checkbox_selection( $ ){ |
| 37 |
|
| 38 |
var checks, first, last, checked, sliced, lastClicked = false; |
| 39 |
|
| 40 |
// Check all checkboxes |
| 41 |
$('.wpbc_selectable_body').find('.check-column').find(':checkbox').on( 'click', function(e) { |
| 42 |
//$('.wpbc_selectable_body').children().children('.check-column').find(':checkbox').on( 'click', function(e) { |
| 43 |
if ( 'undefined' == e.shiftKey ) { return true; } |
| 44 |
if ( e.shiftKey ) { |
| 45 |
if ( !lastClicked ) { return true; } |
| 46 |
//checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' ).filter( ':visible:enabled' ); |
| 47 |
checks = $( lastClicked ).closest( '.wpbc_selectable_body' ).find( ':checkbox' ).filter( ':visible:enabled' ); |
| 48 |
first = checks.index( lastClicked ); |
| 49 |
last = checks.index( this ); |
| 50 |
checked = $(this).prop('checked'); |
| 51 |
if ( 0 < first && 0 < last && first != last ) { |
| 52 |
sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first ); |
| 53 |
sliced.prop( 'checked', function() { |
| 54 |
if ( $(this).closest('.wpbc_row').is(':visible') ) |
| 55 |
return checked; |
| 56 |
|
| 57 |
return false; |
| 58 |
} ).trigger( 'change' ); |
| 59 |
} |
| 60 |
} |
| 61 |
lastClicked = this; |
| 62 |
|
| 63 |
// toggle "check all" checkboxes |
| 64 |
var unchecked = $(this).closest('.wpbc_selectable_body').find(':checkbox').filter(':visible:enabled').not(':checked'); |
| 65 |
$(this).closest('.wpbc_selectable_table').children('.wpbc_selectable_head, .wpbc_selectable_foot').find(':checkbox').prop('checked', function() { |
| 66 |
return ( 0 === unchecked.length ); |
| 67 |
}).trigger( 'change' ); |
| 68 |
|
| 69 |
return true; |
| 70 |
}); |
| 71 |
|
| 72 |
// Head || Foot clicking to select / deselect ALL |
| 73 |
$('.wpbc_selectable_head, .wpbc_selectable_foot').find('.check-column :checkbox').on( 'click', function( event ) { |
| 74 |
var $this = $(this), |
| 75 |
$table = $this.closest( '.wpbc_selectable_table' ), |
| 76 |
controlChecked = $this.prop('checked'), |
| 77 |
toggle = event.shiftKey || $this.data('wp-toggle'); |
| 78 |
|
| 79 |
$table.children( '.wpbc_selectable_body' ).filter(':visible') |
| 80 |
.find('.check-column').find(':checkbox') |
| 81 |
//.children().children('.check-column').find(':checkbox') |
| 82 |
.prop('checked', function() { |
| 83 |
if ( $(this).is(':hidden,:disabled') ) { |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
if ( toggle ) { |
| 88 |
return ! $(this).prop( 'checked' ); |
| 89 |
} else if ( controlChecked ) { |
| 90 |
return true; |
| 91 |
} |
| 92 |
|
| 93 |
return false; |
| 94 |
}).trigger( 'change' ); |
| 95 |
|
| 96 |
$table.children('.wpbc_selectable_head, .wpbc_selectable_foot').filter(':visible') |
| 97 |
.find('.check-column').find(':checkbox') |
| 98 |
//.children().children('.check-column').find(':checkbox') |
| 99 |
.prop('checked', function() { |
| 100 |
if ( toggle ) { |
| 101 |
return false; |
| 102 |
} else if ( controlChecked ) { |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
return false; |
| 107 |
}); |
| 108 |
}); |
| 109 |
|
| 110 |
|
| 111 |
// Visually show selected border |
| 112 |
$( '.wpbc_selectable_body' ).find( '.check-column :checkbox' ).on( 'change', function ( event ){ |
| 113 |
if ( jQuery( this ).is( ':checked' ) ){ |
| 114 |
jQuery( this ).closest( '.wpbc_list_row' ).addClass( 'row_selected_color' ); |
| 115 |
} else { |
| 116 |
jQuery( this ).closest( '.wpbc_list_row' ).removeClass( 'row_selected_color' ); |
| 117 |
} |
| 118 |
|
| 119 |
// Disable text selection while pressing 'shift' |
| 120 |
document.getSelection().removeAllRanges(); |
| 121 |
|
| 122 |
// Show or hide buttons on Actions toolbar at Booking Listing page, if we have some selected bookings. |
| 123 |
wpbc_show_hide_action_buttons_for_selected_bookings(); |
| 124 |
} ); |
| 125 |
|
| 126 |
wpbc_show_hide_action_buttons_for_selected_bookings(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get ID of row, based on clciked element |
| 131 |
* |
| 132 |
* @param this_inbound_element - ususlly this |
| 133 |
* @returns {number} |
| 134 |
*/ |
| 135 |
function wpbc_get_row_id_from_element( this_inbound_element ){ |
| 136 |
|
| 137 |
var element_id = jQuery( this_inbound_element ).closest('.wpbc_listing_usual_row').attr('id'); |
| 138 |
|
| 139 |
element_id = parseInt( element_id.replace( 'row_id_', '' ) ); |
| 140 |
|
| 141 |
return element_id; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Get ID array of selected elements |
| 146 |
*/ |
| 147 |
function wpbc_get_selected_row_id(){ |
| 148 |
|
| 149 |
var $table = jQuery( '.wpbc_listing_container.wpbc_selectable_table'); |
| 150 |
|
| 151 |
var checkboxes = $table.children( '.wpbc_selectable_body' ).filter( ':visible' ).find( '.check-column' ).find( ':checkbox' ); |
| 152 |
|
| 153 |
var selected_id = []; |
| 154 |
jQuery.each( checkboxes, function( key, checkbox ) { |
| 155 |
|
| 156 |
if ( jQuery( checkbox ).is( ':checked' ) ) { |
| 157 |
var element_id = wpbc_get_row_id_from_element( checkbox ); // jQuery( checkbox ).closest('.wpbc_listing_usual_row').attr('id'); |
| 158 |
|
| 159 |
// element_id = parseInt( element_id.replace( 'row_id_', '' ) ); |
| 160 |
|
| 161 |
selected_id.push(element_id); |
| 162 |
} |
| 163 |
|
| 164 |
}); |
| 165 |
|
| 166 |
//console.log( 'wpbc_get_selected_row_id', selected_id ); |
| 167 |
|
| 168 |
return selected_id; |
| 169 |
|
| 170 |
// _.each( json_items_arr, function ( p_val, p_key, p_data ){ |
| 171 |
// |
| 172 |
// }); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
/** |
| 177 |
* Show or hide buttons on Actions toolbar at Booking Listing page, if we have some selected bookings. |
| 178 |
*/ |
| 179 |
function wpbc_show_hide_action_buttons_for_selected_bookings(){ |
| 180 |
|
| 181 |
var selected_rows_arr = wpbc_get_selected_row_id(); |
| 182 |
|
| 183 |
if ( selected_rows_arr.length > 0 ){ |
| 184 |
jQuery( '.hide_button_if_no_selection' ).show(); |
| 185 |
} else { |
| 186 |
jQuery( '.hide_button_if_no_selection' ).hide(); |
| 187 |
} |
| 188 |
} |