| 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 | var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 }; |
| 8 | |
| 9 | function onSelectionChange( evt ) |
| 10 | { |
| 11 | evt.editor.getCommand( 'bidirtl' ).setState( getState( evt.editor, evt.data.path, 'rtl' ) ); |
| 12 | evt.editor.getCommand( 'bidiltr' ).setState( getState( evt.editor, evt.data.path, 'ltr' ) ); |
| 13 | } |
| 14 | |
| 15 | function getState( editor, path, dir ) |
| 16 | { |
| 17 | var firstBlock = getFullySelected( editor.getSelection(), guardElements ); |
| 18 | |
| 19 | firstBlock = firstBlock || path.block || path.blockLimit; |
| 20 | |
| 21 | if ( !firstBlock || firstBlock.getName() == 'body' ) |
| 22 | return CKEDITOR.TRISTATE_OFF; |
| 23 | |
| 24 | return ( firstBlock.getAttribute( 'dir' ) == dir ) ? |
| 25 | CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * |
| 30 | * @param {CKEDITOR.dom.selection} selection |
| 31 | * @param {Object<name,int>} elements |
| 32 | * |
| 33 | * @return {?CKEDITOR.dom.element} Fully selected element. |
| 34 | */ |
| 35 | function getFullySelected( selection, elements ) |
| 36 | { |
| 37 | var selectedElement = selection.getCommonAncestor(); |
| 38 | while( selectedElement.type == CKEDITOR.NODE_ELEMENT |
| 39 | && !( selectedElement.getName() in elements ) |
| 40 | && selectedElement.getParent().getChildCount() == 1 |
| 41 | ) |
| 42 | selectedElement = selectedElement.getParent(); |
| 43 | |
| 44 | return selectedElement.type == CKEDITOR.NODE_ELEMENT |
| 45 | && ( selectedElement.getName() in elements ) |
| 46 | && selectedElement; |
| 47 | } |
| 48 | |
| 49 | function bidiCommand( dir ) |
| 50 | { |
| 51 | return function( editor ) |
| 52 | { |
| 53 | var selection = editor.getSelection(), |
| 54 | enterMode = editor.config.enterMode, |
| 55 | ranges = editor.getSelection().getRanges(); |
| 56 | |
| 57 | if ( ranges ) |
| 58 | { |
| 59 | var selectedElement = getFullySelected( selection, guardElements ); |
| 60 | |
| 61 | // Apply to fully selected elements from guardElements. |
| 62 | if ( selectedElement ) |
| 63 | { |
| 64 | if ( selectedElement.hasAttribute( 'dir' ) && selectedElement.getAttribute( 'dir' ).toLowerCase() == dir ) |
| 65 | selectedElement.removeAttribute( 'dir' ); |
| 66 | else |
| 67 | selectedElement.setAttribute( 'dir', dir ); |
| 68 | |
| 69 | editor.forceNextSelectionCheck(); |
| 70 | } |
| 71 | else |
| 72 | { |
| 73 | // Creates bookmarks for selection, as we may split some blocks. |
| 74 | var bookmarks = selection.createBookmarks(); |
| 75 | |
| 76 | var iterator, |
| 77 | block; |
| 78 | |
| 79 | for ( var i = ranges.length - 1 ; i >= 0 ; i-- ) |
| 80 | { |
| 81 | iterator = ranges[ i ].createIterator(); |
| 82 | iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR; |
| 83 | |
| 84 | while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) ) |
| 85 | { |
| 86 | if ( block.hasAttribute( 'dir' ) && block.getAttribute( 'dir' ).toLowerCase() == dir ) |
| 87 | block.removeAttribute( 'dir' ); |
| 88 | else |
| 89 | block.setAttribute( 'dir', dir ); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | editor.forceNextSelectionCheck(); |
| 94 | // Restore selection position. |
| 95 | selection.selectBookmarks( bookmarks ); |
| 96 | } |
| 97 | |
| 98 | editor.focus(); |
| 99 | } |
| 100 | }; |
| 101 | } |
| 102 | |
| 103 | CKEDITOR.plugins.add( 'bidi', |
| 104 | { |
| 105 | requires : [ 'styles', 'button' ], |
| 106 | |
| 107 | init : function( editor ) |
| 108 | { |
| 109 | // All buttons use the same code to register. So, to avoid |
| 110 | // duplications, let's use this tool function. |
| 111 | var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec ) |
| 112 | { |
| 113 | editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) ); |
| 114 | |
| 115 | editor.ui.addButton( buttonName, |
| 116 | { |
| 117 | label : buttonLabel, |
| 118 | command : commandName |
| 119 | }); |
| 120 | }; |
| 121 | |
| 122 | var lang = editor.lang.bidiswitch; |
| 123 | |
| 124 | addButtonCommand( 'BidiLtr', lang.rtl, 'bidiltr', bidiCommand( 'ltr' ) ); |
| 125 | addButtonCommand( 'BidiRtl', lang.ltr, 'bidirtl', bidiCommand( 'rtl' ) ); |
| 126 | |
| 127 | editor.on( 'selectionChange', onSelectionChange ); |
| 128 | } |
| 129 | }); |
| 130 | |
| 131 | })(); |