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