[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/libraries/ckeditor/_source/plugins/bidi/ -> plugin.js (source)

   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  
   6  (function()
   7  {
   8      var guardElements = { table:1, ul:1, ol:1, blockquote:1, div:1 };
   9      var directSelectionGuardElements = {};
  10      CKEDITOR.tools.extend( directSelectionGuardElements, guardElements, { tr:1, p:1, div:1, li:1 } );
  11  
  12  	function onSelectionChange( evt )
  13      {
  14          evt.editor.getCommand( 'bidirtl' ).setState( getState( evt.editor, evt.data.path, 'rtl' ) );
  15          evt.editor.getCommand( 'bidiltr' ).setState( getState( evt.editor, evt.data.path, 'ltr' ) );
  16      }
  17  
  18  	function getState( editor, path, dir )
  19      {
  20          var useComputedState = editor.config.useComputedState,
  21              selectedElement;
  22  
  23          useComputedState = useComputedState === undefined || useComputedState;
  24  
  25          if ( useComputedState )
  26          {
  27              var selection = editor.getSelection(),
  28                  ranges = selection.getRanges();
  29  
  30              selectedElement = ranges && ranges[ 0 ].getEnclosedNode();
  31  
  32              // If this is not our element of interest, apply to fully selected elements from guardElements.

  33              if ( !selectedElement || selectedElement
  34                      && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )
  35                  )
  36                  selectedElement = getFullySelected( selection, guardElements );
  37          }
  38  
  39          selectedElement = selectedElement || path.block || path.blockLimit;
  40  
  41          if ( !selectedElement || selectedElement.getName() == 'body' )
  42              return CKEDITOR.TRISTATE_OFF;
  43  
  44          selectedElement = useComputedState ?
  45              selectedElement.getComputedStyle( 'direction' ) :
  46              selectedElement.getStyle( 'direction' ) || selectedElement.getAttribute( 'dir' );
  47  
  48          return ( selectedElement == dir ) ?
  49              CKEDITOR.TRISTATE_ON : CKEDITOR.TRISTATE_OFF;
  50      }
  51  
  52  	function switchDir( element, dir, editor )
  53      {
  54          var dirBefore = element.getComputedStyle( 'direction' ),
  55              currentDir = element.getStyle( 'direction' ) || element.getAttribute( 'dir' ) || '';
  56  
  57          element.removeStyle( 'direction' );
  58  
  59          if ( currentDir.toLowerCase() == dir )
  60              element.removeAttribute( 'dir' );
  61          else
  62              element.setAttribute( 'dir', dir );
  63  
  64          // If the element direction changed, we need to switch the margins of

  65          // the element and all its children, so it will get really reflected

  66          // like a mirror. (#5910)

  67          var dirAfter = element.getComputedStyle( 'direction' );
  68          if ( dirAfter != dirBefore )
  69          {
  70              var range = new CKEDITOR.dom.range( element.getDocument() );
  71              range.setStartBefore( element );
  72              range.setEndAfter( element );
  73  
  74              var walker = new CKEDITOR.dom.walker( range );
  75  
  76              var node;
  77              while ( ( node = walker.next() ) )
  78              {
  79                  if ( node.type == CKEDITOR.NODE_ELEMENT )
  80                  {
  81                      // A child with dir defined is to be ignored.

  82                      if ( !node.equals( element ) && node.hasAttribute( 'dir' ) )
  83                      {
  84                          range.setStartAfter( node );
  85                          walker = new CKEDITOR.dom.walker( range );
  86                          continue;
  87                      }
  88  
  89                      // Switch the margins.

  90                      var marginLeft = node.getStyle( 'margin-right' ),
  91                          marginRight = node.getStyle( 'margin-left' );
  92  
  93                      marginLeft ? node.setStyle( 'margin-left', marginLeft ) : node.removeStyle( 'margin-left' );
  94                      marginRight ? node.setStyle( 'margin-right', marginRight ) : node.removeStyle( 'margin-right' );
  95                  }
  96              }
  97          }
  98  
  99          editor.forceNextSelectionCheck();
 100      }
 101  
 102  	function getFullySelected( selection, elements )
 103      {
 104          var selectedElement = selection.getCommonAncestor();
 105          while( selectedElement.type == CKEDITOR.NODE_ELEMENT
 106                  && !( selectedElement.getName() in elements )
 107                  && selectedElement.getParent().getChildCount() == 1
 108              )
 109              selectedElement = selectedElement.getParent();
 110  
 111          return selectedElement.type == CKEDITOR.NODE_ELEMENT
 112              && ( selectedElement.getName() in elements )
 113              && selectedElement;
 114      }
 115  
 116  	function bidiCommand( dir )
 117      {
 118          return function( editor )
 119          {
 120              var selection = editor.getSelection(),
 121                  enterMode = editor.config.enterMode,
 122                  ranges = selection.getRanges();
 123  
 124              if ( ranges && ranges.length )
 125              {
 126                  // Apply do directly selected elements from guardElements.

 127                  var selectedElement = ranges[ 0 ].getEnclosedNode();
 128  
 129                  // If this is not our element of interest, apply to fully selected elements from guardElements.

 130                  if ( !selectedElement || selectedElement
 131                          && !( selectedElement.type == CKEDITOR.NODE_ELEMENT && selectedElement.getName() in directSelectionGuardElements )
 132                      )
 133                      selectedElement = getFullySelected( selection, guardElements );
 134  
 135                  if ( selectedElement )
 136                  {
 137                      if ( !selectedElement.isReadOnly() )
 138                          switchDir( selectedElement, dir, editor );
 139                  }
 140                  else
 141                  {
 142                      // Creates bookmarks for selection, as we may split some blocks.

 143                      var bookmarks = selection.createBookmarks();
 144  
 145                      var iterator,
 146                          block;
 147  
 148                      for ( var i = ranges.length - 1 ; i >= 0 ; i-- )
 149                      {
 150                          // Array of elements processed as guardElements.

 151                          var processedElements = [];
 152                          // Walker searching for guardElements.

 153                          var walker = new CKEDITOR.dom.walker( ranges[ i ] );
 154                          walker.evaluator = function( node ){
 155                              return node.type == CKEDITOR.NODE_ELEMENT
 156                                  && node.getName() in guardElements
 157                                  && !( node.getName() == ( enterMode == CKEDITOR.ENTER_P ) ? 'p' : 'div'
 158                                      && node.getParent().type == CKEDITOR.NODE_ELEMENT
 159                                      && node.getParent().getName() == 'blockquote'
 160                                  );
 161                          };
 162  
 163                          while ( ( block = walker.next() ) )
 164                          {
 165                              switchDir( block, dir, editor );
 166                              processedElements.push( block );
 167                          }
 168  
 169                          iterator = ranges[ i ].createIterator();
 170                          iterator.enlargeBr = enterMode != CKEDITOR.ENTER_BR;
 171  
 172                          while ( ( block = iterator.getNextParagraph( enterMode == CKEDITOR.ENTER_P ? 'p' : 'div' ) ) )
 173                          {
 174                              if ( block.isReadOnly() )
 175                                  continue;
 176  
 177                              var _break = 0;
 178  
 179                              // Check if block have been already processed by the walker above.

 180                              for ( var ii = 0; ii < processedElements.length; ii++ )
 181                              {
 182                                  var parent = block.getParent();
 183  
 184                                  while( parent && parent.getName() != 'body' )
 185                                  {
 186                                      if ( ( parent.$.isSameNode && parent.$.isSameNode( processedElements[ ii ].$ ) )
 187                                              || parent.$ == processedElements[ ii ].$ )
 188                                      {
 189                                          _break = 1;
 190                                          break;
 191                                      }
 192                                      parent = parent.getParent();
 193                                  }
 194  
 195                                  if ( _break )
 196                                      break;
 197                              }
 198  
 199                              if ( !_break )
 200                              {
 201                                  switchDir( block, dir, editor );
 202                              }
 203                          }
 204                      }
 205  
 206                      editor.forceNextSelectionCheck();
 207                      // Restore selection position.

 208                      selection.selectBookmarks( bookmarks );
 209                  }
 210  
 211                  editor.focus();
 212              }
 213          };
 214      }
 215  
 216      CKEDITOR.plugins.add( 'bidi',
 217      {
 218          requires : [ 'styles', 'button' ],
 219  
 220          init : function( editor )
 221          {
 222              // All buttons use the same code to register. So, to avoid

 223              // duplications, let's use this tool function.

 224              var addButtonCommand = function( buttonName, buttonLabel, commandName, commandExec )
 225              {
 226                  editor.addCommand( commandName, new CKEDITOR.command( editor, { exec : commandExec }) );
 227  
 228                  editor.ui.addButton( buttonName,
 229                      {
 230                          label : buttonLabel,
 231                          command : commandName
 232                      });
 233              };
 234  
 235              var lang = editor.lang.bidi;
 236  
 237              addButtonCommand( 'BidiLtr', lang.ltr, 'bidiltr', bidiCommand( 'ltr' ) );
 238              addButtonCommand( 'BidiRtl', lang.rtl, 'bidirtl', bidiCommand( 'rtl' ) );
 239  
 240              editor.on( 'selectionChange', onSelectionChange );
 241          }
 242      });
 243  
 244  })();


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7