2 Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
10 editorFocus : false,
\r
11 modes : { wysiwyg:1, source:1 }
\r
16 exec : function( editor )
\r
18 editor.container.focusNext( true, editor.tabIndex );
\r
22 var blurBackCommand =
\r
24 exec : function( editor )
\r
26 editor.container.focusPrevious( true, editor.tabIndex );
\r
30 function selectNextCellCommand( backward )
\r
33 editorFocus : false,
\r
35 modes : { wysiwyg : 1 },
\r
36 exec : function( editor )
\r
38 if ( editor.focusManager.hasFocus )
\r
40 var sel = editor.getSelection(),
\r
41 ancestor = sel.getCommonAncestor(),
\r
44 if ( ( cell = ( ancestor.getAscendant( 'td', true ) || ancestor.getAscendant( 'th', true ) ) ) )
\r
46 var resultRange = new CKEDITOR.dom.range( editor.document ),
\r
47 next = CKEDITOR.tools.tryThese( function()
\r
49 var row = cell.getParent(),
\r
50 next = row.$.cells[ cell.$.cellIndex + ( backward ? - 1 : 1 ) ];
\r
52 // Invalid any empty value.
\r
53 next.parentNode.parentNode;
\r
58 var row = cell.getParent(),
\r
59 table = row.getAscendant( 'table' ),
\r
60 nextRow = table.$.rows[ row.$.rowIndex + ( backward ? - 1 : 1 ) ];
\r
62 return nextRow.cells[ backward? nextRow.cells.length -1 : 0 ];
\r
65 // Clone one more row at the end of table and select the first newly established cell.
\r
66 if ( ! ( next || backward ) )
\r
68 var table = cell.getAscendant( 'table' ).$,
\r
69 cells = cell.getParent().$.cells;
\r
71 var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document );
\r
73 for ( var i = 0, count = cells.length ; i < count; i++ )
\r
75 var newCell = newRow.append( new CKEDITOR.dom.element(
\r
76 cells[ i ], editor.document ).clone( false, false ) );
\r
77 !CKEDITOR.env.ie && newCell.appendBogus();
\r
80 resultRange.moveToElementEditStart( newRow );
\r
84 next = new CKEDITOR.dom.element( next );
\r
85 resultRange.moveToElementEditStart( next );
\r
86 // Avoid selecting empty block makes the cursor blind.
\r
87 if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) )
\r
88 resultRange.selectNodeContents( next );
\r
93 resultRange.select( true );
\r
102 CKEDITOR.plugins.add( 'tab',
\r
104 requires : [ 'keystrokes' ],
\r
106 init : function( editor )
\r
108 var tabTools = editor.config.enableTabKeyTools !== false,
\r
109 tabSpaces = editor.config.tabSpaces || 0,
\r
112 while ( tabSpaces-- )
\r
117 editor.on( 'key', function( ev )
\r
119 if ( ev.data.keyCode == 9 ) // TAB
\r
121 editor.insertHtml( tabText );
\r
129 editor.on( 'key', function( ev )
\r
131 if ( ev.data.keyCode == 9 && editor.execCommand( 'selectNextCell' ) || // TAB
\r
132 ev.data.keyCode == ( CKEDITOR.SHIFT + 9 ) && editor.execCommand( 'selectPreviousCell' ) ) // SHIFT+TAB
\r
137 if ( CKEDITOR.env.webkit || CKEDITOR.env.gecko )
\r
139 editor.on( 'key', function( ev )
\r
141 var keyCode = ev.data.keyCode;
\r
143 if ( keyCode == 9 && !tabText ) // TAB
\r
146 editor.execCommand( 'blur' );
\r
149 if ( keyCode == ( CKEDITOR.SHIFT + 9 ) ) // SHIFT+TAB
\r
151 editor.execCommand( 'blurBack' );
\r
157 editor.addCommand( 'blur', CKEDITOR.tools.extend( blurCommand, meta ) );
\r
158 editor.addCommand( 'blurBack', CKEDITOR.tools.extend( blurBackCommand, meta ) );
\r
159 editor.addCommand( 'selectNextCell', selectNextCellCommand() );
\r
160 editor.addCommand( 'selectPreviousCell', selectNextCellCommand( true ) );
\r
166 * Moves the UI focus to the element following this element in the tabindex
\r
169 * var element = CKEDITOR.document.getById( 'example' );
\r
170 * element.focusNext();
\r
172 CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren, indexToUse )
\r
175 curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ),
\r
176 passedCurrent, enteredCurrent,
\r
177 elected, electedTabIndex,
\r
178 element, elementTabIndex;
\r
180 if ( curTabIndex <= 0 )
\r
182 // If this element has tabindex <= 0 then we must simply look for any
\r
183 // element following it containing tabindex=0.
\r
185 element = this.getNextSourceNode( ignoreChildren, CKEDITOR.NODE_ELEMENT );
\r
189 if ( element.isVisible() && element.getTabIndex() === 0 )
\r
195 element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
\r
200 // If this element has tabindex > 0 then we must look for:
\r
201 // 1. An element following this element with the same tabindex.
\r
202 // 2. The first element in source other with the lowest tabindex
\r
203 // that is higher than this element tabindex.
\r
204 // 3. The first element with tabindex=0.
\r
206 element = this.getDocument().getBody().getFirst();
\r
208 while ( ( element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) )
\r
210 if ( !passedCurrent )
\r
212 if ( !enteredCurrent && element.equals( this ) )
\r
214 enteredCurrent = true;
\r
216 // Ignore this element, if required.
\r
217 if ( ignoreChildren )
\r
219 if ( !( element = element.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT ) ) )
\r
224 else if ( enteredCurrent && !this.contains( element ) )
\r
228 if ( !element.isVisible() || ( elementTabIndex = element.getTabIndex() ) < 0 )
\r
231 if ( passedCurrent && elementTabIndex == curTabIndex )
\r
237 if ( elementTabIndex > curTabIndex && ( !elected || !electedTabIndex || elementTabIndex < electedTabIndex ) )
\r
240 electedTabIndex = elementTabIndex;
\r
242 else if ( !elected && elementTabIndex === 0 )
\r
245 electedTabIndex = elementTabIndex;
\r
255 * Moves the UI focus to the element before this element in the tabindex order.
\r
257 * var element = CKEDITOR.document.getById( 'example' );
\r
258 * element.focusPrevious();
\r
260 CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren, indexToUse )
\r
263 curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ),
\r
264 passedCurrent, enteredCurrent,
\r
266 electedTabIndex = 0,
\r
269 var element = this.getDocument().getBody().getLast();
\r
271 while ( ( element = element.getPreviousSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) )
\r
273 if ( !passedCurrent )
\r
275 if ( !enteredCurrent && element.equals( this ) )
\r
277 enteredCurrent = true;
\r
279 // Ignore this element, if required.
\r
280 if ( ignoreChildren )
\r
282 if ( !( element = element.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) ) )
\r
287 else if ( enteredCurrent && !this.contains( element ) )
\r
291 if ( !element.isVisible() || ( elementTabIndex = element.getTabIndex() ) < 0 )
\r
294 if ( curTabIndex <= 0 )
\r
296 // If this element has tabindex <= 0 then we must look for:
\r
297 // 1. An element before this one containing tabindex=0.
\r
298 // 2. The last element with the highest tabindex.
\r
300 if ( passedCurrent && elementTabIndex === 0 )
\r
306 if ( elementTabIndex > electedTabIndex )
\r
309 electedTabIndex = elementTabIndex;
\r
314 // If this element has tabindex > 0 we must look for:
\r
315 // 1. An element preceeding this one, with the same tabindex.
\r
316 // 2. The last element in source other with the highest tabindex
\r
317 // that is lower than this element tabindex.
\r
319 if ( passedCurrent && elementTabIndex == curTabIndex )
\r
325 if ( elementTabIndex < curTabIndex && ( !elected || elementTabIndex > electedTabIndex ) )
\r
328 electedTabIndex = elementTabIndex;
\r
338 * Intructs the editor to add a number of spaces (&nbsp;) to the text when
\r
339 * hitting the TAB key. If set to zero, the TAB key will be used to move the
\r
340 * cursor focus to the next element in the page, out of the editor focus.
\r
341 * @name CKEDITOR.config.tabSpaces
\r
345 * config.tabSpaces = 4;
\r
349 * Allow context-sensitive tab key behaviors, including the following scenarios:
\r
350 * <h5>When selection is anchored inside <b>table cells</b>:</h5>
\r
352 * <li>If TAB is pressed, select the contents of the "next" cell. If in the last cell in the table, add a new row to it and focus its first cell.</li>
\r
353 * <li>If SHIFT+TAB is pressed, select the contents of the "previous" cell. Do nothing when it's in the first cell.</li>
\r
355 * @name CKEDITOR.config.enableTabKeyTools
\r
359 * config.enableTabKeyTools = false;
\r
362 // If the TAB key is not supposed to be enabled for navigation, the following
\r
363 // settings could be used alternatively:
\r
364 // config.keystrokes.push(
\r
365 // [ CKEDITOR.ALT + 38 /*Arrow Up*/, 'selectPreviousCell' ],
\r
366 // [ CKEDITOR.ALT + 40 /*Arrow Down*/, 'selectNextCell' ]
\r