Mimimum syndical pour en faire un produit zope / cmf.
[ckeditor.git] / skins / ckeditor / _source / plugins / bidi / plugin.js
diff --git a/skins/ckeditor/_source/plugins/bidi/plugin.js b/skins/ckeditor/_source/plugins/bidi/plugin.js
new file mode 100644 (file)
index 0000000..2233ee1
--- /dev/null
@@ -0,0 +1,334 @@
+/*\r
+Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.\r
+For licensing, see LICENSE.html or http://ckeditor.com/license\r
+*/\r
+\r
+(function()\r
+{\r
+       var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 },\r
+               directSelectionGuardElements = {},\r
+               // All guard elements which can have a direction applied on them.\r
+               allGuardElements = {};\r
+       CKEDITOR.tools.extend( directSelectionGuardElements, guardElements, { tr:1, p:1, div:1, li:1 } );\r
+       CKEDITOR.tools.extend( allGuardElements, directSelectionGuardElements, { td:1 } );\r
+\r
+       function onSelectionChange( e )\r
+       {\r
+               setToolbarStates( e );\r
+               handleMixedDirContent( e );\r
+       }\r
+\r
+       function setToolbarStates( evt )\r
+       {\r
+               var editor = evt.editor,\r
+                       path = evt.data.path;\r
+\r
+               if ( editor.readOnly )\r
+                       return;\r
+\r
+               var useComputedState = editor.config.useComputedState,\r
+                       selectedElement;\r
+\r
+               useComputedState = useComputedState === undefined || useComputedState;\r
+\r
+               // We can use computedState provided by the browser or traverse parents manually.\r
+               if ( !useComputedState )\r
+                       selectedElement = getElementForDirection( path.lastElement );\r
+\r
+               selectedElement = selectedElement || path.block || path.blockLimit;\r
+\r
+               // If we're having BODY here, user probably done CTRL+A, let's try to get the enclosed node, if any.\r
+               if ( selectedElement.is( 'body' ) )\r
+               {\r
+                       var enclosedNode = editor.getSelection().getRanges()[ 0 ].getEnclosedNode();\r
+                       enclosedNode && enclosedNode.type == CKEDITOR.NODE_ELEMENT && ( selectedElement = enclosedNode );\r
+               }\r
+\r
+               if ( !selectedElement  )\r
+                       return;\r
+\r
+               var selectionDir = useComputedState ?\r
+                       selectedElement.getComputedStyle( 'direction' ) :\r
+                       selectedElement.getStyle( 'direction' ) || selectedElement.getAttribute( 'dir' );\r
+\r
+               editor.getCommand( 'bidirtl' ).setState( selectionDir == 'rtl' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
+               editor.getCommand( 'bidiltr' ).setState( selectionDir == 'ltr' ? CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF );\r
+       }\r
+\r
+       function handleMixedDirContent( evt )\r
+       {\r
+               var editor = evt.editor,\r
+                       directionNode = evt.data.path.block || evt.data.path.blockLimit;\r
+\r
+               editor.fire( 'contentDirChanged', directionNode ? directionNode.getComputedStyle( 'direction' ) : editor.lang.dir );\r
+       }\r
+\r
+       /**\r
+        * Returns element with possibility of applying the direction.\r
+        * @param node\r
+        */\r
+       function getElementForDirection( node )\r
+       {\r
+               while ( node && !( node.getName() in allGuardElements || node.is( 'body' ) ) )\r
+               {\r
+                       var parent = node.getParent();\r
+                       if ( !parent )\r
+                               break;\r
+\r
+                       node = parent;\r
+               }\r
+\r
+               return node;\r
+       }\r
+\r
+       function switchDir( element, dir, editor, database )\r
+       {\r
+               if ( element.isReadOnly() )\r
+                       return;\r
+\r
+               // Mark this element as processed by switchDir.\r
+               CKEDITOR.dom.element.setMarker( database, element, 'bidi_processed', 1 );\r
+\r
+               // Check whether one of the ancestors has already been styled.\r
+               var parent = element;\r
+               while ( ( parent = parent.getParent() ) && !parent.is( 'body' ) )\r
+               {\r
+                       if ( parent.getCustomData( 'bidi_processed' ) )\r
+                       {\r
+                               // Ancestor style must dominate.\r
+                               element.removeStyle( 'direction' );\r
+                               element.removeAttribute( 'dir' );\r
+                               return;\r
+                       }\r
+               }\r
+\r
+               var useComputedState = ( 'useComputedState' in editor.config ) ? editor.config.useComputedState : 1;\r
+\r
+               var elementDir = useComputedState ? element.getComputedStyle( 'direction' )\r
+                       : element.getStyle( 'direction' ) || element.hasAttribute( 'dir' );\r
+\r
+               // Stop if direction is same as present.\r
+               if ( elementDir == dir )\r
+                       return;\r
+\r
+               // Clear direction on this element.\r
+               element.removeStyle( 'direction' );\r
+\r
+               // Do the second check when computed state is ON, to check\r
+               // if we need to apply explicit direction on this element.\r
+               if ( useComputedState )\r
+               {\r
+                       element.removeAttribute( 'dir' );\r
+                       if ( dir != element.getComputedStyle( 'direction' ) )\r
+                               element.setAttribute( 'dir', dir );\r
+               }\r
+               else\r
+                       // Set new direction for this element.\r
+                       element.setAttribute( 'dir', dir );\r
+\r
+               editor.forceNextSelectionCheck();\r
+\r
+               return;\r
+       }\r
+\r
+       function getFullySelected( range, elements, enterMode )\r
+       {\r
+               var ancestor = range.getCommonAncestor( false, true );\r
+\r
+               range = range.clone();\r
+               range.enlarge( enterMode == CKEDITOR.ENTER_BR ?\r
+                               CKEDITOR.ENLARGE_LIST_ITEM_CONTENTS\r
+                               : CKEDITOR.ENLARGE_BLOCK_CONTENTS );\r
+\r
+               if ( range.checkBoundaryOfElement( ancestor, CKEDITOR.START )\r
+                               && range.checkBoundaryOfElement( ancestor, CKEDITOR.END ) )\r
+               {\r
+                       var parent;\r
+                       while ( ancestor && ancestor.type == CKEDITOR.NODE_ELEMENT\r
+                                       && ( parent = ancestor.getParent() )\r
+                                       && parent.getChildCount() == 1\r
+                                       && !( ancestor.getName() in elements ) )\r
+                               ancestor = parent;\r
+\r
+                       return ancestor.type == CKEDITOR.NODE_ELEMENT\r
+                                       && ( ancestor.getName() in elements )\r
+                                       && ancestor;\r
+               }\r
+       }\r
+\r
+       function bidiCommand( dir )\r
+       {\r
+               return function( editor )\r
+               {\r
+                       var selection = editor.getSelection(),\r
+                               enterMode = editor.config.enterMode,\r
+                               ranges = selection.getRanges();\r
+\r
+                       if ( ranges && ranges.length )\r
+                       {\r
+                               var database = {};\r
+\r
+                               // Creates bookmarks for selection, as we may split some blocks.\r
+                               var bookmarks = selection.createBookmarks();\r
+\r
+                               var rangeIterator = ranges.createIterator(),\r
+                                       range,\r
+                                       i = 0;\r
+\r
+                               while ( ( range = rangeIterator.getNextRange( 1 ) ) )\r
+                               {\r
+                                       // Apply do directly selected elements from guardElements.\r
+                                       var selectedElement = range.getEnclosedNode();\r
+\r
+                                       // If this is not our element of interest, apply to fully selected elements from guardElements.\r
+                                       if ( !selectedElement || selectedElement\r
+                                                       && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )\r
+                                               )\r
+                                               selectedElement = getFullySelected( range, guardElements, enterMode );\r
+\r
+                                       selectedElement && switchDir( selectedElement, dir, editor, database );\r
+\r
+                                       var iterator,\r
+                                               block;\r
+\r
+                                       // Walker searching for guardElements.\r
+                                       var walker = new CKEDITOR.dom.walker( range );\r
+\r
+                                       var start = bookmarks[ i ].startNode,\r
+                                               end = bookmarks[ i++ ].endNode;\r
+\r
+                                       walker.evaluator = function( node )\r
+                                       {\r
+                                               return !! ( node.type == CKEDITOR.NODE_ELEMENT\r
+                                                               && node.getName() in guardElements\r
+                                                               && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' )\r
+                                                                       && node.getParent().type == CKEDITOR.NODE_ELEMENT\r
+                                                                       && node.getParent().getName() == 'blockquote' )\r
+                                                               // Element must be fully included in the range as well. (#6485).\r
+                                                               && node.getPosition( start ) & CKEDITOR.POSITION_FOLLOWING\r
+                                                               && ( ( node.getPosition( end ) & CKEDITOR.POSITION_PRECEDING + CKEDITOR.POSITION_CONTAINS ) == CKEDITOR.POSITION_PRECEDING ) );\r
+                                       };\r
+\r
+                                       while ( ( block = walker.next() ) )\r
+                                               switchDir( block, dir, editor, database );\r
+\r
+                                       iterator = range.createIterator();\r
+                                       iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;\r
+\r
+                                       while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )\r
+                                               switchDir( block, dir, editor, database );\r
+                                       }\r
+\r
+                               CKEDITOR.dom.element.clearAllMarkers( database );\r
+\r
+                               editor.forceNextSelectionCheck();\r
+                               // Restore selection position.\r
+                               selection.selectBookmarks( bookmarks );\r
+\r
+                               editor.focus();\r
+                       }\r
+               };\r
+       }\r
+\r
+       CKEDITOR.plugins.add( 'bidi',\r
+       {\r
+               requires : [ 'styles', 'button' ],\r
+\r
+               init : function( editor )\r
+               {\r
+                       // All buttons use the same code to register. So, to avoid\r
+                       // duplications, let's use this tool function.\r
+                       var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec )\r
+                       {\r
+                               editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) );\r
+\r
+                               editor.ui.addButton( buttonName,\r
+                                       {\r
+                                               label : buttonLabel,\r
+                                               command : commandName\r
+                                       });\r
+                       };\r
+\r
+                       var lang = editor.lang.bidi;\r
+\r
+                       addButtonCommand( 'BidiLtr', lang.ltr, 'bidiltr', bidiCommand( 'ltr' ) );\r
+                       addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );\r
+\r
+                       editor.on( 'selectionChange', onSelectionChange );\r
+                       editor.on( 'contentDom', function()\r
+                       {\r
+                               editor.document.on( 'dirChanged', function( evt )\r
+                               {\r
+                                       editor.fire( 'dirChanged',\r
+                                               {\r
+                                                       node : evt.data,\r
+                                                       dir : evt.data.getDirection( 1 )\r
+                                               } );\r
+                               });\r
+                       });\r
+               }\r
+       });\r
+\r
+       // If the element direction changed, we need to switch the margins of\r
+       // the element and all its children, so it will get really reflected\r
+       // like a mirror. (#5910)\r
+       function isOffline( el )\r
+       {\r
+               var html = el.getDocument().getBody().getParent();\r
+               while ( el )\r
+               {\r
+                       if ( el.equals( html ) )\r
+                               return false;\r
+                       el = el.getParent();\r
+               }\r
+               return true;\r
+       }\r
+       function dirChangeNotifier( org )\r
+       {\r
+               var isAttribute = org == elementProto.setAttribute,\r
+                       isRemoveAttribute = org == elementProto.removeAttribute,\r
+                       dirStyleRegexp = /\bdirection\s*:\s*(.*?)\s*(:?$|;)/;\r
+\r
+               return function( name, val )\r
+               {\r
+                       if ( !this.getDocument().equals( CKEDITOR.document ) )\r
+                       {\r
+                               var orgDir;\r
+                               if ( ( name == ( isAttribute || isRemoveAttribute ? 'dir' : 'direction' ) ||\r
+                                        name == 'style' && ( isRemoveAttribute || dirStyleRegexp.test( val ) ) ) && !isOffline( this ) )\r
+                               {\r
+                                       orgDir = this.getDirection( 1 );\r
+                                       var retval = org.apply( this, arguments );\r
+                                       if ( orgDir != this.getDirection( 1 ) )\r
+                                       {\r
+                                               this.getDocument().fire( 'dirChanged', this );\r
+                                               return retval;\r
+                                       }\r
+                               }\r
+                       }\r
+\r
+                       return org.apply( this, arguments );\r
+               };\r
+       }\r
+\r
+       var elementProto = CKEDITOR.dom.element.prototype,\r
+               methods = [ 'setStyle', 'removeStyle', 'setAttribute', 'removeAttribute' ];\r
+       for ( var i = 0; i < methods.length; i++ )\r
+               elementProto[ methods[ i ] ] = CKEDITOR.tools.override( elementProto[ methods [ i ] ], dirChangeNotifier );\r
+})();\r
+\r
+/**\r
+ * Fired when the language direction of an element is changed\r
+ * @name CKEDITOR.editor#dirChanged\r
+ * @event\r
+ * @param {CKEDITOR.editor} editor This editor instance.\r
+ * @param {Object} eventData.node The element that is being changed.\r
+ * @param {String} eventData.dir The new direction.\r
+ */\r
+\r
+/**\r
+ * Fired when the language direction in the specific cursor position is changed\r
+ * @name CKEDITOR.editor#contentDirChanged\r
+ * @event\r
+ * @param {String} eventData The direction in the current position.\r
+ */\r