5 * Description: Keyboard navigation for HTML tables
6 * Author: Allan Jardine (www.sprymedia.co.uk)
7 * Created: Fri Mar 13 21:24:02 GMT 2009
8 * Modified: $Date$ by $Author$
10 * License: GPL v2 or BSD 3 point style
11 * Project: Just a little bit of fun :-)
12 * Contact: www.sprymedia.co.uk/contact
14 * Copyright 2009-2011 Allan Jardine, all rights reserved.
16 * This source file is free software, under either the GPL v2 license or a
17 * BSD style license, available at:
18 * http://datatables.net/license_gpl2
19 * http://datatables.net/license_bsd
23 function KeyTable ( oInit )
25 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
27 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
31 * Purpose: Flag whether or not KeyTable events should be processed
32 * Scope: KeyTable - public
38 * Purpose: Container for all event application methods
39 * Scope: KeyTable - public
40 * Notes: This object contains all the public methods for adding and removing events - these
41 * are dynamically added later on
48 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
50 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
53 * Function: fnGetCurrentPosition
54 * Purpose: Get the currently focused cell's position
55 * Returns: array int: [ x, y ]
58 this.fnGetCurrentPosition = function ()
60 return [ _iOldX, _iOldY ];
65 * Function: fnGetCurrentData
66 * Purpose: Get the currently focused cell's data (innerHTML)
67 * Returns: string: - data requested
70 this.fnGetCurrentData = function ()
72 return _nOldFocus.innerHTML;
77 * Function: fnGetCurrentTD
78 * Purpose: Get the currently focused cell
79 * Returns: node: - focused element
82 this.fnGetCurrentTD = function ()
89 * Function: fnSetPosition
90 * Purpose: Set the position of the focused cell
92 * Inputs: int:x - x coordinate
93 * int:y - y coordinate
94 * Notes: Thanks to Rohan Daxini for the basis of this function
96 this.fnSetPosition = function( x, y )
98 if ( typeof x == 'object' && x.nodeName )
104 _fnSetFocus( _fnCellFromCoords(x, y) );
109 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
111 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
115 * Purpose: Body node of the table - cached for renference
116 * Scope: KeyTable - private
123 * Scope: KeyTable - private
125 var _nOldFocus = null;
128 * Variable: _iOldX and _iOldY
129 * Purpose: X and Y coords of the old elemet that was focused on
130 * Scope: KeyTable - private
137 * Purpose: Scope saving for 'this' after a jQuery event
138 * Scope: KeyTable - private
143 * Variable: sFocusClass
144 * Purpose: Class that should be used for focusing on a cell
145 * Scope: KeyTable - private
147 var _sFocusClass = "focus";
150 * Variable: _bKeyCapture
151 * Purpose: Flag for should KeyTable capture key events or not
152 * Scope: KeyTable - private
154 var _bKeyCapture = false;
157 * Variable: _oaoEvents
158 * Purpose: Event cache object, one array for each supported event for speed of searching
159 * Scope: KeyTable - private
169 * Variable: _oDatatable
170 * Purpose: DataTables object for if we are actually using a DataTables table
171 * Scope: KeyTable - private
173 var _oDatatable = null;
177 var _bInputFocused = false;
180 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
182 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
184 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
189 * Function: _fnEventAddTemplate
190 * Purpose: Create a function (with closure for sKey) event addition API
191 * Returns: function: - template function
192 * Inputs: string:sKey - type of event to detect
194 function _fnEventAddTemplate( sKey )
198 * Purpose: API function for adding event to cache
200 * Inputs: 1. node:x - target node to add event for
201 * 2. function:y - callback function to apply
203 * 1. int:x - x coord. of target cell (can be null for live events)
204 * 2. int:y - y coord. of target cell (can be null for live events)
205 * 3. function:z - callback function to apply
206 * Notes: This function is (interally) overloaded (in as much as javascript allows for
207 * that) - the target cell can be given by either node or coords.
209 return function ( x, y, z ) {
210 if ( (x===null || typeof x == "number") &&
211 (y===null || typeof y == "number") &&
212 typeof z == "function" )
214 _fnEventAdd( sKey, x, y, z );
216 else if ( typeof x == "object" && typeof y == "function" )
218 var aCoords = _fnCoordsFromCell( x );
219 _fnEventAdd( sKey, aCoords[0], aCoords[1], y );
223 alert( "Unhandable event type was added: x" +x+ " y:" +y+ " z:" +z );
230 * Function: _fnEventRemoveTemplate
231 * Purpose: Create a function (with closure for sKey) event removal API
232 * Returns: function: - template function
233 * Inputs: string:sKey - type of event to detect
235 function _fnEventRemoveTemplate( sKey )
239 * Purpose: API function for removing event from cache
240 * Returns: int: - number of events removed
241 * Inputs: 1. node:x - target node to remove event from
242 * 2. function:y - callback function to apply
244 * 1. int:x - x coord. of target cell (can be null for live events)
245 * 2. int:y - y coord. of target cell (can be null for live events)
246 * 3. function:z - callback function to remove - optional
247 * Notes: This function is (interally) overloaded (in as much as javascript allows for
248 * that) - the target cell can be given by either node or coords and the function
249 * to remove is optional
251 return function ( x, y, z ) {
252 if ( (x===null || typeof arguments[0] == "number") &&
253 (y===null || typeof arguments[1] == "number" ) )
255 if ( typeof arguments[2] == "function" )
257 _fnEventRemove( sKey, x, y, z );
261 _fnEventRemove( sKey, x, y );
264 else if ( typeof arguments[0] == "object" )
266 var aCoords = _fnCoordsFromCell( x );
267 if ( typeof arguments[1] == "function" )
269 _fnEventRemove( sKey, aCoords[0], aCoords[1], y );
273 _fnEventRemove( sKey, aCoords[0], aCoords[1] );
278 alert( "Unhandable event type was removed: x" +x+ " y:" +y+ " z:" +z );
283 /* Use the template functions to add the event API functions */
284 for ( var sKey in _oaoEvents )
288 this.event[sKey] = _fnEventAddTemplate( sKey );
289 this.event.remove[sKey] = _fnEventRemoveTemplate( sKey );
295 * Function: _fnEventAdd
296 * Purpose: Add an event to the internal cache
298 * Inputs: string:sType - type of event to add, given by the available elements in _oaoEvents
299 * int:x - x-coords to add event to - can be null for "blanket" event
300 * int:y - y-coords to add event to - can be null for "blanket" event
301 * function:fn - callback function for when triggered
303 function _fnEventAdd( sType, x, y, fn )
305 _oaoEvents[sType].push( {
314 * Function: _fnEventRemove
315 * Purpose: Remove an event from the event cache
316 * Returns: int: - number of matching events removed
317 * Inputs: string:sType - type of event to look for
318 * node:nTarget - target table cell
319 * function:fn - optional - remove this function. If not given all handlers of this
320 * type will be removed
322 function _fnEventRemove( sType, x, y, fn )
326 for ( var i=0, iLen=_oaoEvents[sType].length ; i<iLen-iCorrector ; i++ )
328 if ( typeof fn != 'undefined' )
330 if ( _oaoEvents[sType][i-iCorrector].x == x &&
331 _oaoEvents[sType][i-iCorrector].y == y &&
332 _oaoEvents[sType][i-iCorrector].fn == fn )
334 _oaoEvents[sType].splice( i-iCorrector, 1 );
340 if ( _oaoEvents[sType][i-iCorrector].x == x &&
341 _oaoEvents[sType][i-iCorrector].y == y )
343 _oaoEvents[sType].splice( i, 1 );
353 * Function: _fnEventFire
354 * Purpose: Look thought the events cache and fire off the event of interest
355 * Returns: int:iFired - number of events fired
356 * Inputs: string:sType - type of event to look for
357 * int:x - x coord of cell
358 * int:y - y coord of ell
359 * Notes: It might be more efficient to return after the first event has been tirggered,
360 * but that would mean that only one function of a particular type can be
361 * subscribed to a particular node.
363 function _fnEventFire ( sType, x, y )
366 var aEvents = _oaoEvents[sType];
367 for ( var i=0 ; i<aEvents.length ; i++ )
369 if ( (aEvents[i].x == x && aEvents[i].y == y ) ||
370 (aEvents[i].x === null && aEvents[i].y == y ) ||
371 (aEvents[i].x == x && aEvents[i].y === null ) ||
372 (aEvents[i].x === null && aEvents[i].y === null )
375 aEvents[i].fn( _fnCellFromCoords(x,y), x, y );
384 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
389 * Function: _fnSetFocus
390 * Purpose: Set focus on a node, and remove from an old node if needed
392 * Inputs: node:nTarget - node we want to focus on
393 * bool:bAutoScroll - optional - should we scroll the view port to the display
395 function _fnSetFocus( nTarget, bAutoScroll )
397 /* If node already has focus, just ignore this call */
398 if ( _nOldFocus == nTarget )
403 if ( typeof bAutoScroll == 'undefined' )
408 /* Remove old focus (with blur event if needed) */
409 if ( _nOldFocus !== null )
411 _fnRemoveFocus( _nOldFocus );
414 /* Add the new class to highlight the focused cell */
415 jQuery(nTarget).addClass( _sFocusClass );
416 jQuery(nTarget).parent().addClass( _sFocusClass );
418 /* If it's a DataTable then we need to jump the paging to the relevant page */
422 oSettings = _oDatatable.fnSettings();
423 var iRow = _fnFindDtCell( nTarget )[1];
424 var bKeyCaptureCache = _bKeyCapture;
427 while ( iRow >= oSettings.fnDisplayEnd() )
429 if ( oSettings._iDisplayLength >= 0 )
431 /* Make sure we are not over running the display array */
432 if ( oSettings._iDisplayStart + oSettings._iDisplayLength < oSettings.fnRecordsDisplay() )
434 oSettings._iDisplayStart += oSettings._iDisplayLength;
439 oSettings._iDisplayStart = 0;
441 _oDatatable.oApi._fnCalculateEnd( oSettings );
445 while ( iRow < oSettings._iDisplayStart )
447 oSettings._iDisplayStart = oSettings._iDisplayLength>=0 ?
448 oSettings._iDisplayStart - oSettings._iDisplayLength :
451 if ( oSettings._iDisplayStart < 0 )
453 oSettings._iDisplayStart = 0;
455 _oDatatable.oApi._fnCalculateEnd( oSettings );
458 /* Re-draw the table */
459 _oDatatable.oApi._fnDraw( oSettings );
461 /* Restore the key capture */
462 _bKeyCapture = bKeyCaptureCache;
465 /* Cache the information that we are interested in */
466 var aNewPos = _fnCoordsFromCell( nTarget );
467 _nOldFocus = nTarget;
471 var iViewportHeight, iViewportWidth, iScrollTop, iScrollLeft, iHeight, iWidth, aiPos;
474 /* Scroll the viewport such that the new cell is fully visible in the rendered window */
475 iViewportHeight = document.documentElement.clientHeight;
476 iViewportWidth = document.documentElement.clientWidth;
477 iScrollTop = document.body.scrollTop || document.documentElement.scrollTop;
478 iScrollLeft = document.body.scrollLeft || document.documentElement.scrollLeft;
479 iHeight = nTarget.offsetHeight;
480 iWidth = nTarget.offsetWidth;
481 aiPos = _fnGetPos( nTarget );
483 /* Take account of scrolling in DataTables 1.7 - remove scrolling since that would add to
484 * the positioning calculation
486 if ( _oDatatable && typeof oSettings.oScroll != 'undefined' &&
487 (oSettings.oScroll.sX !== "" || oSettings.oScroll.sY !== "") )
489 aiPos[1] -= $(oSettings.nTable.parentNode).scrollTop();
490 aiPos[0] -= $(oSettings.nTable.parentNode).scrollLeft();
493 /* Correct viewport positioning for vertical scrolling */
494 if ( aiPos[1]+iHeight > iScrollTop+iViewportHeight )
496 /* Displayed element if off the bottom of the viewport */
497 _fnSetScrollTop( aiPos[1]+iHeight - iViewportHeight );
499 else if ( aiPos[1] < iScrollTop )
501 /* Displayed element if off the top of the viewport */
502 _fnSetScrollTop( aiPos[1] );
505 /* Correct viewport positioning for horizontal scrolling */
506 if ( aiPos[0]+iWidth > iScrollLeft+iViewportWidth )
508 /* Displayed element is off the bottom of the viewport */
509 _fnSetScrollLeft( aiPos[0]+iWidth - iViewportWidth );
511 else if ( aiPos[0] < iScrollLeft )
513 /* Displayed element if off the Left of the viewport */
514 _fnSetScrollLeft( aiPos[0] );
518 /* Take account of scrolling in DataTables 1.7 */
519 if ( _oDatatable && typeof oSettings.oScroll != 'undefined' &&
520 (oSettings.oScroll.sX !== "" || oSettings.oScroll.sY !== "") )
522 var dtScrollBody = oSettings.nTable.parentNode;
523 iViewportHeight = dtScrollBody.clientHeight;
524 iViewportWidth = dtScrollBody.clientWidth;
525 iScrollTop = dtScrollBody.scrollTop;
526 iScrollLeft = dtScrollBody.scrollLeft;
527 iHeight = nTarget.offsetHeight;
528 iWidth = nTarget.offsetWidth;
530 /* Correct for vertical scrolling */
531 if ( nTarget.offsetTop + iHeight > iViewportHeight+iScrollTop )
533 dtScrollBody.scrollTop = (nTarget.offsetTop + iHeight) - iViewportHeight;
535 else if ( nTarget.offsetTop < iScrollTop )
537 dtScrollBody.scrollTop = nTarget.offsetTop;
540 /* Correct for horizontal scrolling */
541 if ( nTarget.offsetLeft + iWidth > iViewportWidth+iScrollLeft )
543 dtScrollBody.scrollLeft = (nTarget.offsetLeft + iWidth) - iViewportWidth;
545 else if ( nTarget.offsetLeft < iScrollLeft )
547 dtScrollBody.scrollLeft = nTarget.offsetLeft;
551 /* Focused - so we want to capture the keys */
554 /* Fire of the focus event if there is one */
555 _fnEventFire( "focus", _iOldX, _iOldY );
561 * Purpose: Blur focus from the whole table
567 _fnRemoveFocus( _nOldFocus );
576 * Function: _fnRemoveFocus
577 * Purpose: Remove focus from a cell and fire any blur events which are attached
579 * Inputs: node:nTarget - cell of interest
581 function _fnRemoveFocus( nTarget )
583 jQuery(nTarget).removeClass( _sFocusClass );
584 jQuery(nTarget).parent().removeClass( _sFocusClass );
585 _fnEventFire( "blur", _iOldX, _iOldY );
591 * Purpose: Focus on the element that has been clicked on by the user
593 * Inputs: event:e - click event
595 function _fnClick ( e )
598 while ( nTarget.nodeName != "TD" )
600 nTarget = nTarget.parentNode;
603 _fnSetFocus( nTarget );
609 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
615 * Purpose: Deal with a key events, be it moving the focus or return etc.
616 * Returns: bool: - allow browser default action
617 * Inputs: event:e - key event
619 function _fnKey ( e )
621 /* If user or system has blocked KeyTable from doing anything, just ignore this event */
622 if ( _that.block || !_bKeyCapture )
627 /* If a modifier key is pressed (exapct shift), ignore the event */
628 if ( e.metaKey || e.altKey || e.ctrlKey )
634 iTableWidth = _nBody.getElementsByTagName('tr')[0].getElementsByTagName('td').length,
637 /* Get table height and width - done here so as to be dynamic (if table is updated) */
641 * Locate the current node in the DataTable overriding the old positions - the reason for
642 * is is that there might have been some DataTables interaction between the last focus and
645 var oSettings = _oDatatable.fnSettings();
646 iTableHeight = oSettings.aiDisplay.length;
648 var aDtPos = _fnFindDtCell( _nOldFocus );
649 if ( aDtPos === null )
651 /* If the table has been updated such that the focused cell can't be seen - do nothing */
654 _iOldX = aDtPos[ 0 ];
655 _iOldY = aDtPos[ 1 ];
659 iTableHeight = _nBody.getElementsByTagName('tr').length;
662 /* Capture shift+tab to match the left arrow key */
663 var iKey = (e.keyCode == 9 && e.shiftKey) ? -1 : e.keyCode;
667 case 13: /* return */
670 _fnEventFire( "action", _iOldX, _iOldY );
674 if ( !_fnEventFire( "esc", _iOldX, _iOldY ) )
676 /* Only lose focus if there isn't an escape handler on the cell */
685 case 37: /* left arrow */
689 } else if ( _iOldY > 0 ) {
693 /* at start of table */
694 if ( iKey == -1 && _bForm )
696 /* If we are in a form, return focus to the 'input' element such that tabbing will
697 * follow correctly in the browser
699 _bInputFocused = true;
702 /* This timeout is a little nasty - but IE appears to have some asyhnc behaviour for
705 setTimeout( function(){ _bInputFocused = false; }, 0 );
706 _bKeyCapture = false;
717 case 38: /* up arrow */
727 case 39: /* right arrow */
728 if ( _iOldX < iTableWidth-1 ) {
731 } else if ( _iOldY < iTableHeight-1 ) {
735 /* at end of table */
736 if ( iKey == 9 && _bForm )
738 /* If we are in a form, return focus to the 'input' element such that tabbing will
739 * follow correctly in the browser
741 _bInputFocused = true;
744 /* This timeout is a little nasty - but IE appears to have some asyhnc behaviour for
747 setTimeout( function(){ _bInputFocused = false; }, 0 );
748 _bKeyCapture = false;
759 case 40: /* down arrow */
760 if ( _iOldY < iTableHeight-1 ) {
768 default: /* Nothing we are interested in */
772 _fnSetFocus( _fnCellFromCoords(x, y) );
778 * Function: _fnCaptureKeys
779 * Purpose: Start capturing key events for this table
783 function _fnCaptureKeys( )
793 * Function: _fnReleaseKeys
794 * Purpose: Stop capturing key events for this table
798 function _fnReleaseKeys( )
800 _bKeyCapture = false;
805 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
810 * Function: _fnCellFromCoords
811 * Purpose: Calulate the target TD cell from x and y coordinates
812 * Returns: node: - TD target
813 * Inputs: int:x - x coordinate
814 * int:y - y coordinate
816 function _fnCellFromCoords( x, y )
820 var oSettings = _oDatatable.fnSettings();
821 if ( typeof oSettings.aoData[ oSettings.aiDisplay[ y ] ] != 'undefined' )
823 return oSettings.aoData[ oSettings.aiDisplay[ y ] ].nTr.getElementsByTagName('td')[x];
832 return jQuery('tr:eq('+y+')>td:eq('+x+')', _nBody )[0];
838 * Function: _fnCoordsFromCell
839 * Purpose: Calculate the x and y position in a table from a TD cell
840 * Returns: array[2] int: [x, y]
841 * Inputs: node:n - TD cell of interest
842 * Notes: Not actually interested in this for DataTables since it might go out of date
844 function _fnCoordsFromCell( n )
848 var oSettings = _oDatatable.fnSettings();
850 jQuery('td', n.parentNode).index(n),
851 jQuery('tr', n.parentNode.parentNode).index(n.parentNode) + oSettings._iDisplayStart
857 jQuery('td', n.parentNode).index(n),
858 jQuery('tr', n.parentNode.parentNode).index(n.parentNode)
865 * Function: _fnSetScrollTop
866 * Purpose: Set the vertical scrolling position
868 * Inputs: int:iPos - scrolltop
869 * Notes: This is so nasty, but without browser detection you can't tell which you should set
870 * So on browsers that support both, the scroll top will be set twice. I can live with
873 function _fnSetScrollTop( iPos )
875 document.documentElement.scrollTop = iPos;
876 document.body.scrollTop = iPos;
881 * Function: _fnSetScrollLeft
882 * Purpose: Set the horizontal scrolling position
884 * Inputs: int:iPos - scrollleft
886 function _fnSetScrollLeft( iPos )
888 document.documentElement.scrollLeft = iPos;
889 document.body.scrollLeft = iPos;
894 * Function: _fnGetPos
895 * Purpose: Get the position of an object on the rendered page
896 * Returns: array[2] int: [left, right]
897 * Inputs: node:obj - element of interest
899 function _fnGetPos ( obj )
904 if (obj.offsetParent)
906 iLeft = obj.offsetLeft;
907 iTop = obj.offsetTop;
908 obj = obj.offsetParent;
911 iLeft += obj.offsetLeft;
912 iTop += obj.offsetTop;
913 obj = obj.offsetParent;
921 * Function: _fnFindDtCell
922 * Purpose: Get the coords. of a cell from the DataTables internal information
923 * Returns: array[2] int: [x, y] coords. or null if not found
924 * Inputs: node:nTarget - the node of interest
926 function _fnFindDtCell( nTarget )
928 var oSettings = _oDatatable.fnSettings();
929 for ( var i=0, iLen=oSettings.aiDisplay.length ; i<iLen ; i++ )
931 var nTr = oSettings.aoData[ oSettings.aiDisplay[i] ].nTr;
932 var nTds = nTr.getElementsByTagName('td');
933 for ( var j=0, jLen=nTds.length ; j<jLen ; j++ )
935 if ( nTds[j] == nTarget )
946 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
952 * Purpose: Initialise the KeyTable
954 * Inputs: object:oInit - optional - Initalisation object with the following parameters:
955 * array[2] int:focus - x and y coordinates of the initial target
957 * node:focus - the node to set initial focus on
958 * node:table - the table to use, if not given, first table with class 'KeyTable' will be used
959 * string:focusClass - focusing class to give to table elements
960 * object:that - focus
961 * bool:initScroll - scroll the view port on load, default true
962 * int:tabIndex - the tab index to give the hidden input element
964 function _fnInit( oInit, that )
969 /* Capture undefined initialisation and apply the defaults */
970 if ( typeof oInit == 'undefined' ) {
974 if ( typeof oInit.focus == 'undefined' ) {
978 if ( typeof oInit.table == 'undefined' ) {
979 oInit.table = jQuery('table.KeyTable')[0];
981 $(oInit.table).addClass('KeyTable');
984 if ( typeof oInit.focusClass != 'undefined' ) {
985 _sFocusClass = oInit.focusClass;
988 if ( typeof oInit.datatable != 'undefined' ) {
989 _oDatatable = oInit.datatable;
992 if ( typeof oInit.initScroll == 'undefined' ) {
993 oInit.initScroll = true;
996 if ( typeof oInit.form == 'undefined' ) {
1001 /* Cache the tbody node of interest */
1002 _nBody = oInit.table.getElementsByTagName('tbody')[0];
1004 /* If the table is inside a form, then we need a hidden input box which can be used by the
1005 * browser to catch the browser tabbing for our table
1009 var nDiv = document.createElement('div');
1010 _nInput = document.createElement('input');
1011 nDiv.style.height = "1px"; /* Opera requires a little something */
1012 nDiv.style.width = "0px";
1013 nDiv.style.overflow = "hidden";
1014 if ( typeof oInit.tabIndex != 'undefined' )
1016 _nInput.tabIndex = oInit.tabIndex;
1018 nDiv.appendChild(_nInput);
1019 oInit.table.parentNode.insertBefore( nDiv, oInit.table.nextSibling );
1021 jQuery(_nInput).focus( function () {
1022 /* See if we want to 'tab into' the table or out */
1023 if ( !_bInputFocused )
1025 _bKeyCapture = true;
1026 _bInputFocused = false;
1027 if ( typeof oInit.focus.nodeName != "undefined" )
1029 _fnSetFocus( oInit.focus, oInit.initScroll );
1033 _fnSetFocus( _fnCellFromCoords( oInit.focus[0], oInit.focus[1]), oInit.initScroll );
1036 /* Need to interup the thread for this to work */
1037 setTimeout( function() { _nInput.blur(); }, 0 );
1040 _bKeyCapture = false;
1044 /* Set the initial focus on the table */
1045 if ( typeof oInit.focus.nodeName != "undefined" )
1047 _fnSetFocus( oInit.focus, oInit.initScroll );
1051 _fnSetFocus( _fnCellFromCoords( oInit.focus[0], oInit.focus[1]), oInit.initScroll );
1057 * Add event listeners
1058 * Well - I hate myself for doing this, but it would appear that key events in browsers are
1059 * a complete mess, particulay when you consider arrow keys, which of course are one of the
1060 * main areas of interest here. So basically for arrow keys, there is no keypress event in
1061 * Safari and IE, while there is in Firefox and Opera. But Firefox and Opera don't repeat the
1062 * keydown event for an arrow key. OUCH. See the following two articles for more:
1063 * http://www.quirksmode.org/dom/events/keys.html
1064 * https://lists.webkit.org/pipermail/webkit-dev/2007-December/002992.html
1065 * http://unixpapa.com/js/key.html
1066 * PPK considers the IE / Safari method correct (good enough for me!) so we (urgh) detect
1067 * Mozilla and Opera and apply keypress for them, while everything else gets keydown. If
1068 * Mozilla or Opera change their implemention in future, this will need to be updated...
1069 * although at the time of writing (14th March 2009) Minefield still uses the 3.0 behaviour.
1071 if ( jQuery.browser.mozilla || jQuery.browser.opera )
1073 jQuery(document).bind( "keypress", _fnKey );
1077 jQuery(document).bind( "keydown", _fnKey );
1082 jQuery('tbody td', _oDatatable.fnSettings().nTable).live( 'click', _fnClick );
1086 jQuery('td', _nBody).live( 'click', _fnClick );
1089 /* Loose table focus when click outside the table */
1090 jQuery(document).click( function(e) {
1091 var nTarget = e.target;
1092 var bTableClick = false;
1095 if ( nTarget == oInit.table )
1100 nTarget = nTarget.parentNode;
1109 /* Initialise our new object */
1110 _fnInit( oInit, this );