LaravelTest
1349 строк · 30.9 Кб
1/*! KeyTable 2.6.4
2* ©2009-2021 SpryMedia Ltd - datatables.net/license
3*/
4
5/**
6* @summary KeyTable
7* @description Spreadsheet like keyboard navigation for DataTables
8* @version 2.6.4
9* @file dataTables.keyTable.js
10* @author SpryMedia Ltd (www.sprymedia.co.uk)
11* @contact www.sprymedia.co.uk/contact
12* @copyright Copyright 2009-2021 SpryMedia Ltd.
13*
14* This source file is free software, available under the following license:
15* MIT license - http://datatables.net/license/mit
16*
17* This source file is distributed in the hope that it will be useful, but
18* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19* or FITNESS FOR A PARTICULAR PURPOSE. See the license files for details.
20*
21* For details please refer to: http://www.datatables.net
22*/
23
24(function( factory ){25if ( typeof define === 'function' && define.amd ) {26// AMD27define( ['jquery', 'datatables.net'], function ( $ ) {28return factory( $, window, document );29} );30}31else if ( typeof exports === 'object' ) {32// CommonJS33module.exports = function (root, $) {34if ( ! root ) {35root = window;36}37
38if ( ! $ || ! $.fn.dataTable ) {39$ = require('datatables.net')(root, $).$;40}41
42return factory( $, root, root.document );43};44}45else {46// Browser47factory( jQuery, window, document );48}49}(function( $, window, document, undefined ) {50'use strict';51var DataTable = $.fn.dataTable;52var namespaceCounter = 0;53var editorNamespaceCounter = 0;54
55
56var KeyTable = function ( dt, opts ) {57// Sanity check that we are using DataTables 1.10 or newer58if ( ! DataTable.versionCheck || ! DataTable.versionCheck( '1.10.8' ) ) {59throw 'KeyTable requires DataTables 1.10.8 or newer';60}61
62// User and defaults configuration object63this.c = $.extend( true, {},64DataTable.defaults.keyTable,65KeyTable.defaults,66opts
67);68
69// Internal settings70this.s = {71/** @type {DataTable.Api} DataTables' API instance */72dt: new DataTable.Api( dt ),73
74enable: true,75
76/** @type {bool} Flag for if a draw is triggered by focus */77focusDraw: false,78
79/** @type {bool} Flag to indicate when waiting for a draw to happen.80* Will ignore key presses at this point
81*/
82waitingForDraw: false,83
84/** @type {object} Information about the last cell that was focused */85lastFocus: null,86
87/** @type {string} Unique namespace per instance */88namespace: '.keyTable-'+(namespaceCounter++),89
90/** @type {Node} Input element for tabbing into the table */91tabInput: null92};93
94// DOM items95this.dom = {96
97};98
99// Check if row reorder has already been initialised on this table100var settings = this.s.dt.settings()[0];101var exisiting = settings.keytable;102if ( exisiting ) {103return exisiting;104}105
106settings.keytable = this;107this._constructor();108};109
110
111$.extend( KeyTable.prototype, {112/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *113* API methods for DataTables API interface
114*/
115
116/**117* Blur the table's cell focus
118*/
119blur: function ()120{121this._blur();122},123
124/**125* Enable cell focus for the table
126*
127* @param {string} state Can be `true`, `false` or `-string navigation-only`
128*/
129enable: function ( state )130{131this.s.enable = state;132},133
134/**135* Get enable status
136*/
137enabled: function () {138return this.s.enable;139},140
141/**142* Focus on a cell
143* @param {integer} row Row index
144* @param {integer} column Column index
145*/
146focus: function ( row, column )147{148this._focus( this.s.dt.cell( row, column ) );149},150
151/**152* Is the cell focused
153* @param {object} cell Cell index to check
154* @returns {boolean} true if focused, false otherwise
155*/
156focused: function ( cell )157{158var lastFocus = this.s.lastFocus;159
160if ( ! lastFocus ) {161return false;162}163
164var lastIdx = this.s.lastFocus.cell.index();165return cell.row === lastIdx.row && cell.column === lastIdx.column;166},167
168
169/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *170* Constructor
171*/
172
173/**174* Initialise the KeyTable instance
175*
176* @private
177*/
178_constructor: function ()179{180this._tabInput();181
182var that = this;183var dt = this.s.dt;184var table = $( dt.table().node() );185var namespace = this.s.namespace;186var editorBlock = false;187
188// Need to be able to calculate the cell positions relative to the table189if ( table.css('position') === 'static' ) {190table.css( 'position', 'relative' );191}192
193// Click to focus194$( dt.table().body() ).on( 'click'+namespace, 'th, td', function (e) {195if ( that.s.enable === false ) {196return;197}198
199var cell = dt.cell( this );200
201if ( ! cell.any() ) {202return;203}204
205that._focus( cell, null, false, e );206} );207
208// Key events209$( document ).on( 'keydown'+namespace, function (e) {210if ( ! editorBlock ) {211that._key( e );212}213} );214
215// Click blur216if ( this.c.blurable ) {217$( document ).on( 'mousedown'+namespace, function ( e ) {218// Click on the search input will blur focus219if ( $(e.target).parents( '.dataTables_filter' ).length ) {220that._blur();221}222
223// If the click was inside the DataTables container, don't blur224if ( $(e.target).parents().filter( dt.table().container() ).length ) {225return;226}227
228// Don't blur in Editor form229if ( $(e.target).parents('div.DTE').length ) {230return;231}232
233// Or an Editor date input234if (235$(e.target).parents('div.editor-datetime').length ||236$(e.target).parents('div.dt-datetime').length237) {238return;239}240
241//If the click was inside the fixed columns container, don't blur242if ( $(e.target).parents().filter('.DTFC_Cloned').length ) {243return;244}245
246that._blur();247} );248}249
250if ( this.c.editor ) {251var editor = this.c.editor;252
253// Need to disable KeyTable when the main editor is shown254editor.on( 'open.keyTableMain', function (e, mode, action) {255if ( mode !== 'inline' && that.s.enable ) {256that.enable( false );257
258editor.one( 'close'+namespace, function () {259that.enable( true );260} );261}262} );263
264if ( this.c.editOnFocus ) {265dt.on( 'key-focus'+namespace+' key-refocus'+namespace, function ( e, dt, cell, orig ) {266that._editor( null, orig, true );267} );268}269
270// Activate Editor when a key is pressed (will be ignored, if271// already active).272dt.on( 'key'+namespace, function ( e, dt, key, cell, orig ) {273that._editor( key, orig, false );274} );275
276// Active editing on double click - it will already have focus from277// the click event handler above278$( dt.table().body() ).on( 'dblclick'+namespace, 'th, td', function (e) {279if ( that.s.enable === false ) {280return;281}282
283var cell = dt.cell( this );284
285if ( ! cell.any() ) {286return;287}288
289if ( that.s.lastFocus && this !== that.s.lastFocus.cell.node() ) {290return;291}292
293that._editor( null, e, true );294} );295
296// While Editor is busy processing, we don't want to process any key events297editor
298.on('preSubmit', function () {299editorBlock = true;300} )301.on('preSubmitCancelled', function () {302editorBlock = false;303} )304.on('submitComplete', function () {305editorBlock = false;306} );307}308
309// Stave saving310if ( dt.settings()[0].oFeatures.bStateSave ) {311dt.on( 'stateSaveParams'+namespace, function (e, s, d) {312d.keyTable = that.s.lastFocus ?313that.s.lastFocus.cell.index() :314null;315} );316}317
318dt.on( 'column-visibility'+namespace, function (e) {319that._tabInput();320} );321
322// Redraw - retain focus on the current cell323dt.on( 'draw'+namespace, function (e) {324that._tabInput();325
326if ( that.s.focusDraw ) {327return;328}329
330var lastFocus = that.s.lastFocus;331
332if ( lastFocus ) {333var relative = that.s.lastFocus.relative;334var info = dt.page.info();335var row = relative.row + info.start;336
337if ( info.recordsDisplay === 0 ) {338return;339}340
341// Reverse if needed342if ( row >= info.recordsDisplay ) {343row = info.recordsDisplay - 1;344}345
346that._focus( row, relative.column, true, e );347}348} );349
350// Clipboard support351if ( this.c.clipboard ) {352this._clipboard();353}354
355dt.on( 'destroy'+namespace, function () {356that._blur( true );357
358// Event tidy up359dt.off( namespace );360
361$( dt.table().body() )362.off( 'click'+namespace, 'th, td' )363.off( 'dblclick'+namespace, 'th, td' );364
365$( document )366.off( 'mousedown'+namespace )367.off( 'keydown'+namespace )368.off( 'copy'+namespace )369.off( 'paste'+namespace );370} );371
372// Initial focus comes from state or options373var state = dt.state.loaded();374
375if ( state && state.keyTable ) {376// Wait until init is done377dt.one( 'init', function () {378var cell = dt.cell( state.keyTable );379
380// Ensure that the saved cell still exists381if ( cell.any() ) {382cell.focus();383}384} );385}386else if ( this.c.focus ) {387dt.cell( this.c.focus ).focus();388}389},390
391
392/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *393* Private methods
394*/
395
396/**397* Blur the control
398*
399* @param {boolean} [noEvents=false] Don't trigger updates / events (for destroying)
400* @private
401*/
402_blur: function (noEvents)403{404if ( ! this.s.enable || ! this.s.lastFocus ) {405return;406}407
408var cell = this.s.lastFocus.cell;409
410$( cell.node() ).removeClass( this.c.className );411this.s.lastFocus = null;412
413if ( ! noEvents ) {414this._updateFixedColumns(cell.index().column);415
416this._emitEvent( 'key-blur', [ this.s.dt, cell ] );417}418},419
420
421/**422* Clipboard interaction handlers
423*
424* @private
425*/
426_clipboard: function () {427var dt = this.s.dt;428var that = this;429var namespace = this.s.namespace;430
431// IE8 doesn't support getting selected text432if ( ! window.getSelection ) {433return;434}435
436$(document).on( 'copy'+namespace, function (ejq) {437var e = ejq.originalEvent;438var selection = window.getSelection().toString();439var focused = that.s.lastFocus;440
441// Only copy cell text to clipboard if there is no other selection442// and there is a focused cell443if ( ! selection && focused ) {444e.clipboardData.setData(445'text/plain',446focused.cell.render( that.c.clipboardOrthogonal )447);448e.preventDefault();449}450} );451
452$(document).on( 'paste'+namespace, function (ejq) {453var e = ejq.originalEvent;454var focused = that.s.lastFocus;455var activeEl = document.activeElement;456var editor = that.c.editor;457var pastedText;458
459if ( focused && (! activeEl || activeEl.nodeName.toLowerCase() === 'body') ) {460e.preventDefault();461
462if ( window.clipboardData && window.clipboardData.getData ) {463// IE464pastedText = window.clipboardData.getData('Text');465}466else if ( e.clipboardData && e.clipboardData.getData ) {467// Everything else468pastedText = e.clipboardData.getData('text/plain');469}470
471if ( editor ) {472// Got Editor - need to activate inline editing,473// set the value and submit474var options = that._inlineOptions(focused.cell.index());475
476editor
477.inline(options.cell, options.field, options.options)478.set( editor.displayed()[0], pastedText )479.submit();480}481else {482// No editor, so just dump the data in483focused.cell.data( pastedText );484dt.draw(false);485}486}487} );488},489
490
491/**492* Get an array of the column indexes that KeyTable can operate on. This
493* is a merge of the user supplied columns and the visible columns.
494*
495* @private
496*/
497_columns: function ()498{499var dt = this.s.dt;500var user = dt.columns( this.c.columns ).indexes();501var out = [];502
503dt.columns( ':visible' ).every( function (i) {504if ( user.indexOf( i ) !== -1 ) {505out.push( i );506}507} );508
509return out;510},511
512
513/**514* Perform excel like navigation for Editor by triggering an edit on key
515* press
516*
517* @param {integer} key Key code for the pressed key
518* @param {object} orig Original event
519* @private
520*/
521_editor: function ( key, orig, hardEdit )522{523// If nothing focused, we can't take any action524if (! this.s.lastFocus) {525return;526}527
528// DataTables draw event529if (orig && orig.type === 'draw') {530return;531}532
533var that = this;534var dt = this.s.dt;535var editor = this.c.editor;536var editCell = this.s.lastFocus.cell;537var namespace = this.s.namespace + 'e' + editorNamespaceCounter++;538
539// Do nothing if there is already an inline edit in this cell540if ( $('div.DTE', editCell.node()).length ) {541return;542}543
544// Don't activate Editor on control key presses545if ( key !== null && (546(key >= 0x00 && key <= 0x09) ||547key === 0x0b ||548key === 0x0c ||549(key >= 0x0e && key <= 0x1f) ||550(key >= 0x70 && key <= 0x7b) ||551(key >= 0x7f && key <= 0x9f)552) ) {553return;554}555
556if ( orig ) {557orig.stopPropagation();558
559// Return key should do nothing - for textareas it would empty the560// contents561if ( key === 13 ) {562orig.preventDefault();563}564}565
566var editInline = function () {567var options = that._inlineOptions(editCell.index());568
569editor
570.one( 'open'+namespace, function () {571// Remove cancel open572editor.off( 'cancelOpen'+namespace );573
574// Excel style - select all text575if ( ! hardEdit ) {576$('div.DTE_Field_InputControl input, div.DTE_Field_InputControl textarea').select();577}578
579// Reduce the keys the Keys listens for580dt.keys.enable( hardEdit ? 'tab-only' : 'navigation-only' );581
582// On blur of the navigation submit583dt.on( 'key-blur.editor', function (e, dt, cell) {584if ( editor.displayed() && cell.node() === editCell.node() ) {585editor.submit();586}587} );588
589// Highlight the cell a different colour on full edit590if ( hardEdit ) {591$( dt.table().container() ).addClass('dtk-focus-alt');592}593
594// If the dev cancels the submit, we need to return focus595editor.on( 'preSubmitCancelled'+namespace, function () {596setTimeout( function () {597that._focus( editCell, null, false );598}, 50 );599} );600
601editor.on( 'submitUnsuccessful'+namespace, function () {602that._focus( editCell, null, false );603} );604
605// Restore full key navigation on close606editor.one( 'close'+namespace, function () {607dt.keys.enable( true );608dt.off( 'key-blur.editor' );609editor.off( namespace );610$( dt.table().container() ).removeClass('dtk-focus-alt');611
612if (that.s.returnSubmit) {613that.s.returnSubmit = false;614that._emitEvent( 'key-return-submit', [dt, editCell] );615}616} );617} )618.one( 'cancelOpen'+namespace, function () {619// `preOpen` can cancel the display of the form, so it620// might be that the open event handler isn't needed621editor.off( namespace );622} )623.inline(options.cell, options.field, options.options);624};625
626// Editor 1.7 listens for `return` on keyup, so if return is the trigger627// key, we need to wait for `keyup` otherwise Editor would just submit628// the content triggered by this keypress.629if ( key === 13 ) {630hardEdit = true;631
632$(document).one( 'keyup', function () { // immediately removed633editInline();634} );635}636else {637editInline();638}639},640
641
642_inlineOptions: function (cellIdx)643{644if (this.c.editorOptions) {645return this.c.editorOptions(cellIdx);646}647
648return {649cell: cellIdx,650field: undefined,651options: undefined652};653},654
655
656/**657* Emit an event on the DataTable for listeners
658*
659* @param {string} name Event name
660* @param {array} args Event arguments
661* @private
662*/
663_emitEvent: function ( name, args )664{665this.s.dt.iterator( 'table', function ( ctx, i ) {666$(ctx.nTable).triggerHandler( name, args );667} );668},669
670
671/**672* Focus on a particular cell, shifting the table's paging if required
673*
674* @param {DataTables.Api|integer} row Can be given as an API instance that
675* contains the cell to focus or as an integer. As the latter it is the
676* visible row index (from the whole data set) - NOT the data index
677* @param {integer} [column] Not required if a cell is given as the first
678* parameter. Otherwise this is the column data index for the cell to
679* focus on
680* @param {boolean} [shift=true] Should the viewport be moved to show cell
681* @private
682*/
683_focus: function ( row, column, shift, originalEvent )684{685var that = this;686var dt = this.s.dt;687var pageInfo = dt.page.info();688var lastFocus = this.s.lastFocus;689
690if ( ! originalEvent) {691originalEvent = null;692}693
694if ( ! this.s.enable ) {695return;696}697
698if ( typeof row !== 'number' ) {699// Its an API instance - check that there is actually a row700if ( ! row.any() ) {701return;702}703
704// Convert the cell to a row and column705var index = row.index();706column = index.column;707row = dt708.rows( { filter: 'applied', order: 'applied' } )709.indexes()710.indexOf( index.row );711
712// Don't focus rows that were filtered out.713if ( row < 0 ) {714return;715}716
717// For server-side processing normalise the row by adding the start718// point, since `rows().indexes()` includes only rows that are719// available at the client-side720if ( pageInfo.serverSide ) {721row += pageInfo.start;722}723}724
725// Is the row on the current page? If not, we need to redraw to show the726// page727if ( pageInfo.length !== -1 && (row < pageInfo.start || row >= pageInfo.start+pageInfo.length) ) {728this.s.focusDraw = true;729this.s.waitingForDraw = true;730
731dt
732.one( 'draw', function () {733that.s.focusDraw = false;734that.s.waitingForDraw = false;735that._focus( row, column, undefined, originalEvent );736} )737.page( Math.floor( row / pageInfo.length ) )738.draw( false );739
740return;741}742
743// In the available columns?744if ( $.inArray( column, this._columns() ) === -1 ) {745return;746}747
748// De-normalise the server-side processing row, so we select the row749// in its displayed position750if ( pageInfo.serverSide ) {751row -= pageInfo.start;752}753
754// Get the cell from the current position - ignoring any cells which might755// not have been rendered (therefore can't use `:eq()` selector).756var cells = dt.cells( null, column, {search: 'applied', order: 'applied'} ).flatten();757var cell = dt.cell( cells[ row ] );758
759if ( lastFocus ) {760// Don't trigger a refocus on the same cell761if ( lastFocus.node === cell.node() ) {762this._emitEvent( 'key-refocus', [ this.s.dt, cell, originalEvent || null ] );763return;764}765
766// Otherwise blur the old focus767this._blur();768}769
770// Clear focus from other tables771this._removeOtherFocus();772
773var node = $( cell.node() );774node.addClass( this.c.className );775
776this._updateFixedColumns(column);777
778// Shift viewpoint and page to make cell visible779if ( shift === undefined || shift === true ) {780this._scroll( $(window), $(document.body), node, 'offset' );781
782var bodyParent = dt.table().body().parentNode;783if ( bodyParent !== dt.table().header().parentNode ) {784var parent = $(bodyParent.parentNode);785
786this._scroll( parent, parent, node, 'position' );787}788}789
790// Event and finish791this.s.lastFocus = {792cell: cell,793node: cell.node(),794relative: {795row: dt.rows( { page: 'current' } ).indexes().indexOf( cell.index().row ),796column: cell.index().column797}798};799
800this._emitEvent( 'key-focus', [ this.s.dt, cell, originalEvent || null ] );801dt.state.save();802},803
804
805/**806* Handle key press
807*
808* @param {object} e Event
809* @private
810*/
811_key: function ( e )812{813// If we are waiting for a draw to happen from another key event, then814// do nothing for this new key press.815if ( this.s.waitingForDraw ) {816e.preventDefault();817return;818}819
820var enable = this.s.enable;821this.s.returnSubmit = (enable === 'navigation-only' || enable === 'tab-only') && e.keyCode === 13822? true823: false;824
825var navEnable = enable === true || enable === 'navigation-only';826if ( ! enable ) {827return;828}829
830if ( (e.keyCode === 0 || e.ctrlKey || e.metaKey || e.altKey) && !(e.ctrlKey && e.altKey) ) {831return;832}833
834// If not focused, then there is no key action to take835var lastFocus = this.s.lastFocus;836if ( ! lastFocus ) {837return;838}839
840// And the last focus still exists!841if ( ! this.s.dt.cell(lastFocus.node).any() ) {842this.s.lastFocus = null;843return;844}845
846var that = this;847var dt = this.s.dt;848var scrolling = this.s.dt.settings()[0].oScroll.sY ? true : false;849
850// If we are not listening for this key, do nothing851if ( this.c.keys && $.inArray( e.keyCode, this.c.keys ) === -1 ) {852return;853}854
855switch( e.keyCode ) {856case 9: // tab857// `enable` can be tab-only858this._shift( e, e.shiftKey ? 'left' : 'right', true );859break;860
861case 27: // esc862if ( this.c.blurable && enable === true ) {863this._blur();864}865break;866
867case 33: // page up (previous page)868case 34: // page down (next page)869if ( navEnable && !scrolling ) {870e.preventDefault();871
872dt
873.page( e.keyCode === 33 ? 'previous' : 'next' )874.draw( false );875}876break;877
878case 35: // end (end of current page)879case 36: // home (start of current page)880if ( navEnable ) {881e.preventDefault();882var indexes = dt.cells( {page: 'current'} ).indexes();883var colIndexes = this._columns();884
885this._focus( dt.cell(886indexes[ e.keyCode === 35 ? indexes.length-1 : colIndexes[0] ]887), null, true, e );888}889break;890
891case 37: // left arrow892if ( navEnable ) {893this._shift( e, 'left' );894}895break;896
897case 38: // up arrow898if ( navEnable ) {899this._shift( e, 'up' );900}901break;902
903case 39: // right arrow904if ( navEnable ) {905this._shift( e, 'right' );906}907break;908
909case 40: // down arrow910if ( navEnable ) {911this._shift( e, 'down' );912}913break;914
915case 113: // F2 - Excel like hard edit916if ( this.c.editor ) {917this._editor(null, e, true);918break;919}920// else fallthrough921
922default:923// Everything else - pass through only when fully enabled924if ( enable === true ) {925this._emitEvent( 'key', [ dt, e.keyCode, this.s.lastFocus.cell, e ] );926}927break;928}929},930
931/**932* Remove focus from all tables other than this one
933*/
934_removeOtherFocus: function ()935{936var thisTable = this.s.dt.table().node();937
938$.fn.dataTable.tables({api:true}).iterator('table', function (settings) {939if (this.table().node() !== thisTable) {940this.cell.blur();941}942});943},944
945/**946* Scroll a container to make a cell visible in it. This can be used for
947* both DataTables scrolling and native window scrolling.
948*
949* @param {jQuery} container Scrolling container
950* @param {jQuery} scroller Item being scrolled
951* @param {jQuery} cell Cell in the scroller
952* @param {string} posOff `position` or `offset` - which to use for the
953* calculation. `offset` for the document, otherwise `position`
954* @private
955*/
956_scroll: function ( container, scroller, cell, posOff )957{958var offset = cell[posOff]();959var height = cell.outerHeight();960var width = cell.outerWidth();961
962var scrollTop = scroller.scrollTop();963var scrollLeft = scroller.scrollLeft();964var containerHeight = container.height();965var containerWidth = container.width();966
967// If Scroller is being used, the table can be `position: absolute` and that968// needs to be taken account of in the offset. If no Scroller, this will be 0969if ( posOff === 'position' ) {970offset.top += parseInt( cell.closest('table').css('top'), 10 );971}972
973// Top correction974if ( offset.top < scrollTop ) {975scroller.scrollTop( offset.top );976}977
978// Left correction979if ( offset.left < scrollLeft ) {980scroller.scrollLeft( offset.left );981}982
983// Bottom correction984if ( offset.top + height > scrollTop + containerHeight && height < containerHeight ) {985scroller.scrollTop( offset.top + height - containerHeight );986}987
988// Right correction989if ( offset.left + width > scrollLeft + containerWidth && width < containerWidth ) {990scroller.scrollLeft( offset.left + width - containerWidth );991}992},993
994
995/**996* Calculate a single offset movement in the table - up, down, left and
997* right and then perform the focus if possible
998*
999* @param {object} e Event object
1000* @param {string} direction Movement direction
1001* @param {boolean} keyBlurable `true` if the key press can result in the
1002* table being blurred. This is so arrow keys won't blur the table, but
1003* tab will.
1004* @private
1005*/
1006_shift: function ( e, direction, keyBlurable )1007{1008var that = this;1009var dt = this.s.dt;1010var pageInfo = dt.page.info();1011var rows = pageInfo.recordsDisplay;1012var columns = this._columns();1013var last = this.s.lastFocus;1014if ( ! last ) {1015return;1016}1017
1018var currentCell = last.cell;1019if ( ! currentCell ) {1020return;1021}1022
1023var currRow = dt1024.rows( { filter: 'applied', order: 'applied' } )1025.indexes()1026.indexOf( currentCell.index().row );1027
1028// When server-side processing, `rows().indexes()` only gives the rows1029// that are available at the client-side, so we need to normalise the1030// row's current position by the display start point1031if ( pageInfo.serverSide ) {1032currRow += pageInfo.start;1033}1034
1035var currCol = dt1036.columns( columns )1037.indexes()1038.indexOf( currentCell.index().column );1039
1040var1041row = currRow,1042column = columns[ currCol ]; // row is the display, column is an index1043
1044// If the direction is rtl then the logic needs to be inverted from this point forwards1045if($(dt.table().node()).css('direction') === 'rtl') {1046if(direction === 'right') {1047direction = 'left';1048}1049else if(direction === 'left'){1050direction = 'right';1051}1052}1053
1054if ( direction === 'right' ) {1055if ( currCol >= columns.length - 1 ) {1056row++;1057column = columns[0];1058}1059else {1060column = columns[ currCol+1 ];1061}1062}1063else if ( direction === 'left' ) {1064if ( currCol === 0 ) {1065row--;1066column = columns[ columns.length - 1 ];1067}1068else {1069column = columns[ currCol-1 ];1070}1071}1072else if ( direction === 'up' ) {1073row--;1074}1075else if ( direction === 'down' ) {1076row++;1077}1078
1079if ( row >= 0 && row < rows && $.inArray( column, columns ) !== -1 ) {1080if (e) {1081e.preventDefault();1082}1083
1084this._focus( row, column, true, e );1085}1086else if ( ! keyBlurable || ! this.c.blurable ) {1087// No new focus, but if the table isn't blurable, then don't loose1088// focus1089if (e) {1090e.preventDefault();1091}1092}1093else {1094this._blur();1095}1096},1097
1098
1099/**1100* Create and insert a hidden input element that can receive focus on behalf
1101* of the table
1102*
1103* @private
1104*/
1105_tabInput: function ()1106{1107var that = this;1108var dt = this.s.dt;1109var tabIndex = this.c.tabIndex !== null ?1110this.c.tabIndex :1111dt.settings()[0].iTabIndex;1112
1113if ( tabIndex == -1 ) {1114return;1115}1116
1117// Only create the input element once on first class1118if (! this.s.tabInput) {1119var div = $('<div><input type="text" tabindex="'+tabIndex+'"/></div>')1120.css( {1121position: 'absolute',1122height: 1,1123width: 0,1124overflow: 'hidden'1125} );1126
1127div.children().on( 'focus', function (e) {1128var cell = dt.cell(':eq(0)', that._columns(), {page: 'current'});1129
1130if ( cell.any() ) {1131that._focus( cell, null, true, e );1132}1133} );1134
1135this.s.tabInput = div;1136}1137
1138// Insert the input element into the first cell in the table's body1139var cell = this.s.dt.cell(':eq(0)', '0:visible', {page: 'current', order: 'current'}).node();1140if (cell) {1141$(cell).prepend(this.s.tabInput);1142}1143},1144
1145/**1146* Update fixed columns if they are enabled and if the cell we are
1147* focusing is inside a fixed column
1148* @param {integer} column Index of the column being changed
1149* @private
1150*/
1151_updateFixedColumns: function( column )1152{1153var dt = this.s.dt;1154var settings = dt.settings()[0];1155
1156if ( settings._oFixedColumns ) {1157var leftCols = settings._oFixedColumns.s.iLeftColumns;1158var rightCols = settings.aoColumns.length - settings._oFixedColumns.s.iRightColumns;1159
1160if (column < leftCols || column >= rightCols) {1161dt.fixedColumns().update();1162}1163}1164}1165} );1166
1167
1168/**
1169* KeyTable default settings for initialisation
1170*
1171* @namespace
1172* @name KeyTable.defaults
1173* @static
1174*/
1175KeyTable.defaults = {1176/**1177* Can focus be removed from the table
1178* @type {Boolean}
1179*/
1180blurable: true,1181
1182/**1183* Class to give to the focused cell
1184* @type {String}
1185*/
1186className: 'focus',1187
1188/**1189* Enable or disable clipboard support
1190* @type {Boolean}
1191*/
1192clipboard: true,1193
1194/**1195* Orthogonal data that should be copied to clipboard
1196* @type {string}
1197*/
1198clipboardOrthogonal: 'display',1199
1200/**1201* Columns that can be focused. This is automatically merged with the
1202* visible columns as only visible columns can gain focus.
1203* @type {String}
1204*/
1205columns: '', // all1206
1207/**1208* Editor instance to automatically perform Excel like navigation
1209* @type {Editor}
1210*/
1211editor: null,1212
1213/**1214* Trigger editing immediately on focus
1215* @type {boolean}
1216*/
1217editOnFocus: false,1218
1219/**1220* Options to pass to Editor's inline method
1221* @type {function}
1222*/
1223editorOptions: null,1224
1225/**1226* Select a cell to automatically select on start up. `null` for no
1227* automatic selection
1228* @type {cell-selector}
1229*/
1230focus: null,1231
1232/**1233* Array of keys to listen for
1234* @type {null|array}
1235*/
1236keys: null,1237
1238/**1239* Tab index for where the table should sit in the document's tab flow
1240* @type {integer|null}
1241*/
1242tabIndex: null1243};1244
1245
1246
1247KeyTable.version = "2.6.4";1248
1249
1250$.fn.dataTable.KeyTable = KeyTable;1251$.fn.DataTable.KeyTable = KeyTable;1252
1253
1254DataTable.Api.register( 'cell.blur()', function () {1255return this.iterator( 'table', function (ctx) {1256if ( ctx.keytable ) {1257ctx.keytable.blur();1258}1259} );1260} );1261
1262DataTable.Api.register( 'cell().focus()', function () {1263return this.iterator( 'cell', function (ctx, row, column) {1264if ( ctx.keytable ) {1265ctx.keytable.focus( row, column );1266}1267} );1268} );1269
1270DataTable.Api.register( 'keys.disable()', function () {1271return this.iterator( 'table', function (ctx) {1272if ( ctx.keytable ) {1273ctx.keytable.enable( false );1274}1275} );1276} );1277
1278DataTable.Api.register( 'keys.enable()', function ( opts ) {1279return this.iterator( 'table', function (ctx) {1280if ( ctx.keytable ) {1281ctx.keytable.enable( opts === undefined ? true : opts );1282}1283} );1284} );1285
1286DataTable.Api.register( 'keys.enabled()', function ( opts ) {1287var ctx = this.context;1288
1289if (ctx.length) {1290return ctx[0].keytable1291? ctx[0].keytable.enabled()1292: false;1293}1294
1295return false;1296} );1297
1298DataTable.Api.register( 'keys.move()', function ( dir ) {1299return this.iterator( 'table', function (ctx) {1300if ( ctx.keytable ) {1301ctx.keytable._shift( null, dir, false );1302}1303} );1304} );1305
1306// Cell selector
1307DataTable.ext.selector.cell.push( function ( settings, opts, cells ) {1308var focused = opts.focused;1309var kt = settings.keytable;1310var out = [];1311
1312if ( ! kt || focused === undefined ) {1313return cells;1314}1315
1316for ( var i=0, ien=cells.length ; i<ien ; i++ ) {1317if ( (focused === true && kt.focused( cells[i] ) ) ||1318(focused === false && ! kt.focused( cells[i] ) )1319) {1320out.push( cells[i] );1321}1322}1323
1324return out;1325} );1326
1327
1328// Attach a listener to the document which listens for DataTables initialisation
1329// events so we can automatically initialise
1330$(document).on( 'preInit.dt.dtk', function (e, settings, json) {1331if ( e.namespace !== 'dt' ) {1332return;1333}1334
1335var init = settings.oInit.keys;1336var defaults = DataTable.defaults.keys;1337
1338if ( init || defaults ) {1339var opts = $.extend( {}, defaults, init );1340
1341if ( init !== false ) {1342new KeyTable( settings, opts );1343}1344}1345} );1346
1347
1348return KeyTable;1349}));1350