| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
(function(window, document, undefined) { |
| 25 |
|
| 26 |
|
| 27 |
var factory = function( $, DataTable ) { |
| 28 |
"use strict"; |
| 29 |
|
| 30 |
|
| 31 |
DataTable.select = {}; |
| 32 |
DataTable.select.version = '1.0.1'; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
function cellRange( dt, idx, last ) |
| 116 |
{ |
| 117 |
var indexes; |
| 118 |
var columnIndexes; |
| 119 |
var rowIndexes; |
| 120 |
var selectColumns = function ( start, end ) { |
| 121 |
if ( start > end ) { |
| 122 |
var tmp = end; |
| 123 |
end = start; |
| 124 |
start = tmp; |
| 125 |
} |
| 126 |
|
| 127 |
var record = false; |
| 128 |
return dt.columns( ':visible' ).indexes().filter( function (i) { |
| 129 |
if ( i === start ) { |
| 130 |
record = true; |
| 131 |
} |
| 132 |
|
| 133 |
if ( i === end ) { |
| 134 |
record = false; |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
return record; |
| 139 |
} ); |
| 140 |
}; |
| 141 |
|
| 142 |
var selectRows = function ( start, end ) { |
| 143 |
var indexes = dt.rows( { search: 'applied' } ).indexes(); |
| 144 |
|
| 145 |
|
| 146 |
if ( indexes.indexOf( start ) > indexes.indexOf( end ) ) { |
| 147 |
var tmp = end; |
| 148 |
end = start; |
| 149 |
start = tmp; |
| 150 |
} |
| 151 |
|
| 152 |
var record = false; |
| 153 |
return indexes.filter( function (i) { |
| 154 |
if ( i === start ) { |
| 155 |
record = true; |
| 156 |
} |
| 157 |
|
| 158 |
if ( i === end ) { |
| 159 |
record = false; |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
return record; |
| 164 |
} ); |
| 165 |
}; |
| 166 |
|
| 167 |
if ( ! dt.cells( { selected: true } ).any() && ! last ) { |
| 168 |
|
| 169 |
columnIndexes = selectColumns( 0, idx.column ); |
| 170 |
rowIndexes = selectRows( 0 , idx.row ); |
| 171 |
} |
| 172 |
else { |
| 173 |
|
| 174 |
columnIndexes = selectColumns( last.column, idx.column ); |
| 175 |
rowIndexes = selectRows( last.row , idx.row ); |
| 176 |
} |
| 177 |
|
| 178 |
indexes = dt.cells( rowIndexes, columnIndexes ).flatten(); |
| 179 |
|
| 180 |
if ( ! dt.cells( idx, { selected: true } ).any() ) { |
| 181 |
|
| 182 |
dt.cells( indexes ).select(); |
| 183 |
} |
| 184 |
else { |
| 185 |
|
| 186 |
dt.cells( indexes ).deselect(); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
function disableMouseSelection( dt ) |
| 197 |
{ |
| 198 |
var ctx = dt.settings()[0]; |
| 199 |
var selector = ctx._select.selector; |
| 200 |
|
| 201 |
$( dt.table().body() ) |
| 202 |
.off( 'mousedown.dtSelect', selector ) |
| 203 |
.off( 'mouseup.dtSelect', selector ) |
| 204 |
.off( 'click.dtSelect', selector ); |
| 205 |
|
| 206 |
$('body').off( 'click.dtSelect' ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
function enableMouseSelection ( dt ) |
| 216 |
{ |
| 217 |
var body = $( dt.table().body() ); |
| 218 |
var ctx = dt.settings()[0]; |
| 219 |
var selector = ctx._select.selector; |
| 220 |
|
| 221 |
body |
| 222 |
.on( 'mousedown.dtSelect', selector, function(e) { |
| 223 |
|
| 224 |
|
| 225 |
if ( e.shiftKey ) { |
| 226 |
body |
| 227 |
.css( '-moz-user-select', 'none' ) |
| 228 |
.one('selectstart.dtSelect', selector, function () { |
| 229 |
return false; |
| 230 |
} ); |
| 231 |
} |
| 232 |
} ) |
| 233 |
.on( 'mouseup.dtSelect', selector, function(e) { |
| 234 |
|
| 235 |
|
| 236 |
body.css( '-moz-user-select', '' ); |
| 237 |
} ) |
| 238 |
.on( 'click.dtSelect', selector, function ( e ) { |
| 239 |
var items = dt.select.items(); |
| 240 |
var cellIndex = dt.cell( this ).index(); |
| 241 |
var idx; |
| 242 |
|
| 243 |
var ctx = dt.settings()[0]; |
| 244 |
|
| 245 |
|
| 246 |
if ( $(e.target).closest('tbody')[0] != body[0] ) { |
| 247 |
return; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if ( ! dt.cell( e.target ).any() ) { |
| 253 |
return; |
| 254 |
} |
| 255 |
|
| 256 |
if ( items === 'row' ) { |
| 257 |
idx = cellIndex.row; |
| 258 |
typeSelect( e, dt, ctx, 'row', idx ); |
| 259 |
} |
| 260 |
else if ( items === 'column' ) { |
| 261 |
idx = dt.cell( e.target ).index().column; |
| 262 |
typeSelect( e, dt, ctx, 'column', idx ); |
| 263 |
} |
| 264 |
else if ( items === 'cell' ) { |
| 265 |
idx = dt.cell( e.target ).index(); |
| 266 |
typeSelect( e, dt, ctx, 'cell', idx ); |
| 267 |
} |
| 268 |
|
| 269 |
ctx._select_lastCell = cellIndex; |
| 270 |
} ); |
| 271 |
|
| 272 |
|
| 273 |
$('body').on( 'click.dtSelect', function ( e ) { |
| 274 |
if ( ctx._select.blurable ) { |
| 275 |
|
| 276 |
if ( $(e.target).parents().filter( dt.table().container() ).length ) { |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
if ( $(e.target).parents('div.DTE').length ) { |
| 282 |
return; |
| 283 |
} |
| 284 |
|
| 285 |
clear( ctx, true ); |
| 286 |
} |
| 287 |
} ); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
function eventTrigger ( api, type, args, any ) |
| 301 |
{ |
| 302 |
if ( any && ! api.flatten().length ) { |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
args.unshift( api ); |
| 307 |
|
| 308 |
$(api.table().node()).triggerHandler( type+'.dt', args ); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
function info ( api ) |
| 319 |
{ |
| 320 |
var ctx = api.settings()[0]; |
| 321 |
|
| 322 |
if ( ! ctx._select.info || ! ctx.aanFeatures.i ) { |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
var output = $('<span class="select-info"/>'); |
| 327 |
var add = function ( name, num ) { |
| 328 |
output.append( $('<span class="select-item"/>').append( api.i18n( |
| 329 |
'select.'+name+'s', |
| 330 |
{ _: '%d '+name+'s selected', 0: '', 1: '1 '+name+' selected' }, |
| 331 |
num |
| 332 |
) ) ); |
| 333 |
}; |
| 334 |
|
| 335 |
add( 'row', api.rows( { selected: true } ).flatten().length ); |
| 336 |
add( 'column', api.columns( { selected: true } ).flatten().length ); |
| 337 |
add( 'cell', api.cells( { selected: true } ).flatten().length ); |
| 338 |
|
| 339 |
|
| 340 |
$.each( ctx.aanFeatures.i, function ( i, el ) { |
| 341 |
el = $(el); |
| 342 |
|
| 343 |
var exisiting = el.children('span.select-info'); |
| 344 |
if ( exisiting.length ) { |
| 345 |
exisiting.remove(); |
| 346 |
} |
| 347 |
|
| 348 |
if ( output.text() !== '' ) { |
| 349 |
el.append( output ); |
| 350 |
} |
| 351 |
} ); |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
function init ( ctx ) { |
| 365 |
var api = new DataTable.Api( ctx ); |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
ctx.aoRowCreatedCallback.push( { |
| 375 |
fn: function ( row, data, index ) { |
| 376 |
var i, ien; |
| 377 |
var d = ctx.aoData[ index ]; |
| 378 |
|
| 379 |
|
| 380 |
if ( d._select_selected ) { |
| 381 |
$( row ).addClass( 'selected' ); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
for ( i=0, ien=ctx.aoColumns.length ; i<ien ; i++ ) { |
| 387 |
if ( ctx.aoColumns[i]._select_selected || (d._selected_cells && d._selected_cells[i]) ) { |
| 388 |
$(d.anCells[i]).addClass( 'selected' ); |
| 389 |
} |
| 390 |
} |
| 391 |
}, |
| 392 |
sName: 'select-deferRender' |
| 393 |
} ); |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
api.on( 'preXhr.dt.dtSelect', function () { |
| 398 |
|
| 399 |
|
| 400 |
var rows = api.rows( { selected: true } ).ids( true ).filter( function ( d ) { |
| 401 |
return d !== undefined; |
| 402 |
} ); |
| 403 |
|
| 404 |
var cells = api.cells( { selected: true } ).eq(0).map( function ( cellIdx ) { |
| 405 |
var id = api.row( cellIdx.row ).id( true ); |
| 406 |
return id ? |
| 407 |
{ row: id, column: cellIdx.column } : |
| 408 |
undefined; |
| 409 |
} ).filter( function ( d ) { |
| 410 |
return d !== undefined; |
| 411 |
} ); |
| 412 |
|
| 413 |
|
| 414 |
api.one( 'draw.dt.dtSelect', function () { |
| 415 |
api.rows( rows ).select(); |
| 416 |
|
| 417 |
|
| 418 |
if ( cells.any() ) { |
| 419 |
cells.each( function ( id ) { |
| 420 |
api.cells( id.row, id.column ).select(); |
| 421 |
} ); |
| 422 |
} |
| 423 |
} ); |
| 424 |
} ); |
| 425 |
|
| 426 |
|
| 427 |
api.on( 'draw.dtSelect.dt select.dtSelect.dt deselect.dtSelect.dt', function () { |
| 428 |
info( api ); |
| 429 |
} ); |
| 430 |
|
| 431 |
|
| 432 |
api.on( 'destroy.dtSelect', function () { |
| 433 |
disableMouseSelection( api ); |
| 434 |
api.off( '.dtSelect' ); |
| 435 |
} ); |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
function rowColumnRange( dt, type, idx, last ) |
| 449 |
{ |
| 450 |
|
| 451 |
var indexes = dt[type+'s']( { search: 'applied' } ).indexes(); |
| 452 |
var idx1 = $.inArray( last, indexes ); |
| 453 |
var idx2 = $.inArray( idx, indexes ); |
| 454 |
|
| 455 |
if ( ! dt[type+'s']( { selected: true } ).any() && idx1 === -1 ) { |
| 456 |
|
| 457 |
|
| 458 |
indexes.splice( $.inArray( idx, indexes )+1, indexes.length ); |
| 459 |
} |
| 460 |
else { |
| 461 |
|
| 462 |
if ( idx1 > idx2 ) { |
| 463 |
var tmp = idx2; |
| 464 |
idx2 = idx1; |
| 465 |
idx1 = tmp; |
| 466 |
} |
| 467 |
|
| 468 |
indexes.splice( idx2+1, indexes.length ); |
| 469 |
indexes.splice( 0, idx1 ); |
| 470 |
} |
| 471 |
|
| 472 |
if ( ! dt[type]( idx, { selected: true } ).any() ) { |
| 473 |
|
| 474 |
dt[type+'s']( indexes ).select(); |
| 475 |
} |
| 476 |
else { |
| 477 |
|
| 478 |
indexes.splice( $.inArray( idx, indexes ), 1 ); |
| 479 |
dt[type+'s']( indexes ).deselect(); |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
function clear( ctx, force ) |
| 492 |
{ |
| 493 |
if ( force || ctx._select.style === 'single' ) { |
| 494 |
var api = new DataTable.Api( ctx ); |
| 495 |
|
| 496 |
api.rows( { selected: true } ).deselect(); |
| 497 |
api.columns( { selected: true } ).deselect(); |
| 498 |
api.cells( { selected: true } ).deselect(); |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
function typeSelect ( e, dt, ctx, type, idx ) |
| 513 |
{ |
| 514 |
var style = dt.select.style(); |
| 515 |
var isSelected = dt[type]( idx, { selected: true } ).any(); |
| 516 |
|
| 517 |
if ( style === 'os' ) { |
| 518 |
if ( e.ctrlKey || e.metaKey ) { |
| 519 |
|
| 520 |
dt[type]( idx ).select( ! isSelected ); |
| 521 |
} |
| 522 |
else if ( e.shiftKey ) { |
| 523 |
if ( type === 'cell' ) { |
| 524 |
cellRange( dt, idx, ctx._select_lastCell || null ); |
| 525 |
} |
| 526 |
else { |
| 527 |
rowColumnRange( dt, type, idx, ctx._select_lastCell ? |
| 528 |
ctx._select_lastCell[type] : |
| 529 |
null |
| 530 |
); |
| 531 |
} |
| 532 |
} |
| 533 |
else { |
| 534 |
|
| 535 |
|
| 536 |
var selected = dt[type+'s']( { selected: true } ); |
| 537 |
|
| 538 |
if ( isSelected && selected.flatten().length === 1 ) { |
| 539 |
dt[type]( idx ).deselect(); |
| 540 |
} |
| 541 |
else { |
| 542 |
selected.deselect(); |
| 543 |
dt[type]( idx ).select(); |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
else { |
| 548 |
dt[ type ]( idx ).select( ! isSelected ); |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
$.each( [ |
| 562 |
{ type: 'row', prop: 'aoData' }, |
| 563 |
{ type: 'column', prop: 'aoColumns' } |
| 564 |
], function ( i, o ) { |
| 565 |
DataTable.ext.selector[ o.type ].push( function ( settings, opts, indexes ) { |
| 566 |
var selected = opts.selected; |
| 567 |
var data; |
| 568 |
var out = []; |
| 569 |
|
| 570 |
if ( selected === undefined ) { |
| 571 |
return indexes; |
| 572 |
} |
| 573 |
|
| 574 |
for ( var i=0, ien=indexes.length ; i<ien ; i++ ) { |
| 575 |
data = settings[ o.prop ][ indexes[i] ]; |
| 576 |
|
| 577 |
if ( (selected === true && data._select_selected === true) || |
| 578 |
(selected === false && ! data._select_selected ) |
| 579 |
) { |
| 580 |
out.push( indexes[i] ); |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
return out; |
| 585 |
} ); |
| 586 |
} ); |
| 587 |
|
| 588 |
DataTable.ext.selector.cell.push( function ( settings, opts, cells ) { |
| 589 |
var selected = opts.selected; |
| 590 |
var rowData; |
| 591 |
var out = []; |
| 592 |
|
| 593 |
if ( selected === undefined ) { |
| 594 |
return cells; |
| 595 |
} |
| 596 |
|
| 597 |
for ( var i=0, ien=cells.length ; i<ien ; i++ ) { |
| 598 |
rowData = settings.aoData[ cells[i].row ]; |
| 599 |
|
| 600 |
if ( (selected === true && rowData._selected_cells && rowData._selected_cells[ cells[i].column ] === true) || |
| 601 |
(selected === false && ( ! rowData._selected_cells || ! rowData._selected_cells[ cells[i].column ] ) ) |
| 602 |
) { |
| 603 |
out.push( cells[i] ); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
return out; |
| 608 |
} ); |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
var apiRegister = DataTable.Api.register; |
| 621 |
var apiRegisterPlural = DataTable.Api.registerPlural; |
| 622 |
|
| 623 |
apiRegister( 'select()', function () {} ); |
| 624 |
|
| 625 |
apiRegister( 'select.blurable()', function ( flag ) { |
| 626 |
if ( flag === undefined ) { |
| 627 |
return this.context[0]._select.blurable; |
| 628 |
} |
| 629 |
|
| 630 |
return this.iterator( 'table', function ( ctx ) { |
| 631 |
ctx._select.blurable = flag; |
| 632 |
} ); |
| 633 |
} ); |
| 634 |
|
| 635 |
apiRegister( 'select.info()', function ( flag ) { |
| 636 |
if ( info === undefined ) { |
| 637 |
return this.context[0]._select.info; |
| 638 |
} |
| 639 |
|
| 640 |
return this.iterator( 'table', function ( ctx ) { |
| 641 |
ctx._select.info = flag; |
| 642 |
} ); |
| 643 |
} ); |
| 644 |
|
| 645 |
apiRegister( 'select.items()', function ( items ) { |
| 646 |
if ( items === undefined ) { |
| 647 |
return this.context[0]._select.items; |
| 648 |
} |
| 649 |
|
| 650 |
return this.iterator( 'table', function ( ctx ) { |
| 651 |
ctx._select.items = items; |
| 652 |
|
| 653 |
eventTrigger( new DataTable.Api( ctx ), 'selectItems', [ items ] ); |
| 654 |
} ); |
| 655 |
} ); |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
apiRegister( 'select.style()', function ( style ) { |
| 660 |
if ( style === undefined ) { |
| 661 |
return this.context[0]._select.style; |
| 662 |
} |
| 663 |
|
| 664 |
return this.iterator( 'table', function ( ctx ) { |
| 665 |
ctx._select.style = style; |
| 666 |
|
| 667 |
if ( ! ctx._select_init ) { |
| 668 |
init( ctx ); |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
var dt = new DataTable.Api( ctx ); |
| 674 |
disableMouseSelection( dt ); |
| 675 |
|
| 676 |
if ( style !== 'api' ) { |
| 677 |
enableMouseSelection( dt ); |
| 678 |
} |
| 679 |
|
| 680 |
eventTrigger( new DataTable.Api( ctx ), 'selectStyle', [ style ] ); |
| 681 |
} ); |
| 682 |
} ); |
| 683 |
|
| 684 |
apiRegister( 'select.selector()', function ( selector ) { |
| 685 |
if ( selector === undefined ) { |
| 686 |
return this.context[0]._select.selector; |
| 687 |
} |
| 688 |
|
| 689 |
return this.iterator( 'table', function ( ctx ) { |
| 690 |
disableMouseSelection( new DataTable.Api( ctx ) ); |
| 691 |
|
| 692 |
ctx._select.selector = selector; |
| 693 |
|
| 694 |
if ( ctx._select.style !== 'api' ) { |
| 695 |
enableMouseSelection( new DataTable.Api( ctx ) ); |
| 696 |
} |
| 697 |
} ); |
| 698 |
} ); |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
apiRegisterPlural( 'rows().select()', 'row().select()', function ( select ) { |
| 703 |
var api = this; |
| 704 |
|
| 705 |
if ( select === false ) { |
| 706 |
return this.deselect(); |
| 707 |
} |
| 708 |
|
| 709 |
this.iterator( 'row', function ( ctx, idx ) { |
| 710 |
clear( ctx ); |
| 711 |
|
| 712 |
ctx.aoData[ idx ]._select_selected = true; |
| 713 |
$( ctx.aoData[ idx ].nTr ).addClass( 'selected' ); |
| 714 |
} ); |
| 715 |
|
| 716 |
this.iterator( 'table', function ( ctx, i ) { |
| 717 |
eventTrigger( api, 'select', [ 'row', api[i] ], true ); |
| 718 |
} ); |
| 719 |
|
| 720 |
return this; |
| 721 |
} ); |
| 722 |
|
| 723 |
apiRegisterPlural( 'columns().select()', 'column().select()', function ( select ) { |
| 724 |
var api = this; |
| 725 |
|
| 726 |
if ( select === false ) { |
| 727 |
return this.deselect(); |
| 728 |
} |
| 729 |
|
| 730 |
this.iterator( 'column', function ( ctx, idx ) { |
| 731 |
clear( ctx ); |
| 732 |
|
| 733 |
ctx.aoColumns[ idx ]._select_selected = true; |
| 734 |
|
| 735 |
var column = new DataTable.Api( ctx ).column( idx ); |
| 736 |
|
| 737 |
$( column.header() ).addClass( 'selected' ); |
| 738 |
$( column.footer() ).addClass( 'selected' ); |
| 739 |
|
| 740 |
column.nodes().to$().addClass( 'selected' ); |
| 741 |
} ); |
| 742 |
|
| 743 |
this.iterator( 'table', function ( ctx, i ) { |
| 744 |
eventTrigger( api, 'select', [ 'column', api[i] ], true ); |
| 745 |
} ); |
| 746 |
|
| 747 |
return this; |
| 748 |
} ); |
| 749 |
|
| 750 |
apiRegisterPlural( 'cells().select()', 'cell().select()', function ( select ) { |
| 751 |
var api = this; |
| 752 |
|
| 753 |
if ( select === false ) { |
| 754 |
return this.deselect(); |
| 755 |
} |
| 756 |
|
| 757 |
this.iterator( 'cell', function ( ctx, rowIdx, colIdx ) { |
| 758 |
clear( ctx ); |
| 759 |
|
| 760 |
var data = ctx.aoData[ rowIdx ]; |
| 761 |
|
| 762 |
if ( data._selected_cells === undefined ) { |
| 763 |
data._selected_cells = []; |
| 764 |
} |
| 765 |
|
| 766 |
data._selected_cells[ colIdx ] = true; |
| 767 |
|
| 768 |
if ( data.anCells ) { |
| 769 |
$( data.anCells[ colIdx ] ).addClass( 'selected' ); |
| 770 |
} |
| 771 |
} ); |
| 772 |
|
| 773 |
this.iterator( 'table', function ( ctx, i ) { |
| 774 |
eventTrigger( api, 'select', [ 'cell', api[i] ], true ); |
| 775 |
} ); |
| 776 |
|
| 777 |
return this; |
| 778 |
} ); |
| 779 |
|
| 780 |
|
| 781 |
apiRegisterPlural( 'rows().deselect()', 'row().deselect()', function () { |
| 782 |
var api = this; |
| 783 |
|
| 784 |
this.iterator( 'row', function ( ctx, idx ) { |
| 785 |
ctx.aoData[ idx ]._select_selected = false; |
| 786 |
$( ctx.aoData[ idx ].nTr ).removeClass( 'selected' ); |
| 787 |
} ); |
| 788 |
|
| 789 |
this.iterator( 'table', function ( ctx, i ) { |
| 790 |
eventTrigger( api, 'deselect', [ 'row', api[i] ], true ); |
| 791 |
} ); |
| 792 |
|
| 793 |
return this; |
| 794 |
} ); |
| 795 |
|
| 796 |
apiRegisterPlural( 'columns().deselect()', 'column().deselect()', function () { |
| 797 |
var api = this; |
| 798 |
|
| 799 |
this.iterator( 'column', function ( ctx, idx ) { |
| 800 |
ctx.aoColumns[ idx ]._select_selected = false; |
| 801 |
|
| 802 |
var api = new DataTable.Api( ctx ); |
| 803 |
var column = api.column( idx ); |
| 804 |
|
| 805 |
$( column.header() ).removeClass( 'selected' ); |
| 806 |
$( column.footer() ).removeClass( 'selected' ); |
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
api.cells( null, idx ).indexes().each( function (cellIdx) { |
| 812 |
var data = ctx.aoData[ cellIdx.row ]; |
| 813 |
var cellSelected = data._selected_cells; |
| 814 |
|
| 815 |
if ( data.anCells && (! cellSelected || ! cellSelected[ cellIdx.column ]) ) { |
| 816 |
$( data.anCells[ cellIdx.column ] ).removeClass( 'selected' ); |
| 817 |
} |
| 818 |
} ); |
| 819 |
} ); |
| 820 |
|
| 821 |
this.iterator( 'table', function ( ctx, i ) { |
| 822 |
eventTrigger( api, 'deselect', [ 'column', api[i] ], true ); |
| 823 |
} ); |
| 824 |
|
| 825 |
return this; |
| 826 |
} ); |
| 827 |
|
| 828 |
apiRegisterPlural( 'cells().deselect()', 'cell().deselect()', function () { |
| 829 |
var api = this; |
| 830 |
|
| 831 |
this.iterator( 'cell', function ( ctx, rowIdx, colIdx ) { |
| 832 |
var data = ctx.aoData[ rowIdx ]; |
| 833 |
|
| 834 |
data._selected_cells[ colIdx ] = false; |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
if ( data.anCells && ! ctx.aoColumns[ colIdx ]._select_selected ) { |
| 840 |
$( data.anCells[ colIdx ] ).removeClass( 'selected' ); |
| 841 |
} |
| 842 |
} ); |
| 843 |
|
| 844 |
this.iterator( 'table', function ( ctx, i ) { |
| 845 |
eventTrigger( api, 'deselect', [ 'cell', api[i] ], true ); |
| 846 |
} ); |
| 847 |
|
| 848 |
return this; |
| 849 |
} ); |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
function i18n( label, def ) { |
| 857 |
return function (dt) { |
| 858 |
return dt.i18n( 'buttons.'+label, def ); |
| 859 |
}; |
| 860 |
} |
| 861 |
|
| 862 |
$.extend( DataTable.ext.buttons, { |
| 863 |
selected: { |
| 864 |
text: i18n( 'selected', 'Selected' ), |
| 865 |
className: 'buttons-selected', |
| 866 |
init: function ( dt, button, config ) { |
| 867 |
var that = this; |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
dt.on( 'draw.dt.DT select.dt.DT deselect.dt.DT', function () { |
| 872 |
var enable = that.rows( { selected: true } ).any() || |
| 873 |
that.columns( { selected: true } ).any() || |
| 874 |
that.cells( { selected: true } ).any(); |
| 875 |
|
| 876 |
that.enable( enable ); |
| 877 |
} ); |
| 878 |
|
| 879 |
this.disable(); |
| 880 |
} |
| 881 |
}, |
| 882 |
selectedSingle: { |
| 883 |
text: i18n( 'selectedSingle', 'Selected single' ), |
| 884 |
className: 'buttons-selected-single', |
| 885 |
init: function ( dt, button, config ) { |
| 886 |
var that = this; |
| 887 |
|
| 888 |
dt.on( 'draw.dt.DT select.dt.DT deselect.dt.DT', function () { |
| 889 |
var count = dt.rows( { selected: true } ).flatten().length + |
| 890 |
dt.columns( { selected: true } ).flatten().length + |
| 891 |
dt.cells( { selected: true } ).flatten().length; |
| 892 |
|
| 893 |
that.enable( count === 1 ); |
| 894 |
} ); |
| 895 |
|
| 896 |
this.disable(); |
| 897 |
} |
| 898 |
}, |
| 899 |
selectAll: { |
| 900 |
text: i18n( 'selectAll', 'Select all' ), |
| 901 |
className: 'buttons-select-all', |
| 902 |
action: function () { |
| 903 |
var items = this.select.items(); |
| 904 |
this[ items+'s' ]().select(); |
| 905 |
} |
| 906 |
}, |
| 907 |
selectNone: { |
| 908 |
text: i18n( 'selectNone', 'Deselect all' ), |
| 909 |
className: 'buttons-select-none', |
| 910 |
action: function () { |
| 911 |
clear( this.settings()[0], true ); |
| 912 |
} |
| 913 |
} |
| 914 |
} ); |
| 915 |
|
| 916 |
$.each( [ 'Row', 'Column', 'Cell' ], function ( i, item ) { |
| 917 |
var lc = item.toLowerCase(); |
| 918 |
|
| 919 |
DataTable.ext.buttons[ 'select'+item+'s' ] = { |
| 920 |
text: i18n( 'select'+item+'s', 'Select '+lc+'s' ), |
| 921 |
className: 'buttons-select-'+lc+'s', |
| 922 |
action: function () { |
| 923 |
this.select.items( lc ); |
| 924 |
}, |
| 925 |
init: function ( dt, button, config ) { |
| 926 |
var that = this; |
| 927 |
|
| 928 |
dt.on( 'selectItems.dt.DT', function ( e, ctx, items ) { |
| 929 |
that.active( items === lc ); |
| 930 |
} ); |
| 931 |
} |
| 932 |
}; |
| 933 |
} ); |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
$(document).on( 'init.dt.dtSelect', function (e, ctx, json) { |
| 946 |
if ( e.namespace !== 'dt' ) { |
| 947 |
return; |
| 948 |
} |
| 949 |
|
| 950 |
var opts = ctx.oInit.select || DataTable.defaults.select; |
| 951 |
var dt = new DataTable.Api( ctx ); |
| 952 |
|
| 953 |
|
| 954 |
var items = 'row'; |
| 955 |
var style = 'api'; |
| 956 |
var blurable = false; |
| 957 |
var info = true; |
| 958 |
var selector = 'td, th'; |
| 959 |
|
| 960 |
ctx._select = {}; |
| 961 |
|
| 962 |
|
| 963 |
if ( opts === true ) { |
| 964 |
style = 'os'; |
| 965 |
} |
| 966 |
else if ( typeof opts === 'string' ) { |
| 967 |
style = opts; |
| 968 |
} |
| 969 |
else if ( $.isPlainObject( opts ) ) { |
| 970 |
if ( opts.blurable !== undefined ) { |
| 971 |
blurable = opts.blurable; |
| 972 |
} |
| 973 |
|
| 974 |
if ( opts.info !== undefined ) { |
| 975 |
info = opts.info; |
| 976 |
} |
| 977 |
|
| 978 |
if ( opts.items !== undefined ) { |
| 979 |
items = opts.items; |
| 980 |
} |
| 981 |
|
| 982 |
if ( opts.style !== undefined ) { |
| 983 |
style = opts.style; |
| 984 |
} |
| 985 |
|
| 986 |
if ( opts.selector !== undefined ) { |
| 987 |
selector = opts.selector; |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
dt.select.selector( selector ); |
| 992 |
dt.select.items( items ); |
| 993 |
dt.select.style( style ); |
| 994 |
dt.select.blurable( blurable ); |
| 995 |
dt.select.info( info ); |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
if ( $( dt.table().node() ).hasClass( 'selectable' ) ) { |
| 1000 |
dt.select.style( 'os' ); |
| 1001 |
} |
| 1002 |
} ); |
| 1003 |
|
| 1004 |
|
| 1005 |
}; |
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
if ( typeof define === 'function' && define.amd ) { |
| 1010 |
define( ['jquery', 'datatables'], factory ); |
| 1011 |
} |
| 1012 |
else if ( typeof exports === 'object' ) { |
| 1013 |
|
| 1014 |
factory( require('jquery'), require('datatables') ); |
| 1015 |
} |
| 1016 |
else if ( jQuery && !jQuery.fn.dataTable.select ) { |
| 1017 |
|
| 1018 |
factory( jQuery, jQuery.fn.dataTable ); |
| 1019 |
} |
| 1020 |
|
| 1021 |
|
| 1022 |
})(window, document); |
| 1023 |
|