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