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