| | 1 | /* |
| | 2 | * Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. |
| | 3 | * For licensing, see LICENSE.html or http://ckeditor.com/license |
| | 4 | */ |
| | 5 | |
| | 6 | (function() |
| | 7 | { |
| | 8 | |
| | 9 | /** |
| | 10 | * Add to collection with DUP examination. |
| | 11 | * @param {Object} collection |
| | 12 | * @param {Object} element |
| | 13 | * @param {Object} database |
| | 14 | */ |
| | 15 | function addSafely( collection, element, database ) |
| | 16 | { |
| | 17 | // avoid duplicate |
| | 18 | if ( !element.getCustomData( 'block_processed' ) ) |
| | 19 | { |
| | 20 | CKEDITOR.dom.element.setMarker( database, element, |
| | 21 | 'block_processed', true ); |
| | 22 | collection.push( element ); |
| | 23 | } |
| | 24 | } |
| | 25 | |
| | 26 | function getNonEmptyChildren( element ) |
| | 27 | { |
| | 28 | var retval = []; |
| | 29 | var children = element.getChildren(); |
| | 30 | for( var i = 0 ; i < children.count() ; i++ ) |
| | 31 | { |
| | 32 | var child = children.getItem( i ); |
| | 33 | if( ! ( child.type === CKEDITOR.NODE_TEXT |
| | 34 | && /^[ \t\n\r]+$/.test( child.getText() ) ) ) |
| | 35 | retval.push( child ); |
| | 36 | } |
| | 37 | return retval; |
| | 38 | } |
| | 39 | |
| | 40 | |
| | 41 | /** |
| | 42 | * Dialog reused by both 'creatediv' and 'editdiv' commands. |
| | 43 | * @param {Object} editor |
| | 44 | * @param {String} command The command name which indicate what the current command is. |
| | 45 | */ |
| | 46 | function divDialog( editor, command ) |
| | 47 | { |
| | 48 | // Definition of elements at which div operation should stopped. |
| | 49 | var divLimitDefinition = ( function(){ |
| | 50 | |
| | 51 | // Customzie from specialize blockLimit elements |
| | 52 | var definition = CKEDITOR.tools.extend( {}, CKEDITOR.dtd.$blockLimit ); |
| | 53 | |
| | 54 | // Exclude 'div' itself. |
| | 55 | delete definition.div; |
| | 56 | |
| | 57 | // Exclude 'td' and 'th' when 'wrapping table' |
| | 58 | if( editor.config.div_wrapTable ) |
| | 59 | { |
| | 60 | delete definition.td; |
| | 61 | delete definition.th; |
| | 62 | } |
| | 63 | return definition; |
| | 64 | })(); |
| | 65 | |
| | 66 | // DTD of 'div' element |
| | 67 | var dtd = CKEDITOR.dtd.div; |
| | 68 | |
| | 69 | /** |
| | 70 | * Get the first div limit element on the element's path. |
| | 71 | * @param {Object} element |
| | 72 | */ |
| | 73 | function getDivLimitElement( element ) |
| | 74 | { |
| | 75 | var pathElements = new CKEDITOR.dom.elementPath( element ).elements; |
| | 76 | var divLimit; |
| | 77 | for ( var i = 0; i < pathElements.length ; i++ ) |
| | 78 | { |
| | 79 | if ( pathElements[ i ].getName() in divLimitDefinition ) |
| | 80 | { |
| | 81 | divLimit = pathElements[ i ]; |
| | 82 | break; |
| | 83 | } |
| | 84 | } |
| | 85 | return divLimit; |
| | 86 | } |
| | 87 | |
| | 88 | /** |
| | 89 | * Init all fields' setup/commit function. |
| | 90 | * @memberof divDialog |
| | 91 | */ |
| | 92 | function setupFields() |
| | 93 | { |
| | 94 | this.foreach( function( field ) |
| | 95 | { |
| | 96 | // Exclude layout container elements |
| | 97 | if( /^(?!vbox|hbox)/.test( field.type ) ) |
| | 98 | { |
| | 99 | if ( !field.setup ) |
| | 100 | { |
| | 101 | // Read the dialog fields values from the specified |
| | 102 | // element attributes. |
| | 103 | field.setup = function( element ) |
| | 104 | { |
| | 105 | field.setValue( element.getAttribute( field.id ) || '' ); |
| | 106 | }; |
| | 107 | } |
| | 108 | if ( !field.commit ) |
| | 109 | { |
| | 110 | // Set element attributes assigned by the dialog |
| | 111 | // fields. |
| | 112 | field.commit = function( element ) |
| | 113 | { |
| | 114 | var fieldValue = this.getValue(); |
| | 115 | // ignore default element attribute values |
| | 116 | if ( 'dir' == field.id && element.getComputedStyle( 'direction' ) == fieldValue ) |
| | 117 | return true; |
| | 118 | if ( fieldValue ) |
| | 119 | element.setAttribute( field.id, fieldValue ); |
| | 120 | else |
| | 121 | element.removeAttribute( field.id ); |
| | 122 | }; |
| | 123 | } |
| | 124 | } |
| | 125 | } ); |
| | 126 | } |
| | 127 | |
| | 128 | /** |
| | 129 | * Either create or detect the desired block containers among the selected ranges. |
| | 130 | * @param {Object} editor |
| | 131 | * @param {Object} containerTagName The tagName of the container. |
| | 132 | * @param {Object} isCreate Whether it's a creation process OR a detection process. |
| | 133 | */ |
| | 134 | function applyDiv( editor, isCreate ) |
| | 135 | { |
| | 136 | // new adding containers OR detected pre-existed containers. |
| | 137 | var containers = []; |
| | 138 | // node markers store. |
| | 139 | var database = {}; |
| | 140 | // All block level elements which contained by the ranges. |
| | 141 | var containedBlocks = [], block; |
| | 142 | |
| | 143 | // Get all ranges from the selection. |
| | 144 | var selection = editor.document.getSelection(); |
| | 145 | var ranges = selection.getRanges(); |
| | 146 | var bookmarks = selection.createBookmarks(); |
| | 147 | var i, iterator; |
| | 148 | |
| | 149 | // Calcualte a default block tag if we need to create blocks. |
| | 150 | var blockTag = editor.config.enterMode == CKEDITOR.ENTER_DIV ? 'div' : 'p'; |
| | 151 | |
| | 152 | // collect all included elements from dom-iterator |
| | 153 | for( i = 0 ; i < ranges.length ; i++ ) |
| | 154 | { |
| | 155 | iterator = ranges[ i ].createIterator(); |
| | 156 | while( ( block = iterator.getNextParagraph() ) ) |
| | 157 | { |
| | 158 | // include contents of blockLimit elements. |
| | 159 | if( block.getName() in divLimitDefinition ) |
| | 160 | { |
| | 161 | var j, childNodes = block.getChildren(); |
| | 162 | for ( j = 0 ; j < childNodes.count() ; j++ ) |
| | 163 | addSafely( containedBlocks, childNodes.getItem( j ) , database ); |
| | 164 | } |
| | 165 | else |
| | 166 | { |
| | 167 | // Bypass dtd disallowed elements. |
| | 168 | while( !dtd[ block.getName() ] && block.getName() != 'body' ) |
| | 169 | block = block.getParent(); |
| | 170 | addSafely( containedBlocks, block, database ); |
| | 171 | } |
| | 172 | } |
| | 173 | } |
| | 174 | |
| | 175 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| | 176 | |
| | 177 | var blockGroups = groupByDivLimit( containedBlocks ); |
| | 178 | var ancestor, blockEl, divElement; |
| | 179 | |
| | 180 | for( i = 0 ; i < blockGroups.length ; i++ ) |
| | 181 | { |
| | 182 | var currentBlock = blockGroups[ i ][ 0 ]; |
| | 183 | |
| | 184 | // Calculate the common parent node of all contained elements. |
| | 185 | ancestor = currentBlock.getParent(); |
| | 186 | for ( var j = 1 ; j < blockGroups[ i ].length; j++ ) |
| | 187 | ancestor = ancestor.getCommonAncestor( blockGroups[ i ][ j ] ); |
| | 188 | |
| | 189 | if( isCreate ) |
| | 190 | divElement = new CKEDITOR.dom.element( 'div', editor.document ); |
| | 191 | |
| | 192 | // Normalize the blocks in each group to a common parent. |
| | 193 | for( var j = 0; j < blockGroups[ i ].length ; j++ ) |
| | 194 | { |
| | 195 | currentBlock = blockGroups[ i ][ j ]; |
| | 196 | |
| | 197 | while( !currentBlock.getParent().equals( ancestor ) ) |
| | 198 | currentBlock = currentBlock.getParent(); |
| | 199 | |
| | 200 | blockGroups[ i ][ j ] = currentBlock; |
| | 201 | } |
| | 202 | |
| | 203 | // Wrapped blocks counting |
| | 204 | var childCount = 0; |
| | 205 | for ( var j = 0 ; j < blockGroups[ i ].length ; j++ ) |
| | 206 | { |
| | 207 | currentBlock = blockGroups[ i ][ j ]; |
| | 208 | // Avoid DUP |
| | 209 | if ( !currentBlock.getCustomData( 'block_processed' ) ) |
| | 210 | { |
| | 211 | CKEDITOR.dom.element.setMarker( database, currentBlock, 'block_processed', true ); |
| | 212 | |
| | 213 | if( isCreate ) |
| | 214 | { |
| | 215 | // Establish new container, wrapping all elements in this group. |
| | 216 | if( j == 0 ) |
| | 217 | divElement.insertBefore( currentBlock ); |
| | 218 | divElement.append( currentBlock ); |
| | 219 | } |
| | 220 | else |
| | 221 | childCount++; |
| | 222 | } |
| | 223 | } |
| | 224 | |
| | 225 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| | 226 | |
| | 227 | if( isCreate ) |
| | 228 | { |
| | 229 | // Drop pre-existed container, since new container already |
| | 230 | // established. |
| | 231 | if ( command == 'editdiv' && |
| | 232 | ancestor.getName() == 'div' |
| | 233 | && 1 == getNonEmptyChildren( ancestor ).length ) |
| | 234 | ancestor.remove( true ); |
| | 235 | |
| | 236 | // If any non-block nodes are added, shuffle them to new blocks. |
| | 237 | var fixedBlock = null; |
| | 238 | for ( var j = divElement.getChildCount() - 1 ; j >= 0 ; j-- ) |
| | 239 | { |
| | 240 | var child = divElement.getChild( j ); |
| | 241 | |
| | 242 | if ( !( child.type == CKEDITOR.NODE_ELEMENT && CKEDITOR.dtd.$block[ child.getName() ] ) ) |
| | 243 | { |
| | 244 | if ( fixedBlock == null ) |
| | 245 | { |
| | 246 | fixedBlock = editor.document.createElement( blockTag ); |
| | 247 | fixedBlock.insertAfter( child ); |
| | 248 | } |
| | 249 | child.move( fixedBlock, true ); |
| | 250 | } |
| | 251 | else |
| | 252 | fixedBlock = null; |
| | 253 | } |
| | 254 | |
| | 255 | containers.push( divElement ); |
| | 256 | } |
| | 257 | else |
| | 258 | { |
| | 259 | // discover existed container |
| | 260 | if ( ancestor.getName() == 'div' |
| | 261 | && childCount == getNonEmptyChildren( ancestor ).length ) |
| | 262 | containers.push( ancestor ); |
| | 263 | } |
| | 264 | } |
| | 265 | |
| | 266 | selection.selectBookmarks( bookmarks ); |
| | 267 | return containers; |
| | 268 | } |
| | 269 | |
| | 270 | /** |
| | 271 | * Divide a set of nodes to different groups by their path's blocklimit element. |
| | 272 | * Note: the specified nodes should be in source order naturally, which mean they are supposed to producea by following class: |
| | 273 | * * CKEDITOR.dom.range.Iterator |
| | 274 | * * CKEDITOR.dom.domWalker |
| | 275 | * @return {Array []} the grouped nodes |
| | 276 | */ |
| | 277 | function groupByDivLimit( nodes ) |
| | 278 | { |
| | 279 | var groups = [], |
| | 280 | lastDivLimit = null, |
| | 281 | path, block; |
| | 282 | for ( var i = 0 ; i < nodes.length ; i++ ) |
| | 283 | { |
| | 284 | block = nodes[i]; |
| | 285 | var limit = getDivLimitElement( block ); |
| | 286 | if ( !limit.equals( lastDivLimit ) ) |
| | 287 | { |
| | 288 | lastDivLimit = limit ; |
| | 289 | groups.push( [] ) ; |
| | 290 | } |
| | 291 | groups[ groups.length - 1 ].push( block ) ; |
| | 292 | } |
| | 293 | return groups; |
| | 294 | } |
| | 295 | |
| | 296 | /** |
| | 297 | * Hold a collection of created block container elements. |
| | 298 | */ |
| | 299 | var containers = []; |
| | 300 | /** |
| | 301 | * @type divDialog |
| | 302 | */ |
| | 303 | return { |
| | 304 | title : editor.lang.div.title, |
| | 305 | minWidth : 400, |
| | 306 | minHeight : 165, |
| | 307 | contents : |
| | 308 | [ |
| | 309 | { |
| | 310 | id :'info', |
| | 311 | label :editor.lang.common.generalTab, |
| | 312 | title :editor.lang.common.generalTab, |
| | 313 | elements : |
| | 314 | [ |
| | 315 | { |
| | 316 | type :'hbox', |
| | 317 | widths : [ '50%', '50%' ], |
| | 318 | children : |
| | 319 | [ |
| | 320 | { |
| | 321 | id :'elementStyle', |
| | 322 | type :'select', |
| | 323 | style :'width: 100%;', |
| | 324 | label :editor.lang.div.styleSelectLabel, |
| | 325 | 'default' : '', |
| | 326 | items : [], |
| | 327 | setup : function( element ) |
| | 328 | { |
| | 329 | this.setValue( element.$.style.cssText || '' ); |
| | 330 | }, |
| | 331 | commit: function( element ) |
| | 332 | { |
| | 333 | if ( this.getValue() ) |
| | 334 | element.$.style.cssText = this.getValue(); |
| | 335 | else |
| | 336 | element.removeAttribute( 'style' ); |
| | 337 | } |
| | 338 | }, |
| | 339 | { |
| | 340 | id :'class', |
| | 341 | type :'text', |
| | 342 | label :editor.lang.common.cssClass, |
| | 343 | 'default' : '' |
| | 344 | } |
| | 345 | ] |
| | 346 | } |
| | 347 | ] |
| | 348 | }, |
| | 349 | { |
| | 350 | id :'advanced', |
| | 351 | label :editor.lang.common.advancedTab, |
| | 352 | title :editor.lang.common.advancedTab, |
| | 353 | elements : |
| | 354 | [ |
| | 355 | { |
| | 356 | type :'vbox', |
| | 357 | padding :1, |
| | 358 | children : |
| | 359 | [ |
| | 360 | { |
| | 361 | type :'hbox', |
| | 362 | widths : [ '50%', '50%' ], |
| | 363 | children : |
| | 364 | [ |
| | 365 | { |
| | 366 | type :'text', |
| | 367 | id :'id', |
| | 368 | label :editor.lang.common.id, |
| | 369 | 'default' : '' |
| | 370 | }, |
| | 371 | { |
| | 372 | type :'text', |
| | 373 | id :'lang', |
| | 374 | label :editor.lang.link.langCode, |
| | 375 | 'default' : '' |
| | 376 | } |
| | 377 | ] |
| | 378 | }, |
| | 379 | { |
| | 380 | type :'hbox', |
| | 381 | children : |
| | 382 | [ |
| | 383 | { |
| | 384 | type :'text', |
| | 385 | id :'style', |
| | 386 | style :'width: 100%;', |
| | 387 | label :editor.lang.common.cssStyle, |
| | 388 | 'default' : '' |
| | 389 | } |
| | 390 | ] |
| | 391 | }, |
| | 392 | { |
| | 393 | type :'hbox', |
| | 394 | children : |
| | 395 | [ |
| | 396 | { |
| | 397 | type :'text', |
| | 398 | id :'title', |
| | 399 | style :'width: 100%;', |
| | 400 | label :editor.lang.common.advisoryTitle, |
| | 401 | 'default' : '' |
| | 402 | } |
| | 403 | ] |
| | 404 | }, |
| | 405 | { |
| | 406 | type :'select', |
| | 407 | id :'dir', |
| | 408 | style :'width: 100%;', |
| | 409 | label :editor.lang.common.langDir, |
| | 410 | 'default' : '', |
| | 411 | items : |
| | 412 | [ |
| | 413 | [ |
| | 414 | editor.lang.common.langDirLtr, |
| | 415 | 'ltr' |
| | 416 | ], |
| | 417 | [ |
| | 418 | editor.lang.common.langDirRtl, |
| | 419 | 'rtl' |
| | 420 | ] |
| | 421 | ] |
| | 422 | } |
| | 423 | ] |
| | 424 | } |
| | 425 | ] |
| | 426 | } |
| | 427 | ], |
| | 428 | onLoad : function() |
| | 429 | { |
| | 430 | setupFields.call(this); |
| | 431 | }, |
| | 432 | onShow : function() |
| | 433 | { |
| | 434 | containers = []; |
| | 435 | // Whether always create new container regardless of existed |
| | 436 | // ones. |
| | 437 | if ( command == 'editdiv' ) |
| | 438 | { |
| | 439 | // Try to discover the containers that already existed in |
| | 440 | // ranges |
| | 441 | containers = applyDiv( editor ); |
| | 442 | if( containers.length ) |
| | 443 | { |
| | 444 | this._element = containers[ 0 ]; |
| | 445 | // update dialog field values |
| | 446 | this.setupContent( this._element ); |
| | 447 | } |
| | 448 | } |
| | 449 | }, |
| | 450 | onOk : function() |
| | 451 | { |
| | 452 | containers = applyDiv( editor, true ); |
| | 453 | |
| | 454 | // Update elements attributes |
| | 455 | for( var i = 0 ; i < containers.length ; i++ ) |
| | 456 | this.commitContent( containers[ i ] ); |
| | 457 | this.hide(); |
| | 458 | } |
| | 459 | }; |
| | 460 | } |
| | 461 | |
| | 462 | CKEDITOR.dialog.add( 'creatediv', function( editor ) |
| | 463 | { |
| | 464 | return divDialog( editor, 'creatediv' ); |
| | 465 | } ); |
| | 466 | CKEDITOR.dialog.add( 'editdiv', function( editor ) |
| | 467 | { |
| | 468 | return divDialog( editor, 'editdiv' ); |
| | 469 | } ); |
| | 470 | })(); |