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