X-Git-Url: https://jasonwoof.com/gitweb/?a=blobdiff_plain;f=_source%2Fplugins%2Ftable%2Fdialogs%2Ftable.js;h=2fd2ff050dee1389257716d78d47da88f61df95b;hb=e73319a12b56100b29ef456fd74114fe5519e01c;hp=2c02aa2dbba44d8e60c3d0395822adb8c539faff;hpb=ea7e3453c7b0f023b050aca6d9f83ab372860d91;p=ckeditor.git diff --git a/_source/plugins/table/dialogs/table.js b/_source/plugins/table/dialogs/table.js index 2c02aa2..2fd2ff0 100644 --- a/_source/plugins/table/dialogs/table.js +++ b/_source/plugins/table/dialogs/table.js @@ -1,12 +1,11 @@ /* -Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. +Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved. For licensing, see LICENSE.html or http://ckeditor.com/license */ (function() { - var widthPattern = /^(\d+(?:\.\d+)?)(px|%)$/, - heightPattern = /^(\d+(?:\.\d+)?)px$/; + var defaultToPixel = CKEDITOR.tools.cssLength; var commitValue = function( data ) { @@ -16,14 +15,63 @@ For licensing, see LICENSE.html or http://ckeditor.com/license data.info[id] = this.getValue(); }; + function tableColumns( table ) + { + var cols = 0, maxCols = 0; + for ( var i = 0, row, rows = table.$.rows.length; i < rows; i++ ) + { + row = table.$.rows[ i ], cols = 0; + for ( var j = 0, cell, cells = row.cells.length; j < cells; j++ ) + { + cell = row.cells[ j ]; + cols += cell.colSpan; + } + + cols > maxCols && ( maxCols = cols ); + } + + return maxCols; + } + function tableDialog( editor, command ) { - var makeElement = function( name ){ return new CKEDITOR.dom.element( name, editor.document ); }; + var makeElement = function( name ) + { + return new CKEDITOR.dom.element( name, editor.document ); + }; + + var dialogadvtab = editor.plugins.dialogadvtab; return { title : editor.lang.table.title, minWidth : 310, minHeight : CKEDITOR.env.ie ? 310 : 280, + + onLoad : function() + { + var dialog = this; + + var styles = dialog.getContentElement( 'advanced', 'advStyles' ); + + if ( styles ) + { + styles.on( 'change', function( evt ) + { + // Synchronize width value. + var width = this.getStyle( 'width', '' ), + txtWidth = dialog.getContentElement( 'info', 'txtWidth' ); + + txtWidth && txtWidth.setValue( width, true ); + + // Synchronize height value. + var height = this.getStyle( 'height', '' ), + txtHeight = dialog.getContentElement( 'info', 'txtHeight' ); + + txtHeight && txtHeight.setValue( height, true ); + }); + } + }, + onShow : function() { // Detect if there's a selected table. @@ -33,16 +81,20 @@ For licensing, see LICENSE.html or http://ckeditor.com/license var rowsInput = this.getContentElement( 'info', 'txtRows' ), colsInput = this.getContentElement( 'info', 'txtCols' ), - widthInput = this.getContentElement( 'info', 'txtWidth' ); + widthInput = this.getContentElement( 'info', 'txtWidth' ), + heightInput = this.getContentElement( 'info', 'txtHeight' ); + if ( command == 'tableProperties' ) { - if ( ( selectedTable = editor.getSelection().getSelectedElement() ) ) - { - if ( selectedTable.getName() != 'table' ) - selectedTable = null; - } + if ( ( selectedTable = selection.getSelectedElement() ) ) + selectedTable = selectedTable.getAscendant( 'table', true ); else if ( ranges.length > 0 ) { + // Webkit could report the following range on cell selection (#4948): + //
] + if ( CKEDITOR.env.webkit ) + ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT ); + var rangeRoot = ranges[0].getCommonAncestor( true ); selectedTable = rangeRoot.getAscendant( 'table', true ); } @@ -51,23 +103,29 @@ For licensing, see LICENSE.html or http://ckeditor.com/license this._.selectedElement = selectedTable; } - // Enable, disable and select the row, cols, width fields. + // Enable or disable the row, cols, width fields. if ( selectedTable ) { this.setupContent( selectedTable ); rowsInput && rowsInput.disable(); colsInput && colsInput.disable(); - widthInput && widthInput.select(); } else { rowsInput && rowsInput.enable(); colsInput && colsInput.enable(); - rowsInput && rowsInput.select(); } + + // Call the onChange method for the widht and height fields so + // they get reflected into the Advanced tab. + widthInput && widthInput.onChange(); + heightInput && heightInput.onChange(); }, onOk : function() { + var selection = editor.getSelection(), + bms = this._.selectedElement && selection.createBookmarks(); + var table = this._.selectedElement || makeElement( 'table' ), me = this, data = {}; @@ -97,7 +155,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license } } - // Modify the table headers. Depends on havint rows and cols generated + // Modify the table headers. Depends on having rows and cols generated // correctly so it can't be done in commit functions. // Should we make a ? @@ -112,11 +170,11 @@ For licensing, see LICENSE.html or http://ckeditor.com/license for ( i = 0 ; i < theRow.getChildCount() ; i++ ) { var th = theRow.getChild( i ); - if ( th.type == CKEDITOR.NODE_ELEMENT ) + // Skip bookmark nodes. (#6155) + if ( th.type == CKEDITOR.NODE_ELEMENT && !th.data( 'cke-bookmark' ) ) { th.renameNode( 'th' ); - if ( !i ) - th.setAttribute( 'scope', 'col' ); + th.setAttribute( 'scope', 'col' ); } } thead.append( theRow.remove() ); @@ -149,50 +207,55 @@ For licensing, see LICENSE.html or http://ckeditor.com/license // Should we make all first cells in a row TH? if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) ) { - for( row = 0 ; row < table.$.rows.length ; row++ ) + for ( row = 0 ; row < table.$.rows.length ; row++ ) { newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] ); newCell.renameNode( 'th' ); - newCell.setAttribute( 'scope', 'col' ); + newCell.setAttribute( 'scope', 'row' ); } } // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-) if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) ) { - for( i = 0 ; i < table.$.rows.length ; i++ ) + for ( i = 0 ; i < table.$.rows.length ; i++ ) { row = new CKEDITOR.dom.element( table.$.rows[i] ); if ( row.getParent().getName() == 'tbody' ) { newCell = new CKEDITOR.dom.element( row.$.cells[0] ); - newCell.renameNode( 'td'); + newCell.renameNode( 'td' ); newCell.removeAttribute( 'scope' ); } } } // Set the width and height. - var styles = []; - if ( info.txtHeight ) - styles.push( 'height:' + info.txtHeight + 'px' ); - if ( info.txtWidth ) - { - var type = info.cmbWidthType || 'pixels'; - styles.push( 'width:' + info.txtWidth + ( type == 'pixels' ? 'px' : '%' ) ); - } - styles = styles.join( ';' ); - if ( styles ) - table.$.style.cssText = styles; - else + info.txtHeight ? table.setStyle( 'height', info.txtHeight ) : table.removeStyle( 'height' ); + info.txtWidth ? table.setStyle( 'width', info.txtWidth ) : table.removeStyle( 'width' ); + + if ( !table.getAttribute( 'style' ) ) table.removeAttribute( 'style' ); } // Insert the table element if we're creating one. if ( !this._.selectedElement ) + { editor.insertElement( table ); - - return true; + // Override the default cursor position after insertElement to place + // cursor inside the first cell (#7959), IE needs a while. + setTimeout( function() + { + var firstCell = new CKEDITOR.dom.element( table.$.rows[ 0 ].cells[ 0 ] ); + var range = new CKEDITOR.dom.range( editor.document ); + range.moveToPosition( firstCell, CKEDITOR.POSITION_AFTER_START ); + range.select( 1 ); + }, 0 ); + } + // Properly restore the selection, (#4822) but don't break + // because of this, e.g. updated table caption. + else + try { selection.selectBookmarks( bms ); } catch( er ){} }, contents : [ { @@ -216,7 +279,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license id : 'txtRows', 'default' : 3, label : editor.lang.table.rows, - style : 'width:5em', + required : true, + controlStyle : 'width:5em', validate : function() { var pass = true, @@ -241,7 +305,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license id : 'txtCols', 'default' : 2, label : editor.lang.table.columns, - style : 'width:5em', + required : true, + controlStyle : 'width:5em', validate : function() { var pass = true, @@ -257,7 +322,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license }, setup : function( selectedTable ) { - this.setValue( selectedTable.$.rows[0].cells.length); + this.setValue( tableColumns( selectedTable ) ); }, commit : commitValue }, @@ -287,7 +352,8 @@ For licensing, see LICENSE.html or http://ckeditor.com/license for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ ) { // If just one cell isn't a TH then it isn't a header column - if ( selectedTable.$.rows[row].cells[0].nodeName.toLowerCase() != 'th' ) + var headCell = selectedTable.$.rows[row].cells[0]; + if ( headCell && headCell.nodeName.toLowerCase() != 'th' ) { dialog.hasColumnHeaders = false; break; @@ -307,7 +373,7 @@ For licensing, see LICENSE.html or http://ckeditor.com/license id : 'txtBorder', 'default' : 1, label : editor.lang.table.border, - style : 'width:3em', + controlStyle : 'width:3em', validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ), setup : function( selectedTable ) { @@ -325,13 +391,13 @@ For licensing, see LICENSE.html or http://ckeditor.com/license id : 'cmbAlign', type : 'select', 'default' : '', - label : editor.lang.table.align, + label : editor.lang.common.align, items : [ - [ editor.lang.table.alignNotSet , ''], - [ editor.lang.table.alignLeft , 'left'], - [ editor.lang.table.alignCenter , 'center'], - [ editor.lang.table.alignRight , 'right'] + [ editor.lang.common.notSet , ''], + [ editor.lang.common.alignLeft , 'left'], + [ editor.lang.common.alignCenter , 'center'], + [ editor.lang.common.alignRight , 'right'] ], setup : function( selectedTable ) { @@ -360,33 +426,21 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { type : 'text', id : 'txtWidth', - style : 'width:5em', - label : editor.lang.table.width, - 'default' : 200, - validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidWidth ), - setup : function( selectedTable ) + controlStyle : 'width:5em', + label : editor.lang.common.width, + title : editor.lang.common.cssLengthTooltip, + 'default' : 500, + getValue : defaultToPixel, + validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.width ) ), + onChange : function() { - var widthMatch = widthPattern.exec( selectedTable.$.style.width ); - if ( widthMatch ) - this.setValue( widthMatch[1] ); + var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' ); + styles && styles.updateStyle( 'width', this.getValue() ); }, - commit : commitValue - }, - { - id : 'cmbWidthType', - type : 'select', - label : ' ', - 'default' : 'pixels', - items : - [ - [ editor.lang.table.widthPx , 'pixels'], - [ editor.lang.table.widthPc , 'percents'] - ], setup : function( selectedTable ) { - var widthMatch = widthPattern.exec( selectedTable.$.style.width ); - if ( widthMatch ) - this.setValue( widthMatch[2] == 'px' ? 'pixels' : 'percents' ); + var val = selectedTable.getStyle( 'width' ); + val && this.setValue( val ); }, commit : commitValue } @@ -400,21 +454,24 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { type : 'text', id : 'txtHeight', - style : 'width:5em', - label : editor.lang.table.height, + controlStyle : 'width:5em', + label : editor.lang.common.height, + title : editor.lang.common.cssLengthTooltip, 'default' : '', - validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidHeight ), + getValue : defaultToPixel, + validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.height ) ), + onChange : function() + { + var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' ); + styles && styles.updateStyle( 'height', this.getValue() ); + }, + setup : function( selectedTable ) { - var heightMatch = heightPattern.exec( selectedTable.$.style.height ); - if ( heightMatch ) - this.setValue( heightMatch[1] ); + var val = selectedTable.getStyle( 'width' ); + val && this.setValue( val ); }, commit : commitValue - }, - { - type : 'html', - html : '
' + editor.lang.table.widthPx } ] }, @@ -425,10 +482,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { type : 'text', id : 'txtCellSpace', - style : 'width:3em', + controlStyle : 'width:3em', label : editor.lang.table.cellSpace, 'default' : 1, - validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellSpacing ), + validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellSpacing ), setup : function( selectedTable ) { this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' ); @@ -444,10 +501,10 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { type : 'text', id : 'txtCellPad', - style : 'width:3em', + controlStyle : 'width:3em', label : editor.lang.table.cellPad, 'default' : 1, - validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidCellPadding ), + validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellPadding ), setup : function( selectedTable ) { this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' ); @@ -480,17 +537,30 @@ For licensing, see LICENSE.html or http://ckeditor.com/license label : editor.lang.table.caption, setup : function( selectedTable ) { + this.enable(); + var nodeList = selectedTable.getElementsByTag( 'caption' ); if ( nodeList.count() > 0 ) { var caption = nodeList.getItem( 0 ); - caption = ( caption.getChild( 0 ) && caption.getChild( 0 ).getText() ) || ''; - caption = CKEDITOR.tools.trim( caption ); + var firstElementChild = caption.getFirst( CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_ELEMENT ) ); + + if ( firstElementChild && !firstElementChild.equals( caption.getBogus() ) ) + { + this.disable(); + this.setValue( caption.getText() ); + return; + } + + caption = CKEDITOR.tools.trim( caption.getText() ); this.setValue( caption ); } }, commit : function( data, table ) { + if ( !this.isEnabled() ) + return; + var caption = this.getValue(), captionElement = table.getElementsByTag( 'caption' ); if ( caption ) @@ -529,12 +599,15 @@ For licensing, see LICENSE.html or http://ckeditor.com/license { if ( this.getValue() ) selectedTable.setAttribute( 'summary', this.getValue() ); + else + selectedTable.removeAttribute( 'summary' ); } } ] } ] - } + }, + dialogadvtab && dialogadvtab.createAdvancedTab( editor ) ] }; }