]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/static/AdminLTE-2.3.7/plugins/datatables/extensions/KeyTable/js/dataTables.keyTable.js
2 * ©2010-2014 SpryMedia Ltd - datatables.net/license
7 * @description Spreadsheet like keyboard navigation for DataTables
9 * @file dataTables.keyTable.js
10 * @author SpryMedia Ltd (www.sprymedia.co.uk)
11 * @contact www.sprymedia.co.uk/contact
12 * @copyright Copyright 2009-2014 SpryMedia Ltd.
14 * This source file is free software, available under the following license:
15 * MIT license - http://datatables.net/license/mit
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.
21 * For details please refer to: http://www.datatables.net
24 // Global scope for KeyTable for backwards compatibility. Will be removed in 1.3
28 (function(window
, document
, undefined) {
31 var factory = function( $, DataTable
) {
34 KeyTable = function ( oInit
)
36 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
38 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
42 * Purpose: Flag whether or not KeyTable events should be processed
43 * Scope: KeyTable - public
49 * Purpose: Container for all event application methods
50 * Scope: KeyTable - public
51 * Notes: This object contains all the public methods for adding and removing events - these
52 * are dynamically added later on
59 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
61 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
64 * Function: fnGetCurrentPosition
65 * Purpose: Get the currently focused cell's position
66 * Returns: array int: [ x, y ]
69 this.fnGetCurrentPosition = function ()
71 return [ _iOldX
, _iOldY
];
76 * Function: fnGetCurrentData
77 * Purpose: Get the currently focused cell's data (innerHTML)
78 * Returns: string: - data requested
81 this.fnGetCurrentData = function ()
83 return _nOldFocus
.innerHTML
;
88 * Function: fnGetCurrentTD
89 * Purpose: Get the currently focused cell
90 * Returns: node: - focused element
93 this.fnGetCurrentTD = function ()
100 * Function: fnSetPosition
101 * Purpose: Set the position of the focused cell
103 * Inputs: int:x - x coordinate
104 * int:y - y coordinate
105 * Notes: Thanks to Rohan Daxini for the basis of this function
107 this.fnSetPosition = function( x
, y
)
109 if ( typeof x
== 'object' && x
.nodeName
)
115 _fnSetFocus( _fnCellFromCoords(x
, y
) );
122 * Purpose: Blur the current focus
126 this.fnBlur = function()
132 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
134 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
138 * Purpose: Body node of the table - cached for renference
139 * Scope: KeyTable - private
146 * Scope: KeyTable - private
148 var _nOldFocus
= null;
151 * Variable: _iOldX and _iOldY
152 * Purpose: X and Y coords of the old elemet that was focused on
153 * Scope: KeyTable - private
160 * Purpose: Scope saving for 'this' after a jQuery event
161 * Scope: KeyTable - private
166 * Variable: sFocusClass
167 * Purpose: Class that should be used for focusing on a cell
168 * Scope: KeyTable - private
170 var _sFocusClass
= "focus";
173 * Variable: _bKeyCapture
174 * Purpose: Flag for should KeyTable capture key events or not
175 * Scope: KeyTable - private
177 var _bKeyCapture
= false;
180 * Variable: _oaoEvents
181 * Purpose: Event cache object, one array for each supported event for speed of searching
182 * Scope: KeyTable - private
192 * Variable: _oDatatable
193 * Purpose: DataTables settings object for if we are actually using a
195 * Scope: KeyTable - private
197 var _oDatatable
= null;
201 var _bInputFocused
= false;
204 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
206 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
208 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
213 * Function: _fnEventAddTemplate
214 * Purpose: Create a function (with closure for sKey) event addition API
215 * Returns: function: - template function
216 * Inputs: string:sKey - type of event to detect
218 function _fnEventAddTemplate( sKey
)
222 * Purpose: API function for adding event to cache
224 * Inputs: 1. node:x - target node to add event for
225 * 2. function:y - callback function to apply
227 * 1. int:x - x coord. of target cell (can be null for live events)
228 * 2. int:y - y coord. of target cell (can be null for live events)
229 * 3. function:z - callback function to apply
230 * Notes: This function is (interally) overloaded (in as much as javascript allows for
231 * that) - the target cell can be given by either node or coords.
233 return function ( x
, y
, z
) {
234 if ( (x
===null || typeof x
== "number") &&
235 (y
===null || typeof y
== "number") &&
236 typeof z
== "function" )
238 _fnEventAdd( sKey
, x
, y
, z
);
240 else if ( typeof x
== "object" && typeof y
== "function" )
242 var aCoords
= _fnCoordsFromCell( x
);
243 _fnEventAdd( sKey
, aCoords
[0], aCoords
[1], y
);
247 alert( "Unhandable event type was added: x" +x
+ " y:" +y
+ " z:" +z
);
254 * Function: _fnEventRemoveTemplate
255 * Purpose: Create a function (with closure for sKey) event removal API
256 * Returns: function: - template function
257 * Inputs: string:sKey - type of event to detect
259 function _fnEventRemoveTemplate( sKey
)
263 * Purpose: API function for removing event from cache
264 * Returns: int: - number of events removed
265 * Inputs: 1. node:x - target node to remove event from
266 * 2. function:y - callback function to apply
268 * 1. int:x - x coord. of target cell (can be null for live events)
269 * 2. int:y - y coord. of target cell (can be null for live events)
270 * 3. function:z - callback function to remove - optional
271 * Notes: This function is (interally) overloaded (in as much as javascript allows for
272 * that) - the target cell can be given by either node or coords and the function
273 * to remove is optional
275 return function ( x
, y
, z
) {
276 if ( (x
===null || typeof arguments
[0] == "number") &&
277 (y
===null || typeof arguments
[1] == "number" ) )
279 if ( typeof arguments
[2] == "function" )
281 _fnEventRemove( sKey
, x
, y
, z
);
285 _fnEventRemove( sKey
, x
, y
);
288 else if ( typeof arguments
[0] == "object" )
290 var aCoords
= _fnCoordsFromCell( x
);
291 if ( typeof arguments
[1] == "function" )
293 _fnEventRemove( sKey
, aCoords
[0], aCoords
[1], y
);
297 _fnEventRemove( sKey
, aCoords
[0], aCoords
[1] );
302 alert( "Unhandable event type was removed: x" +x
+ " y:" +y
+ " z:" +z
);
307 /* Use the template functions to add the event API functions */
308 for ( var sKey
in _oaoEvents
)
312 this.event
[sKey
] = _fnEventAddTemplate( sKey
);
313 this.event
.remove
[sKey
] = _fnEventRemoveTemplate( sKey
);
319 * Function: _fnEventAdd
320 * Purpose: Add an event to the internal cache
322 * Inputs: string:sType - type of event to add, given by the available elements in _oaoEvents
323 * int:x - x-coords to add event to - can be null for "blanket" event
324 * int:y - y-coords to add event to - can be null for "blanket" event
325 * function:fn - callback function for when triggered
327 function _fnEventAdd( sType
, x
, y
, fn
)
329 _oaoEvents
[sType
].push( {
338 * Function: _fnEventRemove
339 * Purpose: Remove an event from the event cache
340 * Returns: int: - number of matching events removed
341 * Inputs: string:sType - type of event to look for
342 * node:nTarget - target table cell
343 * function:fn - optional - remove this function. If not given all handlers of this
344 * type will be removed
346 function _fnEventRemove( sType
, x
, y
, fn
)
350 for ( var i
=0, iLen
=_oaoEvents
[sType
].length
; i
<iLen
-iCorrector
; i
++ )
352 if ( typeof fn
!= 'undefined' )
354 if ( _oaoEvents
[sType
][i
-iCorrector
].x
== x
&&
355 _oaoEvents
[sType
][i
-iCorrector
].y
== y
&&
356 _oaoEvents
[sType
][i
-iCorrector
].fn
== fn
)
358 _oaoEvents
[sType
].splice( i
-iCorrector
, 1 );
364 if ( _oaoEvents
[sType
][i
-iCorrector
].x
== x
&&
365 _oaoEvents
[sType
][i
-iCorrector
].y
== y
)
367 _oaoEvents
[sType
].splice( i
, 1 );
377 * Function: _fnEventFire
378 * Purpose: Look thought the events cache and fire off the event of interest
379 * Returns: int:iFired - number of events fired
380 * Inputs: string:sType - type of event to look for
381 * int:x - x coord of cell
382 * int:y - y coord of ell
383 * Notes: It might be more efficient to return after the first event has been tirggered,
384 * but that would mean that only one function of a particular type can be
385 * subscribed to a particular node.
387 function _fnEventFire ( sType
, x
, y
)
390 var aEvents
= _oaoEvents
[sType
];
391 for ( var i
=0 ; i
<aEvents
.length
; i
++ )
393 if ( (aEvents
[i
].x
== x
&& aEvents
[i
].y
== y
) ||
394 (aEvents
[i
].x
=== null && aEvents
[i
].y
== y
) ||
395 (aEvents
[i
].x
== x
&& aEvents
[i
].y
=== null ) ||
396 (aEvents
[i
].x
=== null && aEvents
[i
].y
=== null )
399 aEvents
[i
].fn( _fnCellFromCoords(x
,y
), x
, y
);
408 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
413 * Function: _fnSetFocus
414 * Purpose: Set focus on a node, and remove from an old node if needed
416 * Inputs: node:nTarget - node we want to focus on
417 * bool:bAutoScroll - optional - should we scroll the view port to the display
419 function _fnSetFocus( nTarget
, bAutoScroll
)
421 /* If node already has focus, just ignore this call */
422 if ( _nOldFocus
== nTarget
)
427 if ( typeof bAutoScroll
== 'undefined' )
432 /* Remove old focus (with blur event if needed) */
433 if ( _nOldFocus
!== null )
435 _fnRemoveFocus( _nOldFocus
);
438 /* Add the new class to highlight the focused cell */
439 $(nTarget
).addClass( _sFocusClass
);
440 $(nTarget
).parent().addClass( _sFocusClass
);
442 /* If it's a DataTable then we need to jump the paging to the relevant page */
446 oSettings
= _oDatatable
;
447 var iRow
= _fnFindDtCell( nTarget
)[1];
448 var bKeyCaptureCache
= _bKeyCapture
;
451 while ( iRow
>= oSettings
.fnDisplayEnd() )
453 if ( oSettings
._iDisplayLength
>= 0 )
455 /* Make sure we are not over running the display array */
456 if ( oSettings
._iDisplayStart
+ oSettings
._iDisplayLength
< oSettings
.fnRecordsDisplay() )
458 oSettings
._iDisplayStart
+= oSettings
._iDisplayLength
;
463 oSettings
._iDisplayStart
= 0;
465 _oDatatable
.oApi
._fnCalculateEnd( oSettings
);
469 while ( iRow
< oSettings
._iDisplayStart
)
471 oSettings
._iDisplayStart
= oSettings
._iDisplayLength
>=0 ?
472 oSettings
._iDisplayStart
- oSettings
._iDisplayLength
:
475 if ( oSettings
._iDisplayStart
< 0 )
477 oSettings
._iDisplayStart
= 0;
479 _oDatatable
.oApi
._fnCalculateEnd( oSettings
);
482 /* Re-draw the table */
483 _oDatatable
.oApi
._fnDraw( oSettings
);
485 /* Restore the key capture */
486 _bKeyCapture
= bKeyCaptureCache
;
489 /* Cache the information that we are interested in */
490 var aNewPos
= _fnCoordsFromCell( nTarget
);
491 _nOldFocus
= nTarget
;
495 var iViewportHeight
, iViewportWidth
, iScrollTop
, iScrollLeft
, iHeight
, iWidth
, aiPos
;
498 /* Scroll the viewport such that the new cell is fully visible in the rendered window */
499 iViewportHeight
= $(window
).height();
500 iViewportWidth
= $(window
).width();
501 iScrollTop
= $(document
).scrollTop();
502 iScrollLeft
= $(document
).scrollLeft();
503 iHeight
= nTarget
.offsetHeight
;
504 iWidth
= nTarget
.offsetWidth
;
505 aiPos
= _fnGetPos( nTarget
);
507 /* Take account of scrolling in DataTables 1.7 - remove scrolling since that would add to
508 * the positioning calculation
510 if ( _oDatatable
&& typeof oSettings
.oScroll
!= 'undefined' &&
511 (oSettings
.oScroll
.sX
!== "" || oSettings
.oScroll
.sY
!== "") )
513 aiPos
[1] -= $(oSettings
.nTable
.parentNode
).scrollTop();
514 aiPos
[0] -= $(oSettings
.nTable
.parentNode
).scrollLeft();
517 /* Correct viewport positioning for vertical scrolling */
518 if ( aiPos
[1]+iHeight
> iScrollTop
+iViewportHeight
)
520 /* Displayed element if off the bottom of the viewport */
521 _fnSetScrollTop( aiPos
[1]+iHeight
- iViewportHeight
);
523 else if ( aiPos
[1] < iScrollTop
)
525 /* Displayed element if off the top of the viewport */
526 _fnSetScrollTop( aiPos
[1] );
529 /* Correct viewport positioning for horizontal scrolling */
530 if ( aiPos
[0]+iWidth
> iScrollLeft
+iViewportWidth
)
532 /* Displayed element is off the bottom of the viewport */
533 _fnSetScrollLeft( aiPos
[0]+iWidth
- iViewportWidth
);
535 else if ( aiPos
[0] < iScrollLeft
)
537 /* Displayed element if off the Left of the viewport */
538 _fnSetScrollLeft( aiPos
[0] );
542 /* Take account of scrolling in DataTables 1.7 */
543 if ( _oDatatable
&& typeof oSettings
.oScroll
!= 'undefined' &&
544 (oSettings
.oScroll
.sX
!== "" || oSettings
.oScroll
.sY
!== "") )
546 var dtScrollBody
= oSettings
.nTable
.parentNode
;
547 iViewportHeight
= dtScrollBody
.clientHeight
;
548 iViewportWidth
= dtScrollBody
.clientWidth
;
549 iScrollTop
= dtScrollBody
.scrollTop
;
550 iScrollLeft
= dtScrollBody
.scrollLeft
;
551 iHeight
= nTarget
.offsetHeight
;
552 iWidth
= nTarget
.offsetWidth
;
554 /* Correct for vertical scrolling */
555 if ( nTarget
.offsetTop
+ iHeight
> iViewportHeight
+iScrollTop
)
557 dtScrollBody
.scrollTop
= (nTarget
.offsetTop
+ iHeight
) - iViewportHeight
;
559 else if ( nTarget
.offsetTop
< iScrollTop
)
561 dtScrollBody
.scrollTop
= nTarget
.offsetTop
;
564 /* Correct for horizontal scrolling */
565 if ( nTarget
.offsetLeft
+ iWidth
> iViewportWidth
+iScrollLeft
)
567 dtScrollBody
.scrollLeft
= (nTarget
.offsetLeft
+ iWidth
) - iViewportWidth
;
569 else if ( nTarget
.offsetLeft
< iScrollLeft
)
571 dtScrollBody
.scrollLeft
= nTarget
.offsetLeft
;
575 /* Focused - so we want to capture the keys */
578 /* Fire of the focus event if there is one */
579 _fnEventFire( "focus", _iOldX
, _iOldY
);
585 * Purpose: Blur focus from the whole table
591 _fnRemoveFocus( _nOldFocus
);
600 * Function: _fnRemoveFocus
601 * Purpose: Remove focus from a cell and fire any blur events which are attached
603 * Inputs: node:nTarget - cell of interest
605 function _fnRemoveFocus( nTarget
)
607 $(nTarget
).removeClass( _sFocusClass
);
608 $(nTarget
).parent().removeClass( _sFocusClass
);
609 _fnEventFire( "blur", _iOldX
, _iOldY
);
615 * Purpose: Focus on the element that has been clicked on by the user
617 * Inputs: event:e - click event
619 function _fnClick ( e
)
622 while ( nTarget
.nodeName
!= "TD" )
624 nTarget
= nTarget
.parentNode
;
627 _fnSetFocus( nTarget
);
633 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
639 * Purpose: Deal with a key events, be it moving the focus or return etc.
640 * Returns: bool: - allow browser default action
641 * Inputs: event:e - key event
643 function _fnKey ( e
)
645 /* If user or system has blocked KeyTable from doing anything, just ignore this event */
646 if ( _that
.block
|| !_bKeyCapture
)
651 /* If a modifier key is pressed (exapct shift), ignore the event */
652 if ( e
.metaKey
|| e
.altKey
|| e
.ctrlKey
)
658 iTableWidth
= _nBody
.getElementsByTagName('tr')[0].getElementsByTagName('td').length
,
661 /* Get table height and width - done here so as to be dynamic (if table is updated) */
665 * Locate the current node in the DataTable overriding the old positions - the reason for
666 * is is that there might have been some DataTables interaction between the last focus and
669 iTableHeight
= _oDatatable
.aiDisplay
.length
;
671 var aDtPos
= _fnFindDtCell( _nOldFocus
);
672 if ( aDtPos
=== null )
674 /* If the table has been updated such that the focused cell can't be seen - do nothing */
677 _iOldX
= aDtPos
[ 0 ];
678 _iOldY
= aDtPos
[ 1 ];
682 iTableHeight
= _nBody
.getElementsByTagName('tr').length
;
685 /* Capture shift+tab to match the left arrow key */
686 var iKey
= (e
.keyCode
== 9 && e
.shiftKey
) ? -1 : e
.keyCode
;
690 case 13: /* return */
693 _fnEventFire( "action", _iOldX
, _iOldY
);
697 if ( !_fnEventFire( "esc", _iOldX
, _iOldY
) )
699 /* Only lose focus if there isn't an escape handler on the cell */
708 case 37: /* left arrow */
712 } else if ( _iOldY
> 0 ) {
716 /* at start of table */
717 if ( iKey
== -1 && _bForm
)
719 /* If we are in a form, return focus to the 'input' element such that tabbing will
720 * follow correctly in the browser
722 _bInputFocused
= true;
725 /* This timeout is a little nasty - but IE appears to have some asyhnc behaviour for
728 setTimeout( function(){ _bInputFocused
= false; }, 0 );
729 _bKeyCapture
= false;
740 case 38: /* up arrow */
754 case 33: /* page up */
763 case 39: /* right arrow */
764 if ( _iOldX
< iTableWidth
-1 ) {
767 } else if ( _iOldY
< iTableHeight
-1 ) {
771 /* at end of table */
772 if ( iKey
== 9 && _bForm
)
774 /* If we are in a form, return focus to the 'input' element such that tabbing will
775 * follow correctly in the browser
777 _bInputFocused
= true;
780 /* This timeout is a little nasty - but IE appears to have some asyhnc behaviour for
783 setTimeout( function(){ _bInputFocused
= false; }, 0 );
784 _bKeyCapture
= false;
795 case 40: /* down arrow */
796 if ( _iOldY
< iTableHeight
-1 ) {
809 case 34: /* page down */
812 if (y
> iTableHeight
-1) {
817 default: /* Nothing we are interested in */
821 _fnSetFocus( _fnCellFromCoords(x
, y
) );
827 * Function: _fnCaptureKeys
828 * Purpose: Start capturing key events for this table
832 function _fnCaptureKeys( )
842 * Function: _fnReleaseKeys
843 * Purpose: Stop capturing key events for this table
847 function _fnReleaseKeys( )
849 _bKeyCapture
= false;
854 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
859 * Function: _fnCellFromCoords
860 * Purpose: Calulate the target TD cell from x and y coordinates
861 * Returns: node: - TD target
862 * Inputs: int:x - x coordinate
863 * int:y - y coordinate
865 function _fnCellFromCoords( x
, y
)
869 if ( typeof _oDatatable
.aoData
[ _oDatatable
.aiDisplay
[ y
] ] != 'undefined' )
871 return _oDatatable
.aoData
[ _oDatatable
.aiDisplay
[ y
] ].nTr
.getElementsByTagName('td')[x
];
880 return $('tr:eq('+y
+')>td:eq('+x
+')', _nBody
)[0];
886 * Function: _fnCoordsFromCell
887 * Purpose: Calculate the x and y position in a table from a TD cell
888 * Returns: array[2] int: [x, y]
889 * Inputs: node:n - TD cell of interest
890 * Notes: Not actually interested in this for DataTables since it might go out of date
892 function _fnCoordsFromCell( n
)
897 $('td', n
.parentNode
).index(n
),
898 $('tr', n
.parentNode
.parentNode
).index(n
.parentNode
) + _oDatatable
._iDisplayStart
904 $('td', n
.parentNode
).index(n
),
905 $('tr', n
.parentNode
.parentNode
).index(n
.parentNode
)
912 * Function: _fnSetScrollTop
913 * Purpose: Set the vertical scrolling position
915 * Inputs: int:iPos - scrolltop
916 * Notes: This is so nasty, but without browser detection you can't tell which you should set
917 * So on browsers that support both, the scroll top will be set twice. I can live with
920 function _fnSetScrollTop( iPos
)
922 document
.documentElement
.scrollTop
= iPos
;
923 document
.body
.scrollTop
= iPos
;
928 * Function: _fnSetScrollLeft
929 * Purpose: Set the horizontal scrolling position
931 * Inputs: int:iPos - scrollleft
933 function _fnSetScrollLeft( iPos
)
935 document
.documentElement
.scrollLeft
= iPos
;
936 document
.body
.scrollLeft
= iPos
;
941 * Function: _fnGetPos
942 * Purpose: Get the position of an object on the rendered page
943 * Returns: array[2] int: [left, right]
944 * Inputs: node:obj - element of interest
946 function _fnGetPos ( obj
)
951 if (obj
.offsetParent
)
953 iLeft
= obj
.offsetLeft
;
954 iTop
= obj
.offsetTop
;
955 obj
= obj
.offsetParent
;
958 iLeft
+= obj
.offsetLeft
;
959 iTop
+= obj
.offsetTop
;
960 obj
= obj
.offsetParent
;
968 * Function: _fnFindDtCell
969 * Purpose: Get the coords. of a cell from the DataTables internal information
970 * Returns: array[2] int: [x, y] coords. or null if not found
971 * Inputs: node:nTarget - the node of interest
973 function _fnFindDtCell( nTarget
)
975 for ( var i
=0, iLen
=_oDatatable
.aiDisplay
.length
; i
<iLen
; i
++ )
977 var nTr
= _oDatatable
.aoData
[ _oDatatable
.aiDisplay
[i
] ].nTr
;
978 var nTds
= nTr
.getElementsByTagName('td');
979 for ( var j
=0, jLen
=nTds
.length
; j
<jLen
; j
++ )
981 if ( nTds
[j
] == nTarget
)
992 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
998 * Purpose: Initialise the KeyTable
1000 * Inputs: object:oInit - optional - Initalisation object with the following parameters:
1001 * array[2] int:focus - x and y coordinates of the initial target
1003 * node:focus - the node to set initial focus on
1004 * node:table - the table to use, if not given, first table with class 'KeyTable' will be used
1005 * string:focusClass - focusing class to give to table elements
1006 * object:that - focus
1007 * bool:initScroll - scroll the view port on load, default true
1008 * int:tabIndex - the tab index to give the hidden input element
1010 function _fnInit( table
, datatable
, oInit
, that
)
1015 /* Capture undefined initialisation and apply the defaults */
1016 if ( typeof oInit
== 'undefined' ) {
1020 if ( typeof oInit
.focus
== 'undefined' ) {
1021 oInit
.focus
= [0,0];
1024 oInit
.table
= table
;
1025 $(oInit
.table
).addClass('KeyTable');
1027 if ( typeof oInit
.focusClass
!= 'undefined' ) {
1028 _sFocusClass
= oInit
.focusClass
;
1031 if ( typeof datatable
!= 'undefined' ) {
1032 _oDatatable
= datatable
;
1035 if ( typeof oInit
.initScroll
== 'undefined' ) {
1036 oInit
.initScroll
= true;
1039 if ( typeof oInit
.form
== 'undefined' ) {
1042 _bForm
= oInit
.form
;
1044 /* Cache the tbody node of interest */
1045 _nBody
= oInit
.table
.getElementsByTagName('tbody')[0];
1047 /* If the table is inside a form, then we need a hidden input box which can be used by the
1048 * browser to catch the browser tabbing for our table
1052 var nDiv
= document
.createElement('div');
1053 _nInput
= document
.createElement('input');
1054 nDiv
.style
.height
= "1px"; /* Opera requires a little something */
1055 nDiv
.style
.width
= "0px";
1056 nDiv
.style
.overflow
= "hidden";
1057 if ( typeof oInit
.tabIndex
!= 'undefined' )
1059 _nInput
.tabIndex
= oInit
.tabIndex
;
1061 nDiv
.appendChild(_nInput
);
1062 oInit
.table
.parentNode
.insertBefore( nDiv
, oInit
.table
.nextSibling
);
1064 $(_nInput
).focus( function () {
1065 /* See if we want to 'tab into' the table or out */
1066 if ( !_bInputFocused
)
1068 _bKeyCapture
= true;
1069 _bInputFocused
= false;
1070 if ( typeof oInit
.focus
.nodeName
!= "undefined" )
1072 _fnSetFocus( oInit
.focus
, oInit
.initScroll
);
1076 _fnSetFocus( _fnCellFromCoords( oInit
.focus
[0], oInit
.focus
[1]), oInit
.initScroll
);
1079 /* Need to interup the thread for this to work */
1080 setTimeout( function() { _nInput
.blur(); }, 0 );
1083 _bKeyCapture
= false;
1087 /* Set the initial focus on the table */
1088 if ( typeof oInit
.focus
.nodeName
!= "undefined" )
1090 _fnSetFocus( oInit
.focus
, oInit
.initScroll
);
1094 _fnSetFocus( _fnCellFromCoords( oInit
.focus
[0], oInit
.focus
[1]), oInit
.initScroll
);
1099 /* Add event listeners */
1100 $(document
).bind( "keydown", _fnKey
);
1104 $(_oDatatable
.nTable
).on( 'click', 'td', _fnClick
);
1108 $(_nBody
).on( 'click', 'td', _fnClick
);
1111 /* Loose table focus when click outside the table */
1112 $(document
).click( function(e
) {
1113 var nTarget
= e
.target
;
1114 var bTableClick
= false;
1117 if ( nTarget
== oInit
.table
)
1122 nTarget
= nTarget
.parentNode
;
1131 var table
, datatable
;
1133 if ( oInit
=== undefined ) {
1134 table
= $('table.KeyTable')[0];
1137 else if ( $.isPlainObject( oInit
) ) {
1138 table
= oInit
.table
;
1139 datatable
= oInit
.datatable
;
1142 datatable
= new $.fn
.dataTable
.Api( oInit
).settings()[0];
1143 table
= datatable
.nTable
;
1145 /* Initialise our new object */
1146 _fnInit( table
, datatable
, oInit
, this );
1150 KeyTable
.version
= "1.2.1";
1153 $.fn
.dataTable
.KeyTable
= KeyTable
;
1154 $.fn
.DataTable
.KeyTable
= KeyTable
;
1161 // Define as an AMD module if possible
1162 if ( typeof define
=== 'function' && define
.amd
) {
1163 define( ['jquery', 'datatables'], factory
);
1165 else if ( typeof exports
=== 'object' ) {
1167 factory( require('jquery'), require('datatables') );
1169 else if ( jQuery
&& !jQuery
.fn
.dataTable
.KeyTable
) {
1170 // Otherwise simply initialise as normal, stopping multiple evaluation
1171 factory( jQuery
, jQuery
.fn
.dataTable
);
1175 })(window
, document
);