source: trunk/admin/inc/ckeditor/_source/plugins/table/dialogs/table.js@ 239

Last change on this file since 239 was 239, checked in by luc, 9 years ago

Admin: correzione visulaizzazione immissione dati spoglio per Chrome e Safari - Aggiornamento dell'editor da FCKeditor a CKeditor , accessibili anche a Chrome e Safari.

  • Property svn:executable set to *
File size: 19.3 KB
Line 
1/*
2Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
3For licensing, see LICENSE.html or http://ckeditor.com/license
4*/
5
6(function()
7{
8 var defaultToPixel = CKEDITOR.tools.cssLength;
9
10 var commitValue = function( data )
11 {
12 var id = this.id;
13 if ( !data.info )
14 data.info = {};
15 data.info[id] = this.getValue();
16 };
17
18 function tableColumns( table )
19 {
20 var cols = 0, maxCols = 0;
21 for ( var i = 0, row, rows = table.$.rows.length; i < rows; i++ )
22 {
23 row = table.$.rows[ i ], cols = 0;
24 for ( var j = 0, cell, cells = row.cells.length; j < cells; j++ )
25 {
26 cell = row.cells[ j ];
27 cols += cell.colSpan;
28 }
29
30 cols > maxCols && ( maxCols = cols );
31 }
32
33 return maxCols;
34 }
35
36 function tableDialog( editor, command )
37 {
38 var makeElement = function( name )
39 {
40 return new CKEDITOR.dom.element( name, editor.document );
41 };
42
43 var dialogadvtab = editor.plugins.dialogadvtab;
44
45 return {
46 title : editor.lang.table.title,
47 minWidth : 310,
48 minHeight : CKEDITOR.env.ie ? 310 : 280,
49
50 onLoad : function()
51 {
52 var dialog = this;
53
54 var styles = dialog.getContentElement( 'advanced', 'advStyles' );
55
56 if ( styles )
57 {
58 styles.on( 'change', function( evt )
59 {
60 // Synchronize width value.
61 var width = this.getStyle( 'width', '' ),
62 txtWidth = dialog.getContentElement( 'info', 'txtWidth' );
63
64 txtWidth && txtWidth.setValue( width, true );
65
66 // Synchronize height value.
67 var height = this.getStyle( 'height', '' ),
68 txtHeight = dialog.getContentElement( 'info', 'txtHeight' );
69
70 txtHeight && txtHeight.setValue( height, true );
71 });
72 }
73 },
74
75 onShow : function()
76 {
77 // Detect if there's a selected table.
78 var selection = editor.getSelection(),
79 ranges = selection.getRanges(),
80 selectedTable = null;
81
82 var rowsInput = this.getContentElement( 'info', 'txtRows' ),
83 colsInput = this.getContentElement( 'info', 'txtCols' ),
84 widthInput = this.getContentElement( 'info', 'txtWidth' ),
85 heightInput = this.getContentElement( 'info', 'txtHeight' );
86
87 if ( command == 'tableProperties' )
88 {
89 if ( ( selectedTable = selection.getSelectedElement() ) )
90 selectedTable = selectedTable.getAscendant( 'table', true );
91 else if ( ranges.length > 0 )
92 {
93 // Webkit could report the following range on cell selection (#4948):
94 // <table><tr><td>[&nbsp;</td></tr></table>]
95 if ( CKEDITOR.env.webkit )
96 ranges[ 0 ].shrink( CKEDITOR.NODE_ELEMENT );
97
98 var rangeRoot = ranges[0].getCommonAncestor( true );
99 selectedTable = rangeRoot.getAscendant( 'table', true );
100 }
101
102 // Save a reference to the selected table, and push a new set of default values.
103 this._.selectedElement = selectedTable;
104 }
105
106 // Enable or disable the row, cols, width fields.
107 if ( selectedTable )
108 {
109 this.setupContent( selectedTable );
110 rowsInput && rowsInput.disable();
111 colsInput && colsInput.disable();
112 }
113 else
114 {
115 rowsInput && rowsInput.enable();
116 colsInput && colsInput.enable();
117 }
118
119 // Call the onChange method for the widht and height fields so
120 // they get reflected into the Advanced tab.
121 widthInput && widthInput.onChange();
122 heightInput && heightInput.onChange();
123 },
124 onOk : function()
125 {
126 var selection = editor.getSelection(),
127 bms = this._.selectedElement && selection.createBookmarks();
128
129 var table = this._.selectedElement || makeElement( 'table' ),
130 me = this,
131 data = {};
132
133 this.commitContent( data, table );
134
135 if ( data.info )
136 {
137 var info = data.info;
138
139 // Generate the rows and cols.
140 if ( !this._.selectedElement )
141 {
142 var tbody = table.append( makeElement( 'tbody' ) ),
143 rows = parseInt( info.txtRows, 10 ) || 0,
144 cols = parseInt( info.txtCols, 10 ) || 0;
145
146 for ( var i = 0 ; i < rows ; i++ )
147 {
148 var row = tbody.append( makeElement( 'tr' ) );
149 for ( var j = 0 ; j < cols ; j++ )
150 {
151 var cell = row.append( makeElement( 'td' ) );
152 if ( !CKEDITOR.env.ie )
153 cell.append( makeElement( 'br' ) );
154 }
155 }
156 }
157
158 // Modify the table headers. Depends on having rows and cols generated
159 // correctly so it can't be done in commit functions.
160
161 // Should we make a <thead>?
162 var headers = info.selHeaders;
163 if ( !table.$.tHead && ( headers == 'row' || headers == 'both' ) )
164 {
165 var thead = new CKEDITOR.dom.element( table.$.createTHead() );
166 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
167 var theRow = tbody.getElementsByTag( 'tr' ).getItem( 0 );
168
169 // Change TD to TH:
170 for ( i = 0 ; i < theRow.getChildCount() ; i++ )
171 {
172 var th = theRow.getChild( i );
173 // Skip bookmark nodes. (#6155)
174 if ( th.type == CKEDITOR.NODE_ELEMENT && !th.data( 'cke-bookmark' ) )
175 {
176 th.renameNode( 'th' );
177 th.setAttribute( 'scope', 'col' );
178 }
179 }
180 thead.append( theRow.remove() );
181 }
182
183 if ( table.$.tHead !== null && !( headers == 'row' || headers == 'both' ) )
184 {
185 // Move the row out of the THead and put it in the TBody:
186 thead = new CKEDITOR.dom.element( table.$.tHead );
187 tbody = table.getElementsByTag( 'tbody' ).getItem( 0 );
188
189 var previousFirstRow = tbody.getFirst();
190 while ( thead.getChildCount() > 0 )
191 {
192 theRow = thead.getFirst();
193 for ( i = 0; i < theRow.getChildCount() ; i++ )
194 {
195 var newCell = theRow.getChild( i );
196 if ( newCell.type == CKEDITOR.NODE_ELEMENT )
197 {
198 newCell.renameNode( 'td' );
199 newCell.removeAttribute( 'scope' );
200 }
201 }
202 theRow.insertBefore( previousFirstRow );
203 }
204 thead.remove();
205 }
206
207 // Should we make all first cells in a row TH?
208 if ( !this.hasColumnHeaders && ( headers == 'col' || headers == 'both' ) )
209 {
210 for ( row = 0 ; row < table.$.rows.length ; row++ )
211 {
212 newCell = new CKEDITOR.dom.element( table.$.rows[ row ].cells[ 0 ] );
213 newCell.renameNode( 'th' );
214 newCell.setAttribute( 'scope', 'row' );
215 }
216 }
217
218 // Should we make all first TH-cells in a row make TD? If 'yes' we do it the other way round :-)
219 if ( ( this.hasColumnHeaders ) && !( headers == 'col' || headers == 'both' ) )
220 {
221 for ( i = 0 ; i < table.$.rows.length ; i++ )
222 {
223 row = new CKEDITOR.dom.element( table.$.rows[i] );
224 if ( row.getParent().getName() == 'tbody' )
225 {
226 newCell = new CKEDITOR.dom.element( row.$.cells[0] );
227 newCell.renameNode( 'td' );
228 newCell.removeAttribute( 'scope' );
229 }
230 }
231 }
232
233 // Set the width and height.
234 info.txtHeight ? table.setStyle( 'height', info.txtHeight ) : table.removeStyle( 'height' );
235 info.txtWidth ? table.setStyle( 'width', info.txtWidth ) : table.removeStyle( 'width' );
236
237 if ( !table.getAttribute( 'style' ) )
238 table.removeAttribute( 'style' );
239 }
240
241 // Insert the table element if we're creating one.
242 if ( !this._.selectedElement )
243 {
244 editor.insertElement( table );
245 // Override the default cursor position after insertElement to place
246 // cursor inside the first cell (#7959), IE needs a while.
247 setTimeout( function()
248 {
249 var firstCell = new CKEDITOR.dom.element( table.$.rows[ 0 ].cells[ 0 ] );
250 var range = new CKEDITOR.dom.range( editor.document );
251 range.moveToPosition( firstCell, CKEDITOR.POSITION_AFTER_START );
252 range.select( 1 );
253 }, 0 );
254 }
255 // Properly restore the selection, (#4822) but don't break
256 // because of this, e.g. updated table caption.
257 else
258 try { selection.selectBookmarks( bms ); } catch( er ){}
259 },
260 contents : [
261 {
262 id : 'info',
263 label : editor.lang.table.title,
264 elements :
265 [
266 {
267 type : 'hbox',
268 widths : [ null, null ],
269 styles : [ 'vertical-align:top' ],
270 children :
271 [
272 {
273 type : 'vbox',
274 padding : 0,
275 children :
276 [
277 {
278 type : 'text',
279 id : 'txtRows',
280 'default' : 3,
281 label : editor.lang.table.rows,
282 required : true,
283 controlStyle : 'width:5em',
284 validate : function()
285 {
286 var pass = true,
287 value = this.getValue();
288 pass = pass && CKEDITOR.dialog.validate.integer()( value )
289 && value > 0;
290 if ( !pass )
291 {
292 alert( editor.lang.table.invalidRows );
293 this.select();
294 }
295 return pass;
296 },
297 setup : function( selectedElement )
298 {
299 this.setValue( selectedElement.$.rows.length );
300 },
301 commit : commitValue
302 },
303 {
304 type : 'text',
305 id : 'txtCols',
306 'default' : 2,
307 label : editor.lang.table.columns,
308 required : true,
309 controlStyle : 'width:5em',
310 validate : function()
311 {
312 var pass = true,
313 value = this.getValue();
314 pass = pass && CKEDITOR.dialog.validate.integer()( value )
315 && value > 0;
316 if ( !pass )
317 {
318 alert( editor.lang.table.invalidCols );
319 this.select();
320 }
321 return pass;
322 },
323 setup : function( selectedTable )
324 {
325 this.setValue( tableColumns( selectedTable ) );
326 },
327 commit : commitValue
328 },
329 {
330 type : 'html',
331 html : '&nbsp;'
332 },
333 {
334 type : 'select',
335 id : 'selHeaders',
336 'default' : '',
337 label : editor.lang.table.headers,
338 items :
339 [
340 [ editor.lang.table.headersNone, '' ],
341 [ editor.lang.table.headersRow, 'row' ],
342 [ editor.lang.table.headersColumn, 'col' ],
343 [ editor.lang.table.headersBoth, 'both' ]
344 ],
345 setup : function( selectedTable )
346 {
347 // Fill in the headers field.
348 var dialog = this.getDialog();
349 dialog.hasColumnHeaders = true;
350
351 // Check if all the first cells in every row are TH
352 for ( var row = 0 ; row < selectedTable.$.rows.length ; row++ )
353 {
354 // If just one cell isn't a TH then it isn't a header column
355 var headCell = selectedTable.$.rows[row].cells[0];
356 if ( headCell && headCell.nodeName.toLowerCase() != 'th' )
357 {
358 dialog.hasColumnHeaders = false;
359 break;
360 }
361 }
362
363 // Check if the table contains <thead>.
364 if ( ( selectedTable.$.tHead !== null) )
365 this.setValue( dialog.hasColumnHeaders ? 'both' : 'row' );
366 else
367 this.setValue( dialog.hasColumnHeaders ? 'col' : '' );
368 },
369 commit : commitValue
370 },
371 {
372 type : 'text',
373 id : 'txtBorder',
374 'default' : 1,
375 label : editor.lang.table.border,
376 controlStyle : 'width:3em',
377 validate : CKEDITOR.dialog.validate['number']( editor.lang.table.invalidBorder ),
378 setup : function( selectedTable )
379 {
380 this.setValue( selectedTable.getAttribute( 'border' ) || '' );
381 },
382 commit : function( data, selectedTable )
383 {
384 if ( this.getValue() )
385 selectedTable.setAttribute( 'border', this.getValue() );
386 else
387 selectedTable.removeAttribute( 'border' );
388 }
389 },
390 {
391 id : 'cmbAlign',
392 type : 'select',
393 'default' : '',
394 label : editor.lang.common.align,
395 items :
396 [
397 [ editor.lang.common.notSet , ''],
398 [ editor.lang.common.alignLeft , 'left'],
399 [ editor.lang.common.alignCenter , 'center'],
400 [ editor.lang.common.alignRight , 'right']
401 ],
402 setup : function( selectedTable )
403 {
404 this.setValue( selectedTable.getAttribute( 'align' ) || '' );
405 },
406 commit : function( data, selectedTable )
407 {
408 if ( this.getValue() )
409 selectedTable.setAttribute( 'align', this.getValue() );
410 else
411 selectedTable.removeAttribute( 'align' );
412 }
413 }
414 ]
415 },
416 {
417 type : 'vbox',
418 padding : 0,
419 children :
420 [
421 {
422 type : 'hbox',
423 widths : [ '5em' ],
424 children :
425 [
426 {
427 type : 'text',
428 id : 'txtWidth',
429 controlStyle : 'width:5em',
430 label : editor.lang.common.width,
431 title : editor.lang.common.cssLengthTooltip,
432 'default' : 500,
433 getValue : defaultToPixel,
434 validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.width ) ),
435 onChange : function()
436 {
437 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
438 styles && styles.updateStyle( 'width', this.getValue() );
439 },
440 setup : function( selectedTable )
441 {
442 var val = selectedTable.getStyle( 'width' );
443 val && this.setValue( val );
444 },
445 commit : commitValue
446 }
447 ]
448 },
449 {
450 type : 'hbox',
451 widths : [ '5em' ],
452 children :
453 [
454 {
455 type : 'text',
456 id : 'txtHeight',
457 controlStyle : 'width:5em',
458 label : editor.lang.common.height,
459 title : editor.lang.common.cssLengthTooltip,
460 'default' : '',
461 getValue : defaultToPixel,
462 validate : CKEDITOR.dialog.validate.cssLength( editor.lang.common.invalidCssLength.replace( '%1', editor.lang.common.height ) ),
463 onChange : function()
464 {
465 var styles = this.getDialog().getContentElement( 'advanced', 'advStyles' );
466 styles && styles.updateStyle( 'height', this.getValue() );
467 },
468
469 setup : function( selectedTable )
470 {
471 var val = selectedTable.getStyle( 'width' );
472 val && this.setValue( val );
473 },
474 commit : commitValue
475 }
476 ]
477 },
478 {
479 type : 'html',
480 html : '&nbsp;'
481 },
482 {
483 type : 'text',
484 id : 'txtCellSpace',
485 controlStyle : 'width:3em',
486 label : editor.lang.table.cellSpace,
487 'default' : 1,
488 validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellSpacing ),
489 setup : function( selectedTable )
490 {
491 this.setValue( selectedTable.getAttribute( 'cellSpacing' ) || '' );
492 },
493 commit : function( data, selectedTable )
494 {
495 if ( this.getValue() )
496 selectedTable.setAttribute( 'cellSpacing', this.getValue() );
497 else
498 selectedTable.removeAttribute( 'cellSpacing' );
499 }
500 },
501 {
502 type : 'text',
503 id : 'txtCellPad',
504 controlStyle : 'width:3em',
505 label : editor.lang.table.cellPad,
506 'default' : 1,
507 validate : CKEDITOR.dialog.validate.number( editor.lang.table.invalidCellPadding ),
508 setup : function( selectedTable )
509 {
510 this.setValue( selectedTable.getAttribute( 'cellPadding' ) || '' );
511 },
512 commit : function( data, selectedTable )
513 {
514 if ( this.getValue() )
515 selectedTable.setAttribute( 'cellPadding', this.getValue() );
516 else
517 selectedTable.removeAttribute( 'cellPadding' );
518 }
519 }
520 ]
521 }
522 ]
523 },
524 {
525 type : 'html',
526 align : 'right',
527 html : ''
528 },
529 {
530 type : 'vbox',
531 padding : 0,
532 children :
533 [
534 {
535 type : 'text',
536 id : 'txtCaption',
537 label : editor.lang.table.caption,
538 setup : function( selectedTable )
539 {
540 this.enable();
541
542 var nodeList = selectedTable.getElementsByTag( 'caption' );
543 if ( nodeList.count() > 0 )
544 {
545 var caption = nodeList.getItem( 0 );
546 var firstElementChild = caption.getFirst( CKEDITOR.dom.walker.nodeType( CKEDITOR.NODE_ELEMENT ) );
547
548 if ( firstElementChild && !firstElementChild.equals( caption.getBogus() ) )
549 {
550 this.disable();
551 this.setValue( caption.getText() );
552 return;
553 }
554
555 caption = CKEDITOR.tools.trim( caption.getText() );
556 this.setValue( caption );
557 }
558 },
559 commit : function( data, table )
560 {
561 if ( !this.isEnabled() )
562 return;
563
564 var caption = this.getValue(),
565 captionElement = table.getElementsByTag( 'caption' );
566 if ( caption )
567 {
568 if ( captionElement.count() > 0 )
569 {
570 captionElement = captionElement.getItem( 0 );
571 captionElement.setHtml( '' );
572 }
573 else
574 {
575 captionElement = new CKEDITOR.dom.element( 'caption', editor.document );
576 if ( table.getChildCount() )
577 captionElement.insertBefore( table.getFirst() );
578 else
579 captionElement.appendTo( table );
580 }
581 captionElement.append( new CKEDITOR.dom.text( caption, editor.document ) );
582 }
583 else if ( captionElement.count() > 0 )
584 {
585 for ( var i = captionElement.count() - 1 ; i >= 0 ; i-- )
586 captionElement.getItem( i ).remove();
587 }
588 }
589 },
590 {
591 type : 'text',
592 id : 'txtSummary',
593 label : editor.lang.table.summary,
594 setup : function( selectedTable )
595 {
596 this.setValue( selectedTable.getAttribute( 'summary' ) || '' );
597 },
598 commit : function( data, selectedTable )
599 {
600 if ( this.getValue() )
601 selectedTable.setAttribute( 'summary', this.getValue() );
602 else
603 selectedTable.removeAttribute( 'summary' );
604 }
605 }
606 ]
607 }
608 ]
609 },
610 dialogadvtab && dialogadvtab.createAdvancedTab( editor )
611 ]
612 };
613 }
614
615 CKEDITOR.dialog.add( 'table', function( editor )
616 {
617 return tableDialog( editor, 'table' );
618 } );
619 CKEDITOR.dialog.add( 'tableProperties', function( editor )
620 {
621 return tableDialog( editor, 'tableProperties' );
622 } );
623})();
Note: See TracBrowser for help on using the repository browser.