| 1 | /* |
| 2 | Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved. |
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | (function(){ |
| 6 | |
| 7 | function onSelectionChange( evt ) |
| 8 | { |
| 9 | var command = evt.editor.getCommand( 'rtlSwitch' ); |
| 10 | command.state = getState( evt.editor, evt.data.path, 'rtl' ); |
| 11 | command.fire( 'state' ); |
| 12 | |
| 13 | command = evt.editor.getCommand( 'ltrSwitch' ); |
| 14 | command.state = getState( evt.editor, evt.data.path, 'ltr' ); |
| 15 | command.fire( 'state' ); |
| 16 | } |
| 17 | |
| 18 | function getState( editor, path, dir ) |
| 19 | { |
| 20 | var firstBlock = path.block || path.blockLimit; |
| 21 | |
| 22 | if ( !firstBlock || firstBlock.getName() == 'body' ) |
| 23 | return CKEDITOR.TRISTATE_OFF; |
| 24 | |
| 25 | return ( firstBlock.getAttribute( 'dir' ) == dir ) ? |
| 26 | CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF; |
| 27 | } |
| 28 | |
| 29 | function bidiCommand( dir ) |
| 30 | { |
| 31 | return function( editor ) |
| 32 | { |
| 33 | var selection = editor.getSelection(), |
| 34 | enterMode = editor.config.enterMode, |
| 35 | ranges = editor.getSelection().getRanges(); |
| 36 | |
| 37 | // Creates bookmarks for selection, as we may split some blocks. |
| 38 | var bookmarks = selection.createBookmarks(); |
| 39 | |
| 40 | if ( ranges ) |
| 41 | { |
| 42 | var iterator, |
| 43 | block; |
| 44 | |
| 45 | for ( var i = ranges.length - 1 ; i >= 0 ; i-- ) |
| 46 | { |
| 47 | iterator = ranges[ i ].createIterator(); |
| 48 | iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR; |
| 49 | |
| 50 | while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) ) |
| 51 | { |
| 52 | if ( block.hasAttribute( 'dir' ) && block.getAttribute( 'dir' ).toLowerCase() == dir ) |
| 53 | block.removeAttribute( 'dir' ); |
| 54 | else |
| 55 | block.setAttribute( 'dir', dir ); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | editor.focus(); |
| 60 | editor.forceNextSelectionCheck(); |
| 61 | // Restore selection position. |
| 62 | selection.selectBookmarks( bookmarks ); |
| 63 | } |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | CKEDITOR.plugins.add( 'bidiswitch', |
| 68 | { |
| 69 | requires : [ 'styles', 'button' ], |
| 70 | |
| 71 | init : function( editor ) |
| 72 | { |
| 73 | // All buttons use the same code to register. So, to avoid |
| 74 | // duplications, let's use this tool function. |
| 75 | var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec ) |
| 76 | { |
| 77 | editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) ); |
| 78 | |
| 79 | editor.ui.addButton( buttonName, |
| 80 | { |
| 81 | label : buttonLabel, |
| 82 | command : commandName |
| 83 | }); |
| 84 | }; |
| 85 | |
| 86 | var lang = editor.lang.bidiswitch; |
| 87 | |
| 88 | addButtonCommand( 'BidiRtl', lang.rtl, 'rtlSwitch', bidiCommand( 'rtl' ) ); |
| 89 | addButtonCommand( 'BidiLtr', lang.ltr, 'ltrSwitch', bidiCommand( 'ltr' ) ); |
| 90 | |
| 91 | editor.on( 'selectionChange', onSelectionChange ); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | })(); |