Nation Notes module contributed by Z&H Healthcare.
[openemr.git] / library / custom_template / ckeditor / _source / plugins / bidi / plugin.js
blob635ff0f5aa596f4522d91acc47cdb343a38255f4
1 /*
2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3 For licensing, see LICENSE.html or http://ckeditor.com/license
4 */
6 (function()
8         var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 },
9                 directSelectionGuardElements = {},
10                 // All guard elements which can have a direction applied on them.
11                 allGuardElements = {};
12         CKEDITOR.tools.extend( directSelectionGuardElements, guardElements, { tr:1, p:1, div:1, li:1 } );
13         CKEDITOR.tools.extend( allGuardElements, directSelectionGuardElements, { td:1 } );
15         function onSelectionChange( e )
16         {
17                 setToolbarStates( e );
18                 handleMixedDirContent( e );
19         }
21         function setToolbarStates( evt )
22         {
23                 var editor = evt.editor,
24                         path = evt.data.path;
25                 var useComputedState = editor.config.useComputedState,
26                         selectedElement;
28                 useComputedState = useComputedState === undefined || useComputedState;
30                 // We can use computedState provided by the browser or traverse parents manually.
31                 if ( !useComputedState )
32                         selectedElement = getElementForDirection( path.lastElement );
34                 selectedElement = selectedElement || path.block || path.blockLimit;
36                 // If we're having BODY here, user probably done CTRL+A, let's try to get the enclosed node, if any.
37                 selectedElement.is( 'body' ) &&
38                         ( selectedElement = editor.getSelection().getRanges()[ 0 ].getEnclosedNode() );
40                 if ( !selectedElement )
41                         return;
43                 var selectionDir = useComputedState ?
44                         selectedElement.getComputedStyle( 'direction' ) :
45                         selectedElement.getStyle( 'direction' ) || selectedElement.getAttribute( 'dir' );
47                 editor.getCommand( 'bidirtl' ).setState( selectionDir == 'rtl' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
48                 editor.getCommand( 'bidiltr' ).setState( selectionDir == 'ltr' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );
49         }
51         function handleMixedDirContent( evt )
52         {
53                 var editor = evt.editor,
54                         chromeRoot = editor.container.getChild( 1 ),
55                         directionNode = evt.data.path.block || evt.data.path.blockLimit;
57                 if ( directionNode && editor.lang.dir != directionNode.getComputedStyle( 'direction' ) )
58                         chromeRoot.addClass( 'cke_mixed_dir_content' );
59                 else
60                         chromeRoot.removeClass( 'cke_mixed_dir_content' );
61         }
63         /**
64          * Returns element with possibility of applying the direction.
65          * @param node
66          */
67         function getElementForDirection( node )
68         {
69                 while ( node && !( node.getName() in allGuardElements || node.is( 'body' ) ) )
70                 {
71                         var parent = node.getParent();
72                         if ( !parent )
73                                 break;
75                         node = parent;
76                 }
78                 return node;
79         }
81         function switchDir( element, dir, editor, database )
82         {
83                 // Mark this element as processed by switchDir.
84                 CKEDITOR.dom.element.setMarker( database, element, 'bidi_processed', 1 );
86                 // Check whether one of the ancestors has already been styled.
87                 var parent = element;
88                 while ( ( parent = parent.getParent() ) && !parent.is( 'body' ) )
89                 {
90                         if ( parent.getCustomData( 'bidi_processed' ) )
91                         {
92                                 // Ancestor style must dominate.
93                                 element.removeStyle( 'direction' );
94                                 element.removeAttribute( 'dir' );
95                                 return null;
96                         }
97                 }
99                 var useComputedState = ( 'useComputedState' in editor.config ) ? editor.config.useComputedState : 1;
101                 var elementDir = useComputedState ? element.getComputedStyle( 'direction' )
102                         : element.getStyle( 'direction' ) || element.hasAttribute( 'dir' );
104                 // Stop if direction is same as present.
105                 if ( elementDir == dir )
106                         return null;
108                 // Reuse computedState if we already have it.
109                 var dirBefore = useComputedState ? elementDir : element.getComputedStyle( 'direction' );
111                 // Clear direction on this element.
112                 element.removeStyle( 'direction' );
114                 // Do the second check when computed state is ON, to check
115                 // if we need to apply explicit direction on this element.
116                 if ( useComputedState )
117                 {
118                         element.removeAttribute( 'dir' );
119                         if ( dir != element.getComputedStyle( 'direction' ) )
120                                 element.setAttribute( 'dir', dir );
121                 }
122                 else
123                         // Set new direction for this element.
124                         element.setAttribute( 'dir', dir );
126                 // If the element direction changed, we need to switch the margins of
127                 // the element and all its children, so it will get really reflected
128                 // like a mirror. (#5910)
129                 if ( dir != dirBefore )
130                 {
131                         editor.fire( 'dirChanged',
132                                 {
133                                         node : element,
134                                         dir : dir
135                                 } );
136                 }
138                 editor.forceNextSelectionCheck();
140                 return null;
141         }
143         function getFullySelected( range, elements, enterMode )
144         {
145                 var ancestor = range.getCommonAncestor( false, true );
147                 range = range.clone();
148                 range.enlarge( enterMode == CKEDITOR.ENTER_BR ?
149                                 CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS
150                                 : CKEDITOR.ENLARGE_BLOCK_CONTENTS );
152                 if ( range.checkBoundaryOfElement( ancestor, CKEDITOR.START )
153                                 && range.checkBoundaryOfElement( ancestor, CKEDITOR.END ) )
154                 {
155                         var parent;
156                         while ( ancestor && ancestor.type == CKEDITOR.NODE_ELEMENT
157                                         && ( parent = ancestor.getParent() )
158                                         && parent.getChildCount() == 1
159                                         && !( ancestor.getName() in elements ) )
160                                 ancestor = parent;
162                         return ancestor.type == CKEDITOR.NODE_ELEMENT
163                                         && ( ancestor.getName() in elements )
164                                         && ancestor;
165                 }
166         }
168         function bidiCommand( dir )
169         {
170                 return function( editor )
171                 {
172                         var selection = editor.getSelection(),
173                                 enterMode = editor.config.enterMode,
174                                 ranges = selection.getRanges();
176                         if ( ranges && ranges.length )
177                         {
178                                 var database = {};
180                                 // Creates bookmarks for selection, as we may split some blocks.
181                                 var bookmarks = selection.createBookmarks();
183                                 var rangeIterator = ranges.createIterator(),
184                                         range,
185                                         i = 0;
187                                 while ( ( range = rangeIterator.getNextRange( 1 ) ) )
188                                 {
189                                         // Apply do directly selected elements from guardElements.
190                                         var selectedElement = range.getEnclosedNode();
192                                         // If this is not our element of interest, apply to fully selected elements from guardElements.
193                                         if ( !selectedElement || selectedElement
194                                                         && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )
195                                                 )
196                                                 selectedElement = getFullySelected( range, guardElements, enterMode );
198                                         if ( selectedElement && !selectedElement.isReadOnly() )
199                                                 switchDir( selectedElement, dir, editor, database );
201                                         var iterator,
202                                                 block;
204                                         // Walker searching for guardElements.
205                                         var walker = new CKEDITOR.dom.walker( range );
207                                         var start = bookmarks[ i ].startNode,
208                                                 end = bookmarks[ i++ ].endNode;
210                                         walker.evaluator = function( node )
211                                         {
212                                                 return !! ( node.type == CKEDITOR.NODE_ELEMENT
213                                                                 && node.getName() in guardElements
214                                                                 && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' )
215                                                                         && node.getParent().type == CKEDITOR.NODE_ELEMENT
216                                                                         && node.getParent().getName() == 'blockquote' )
217                                                                 // Element must be fully included in the range as well. (#6485).
218                                                                 && node.getPosition( start ) & CKEDITOR.POSITION_FOLLOWING
219                                                                 && ( ( node.getPosition( end ) & CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_CONTAINS ) == CKEDITOR.POSITION_PRECEDING ) );
220                                         };
222                                         while ( ( block = walker.next() ) )
223                                                 switchDir( block, dir, editor, database );
225                                         iterator = range.createIterator();
226                                         iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
228                                         while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
229                                                 !block.isReadOnly() && switchDir( block, dir, editor, database );
230                                         }
232                                 CKEDITOR.dom.element.clearAllMarkers( database );
234                                 editor.forceNextSelectionCheck();
235                                 // Restore selection position.
236                                 selection.selectBookmarks( bookmarks );
238                                 editor.focus();
239                         }
240                 };
241         }
243         CKEDITOR.plugins.add( 'bidi',
244         {
245                 requires : [ 'styles', 'button' ],
247                 init : function( editor )
248                 {
249                         // All buttons use the same code to register. So, to avoid
250                         // duplications, let's use this tool function.
251                         var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec )
252                         {
253                                 editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) );
255                                 editor.ui.addButton( buttonName,
256                                         {
257                                                 label : buttonLabel,
258                                                 command : commandName
259                                         });
260                         };
262                         var lang = editor.lang.bidi;
264                         addButtonCommand( 'BidiLtr', lang.ltr, 'bidiltr', bidiCommand( 'ltr' ) );
265                         addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );
267                         editor.on( 'selectionChange', onSelectionChange );
268                 }
269         });
271 })();
274  * Fired when the language direction of an element is changed
275  * @name CKEDITOR.editor#dirChanged
276  * @event
277  * @param {CKEDITOR.editor} editor This editor instance.
278  * @param {Object} eventData.node The element that is being changed.
279  * @param {String} eventData.dir The new direction.
280  */