| | 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 i, iterator; |
| | 147 | |
| | 148 | // collect all included elements from dom-iterator |
| | 149 | for( i = 0 ; i < ranges.length ; i++ ) |
| | 150 | { |
| | 151 | iterator = ranges[ i ].createIterator(); |
| | 152 | while( ( block = iterator.getNextParagraph() ) ) |
| | 153 | { |
| | 154 | // include contents of blockLimit elements. |
| | 155 | if( block.getName() in divLimitDefinition ) |
| | 156 | { |
| | 157 | var j, childNodes = block.getChildren(); |
| | 158 | for ( j = 0 ; j < childNodes.count() ; j++ ) |
| | 159 | addSafely( containedBlocks, childNodes.getItem( j ) , database ); |
| | 160 | } |
| | 161 | else |
| | 162 | { |
| | 163 | // Bypass dtd disallowed elements. |
| | 164 | while( !dtd[ block.getName() ] && block.getName() != 'body' ) |
| | 165 | block = block.getParent(); |
| | 166 | addSafely( containedBlocks, block, database ); |
| | 167 | } |
| | 168 | } |
| | 169 | } |
| | 170 | |
| | 171 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| | 172 | |
| | 173 | var blockGroups = groupByDivLimit( containedBlocks ); |
| | 174 | var ancestor, blockEl, divElement; |
| | 175 | |
| | 176 | for( i = 0 ; i < blockGroups.length ; i++ ) |
| | 177 | { |
| | 178 | var currentBlock = blockGroups[ i ][ 0 ]; |
| | 179 | |
| | 180 | // Calculate the common parent node of all contained elements. |
| | 181 | ancestor = currentBlock.getParent(); |
| | 182 | for ( var j = 1 ; j < blockGroups[ i ].length; j++ ) |
| | 183 | ancestor = ancestor.getCommonAncestor( blockGroups[ i ][ j ] ); |
| | 184 | |
| | 185 | if( isCreate ) |
| | 186 | divElement = new CKEDITOR.dom.element( 'div', editor.document ); |
| | 187 | |
| | 188 | // Normalize the blocks in each group to a common parent. |
| | 189 | for( var j = 0; j < blockGroups[ i ].length ; j++ ) |
| | 190 | { |
| | 191 | currentBlock = blockGroups[ i ][ j ]; |
| | 192 | |
| | 193 | while( !currentBlock.getParent().equals( ancestor ) ) |
| | 194 | currentBlock = currentBlock.getParent(); |
| | 195 | |
| | 196 | blockGroups[ i ][ j ] = currentBlock; |
| | 197 | } |
| | 198 | |
| | 199 | // Wrapped blocks counting |
| | 200 | var childCount = 0; |
| | 201 | for ( var j = 0 ; j < blockGroups[ i ].length ; j++ ) |
| | 202 | { |
| | 203 | currentBlock = blockGroups[ i ][ j ]; |
| | 204 | // Avoid DUP |
| | 205 | if ( !currentBlock.getCustomData( 'block_processed' ) ) |
| | 206 | { |
| | 207 | CKEDITOR.dom.element.setMarker( database, currentBlock, 'block_processed', true ); |
| | 208 | |
| | 209 | if( isCreate ) |
| | 210 | { |
| | 211 | // Establish new container, wrapping all elements in this group. |
| | 212 | if( j == 0 ) |
| | 213 | divElement.insertBefore( currentBlock ); |
| | 214 | divElement.append( currentBlock ); |
| | 215 | } |
| | 216 | else |
| | 217 | childCount++; |
| | 218 | } |
| | 219 | } |
| | 220 | |
| | 221 | CKEDITOR.dom.element.clearAllMarkers( database ); |
| | 222 | |
| | 223 | if( isCreate ) |
| | 224 | { |
| | 225 | // Drop pre-existed container, since new container already |
| | 226 | // established. |
| | 227 | if ( command == 'editdiv' && |
| | 228 | ancestor.getName() == 'div' |
| | 229 | && 1 == getNonEmptyChildren( ancestor ).length ) |
| | 230 | ancestor.remove( true ); |
| | 231 | containers.push( divElement ); |
| | 232 | } |
| | 233 | else |
| | 234 | { |
| | 235 | // discover existed container |
| | 236 | if ( ancestor.getName() == 'div' |
| | 237 | && childCount == getNonEmptyChildren( ancestor ).length ) |
| | 238 | containers.push( ancestor ); |
| | 239 | } |
| | 240 | } |
| | 241 | |
| | 242 | return containers; |
| | 243 | } |
| | 244 | |
| | 245 | /** |
| | 246 | * Divide a set of nodes to different groups by their path's blocklimit element. |
| | 247 | * Note: the specified nodes should be in source order naturally, which mean they are supposed to producea by following class: |
| | 248 | * * CKEDITOR.dom.range.Iterator |
| | 249 | * * CKEDITOR.dom.domWalker |
| | 250 | * @return {Array []} the grouped nodes |
| | 251 | */ |
| | 252 | function groupByDivLimit( nodes ) |
| | 253 | { |
| | 254 | var groups = [], |
| | 255 | lastDivLimit = null, |
| | 256 | path, block; |
| | 257 | for ( var i = 0 ; i < nodes.length ; i++ ) |
| | 258 | { |
| | 259 | block = nodes[i]; |
| | 260 | console.log( block.$ ); |
| | 261 | var limit = getDivLimitElement( block ); |
| | 262 | if ( !limit.equals( lastDivLimit ) ) |
| | 263 | { |
| | 264 | lastDivLimit = limit ; |
| | 265 | groups.push( [] ) ; |
| | 266 | } |
| | 267 | groups[ groups.length - 1 ].push( block ) ; |
| | 268 | } |
| | 269 | return groups; |
| | 270 | } |
| | 271 | |
| | 272 | function compareNodes() |
| | 273 | { |
| | 274 | return arguments[ 0 ].equals( arguments[ 1 ] ); |
| | 275 | } |
| | 276 | |
| | 277 | /** |
| | 278 | * Hold a collection of created block container elements. |
| | 279 | */ |
| | 280 | var containers = []; |
| | 281 | /** |
| | 282 | * @type divDialog |
| | 283 | */ |
| | 284 | return { |
| | 285 | title : editor.lang.div.title, |
| | 286 | minWidth : 400, |
| | 287 | minHeight : 180, |
| | 288 | contents : |
| | 289 | [ |
| | 290 | { |
| | 291 | id :'info', |
| | 292 | label :editor.lang.common.generalTab, |
| | 293 | title :editor.lang.common.generalTab, |
| | 294 | elements : |
| | 295 | [ |
| | 296 | { |
| | 297 | type :'hbox', |
| | 298 | widths : [ '50%', '50%' ], |
| | 299 | children : |
| | 300 | [ |
| | 301 | { |
| | 302 | id :'elementStyle', |
| | 303 | type :'select', |
| | 304 | style :'width: 100%;', |
| | 305 | label :editor.lang.div.styleSelectLabel, |
| | 306 | 'default' : '', |
| | 307 | items : [], |
| | 308 | setup : function( element ) |
| | 309 | { |
| | 310 | this.setValue( element.$.style.cssText || '' ); |
| | 311 | }, |
| | 312 | commit: function( element ) |
| | 313 | { |
| | 314 | if ( this.getValue() ) |
| | 315 | element.$.style.cssText = this.getValue(); |
| | 316 | else |
| | 317 | element.removeAttribute( 'style' ); |
| | 318 | } |
| | 319 | }, |
| | 320 | { |
| | 321 | id :'class', |
| | 322 | type :'text', |
| | 323 | label :editor.lang.common.cssClass, |
| | 324 | 'default' : '' |
| | 325 | } |
| | 326 | ] |
| | 327 | } |
| | 328 | ] |
| | 329 | }, |
| | 330 | { |
| | 331 | id :'advanced', |
| | 332 | label :editor.lang.common.advancedTab, |
| | 333 | title :editor.lang.common.advancedTab, |
| | 334 | elements : |
| | 335 | [ |
| | 336 | { |
| | 337 | type :'vbox', |
| | 338 | padding :1, |
| | 339 | children : |
| | 340 | [ |
| | 341 | { |
| | 342 | type :'hbox', |
| | 343 | widths : [ '50%', '50%' ], |
| | 344 | children : |
| | 345 | [ |
| | 346 | { |
| | 347 | type :'text', |
| | 348 | id :'id', |
| | 349 | label :editor.lang.common.id, |
| | 350 | 'default' : '' |
| | 351 | }, |
| | 352 | { |
| | 353 | type :'text', |
| | 354 | id :'lang', |
| | 355 | label :editor.lang.link.langCode, |
| | 356 | 'default' : '' |
| | 357 | } |
| | 358 | ] |
| | 359 | }, |
| | 360 | { |
| | 361 | type :'hbox', |
| | 362 | children : |
| | 363 | [ |
| | 364 | { |
| | 365 | type :'text', |
| | 366 | id :'style', |
| | 367 | style :'width: 100%;', |
| | 368 | label :editor.lang.common.cssStyle, |
| | 369 | 'default' : '' |
| | 370 | } |
| | 371 | ] |
| | 372 | }, |
| | 373 | { |
| | 374 | type :'hbox', |
| | 375 | children : |
| | 376 | [ |
| | 377 | { |
| | 378 | type :'text', |
| | 379 | id :'title', |
| | 380 | style :'width: 100%;', |
| | 381 | label :editor.lang.common.advisoryTitle, |
| | 382 | 'default' : '' |
| | 383 | } |
| | 384 | ] |
| | 385 | }, |
| | 386 | { |
| | 387 | type :'select', |
| | 388 | id :'dir', |
| | 389 | style :'width: 100%;', |
| | 390 | label :editor.lang.common.langDir, |
| | 391 | 'default' : '', |
| | 392 | items : |
| | 393 | [ |
| | 394 | [ |
| | 395 | editor.lang.common.langDirLtr, |
| | 396 | 'ltr' |
| | 397 | ], |
| | 398 | [ |
| | 399 | editor.lang.common.langDirRtl, |
| | 400 | 'rtl' |
| | 401 | ] |
| | 402 | ] |
| | 403 | } |
| | 404 | ] |
| | 405 | } |
| | 406 | ] |
| | 407 | } |
| | 408 | ], |
| | 409 | onLoad : function() |
| | 410 | { |
| | 411 | setupFields.call(this); |
| | 412 | }, |
| | 413 | onShow : function() |
| | 414 | { |
| | 415 | containers = []; |
| | 416 | // Whether always create new container regardless of existed |
| | 417 | // ones. |
| | 418 | if ( command == 'editdiv' ) |
| | 419 | { |
| | 420 | // Try to discover the containers that already existed in |
| | 421 | // ranges |
| | 422 | containers = applyDiv( editor ); |
| | 423 | if( containers.length ) |
| | 424 | { |
| | 425 | this._element = containers[ 0 ]; |
| | 426 | // update dialog field values |
| | 427 | this.setupContent( this._element ); |
| | 428 | } |
| | 429 | } |
| | 430 | }, |
| | 431 | onOk : function() |
| | 432 | { |
| | 433 | containers = applyDiv( editor, true ); |
| | 434 | |
| | 435 | // Update elements attributes |
| | 436 | for( var i = 0 ; i < containers.length ; i++ ) |
| | 437 | this.commitContent( containers[ i ] ); |
| | 438 | this.hide(); |
| | 439 | } |
| | 440 | }; |
| | 441 | } |
| | 442 | |
| | 443 | CKEDITOR.dialog.add( 'creatediv', function( editor ) |
| | 444 | { |
| | 445 | return divDialog( editor, 'creatediv' ); |
| | 446 | } ); |
| | 447 | CKEDITOR.dialog.add( 'editdiv', function( editor ) |
| | 448 | { |
| | 449 | return divDialog( editor, 'editdiv' ); |
| | 450 | } ); |
| | 451 | })(); |