* Upgraded TinyMCE to version 2.0RC3. This fixes a conflict with
[citadel.git] / webcit / tiny_mce / plugins / table / editor_plugin_src.js
1 /* Import plugin specific language pack */
2 tinyMCE.importPluginLanguagePack('table', 'en,ar,cs,da,de,el,es,fi,fr_ca,hu,it,ja,ko,nl,no,pl,pt,sv,tw,zh_cn,fr,de,he,no');
3
4 function TinyMCE_table_getInfo() {
5         return {
6                 longname : 'Tables',
7                 author : 'Moxiecode Systems',
8                 authorurl : 'http://tinymce.moxiecode.com',
9                 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_table.html',
10                 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
11         };
12 };
13
14 function TinyMCE_table_initInstance(inst) {
15         if (tinyMCE.isGecko) {
16                 var doc = inst.getDoc();
17
18                 tinyMCE.addEvent(doc, "mouseup", TinyMCE_table_mouseDownHandler);
19
20                 try {
21                         // For future FF versions
22                         doc.execCommand('enableInlineTableEditing', false, false);
23                 } catch (e) {
24                         // Ignore
25                 }
26         }
27
28         inst.tableRowClipboard = null;
29 }
30
31 function TinyMCE_table_mouseDownHandler(e) {
32         var elm = tinyMCE.isMSIE ? event.srcElement : e.target;
33         var focusElm = tinyMCE.selectedInstance.getFocusElement();
34
35         // If press on special Mozilla create TD/TR thingie
36         if (elm.nodeName == "BODY" && (focusElm.nodeName == "TD" || focusElm.nodeName == "TH" || (focusElm.parentNode && focusElm.parentNode.nodeName == "TD") ||(focusElm.parentNode && focusElm.parentNode.nodeName == "TH") )) {
37                 window.setTimeout(function() {
38                         var tableElm = tinyMCE.getParentElement(focusElm, "table");
39                         tinyMCE.handleVisualAid(tableElm, true, tinyMCE.settings['visual'], tinyMCE.selectedInstance);
40                 }, 10);
41         }
42 }
43
44 /**
45  * Returns the HTML contents of the table control.
46  */
47 function TinyMCE_table_getControlHTML(control_name) {
48         var controls = new Array(
49                 ['table', 'table.gif', '{$lang_table_desc}', 'mceInsertTable', true],
50                 ['delete_col', 'table_delete_col.gif', '{$lang_table_delete_col_desc}', 'mceTableDeleteCol'],
51                 ['delete_row', 'table_delete_row.gif', '{$lang_table_delete_row_desc}', 'mceTableDeleteRow'],
52                 ['col_after', 'table_insert_col_after.gif', '{$lang_table_col_after_desc}', 'mceTableInsertColAfter'],
53                 ['col_before', 'table_insert_col_before.gif', '{$lang_table_col_before_desc}', 'mceTableInsertColBefore'],
54                 ['row_after', 'table_insert_row_after.gif', '{$lang_table_row_after_desc}', 'mceTableInsertRowAfter'],
55                 ['row_before', 'table_insert_row_before.gif', '{$lang_table_row_before_desc}', 'mceTableInsertRowBefore'],
56                 ['row_props', 'table_row_props.gif', '{$lang_table_row_desc}', 'mceTableRowProps', true],
57                 ['cell_props', 'table_cell_props.gif', '{$lang_table_cell_desc}', 'mceTableCellProps', true],
58                 ['split_cells', 'table_split_cells.gif', '{$lang_table_split_cells_desc}', 'mceTableSplitCells', true],
59                 ['merge_cells', 'table_merge_cells.gif', '{$lang_table_merge_cells_desc}', 'mceTableMergeCells', true]);
60
61         // Render table control
62         for (var i=0; i<controls.length; i++) {
63                 var but = controls[i];
64
65                 if (but[0] == control_name && (tinyMCE.isMSIE || !tinyMCE.settings['button_tile_map']))
66                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'' + but[3] + '\', ' + (but.length > 4 ? but[4] : false) + (but.length > 5 ? ', \'' + but[5] + '\'' : '') + ');" target="_self" onmousedown="return false;"><img id="{$editor_id}_' + but[0] + '" src="{$pluginurl}/images/' + but[1] + '" title="' + but[2] + '" width="20" height="20" class="mceButtonDisabled" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" /></a>';
67                 else if (but[0] == control_name)
68                         return '<a href="javascript:tinyMCE.execInstanceCommand(\'{$editor_id}\',\'' + but[3] + '\', ' + (but.length > 4 ? but[4] : false) + (but.length > 5 ? ', \'' + but[5] + '\'' : '') + ');" target="_self" onmousedown="return false;"><img id="{$editor_id}_' + but[0] + '" src="{$themeurl}/images/spacer.gif" style="background-image:url({$pluginurl}/images/buttons.gif); background-position: ' + (0-(i*20)) + 'px 0px" title="' + but[2] + '" width="20" height="20" class="mceButtonDisabled" onmouseover="tinyMCE.switchClass(this,\'mceButtonOver\');" onmouseout="tinyMCE.restoreClass(this);" onmousedown="tinyMCE.restoreAndSwitchClass(this,\'mceButtonDown\');" /></a>';
69         }
70
71         // Special tablecontrols
72         if (control_name == "tablecontrols") {
73                 var html = "";
74
75                 html += tinyMCE.getControlHTML("table");
76                 html += tinyMCE.getControlHTML("separator");
77                 html += tinyMCE.getControlHTML("row_props");
78                 html += tinyMCE.getControlHTML("cell_props");
79                 html += tinyMCE.getControlHTML("separator");
80                 html += tinyMCE.getControlHTML("row_before");
81                 html += tinyMCE.getControlHTML("row_after");
82                 html += tinyMCE.getControlHTML("delete_row");
83                 html += tinyMCE.getControlHTML("separator");
84                 html += tinyMCE.getControlHTML("col_before");
85                 html += tinyMCE.getControlHTML("col_after");
86                 html += tinyMCE.getControlHTML("delete_col");
87                 html += tinyMCE.getControlHTML("separator");
88                 html += tinyMCE.getControlHTML("split_cells");
89                 html += tinyMCE.getControlHTML("merge_cells");
90
91                 return html;
92         }
93
94         return "";
95 }
96
97 /**
98  * Executes the table commands.
99  */
100 function TinyMCE_table_execCommand(editor_id, element, command, user_interface, value) {
101         // Is table command
102         switch (command) {
103                 case "mceInsertTable":
104                 case "mceTableRowProps":
105                 case "mceTableCellProps":
106                 case "mceTableSplitCells":
107                 case "mceTableMergeCells":
108                 case "mceTableInsertRowBefore":
109                 case "mceTableInsertRowAfter":
110                 case "mceTableDeleteRow":
111                 case "mceTableInsertColBefore":
112                 case "mceTableInsertColAfter":
113                 case "mceTableDeleteCol":
114                 case "mceTableCutRow":
115                 case "mceTableCopyRow":
116                 case "mceTablePasteRowBefore":
117                 case "mceTablePasteRowAfter":
118                 case "mceTableDelete":
119                         var inst = tinyMCE.getInstanceById(editor_id);
120
121                         inst.execCommand('mceBeginUndoLevel');
122                         TinyMCE_table_doExecCommand(editor_id, element, command, user_interface, value);
123                         inst.execCommand('mceEndUndoLevel');
124
125                         return true;
126         }
127
128         // Pass to next handler in chain
129         return false;
130 }
131
132 /**
133  * Executes the table commands.
134  */
135 function TinyMCE_table_doExecCommand(editor_id, element, command, user_interface, value) {
136         var inst = tinyMCE.getInstanceById(editor_id);
137         var focusElm = inst.getFocusElement();
138         var trElm = tinyMCE.getParentElement(focusElm, "tr");
139         var tdElm = tinyMCE.getParentElement(focusElm, "td,th");
140         var tableElm = tinyMCE.getParentElement(focusElm, "table");
141         var doc = inst.contentWindow.document;
142         var tableBorder = tableElm ? tableElm.getAttribute("border") : "";
143
144         // Get first TD if no TD found
145         if (trElm && tdElm == null)
146                 tdElm = trElm.cells[0];
147
148         // ------- Inner functions ---------
149         function inArray(ar, v) {
150                 for (var i=0; i<ar.length; i++) {
151                         // Is array
152                         if (ar[i].length > 0 && inArray(ar[i], v))
153                                 return true;
154
155                         // Found value
156                         if (ar[i] == v)
157                                 return true;
158                 }
159
160                 return false;
161         }
162
163         function makeTD() {
164                 var newTD = doc.createElement("td");
165                 newTD.innerHTML = "&nbsp;";
166         }
167
168         function getColRowSpan(td) {
169                 var colspan = tinyMCE.getAttrib(td, "colspan");
170                 var rowspan = tinyMCE.getAttrib(td, "rowspan");
171
172                 colspan = colspan == "" ? 1 : parseInt(colspan);
173                 rowspan = rowspan == "" ? 1 : parseInt(rowspan);
174
175                 return {colspan : colspan, rowspan : rowspan};
176         }
177
178         function getCellPos(grid, td) {
179                 for (var y=0; y<grid.length; y++) {
180                         for (var x=0; x<grid[y].length; x++) {
181                                 if (grid[y][x] == td)
182                                         return {cellindex : x, rowindex : y};
183                         }
184                 }
185
186                 return null;
187         }
188
189         function getCell(grid, row, col) {
190                 if (grid[row] && grid[row][col])
191                         return grid[row][col];
192
193                 return null;
194         }
195
196         function getTableGrid(table) {
197                 var grid = new Array();
198                 var rows = table.rows;
199
200                 for (var y=0; y<rows.length; y++) {
201                         for (var x=0; x<rows[y].cells.length; x++) {
202                                 var td = rows[y].cells[x];
203                                 var sd = getColRowSpan(td);
204
205                                 // All ready filled
206                                 for (xstart = x; grid[y] && grid[y][xstart]; xstart++) ;
207
208                                 // Fill box
209                                 for (var y2=y; y2<y+sd['rowspan']; y2++) {
210                                         if (!grid[y2])
211                                                 grid[y2] = new Array();
212
213                                         for (var x2=xstart; x2<xstart+sd['colspan']; x2++) {
214                                                 grid[y2][x2] = td;
215                                         }
216                                 }
217                         }
218                 }
219
220                 return grid;
221         }
222
223         function trimRow(table, tr, td, new_tr) {
224                 var grid = getTableGrid(table);
225                 var cpos = getCellPos(grid, td);
226
227                 // Time to crop away some
228                 if (new_tr.cells.length != tr.childNodes.length) {
229                         var cells = tr.childNodes;
230                         var lastElm = null;
231
232                         for (var x=0; td = getCell(grid, cpos.rowindex, x); x++) {
233                                 var remove = true;
234                                 var sd = getColRowSpan(td);
235
236                                 // Remove due to rowspan
237                                 if (inArray(cells, td)) {
238                                         new_tr.childNodes[x]._delete = true;
239                                 } else if ((lastElm == null || td != lastElm) && sd.colspan > 1) { // Remove due to colspan
240                                         for (var i=x; i<x+td.colSpan; i++)
241                                                 new_tr.childNodes[i]._delete = true;
242                                 }
243
244                                 if ((lastElm == null || td != lastElm) && sd.rowspan > 1)
245                                         td.rowSpan = sd.rowspan + 1;
246
247                                 lastElm = td;
248                         }
249
250                         deleteMarked(tableElm);
251                 }
252         }
253
254         function prevElm(node, name) {
255                 while ((node = node.previousSibling) != null) {
256                         if (node.nodeName == name)
257                                 return node;
258                 }
259
260                 return null;
261         }
262
263         function nextElm(node, names) {
264
265         var namesAr = names.split(',');
266
267                 while ((node = node.nextSibling) != null) {
268             for (var i=0; i<namesAr.length; i++) {
269                 if (node.nodeName.toLowerCase() == namesAr[i].toLowerCase() )
270                     return node;
271             }
272                 }
273
274                 return null;
275         }
276
277         function deleteMarked(tbl) {
278                 if (tbl.rows == 0)
279                         return;
280
281                 var tr = tbl.rows[0];
282                 do {
283                         var next = nextElm(tr, "TR");
284
285                         // Delete row
286                         if (tr._delete) {
287                                 tr.parentNode.removeChild(tr);
288                                 continue;
289                         }
290
291                         // Delete cells
292                         var td = tr.cells[0];
293                         if (td.cells > 1) {
294                                 do {
295                                         var nexttd = nextElm(td, "TD,TH");
296
297                                         if (td._delete)
298                                                 td.parentNode.removeChild(td);
299                                 } while ((td = nexttd) != null);
300                         }
301                 } while ((tr = next) != null);
302         }
303
304         function addRows(td_elm, tr_elm, rowspan) {
305                 // Add rows
306                 td_elm.rowSpan = 1;
307                 var trNext = nextElm(tr_elm, "TR");
308                 for (var i=1; i<rowspan && trNext; i++) {
309                         var newTD = doc.createElement("td");
310                         newTD.innerHTML = "&nbsp;";
311
312                         if (tinyMCE.isMSIE)
313                                 trNext.insertBefore(newTD, trNext.cells(td_elm.cellIndex));
314                         else
315                                 trNext.insertBefore(newTD, trNext.cells[td_elm.cellIndex]);
316
317                         trNext = nextElm(trNext, "TR");
318                 }
319         }
320
321         function copyRow(doc, table, tr) {
322                 var grid = getTableGrid(table);
323                 var newTR = tr.cloneNode(false);
324                 var cpos = getCellPos(grid, tr.cells[0]);
325                 var lastCell = null;
326                 var tableBorder = tinyMCE.getAttrib(table, "border");
327                 var tdElm = null;
328
329                 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
330                         var newTD = null;
331
332                         if (lastCell != tdElm) {
333                                 for (var i=0; i<tr.cells.length; i++) {
334                                         if (tdElm == tr.cells[i]) {
335                                                 newTD = tdElm.cloneNode(true);
336                                                 break;
337                                         }
338                                 }
339                         }
340
341                         if (newTD == null) {
342                                 newTD = doc.createElement("td");
343                                 newTD.innerHTML = "&nbsp;";
344                         }
345
346                         // Reset col/row span
347                         newTD.colSpan = 1;
348                         newTD.rowSpan = 1;
349
350                         newTR.appendChild(newTD);
351
352                         lastCell = tdElm;
353                 }
354
355                 return newTR;
356         }
357
358         // ---- Commands -----
359
360         // Handle commands
361         switch (command) {
362                 case "mceTableRowProps":
363                         if (trElm == null)
364                                 return true;
365
366                         if (user_interface) {
367                                 // Setup template
368                                 var template = new Array();
369
370                                 template['file'] = '../../plugins/table/row.htm';
371                                 template['width'] = 380;
372                                 template['height'] = 295;
373
374                                 // Language specific width and height addons
375                                 template['width'] += tinyMCE.getLang('lang_table_rowprops_delta_width', 0);
376                                 template['height'] += tinyMCE.getLang('lang_table_rowprops_delta_width', 0);
377
378                                 // Open window
379                                 tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes"});
380                         }
381
382                         return true;
383
384                 case "mceTableCellProps":
385                         if (tdElm == null)
386                                 return true;
387
388                         if (user_interface) {
389                                 // Setup template
390                                 var template = new Array();
391
392                                 template['file'] = '../../plugins/table/cell.htm';
393                                 template['width'] = 380;
394                                 template['height'] = 295;
395
396                                 // Language specific width and height addons
397                                 template['width'] += tinyMCE.getLang('lang_table_cellprops_delta_width', 0);
398                                 template['height'] += tinyMCE.getLang('lang_table_cellprops_delta_width', 0);
399
400                                 // Open window
401                                 tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes"});
402                         }
403
404                         return true;
405
406                 case "mceInsertTable":
407                         if (user_interface) {
408                                 // Setup template
409                                 var template = new Array();
410
411                                 template['file'] = '../../plugins/table/table.htm';
412                                 template['width'] = 380;
413                                 template['height'] = 295;
414
415                                 // Language specific width and height addons
416                                 template['width'] += tinyMCE.getLang('lang_table_table_delta_width', 0);
417                                 template['height'] += tinyMCE.getLang('lang_table_table_delta_height', 0);
418
419                                 // Open window
420                                 tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes", action : value});
421                         }
422
423                         return true;
424
425                 case "mceTableDelete":
426                         var table = tinyMCE.getParentElement(inst.getFocusElement(), "table");
427                         if (table) {
428                                 table.parentNode.removeChild(table);
429                                 inst.repaint();
430                         }
431                         return true;
432
433                 case "mceTableSplitCells":
434                 case "mceTableMergeCells":
435                 case "mceTableInsertRowBefore":
436                 case "mceTableInsertRowAfter":
437                 case "mceTableDeleteRow":
438                 case "mceTableInsertColBefore":
439                 case "mceTableInsertColAfter":
440                 case "mceTableDeleteCol":
441                 case "mceTableCutRow":
442                 case "mceTableCopyRow":
443                 case "mceTablePasteRowBefore":
444                 case "mceTablePasteRowAfter":
445                         // No table just return (invalid command)
446                         if (!tableElm)
447                                 return true;
448
449                         // Table has a tbody use that reference
450                         // Changed logic by ApTest 2005.07.12 (www.aptest.com)
451                         // Now lookk at the focused element and take its parentNode.  That will be a tbody or a table.
452                         if (tableElm != trElm.parentNode)
453                                 tableElm = trElm.parentNode;
454
455                         if (tableElm && trElm) {
456                                 switch (command) {
457                                         case "mceTableInsertRowBefore":
458                                                 if (!trElm || !tdElm)
459                                                         return true;
460
461                                                 var grid = getTableGrid(tableElm);
462                                                 var cpos = getCellPos(grid, tdElm);
463                                                 var newTR = doc.createElement("tr");
464                                                 var lastTDElm = null;
465
466                                                 cpos.rowindex--;
467                                                 if (cpos.rowindex < 0)
468                                                         cpos.rowindex = 0;
469
470                                                 // Create cells
471                                                 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
472                                                         if (tdElm != lastTDElm) {
473                                                                 var sd = getColRowSpan(tdElm);
474
475                                                                 if (sd['rowspan'] == 1) {
476                                                                         var newTD = doc.createElement("td");
477
478                                                                         newTD.innerHTML = "&nbsp;";
479                                                                         newTD.colSpan = tdElm.colSpan;
480
481                                                                         newTR.appendChild(newTD);
482                                                                 } else
483                                                                         tdElm.rowSpan = sd['rowspan'] + 1;
484
485                                                                 lastTDElm = tdElm;
486                                                         }
487                                                 }
488
489                                                 trElm.parentNode.insertBefore(newTR, trElm);
490                                         break;
491
492                                         case "mceTableCutRow":
493                                                 if (!trElm || !tdElm)
494                                                         return true;
495
496                                                 inst.tableRowClipboard = copyRow(doc, tableElm, trElm);
497                                                 inst.execCommand("mceTableDeleteRow");
498                                                 break;
499
500                                         case "mceTableCopyRow":
501                                                 if (!trElm || !tdElm)
502                                                         return true;
503
504                                                 inst.tableRowClipboard = copyRow(doc, tableElm, trElm);
505                                                 break;
506
507                                         case "mceTablePasteRowBefore":
508                                                 if (!trElm || !tdElm)
509                                                         return true;
510
511                                                 var newTR = inst.tableRowClipboard.cloneNode(true);
512
513                                                 var prevTR = prevElm(trElm, "TR");
514                                                 if (prevTR != null)
515                                                         trimRow(tableElm, prevTR, prevTR.cells[0], newTR);
516
517                                                 trElm.parentNode.insertBefore(newTR, trElm);
518                                                 break;
519
520                                         case "mceTablePasteRowAfter":
521                                                 if (!trElm || !tdElm)
522                                                         return true;
523                                                 
524                                                 var nextTR = nextElm(trElm, "TR");
525                                                 var newTR = inst.tableRowClipboard.cloneNode(true);
526
527                                                 trimRow(tableElm, trElm, tdElm, newTR);
528
529                                                 if (nextTR == null)
530                                                         trElm.parentNode.appendChild(newTR);
531                                                 else
532                                                         nextTR.parentNode.insertBefore(newTR, nextTR);
533
534                                                 break;
535
536                                         case "mceTableInsertRowAfter":
537                                                 if (!trElm || !tdElm)
538                                                         return true;
539
540                                                 var grid = getTableGrid(tableElm);
541                                                 var cpos = getCellPos(grid, tdElm);
542                                                 var newTR = doc.createElement("tr");
543                                                 var lastTDElm = null;
544
545                                                 // Create cells
546                                                 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
547                                                         if (tdElm != lastTDElm) {
548                                                                 var sd = getColRowSpan(tdElm);
549
550                                                                 if (sd['rowspan'] == 1) {
551                                                                         var newTD = doc.createElement("td");
552
553                                                                         newTD.innerHTML = "&nbsp;";
554                                                                         newTD.colSpan = tdElm.colSpan;
555
556                                                                         newTR.appendChild(newTD);
557                                                                 } else
558                                                                         tdElm.rowSpan = sd['rowspan'] + 1;
559
560                                                                 lastTDElm = tdElm;
561                                                         }
562                                                 }
563
564                                                 if (newTR.hasChildNodes()) {
565                                                         var nextTR = nextElm(trElm, "TR");
566                                                         if (nextTR)
567                                                                 nextTR.parentNode.insertBefore(newTR, nextTR);
568                                                         else
569                                                                 tableElm.appendChild(newTR);
570                                                 }
571                                         break;
572
573                                         case "mceTableDeleteRow":
574                                                 if (!trElm || !tdElm)
575                                                         return true;
576         
577                                                 var grid = getTableGrid(tableElm);
578                                                 var cpos = getCellPos(grid, tdElm);
579
580                                                 // Only one row, remove whole table
581                                                 if (grid.length == 1) {
582                                                         tableElm.parentNode.removeChild(tableElm);
583                                                         return true;
584                                                 }
585
586                                                 // Move down row spanned cells
587                                                 var cells = trElm.cells;
588                                                 var nextTR = nextElm(trElm, "TR");
589                                                 for (var x=0; x<cells.length; x++) {
590                                                         if (cells[x].rowSpan > 1) {
591                                                                 var newTD = cells[x].cloneNode(true);
592                                                                 var sd = getColRowSpan(cells[x]);
593
594                                                                 newTD.rowSpan = sd.rowspan - 1;
595
596                                                                 var nextTD = nextTR.cells[x];
597
598                                                                 if (nextTD == null)
599                                                                         nextTR.appendChild(newTD);
600                                                                 else
601                                                                         nextTR.insertBefore(newTD, nextTD);
602                                                         }
603                                                 }
604
605                                                 // Delete cells
606                                                 var lastTDElm = null;
607                                                 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
608                                                         if (tdElm != lastTDElm) {
609                                                                 var sd = getColRowSpan(tdElm);
610
611                                                                 if (sd.rowspan > 1) {
612                                                                         tdElm.rowSpan = sd.rowspan - 1;
613                                                                 } else {
614                                                                         trElm = tdElm.parentNode;
615
616                                                                         if (trElm.parentNode)
617                                                                                 trElm._delete = true;
618                                                                 }
619
620                                                                 lastTDElm = tdElm;
621                                                         }
622                                                 }
623
624                                                 deleteMarked(tableElm);
625
626                                                 cpos.rowindex--;
627                                                 if (cpos.rowindex < 0)
628                                                         cpos.rowindex = 0;
629
630                                                 inst.selectNode(getCell(grid, cpos.rowindex, 0), true, true);
631                                         break;
632
633                                         case "mceTableInsertColBefore":
634                                                 if (!trElm || !tdElm)
635                                                         return true;
636
637                                                 var grid = getTableGrid(tableElm);
638                                                 var cpos = getCellPos(grid, tdElm);
639                                                 var lastTDElm = null;
640
641                                                 for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
642                                                         if (tdElm != lastTDElm) {
643                                                                 var sd = getColRowSpan(tdElm);
644
645                                                                 if (sd['colspan'] == 1) {
646                                                                         var newTD = doc.createElement("td");
647
648                                                                         newTD.innerHTML = "&nbsp;";
649                                                                         newTD.rowSpan = tdElm.rowSpan;
650
651                                                                         tdElm.parentNode.insertBefore(newTD, tdElm);
652                                                                 } else
653                                                                         tdElm.colSpan++;
654
655                                                                 lastTDElm = tdElm;
656                                                         }
657                                                 }
658                                         break;
659
660                                         case "mceTableInsertColAfter":
661                                                 if (!trElm || !tdElm)
662                                                         return true;
663
664                                                 var grid = getTableGrid(tableElm);
665                                                 var cpos = getCellPos(grid, tdElm);
666                                                 var lastTDElm = null;
667
668                                                 for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
669                                                         if (tdElm != lastTDElm) {
670                                                                 var sd = getColRowSpan(tdElm);
671
672                                                                 if (sd['colspan'] == 1) {
673                                                                         var newTD = doc.createElement("td");
674
675                                                                         newTD.innerHTML = "&nbsp;";
676                                                                         newTD.rowSpan = tdElm.rowSpan;
677
678                                                                         var nextTD = nextElm(tdElm, "TD");
679                                                                         if (nextTD == null)
680                                                                                 tdElm.parentNode.appendChild(newTD);
681                                                                         else
682                                                                                 nextTD.parentNode.insertBefore(newTD, nextTD);
683                                                                 } else
684                                                                         tdElm.colSpan++;
685
686                                                                 lastTDElm = tdElm;
687                                                         }
688                                                 }
689                                         break;
690
691                                         case "mceTableDeleteCol":
692                                                 if (!trElm || !tdElm)
693                                                         return true;
694
695                                                 var grid = getTableGrid(tableElm);
696                                                 var cpos = getCellPos(grid, tdElm);
697                                                 var lastTDElm = null;
698
699                                                 // Only one col, remove whole table
700                                                 if (grid.length > 1 && grid[0].length <= 1) {
701                                                         tableElm.parentNode.removeChild(tableElm);
702                                                         return true;
703                                                 }
704
705                                                 // Delete cells
706                                                 for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
707                                                         if (tdElm != lastTDElm) {
708                                                                 var sd = getColRowSpan(tdElm);
709
710                                                                 if (sd['colspan'] > 1)
711                                                                         tdElm.colSpan = sd['colspan'] - 1;
712                                                                 else {
713                                                                         if (tdElm.parentNode)
714                                                                                 tdElm.parentNode.removeChild(tdElm);
715                                                                 }
716
717                                                                 lastTDElm = tdElm;
718                                                         }
719                                                 }
720
721                                                 cpos.cellindex--;
722                                                 if (cpos.cellindex < 0)
723                                                         cpos.cellindex = 0;
724
725                                                 inst.selectNode(getCell(grid, 0, cpos.cellindex), true, true);
726                                         break;
727
728                                 case "mceTableSplitCells":
729                                         if (!trElm || !tdElm)
730                                                 return true;
731
732                                         var spandata = getColRowSpan(tdElm);
733
734                                         var colspan = spandata["colspan"];
735                                         var rowspan = spandata["rowspan"];
736
737                                         // Needs splitting
738                                         if (colspan > 1 || rowspan > 1) {
739                                                 // Generate cols
740                                                 tdElm.colSpan = 1;
741                                                 for (var i=1; i<colspan; i++) {
742                                                         var newTD = doc.createElement("td");
743
744                                                         newTD.innerHTML = "&nbsp;";
745
746                                                         trElm.insertBefore(newTD, nextElm(tdElm, "TD"));
747
748                                                         if (rowspan > 1)
749                                                                 addRows(newTD, trElm, rowspan);
750                                                 }
751
752                                                 addRows(tdElm, trElm, rowspan);
753                                         }
754
755                                         // Apply visual aids
756                                         tableElm = tinyMCE.getParentElement(inst.getFocusElement(), "table");
757                                         break;
758
759                                 case "mceTableMergeCells":
760                                         var rows = new Array();
761                                         var sel = inst.getSel();
762                                         var grid = getTableGrid(tableElm);
763
764                                         if (tinyMCE.isMSIE || sel.rangeCount == 1) {
765                                                 if (user_interface) {
766                                                         // Setup template
767                                                         var template = new Array();
768                                                         var sp = getColRowSpan(tdElm);
769
770                                                         template['file'] = '../../plugins/table/merge_cells.htm';
771                                                         template['width'] = 250;
772                                                         template['height'] = 105;
773
774                                                         // Language specific width and height addons
775                                                         template['width'] += tinyMCE.getLang('lang_table_merge_cells_delta_width', 0);
776                                                         template['height'] += tinyMCE.getLang('lang_table_merge_cells_delta_height', 0);
777
778                                                         // Open window
779                                                         tinyMCE.openWindow(template, {editor_id : inst.editorId, inline : "yes", action : "update", numcols : sp.colspan, numrows : sp.rowspan});
780
781                                                         return true;
782                                                 } else {
783                                                         var numRows = parseInt(value['numrows']);
784                                                         var numCols = parseInt(value['numcols']);
785                                                         var cpos = getCellPos(grid, tdElm);
786
787                                                         if (("" + numRows) == "NaN")
788                                                                 numRows = 1;
789
790                                                         if (("" + numCols) == "NaN")
791                                                                 numCols = 1;
792
793                                                         // Get rows and cells
794                                                         var tRows = tableElm.rows;
795                                                         for (var y=cpos.rowindex; y<grid.length; y++) {
796                                                                 var rowCells = new Array();
797
798                                                                 for (var x=cpos.cellindex; x<grid[y].length; x++) {
799                                                                         var td = getCell(grid, y, x);
800
801                                                                         if (td && !inArray(rows, td) && !inArray(rowCells, td)) {
802                                                                                 var cp = getCellPos(grid, td);
803
804                                                                                 // Within range
805                                                                                 if (cp.cellindex < cpos.cellindex+numCols && cp.rowindex < cpos.rowindex+numRows)
806                                                                                         rowCells[rowCells.length] = td;
807                                                                         }
808                                                                 }
809
810                                                                 if (rowCells.length > 0)
811                                                                         rows[rows.length] = rowCells;
812                                                         }
813
814                                                         //return true;
815                                                 }
816                                         } else {
817                                                 var cells = new Array();
818                                                 var sel = inst.getSel();
819                                                 var lastTR = null;
820                                                 var curRow = null;
821                                                 var x1 = -1, y1 = -1, x2, y2;
822
823                                                 // Only one cell selected, whats the point?
824                                                 if (sel.rangeCount < 2)
825                                                         return true;
826
827                                                 // Get all selected cells
828                                                 for (var i=0; i<sel.rangeCount; i++) {
829                                                         var rng = sel.getRangeAt(i);
830                                                         var tdElm = rng.startContainer.childNodes[rng.startOffset];
831
832                                                         if (!tdElm)
833                                                                 break;
834
835                                                         if (tdElm.nodeName == "TD")
836                                                                 cells[cells.length] = tdElm;
837                                                 }
838
839                                                 // Get rows and cells
840                                                 var tRows = tableElm.rows;
841                                                 for (var y=0; y<tRows.length; y++) {
842                                                         var rowCells = new Array();
843
844                                                         for (var x=0; x<tRows[y].cells.length; x++) {
845                                                                 var td = tRows[y].cells[x];
846
847                                                                 for (var i=0; i<cells.length; i++) {
848                                                                         if (td == cells[i]) {
849                                                                                 rowCells[rowCells.length] = td;
850                                                                         }
851                                                                 }
852                                                         }
853
854                                                         if (rowCells.length > 0)
855                                                                 rows[rows.length] = rowCells;
856                                                 }
857
858                                                 // Find selected cells in grid and box
859                                                 var curRow = new Array();
860                                                 var lastTR = null;
861                                                 for (var y=0; y<grid.length; y++) {
862                                                         for (var x=0; x<grid[y].length; x++) {
863                                                                 grid[y][x]._selected = false;
864
865                                                                 for (var i=0; i<cells.length; i++) {
866                                                                         if (grid[y][x] == cells[i]) {
867                                                                                 // Get start pos
868                                                                                 if (x1 == -1) {
869                                                                                         x1 = x;
870                                                                                         y1 = y;
871                                                                                 }
872
873                                                                                 // Get end pos
874                                                                                 x2 = x;
875                                                                                 y2 = y;
876
877                                                                                 grid[y][x]._selected = true;
878                                                                         }
879                                                                 }
880                                                         }
881                                                 }
882
883                                                 // Is there gaps, if so deny
884                                                 for (var y=y1; y<=y2; y++) {
885                                                         for (var x=x1; x<=x2; x++) {
886                                                                 if (!grid[y][x]._selected) {
887                                                                         alert("Invalid selection for merge.");
888                                                                         return true;
889                                                                 }
890                                                         }
891                                                 }
892                                         }
893
894                                         // Validate selection and get total rowspan and colspan
895                                         var rowSpan = 1, colSpan = 1;
896
897                                         // Validate horizontal and get total colspan
898                                         var lastRowSpan = -1;
899                                         for (var y=0; y<rows.length; y++) {
900                                                 var rowColSpan = 0;
901
902                                                 for (var x=0; x<rows[y].length; x++) {
903                                                         var sd = getColRowSpan(rows[y][x]);
904
905                                                         rowColSpan += sd['colspan'];
906
907                                                         if (lastRowSpan != -1 && sd['rowspan'] != lastRowSpan) {
908                                                                 alert("Invalid selection for merge.");
909                                                                 return true;
910                                                         }
911
912                                                         lastRowSpan = sd['rowspan'];
913                                                 }
914
915                                                 if (rowColSpan > colSpan)
916                                                         colSpan = rowColSpan;
917
918                                                 lastRowSpan = -1;
919                                         }
920
921                                         // Validate vertical and get total rowspan
922                                         var lastColSpan = -1;
923                                         for (var x=0; x<rows[0].length; x++) {
924                                                 var colRowSpan = 0;
925
926                                                 for (var y=0; y<rows.length; y++) {
927                                                         var sd = getColRowSpan(rows[y][x]);
928
929                                                         colRowSpan += sd['rowspan'];
930
931                                                         if (lastColSpan != -1 && sd['colspan'] != lastColSpan) {
932                                                                 alert("Invalid selection for merge.");
933                                                                 return true;
934                                                         }
935
936                                                         lastColSpan = sd['colspan'];
937                                                 }
938
939                                                 if (colRowSpan > rowSpan)
940                                                         rowSpan = colRowSpan;
941
942                                                 lastColSpan = -1;
943                                         }
944
945                                         // Setup td
946                                         tdElm = rows[0][0];
947                                         tdElm.rowSpan = rowSpan;
948                                         tdElm.colSpan = colSpan;
949
950                                         // Merge cells
951                                         for (var y=0; y<rows.length; y++) {
952                                                 for (var x=0; x<rows[y].length; x++) {
953                                                         var html = rows[y][x].innerHTML;
954                                                         var chk = tinyMCE.regexpReplace(html, "[ \t\r\n]", "");
955
956                                                         if (chk != "<br/>" && chk != "<br>" && chk != "&nbsp;" && (x+y > 0))
957                                                                 tdElm.innerHTML += html;
958
959                                                         // Not current cell
960                                                         if (rows[y][x] != tdElm && !rows[y][x]._deleted) {
961                                                                 var cpos = getCellPos(grid, rows[y][x]);
962                                                                 var tr = rows[y][x].parentNode;
963
964                                                                 tr.removeChild(rows[y][x]);
965                                                                 rows[y][x]._deleted = true;
966
967                                                                 // Empty TR, remove it
968                                                                 if (!tr.hasChildNodes()) {
969                                                                         tr.parentNode.removeChild(tr);
970
971                                                                         var lastCell = null;
972                                                                         for (var x=0; cellElm = getCell(grid, cpos.rowindex, x); x++) {
973                                                                                 if (cellElm != lastCell && cellElm.rowSpan > 1)
974                                                                                         cellElm.rowSpan--;
975
976                                                                                 lastCell = cellElm;
977                                                                         }
978
979                                                                         if (tdElm.rowSpan > 1)
980                                                                                 tdElm.rowSpan--;
981                                                                 }
982                                                         }
983                                                 }
984                                         }
985
986                                         break;
987                                 }
988
989                                 tableElm = tinyMCE.getParentElement(inst.getFocusElement(), "table");
990                                 tinyMCE.handleVisualAid(tableElm, true, tinyMCE.settings['visual'], tinyMCE.selectedInstance);
991                                 tinyMCE.triggerNodeChange();
992                                 inst.repaint();
993                         }
994
995                 return true;
996         }
997
998         // Pass to next handler in chain
999         return false;
1000 }
1001
1002 function TinyMCE_table_handleNodeChange(editor_id, node, undo_index, undo_levels, visual_aid, any_selection) {
1003         var colspan = "1", rowspan = "1";
1004
1005         var inst = tinyMCE.getInstanceById(editor_id);
1006
1007         // Reset table controls
1008         tinyMCE.switchClassSticky(editor_id + '_table', 'mceButtonNormal');
1009         tinyMCE.switchClassSticky(editor_id + '_row_props', 'mceButtonDisabled', true);
1010         tinyMCE.switchClassSticky(editor_id + '_cell_props', 'mceButtonDisabled', true);
1011         tinyMCE.switchClassSticky(editor_id + '_row_before', 'mceButtonDisabled', true);
1012         tinyMCE.switchClassSticky(editor_id + '_row_after', 'mceButtonDisabled', true);
1013         tinyMCE.switchClassSticky(editor_id + '_delete_row', 'mceButtonDisabled', true);
1014         tinyMCE.switchClassSticky(editor_id + '_col_before', 'mceButtonDisabled', true);
1015         tinyMCE.switchClassSticky(editor_id + '_col_after', 'mceButtonDisabled', true);
1016         tinyMCE.switchClassSticky(editor_id + '_delete_col', 'mceButtonDisabled', true);
1017         tinyMCE.switchClassSticky(editor_id + '_split_cells', 'mceButtonDisabled', true);
1018         tinyMCE.switchClassSticky(editor_id + '_merge_cells', 'mceButtonDisabled', true);
1019
1020         // Within a tr element
1021         if (tinyMCE.getParentElement(node, "tr"))
1022                 tinyMCE.switchClassSticky(editor_id + '_row_props', 'mceButtonSelected', false);
1023
1024         // Within a td element
1025         if (tdElm = tinyMCE.getParentElement(node, "td,th")) {
1026                 tinyMCE.switchClassSticky(editor_id + '_cell_props', 'mceButtonSelected', false);
1027                 tinyMCE.switchClassSticky(editor_id + '_row_before', 'mceButtonNormal', false);
1028                 tinyMCE.switchClassSticky(editor_id + '_row_after', 'mceButtonNormal', false);
1029                 tinyMCE.switchClassSticky(editor_id + '_delete_row', 'mceButtonNormal', false);
1030                 tinyMCE.switchClassSticky(editor_id + '_col_before', 'mceButtonNormal', false);
1031                 tinyMCE.switchClassSticky(editor_id + '_col_after', 'mceButtonNormal', false);
1032                 tinyMCE.switchClassSticky(editor_id + '_delete_col', 'mceButtonNormal', false);
1033
1034                 colspan = tinyMCE.getAttrib(tdElm, "colspan");
1035                 rowspan = tinyMCE.getAttrib(tdElm, "rowspan");
1036
1037                 colspan = colspan == "" ? "1" : colspan;
1038                 rowspan = rowspan == "" ? "1" : rowspan;
1039
1040                 if (colspan != "1" || rowspan != "1")
1041                         tinyMCE.switchClassSticky(editor_id + '_split_cells', 'mceButtonNormal', false);
1042         }
1043
1044         // Within table
1045         if (tinyMCE.getParentElement(node, "table")) {
1046                 tinyMCE.switchClassSticky(editor_id + '_table', 'mceButtonSelected');
1047                 tinyMCE.switchClassSticky(editor_id + '_merge_cells', 'mceButtonNormal', false);
1048         }
1049 }