| | 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 | // Array of elements processed as guardElements. |
| | 99 | var processedElements = []; |
| | 100 | // Walker searching for guardElements. |
| | 101 | var walker = new CKEDITOR.dom.walker( ranges[ i ] ); |
| | 102 | walker.evaluator = function( node ){ |
| | 103 | return node.type == CKEDITOR.NODE_ELEMENT |
| | 104 | && node.getName() in guardElements |
| | 105 | && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ) ? 'p' : 'div' |
| | 106 | && node.getParent().type == CKEDITOR.NODE_ELEMENT |
| | 107 | && node.getParent().getName() == 'blockquote' |
| | 108 | ); |
| | 109 | }; |
| | 110 | |
| | 111 | while ( block = walker.next() ) |
| | 112 | { |
| | 113 | processedElements.push( block ); |
| | 114 | if ( block.hasAttribute( 'dir' ) && block.getAttribute( 'dir' ).toLowerCase() == dir ) |
| | 115 | block.removeAttribute( 'dir' ); |
| | 116 | else |
| | 117 | block.setAttribute( 'dir', dir ); |
| | 118 | } |
| | 119 | |
| | 120 | iterator = ranges[ i ].createIterator(); |
| | 121 | iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR; |
| | 122 | |
| | 123 | while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) ) |
| | 124 | { |
| | 125 | var _break; |
| | 126 | |
| | 127 | // Check if block have been already processed by the walker above. |
| | 128 | for ( var ii = 0; ii < processedElements.length; ii++ ) |
| | 129 | { |
| | 130 | var parent = block.getParent(); |
| | 131 | |
| | 132 | while( parent && parent.getName() != 'body' ) |
| | 133 | { |
| | 134 | if ( parent.$.isSameNode( processedElements[ ii ].$ ) ) |
| | 135 | { |
| | 136 | _break = 1; |
| | 137 | break; |
| | 138 | } |
| | 139 | parent = parent.getParent(); |
| | 140 | } |
| | 141 | |
| | 142 | if ( _break ) |
| | 143 | break; |
| | 144 | } |
| | 145 | |
| | 146 | if ( !_break ) |
| | 147 | { |
| | 148 | if ( block.hasAttribute( 'dir' ) && block.getAttribute( 'dir' ).toLowerCase() == dir ) |
| | 149 | block.removeAttribute( 'dir' ); |
| | 150 | else |
| | 151 | block.setAttribute( 'dir', dir ); |
| | 152 | } |
| | 153 | } |
| | 154 | } |
| | 155 | |
| | 156 | editor.forceNextSelectionCheck(); |
| | 157 | // Restore selection position. |
| | 158 | selection.selectBookmarks( bookmarks ); |
| | 159 | } |
| | 160 | |
| | 161 | editor.focus(); |
| | 162 | } |
| | 163 | }; |
| | 164 | } |
| | 165 | |
| | 166 | CKEDITOR.plugins.add( 'bidi', |
| | 167 | { |
| | 168 | requires : [ 'styles', 'button' ], |
| | 169 | |
| | 170 | init : function( editor ) |
| | 171 | { |
| | 172 | // All buttons use the same code to register. So, to avoid |
| | 173 | // duplications, let's use this tool function. |
| | 174 | var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec ) |
| | 175 | { |
| | 176 | editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) ); |
| | 177 | |
| | 178 | editor.ui.addButton( buttonName, |
| | 179 | { |
| | 180 | label : buttonLabel, |
| | 181 | command : commandName |
| | 182 | }); |
| | 183 | }; |
| | 184 | |
| | 185 | var lang = editor.lang.bidi; |
| | 186 | |
| | 187 | addButtonCommand( 'BidiLtr', lang.rtl, 'bidiltr', bidiCommand( 'ltr' ) ); |
| | 188 | addButtonCommand( 'BidiRtl', lang.ltr, 'bidirtl', bidiCommand( 'rtl' ) ); |
| | 189 | |
| | 190 | editor.on( 'selectionChange', onSelectionChange ); |
| | 191 | } |
| | 192 | }); |
| | 193 | |
| | 194 | })(); |