| 1 |
/** |
| 2 |
* Checkbox Selection functions for Listing. |
| 3 |
*/ |
| 4 |
|
| 5 |
/** |
| 6 |
* Selections of several checkboxes like in gMail with shift :) |
| 7 |
* Need to have this structure: |
| 8 |
* .wpbc_selectable_table |
| 9 |
* .wpbc_selectable_head |
| 10 |
* .check-column |
| 11 |
* :checkbox |
| 12 |
* .wpbc_selectable_body |
| 13 |
* .wpbc_row |
| 14 |
* .check-column |
| 15 |
* :checkbox |
| 16 |
* .wpbc_selectable_foot |
| 17 |
* .check-column |
| 18 |
* :checkbox |
| 19 |
*/ |
| 20 |
function wpbc_define_gmail_checkbox_selection( $ ){ |
| 21 |
|
| 22 |
var checks, first, last, checked, sliced, lastClicked = false; |
| 23 |
|
| 24 |
// Check all checkboxes. |
| 25 |
$( '.wpbc_selectable_body' ).find( '.check-column' ).find( ':checkbox' ).on( |
| 26 |
'click', |
| 27 |
function (e) { |
| 28 |
if ( 'undefined' == e.shiftKey ) { |
| 29 |
return true; |
| 30 |
} |
| 31 |
if ( e.shiftKey ) { |
| 32 |
if ( ! lastClicked ) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
checks = $( lastClicked ).closest( '.wpbc_selectable_body' ).find( ':checkbox' ).filter( ':visible:enabled' ); |
| 36 |
first = checks.index( lastClicked ); |
| 37 |
last = checks.index( this ); |
| 38 |
checked = $( this ).prop( 'checked' ); |
| 39 |
if ( 0 < first && 0 < last && first != last ) { |
| 40 |
sliced = (last > first) ? checks.slice( first, last ) : checks.slice( last, first ); |
| 41 |
sliced.prop( |
| 42 |
'checked', |
| 43 |
function () { |
| 44 |
if ( $( this ).closest( '.wpbc_row' ).is( ':visible' ) ) { |
| 45 |
return checked; |
| 46 |
} |
| 47 |
return false; |
| 48 |
} |
| 49 |
).trigger( 'change' ); |
| 50 |
} |
| 51 |
} |
| 52 |
lastClicked = this; |
| 53 |
|
| 54 |
// toggle "check all" checkboxes. |
| 55 |
var unchecked = $( this ).closest( '.wpbc_selectable_body' ).find( ':checkbox' ).filter( ':visible:enabled' ).not( ':checked' ); |
| 56 |
$( this ).closest( '.wpbc_selectable_table' ).children( '.wpbc_selectable_head, .wpbc_selectable_foot' ).find( ':checkbox' ).prop( |
| 57 |
'checked', |
| 58 |
function () { |
| 59 |
return (0 === unchecked.length); |
| 60 |
} |
| 61 |
).trigger( 'change' ); |
| 62 |
|
| 63 |
return true; |
| 64 |
} |
| 65 |
); |
| 66 |
|
| 67 |
// Head || Foot clicking to select / deselect ALL. |
| 68 |
$( '.wpbc_selectable_head, .wpbc_selectable_foot' ).find( '.check-column :checkbox' ).on( |
| 69 |
'click', |
| 70 |
function (event) { |
| 71 |
var $this = $( this ), |
| 72 |
$table = $this.closest( '.wpbc_selectable_table' ), |
| 73 |
controlChecked = $this.prop( 'checked' ), |
| 74 |
toggle = event.shiftKey || $this.data( 'wp-toggle' ); |
| 75 |
|
| 76 |
$table.children( '.wpbc_selectable_body' ).filter( ':visible' ) |
| 77 |
.find( '.check-column' ).find( ':checkbox' ) |
| 78 |
.prop( |
| 79 |
'checked', |
| 80 |
function () { |
| 81 |
if ( $( this ).is( ':hidden,:disabled' ) ) { |
| 82 |
return false; |
| 83 |
} |
| 84 |
if ( toggle ) { |
| 85 |
return ! $( this ).prop( 'checked' ); |
| 86 |
} else if ( controlChecked ) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
return false; |
| 90 |
} |
| 91 |
).trigger( 'change' ); |
| 92 |
|
| 93 |
$table.children( '.wpbc_selectable_head, .wpbc_selectable_foot' ).filter( ':visible' ) |
| 94 |
.find( '.check-column' ).find( ':checkbox' ) |
| 95 |
.prop( |
| 96 |
'checked', |
| 97 |
function () { |
| 98 |
if ( toggle ) { |
| 99 |
return false; |
| 100 |
} else if ( controlChecked ) { |
| 101 |
return true; |
| 102 |
} |
| 103 |
return false; |
| 104 |
} |
| 105 |
); |
| 106 |
} |
| 107 |
); |
| 108 |
|
| 109 |
|
| 110 |
// Visually show selected border. |
| 111 |
$( '.wpbc_selectable_body' ).find( '.check-column :checkbox' ).on( |
| 112 |
'change', |
| 113 |
function (event) { |
| 114 |
if ( jQuery( this ).is( ':checked' ) ) { |
| 115 |
jQuery( this ).closest( '.wpbc_list_row' ).addClass( 'row_selected_color' ); |
| 116 |
} else { |
| 117 |
jQuery( this ).closest( '.wpbc_list_row' ).removeClass( 'row_selected_color' ); |
| 118 |
} |
| 119 |
|
| 120 |
// Disable text selection while pressing 'shift'. |
| 121 |
document.getSelection().removeAllRanges(); |
| 122 |
|
| 123 |
// Show or hide buttons on Actions toolbar at Booking Listing page, if we have some selected bookings. |
| 124 |
wpbc_show_hide_action_buttons_for_selected_bookings(); |
| 125 |
} |
| 126 |
); |
| 127 |
|
| 128 |
wpbc_show_hide_action_buttons_for_selected_bookings(); |
| 129 |
} |
| 130 |
|