2 Copyright (c) 2003-2013, 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
17 exec : function( editor )
\r
19 editor.container.focusNext( true, editor.tabIndex );
\r
23 var blurBackCommand =
\r
26 exec : function( editor )
\r
28 editor.container.focusPrevious( true, editor.tabIndex );
\r
32 function selectNextCellCommand( backward )
\r
35 editorFocus : false,
\r
37 modes : { wysiwyg : 1 },
\r
38 exec : function( editor )
\r
40 if ( editor.focusManager.hasFocus )
\r
42 var sel = editor.getSelection(),
\r
43 ancestor = sel.getCommonAncestor(),
\r
46 if ( ( cell = ( ancestor.getAscendant( 'td', true ) || ancestor.getAscendant( 'th', true ) ) ) )
\r
48 var resultRange = new CKEDITOR.dom.range( editor.document ),
\r
49 next = CKEDITOR.tools.tryThese( function()
\r
51 var row = cell.getParent(),
\r
52 next = row.$.cells[ cell.$.cellIndex + ( backward ? - 1 : 1 ) ];
\r
54 // Invalid any empty value.
\r
55 next.parentNode.parentNode;
\r
60 var row = cell.getParent(),
\r
61 table = row.getAscendant( 'table' ),
\r
62 nextRow = table.$.rows[ row.$.rowIndex + ( backward ? - 1 : 1 ) ];
\r
64 return nextRow.cells[ backward? nextRow.cells.length -1 : 0 ];
\r
67 // Clone one more row at the end of table and select the first newly established cell.
\r
68 if ( ! ( next || backward ) )
\r
70 var table = cell.getAscendant( 'table' ).$,
\r
71 cells = cell.getParent().$.cells;
\r
73 var newRow = new CKEDITOR.dom.element( table.insertRow( -1 ), editor.document );
\r
75 for ( var i = 0, count = cells.length ; i < count; i++ )
\r
77 var newCell = newRow.append( new CKEDITOR.dom.element(
\r
78 cells[ i ], editor.document ).clone( false, false ) );
\r
79 !CKEDITOR.env.ie && newCell.appendBogus();
\r
82 resultRange.moveToElementEditStart( newRow );
\r
86 next = new CKEDITOR.dom.element( next );
\r
87 resultRange.moveToElementEditStart( next );
\r
88 // Avoid selecting empty block makes the cursor blind.
\r
89 if ( !( resultRange.checkStartOfBlock() && resultRange.checkEndOfBlock() ) )
\r
90 resultRange.selectNodeContents( next );
\r
95 resultRange.select( true );
\r
104 CKEDITOR.plugins.add( 'tab',
\r
106 requires : [ 'keystrokes' ],
\r
108 init : function( editor )
\r
110 var tabTools = editor.config.enableTabKeyTools !== false,
\r
111 tabSpaces = editor.config.tabSpaces || 0,
\r
114 while ( tabSpaces-- )
\r
119 editor.on( 'key', function( ev )
\r
121 if ( ev.data.keyCode == 9 ) // TAB
\r
123 editor.insertHtml( tabText );
\r
131 editor.on( 'key', function( ev )
\r
133 if ( ev.data.keyCode == 9 && editor.execCommand( 'selectNextCell' ) || // TAB
\r
134 ev.data.keyCode == ( CKEDITOR.SHIFT + 9 ) && editor.execCommand( 'selectPreviousCell' ) ) // SHIFT+TAB
\r
139 if ( CKEDITOR.env.webkit || CKEDITOR.env.gecko )
\r
141 editor.on( 'key', function( ev )
\r
143 var keyCode = ev.data.keyCode;
\r
145 if ( keyCode == 9 && !tabText ) // TAB
\r
148 editor.execCommand( 'blur' );
\r
151 if ( keyCode == ( CKEDITOR.SHIFT + 9 ) ) // SHIFT+TAB
\r
153 editor.execCommand( 'blurBack' );
\r
159 editor.addCommand( 'blur', CKEDITOR.tools.extend( blurCommand, meta ) );
\r
160 editor.addCommand( 'blurBack', CKEDITOR.tools.extend( blurBackCommand, meta ) );
\r
161 editor.addCommand( 'selectNextCell', selectNextCellCommand() );
\r
162 editor.addCommand( 'selectPreviousCell', selectNextCellCommand( true ) );
\r
168 * Moves the UI focus to the element following this element in the tabindex
\r
171 * var element = CKEDITOR.document.getById( 'example' );
\r
172 * element.focusNext();
\r
174 CKEDITOR.dom.element.prototype.focusNext = function( ignoreChildren, indexToUse )
\r
177 curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ),
\r
178 passedCurrent, enteredCurrent,
\r
179 elected, electedTabIndex,
\r
180 element, elementTabIndex;
\r
182 if ( curTabIndex <= 0 )
\r
184 // If this element has tabindex <= 0 then we must simply look for any
\r
185 // element following it containing tabindex=0.
\r
187 element = this.getNextSourceNode( ignoreChildren, CKEDITOR.NODE_ELEMENT );
\r
191 if ( element.isVisible() && element.getTabIndex() === 0 )
\r
197 element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT );
\r
202 // If this element has tabindex > 0 then we must look for:
\r
203 // 1. An element following this element with the same tabindex.
\r
204 // 2. The first element in source other with the lowest tabindex
\r
205 // that is higher than this element tabindex.
\r
206 // 3. The first element with tabindex=0.
\r
208 element = this.getDocument().getBody().getFirst();
\r
210 while ( ( element = element.getNextSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) )
\r
212 if ( !passedCurrent )
\r
214 if ( !enteredCurrent && element.equals( this ) )
\r
216 enteredCurrent = true;
\r
218 // Ignore this element, if required.
\r
219 if ( ignoreChildren )
\r
221 if ( !( element = element.getNextSourceNode( true, CKEDITOR.NODE_ELEMENT ) ) )
\r
226 else if ( enteredCurrent && !this.contains( element ) )
\r
230 if ( !element.isVisible() || ( elementTabIndex = element.getTabIndex() ) < 0 )
\r
233 if ( passedCurrent && elementTabIndex == curTabIndex )
\r
239 if ( elementTabIndex > curTabIndex && ( !elected || !electedTabIndex || elementTabIndex < electedTabIndex ) )
\r
242 electedTabIndex = elementTabIndex;
\r
244 else if ( !elected && elementTabIndex === 0 )
\r
247 electedTabIndex = elementTabIndex;
\r
257 * Moves the UI focus to the element before this element in the tabindex order.
\r
259 * var element = CKEDITOR.document.getById( 'example' );
\r
260 * element.focusPrevious();
\r
262 CKEDITOR.dom.element.prototype.focusPrevious = function( ignoreChildren, indexToUse )
\r
265 curTabIndex = ( indexToUse === undefined ? this.getTabIndex() : indexToUse ),
\r
266 passedCurrent, enteredCurrent,
\r
268 electedTabIndex = 0,
\r
271 var element = this.getDocument().getBody().getLast();
\r
273 while ( ( element = element.getPreviousSourceNode( false, CKEDITOR.NODE_ELEMENT ) ) )
\r
275 if ( !passedCurrent )
\r
277 if ( !enteredCurrent && element.equals( this ) )
\r
279 enteredCurrent = true;
\r
281 // Ignore this element, if required.
\r
282 if ( ignoreChildren )
\r
284 if ( !( element = element.getPreviousSourceNode( true, CKEDITOR.NODE_ELEMENT ) ) )
\r
289 else if ( enteredCurrent && !this.contains( element ) )
\r
293 if ( !element.isVisible() || ( elementTabIndex = element.getTabIndex() ) < 0 )
\r
296 if ( curTabIndex <= 0 )
\r
298 // If this element has tabindex <= 0 then we must look for:
\r
299 // 1. An element before this one containing tabindex=0.
\r
300 // 2. The last element with the highest tabindex.
\r
302 if ( passedCurrent && elementTabIndex === 0 )
\r
308 if ( elementTabIndex > electedTabIndex )
\r
311 electedTabIndex = elementTabIndex;
\r
316 // If this element has tabindex > 0 we must look for:
\r
317 // 1. An element preceeding this one, with the same tabindex.
\r
318 // 2. The last element in source other with the highest tabindex
\r
319 // that is lower than this element tabindex.
\r
321 if ( passedCurrent && elementTabIndex == curTabIndex )
\r
327 if ( elementTabIndex < curTabIndex && ( !elected || elementTabIndex > electedTabIndex ) )
\r
330 electedTabIndex = elementTabIndex;
\r
340 * Intructs the editor to add a number of spaces (&nbsp;) to the text when
\r
341 * hitting the TAB key. If set to zero, the TAB key will be used to move the
\r
342 * cursor focus to the next element in the page, out of the editor focus.
\r
343 * @name CKEDITOR.config.tabSpaces
\r
347 * config.tabSpaces = 4;
\r
351 * Allow context-sensitive tab key behaviors, including the following scenarios:
\r
352 * <h5>When selection is anchored inside <b>table cells</b>:</h5>
\r
354 * <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
355 * <li>If SHIFT+TAB is pressed, select the contents of the "previous" cell. Do nothing when it's in the first cell.</li>
\r
357 * @name CKEDITOR.config.enableTabKeyTools
\r
361 * config.enableTabKeyTools = false;
\r
364 // If the TAB key is not supposed to be enabled for navigation, the following
\r
365 // settings could be used alternatively:
\r
366 // config.keystrokes.push(
\r
367 // [ CKEDITOR.ALT + 38 /*Arrow Up*/, 'selectPreviousCell' ],
\r
368 // [ CKEDITOR.ALT + 40 /*Arrow Down*/, 'selectNextCell' ]
\r