2 * $Id: editor_plugin_src.js 768 2008-04-04 13:52:49Z spocke $
\r
5 * @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
\r
9 var each = tinymce.each;
\r
11 tinymce.create('tinymce.plugins.TablePlugin', {
\r
12 init : function(ed, url) {
\r
20 ['table', 'table.desc', 'mceInsertTable', true],
\r
21 ['delete_table', 'table.del', 'mceTableDelete'],
\r
22 ['delete_col', 'table.delete_col_desc', 'mceTableDeleteCol'],
\r
23 ['delete_row', 'table.delete_row_desc', 'mceTableDeleteRow'],
\r
24 ['col_after', 'table.col_after_desc', 'mceTableInsertColAfter'],
\r
25 ['col_before', 'table.col_before_desc', 'mceTableInsertColBefore'],
\r
26 ['row_after', 'table.row_after_desc', 'mceTableInsertRowAfter'],
\r
27 ['row_before', 'table.row_before_desc', 'mceTableInsertRowBefore'],
\r
28 ['row_props', 'table.row_desc', 'mceTableRowProps', true],
\r
29 ['cell_props', 'table.cell_desc', 'mceTableCellProps', true],
\r
30 ['split_cells', 'table.split_cells_desc', 'mceTableSplitCells', true],
\r
31 ['merge_cells', 'table.merge_cells_desc', 'mceTableMergeCells', true]
\r
33 ed.addButton(c[0], {title : c[1], cmd : c[2], ui : c[3]});
\r
36 ed.onInit.add(function() {
\r
37 if (ed && ed.plugins.contextmenu) {
\r
38 ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
\r
39 var sm, se = ed.selection, el = se.getNode() || ed.getBody();
\r
41 if (ed.dom.getParent(e, 'td') || ed.dom.getParent(e, 'th')) {
\r
44 if (el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) {
\r
45 m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});
\r
46 m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});
\r
50 if (el.nodeName == 'IMG' && el.className.indexOf('mceItem') == -1) {
\r
51 m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});
\r
55 m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', ui : true, value : {action : 'insert'}});
\r
56 m.add({title : 'table.props_desc', icon : 'table_props', cmd : 'mceInsertTable', ui : true});
\r
57 m.add({title : 'table.del', icon : 'delete_table', cmd : 'mceTableDelete', ui : true});
\r
61 sm = m.addMenu({title : 'table.cell'});
\r
62 sm.add({title : 'table.cell_desc', icon : 'cell_props', cmd : 'mceTableCellProps', ui : true});
\r
63 sm.add({title : 'table.split_cells_desc', icon : 'split_cells', cmd : 'mceTableSplitCells', ui : true});
\r
64 sm.add({title : 'table.merge_cells_desc', icon : 'merge_cells', cmd : 'mceTableMergeCells', ui : true});
\r
67 sm = m.addMenu({title : 'table.row'});
\r
68 sm.add({title : 'table.row_desc', icon : 'row_props', cmd : 'mceTableRowProps', ui : true});
\r
69 sm.add({title : 'table.row_before_desc', icon : 'row_before', cmd : 'mceTableInsertRowBefore'});
\r
70 sm.add({title : 'table.row_after_desc', icon : 'row_after', cmd : 'mceTableInsertRowAfter'});
\r
71 sm.add({title : 'table.delete_row_desc', icon : 'delete_row', cmd : 'mceTableDeleteRow'});
\r
73 sm.add({title : 'table.cut_row_desc', icon : 'cut', cmd : 'mceTableCutRow'});
\r
74 sm.add({title : 'table.copy_row_desc', icon : 'copy', cmd : 'mceTableCopyRow'});
\r
75 sm.add({title : 'table.paste_row_before_desc', icon : 'paste', cmd : 'mceTablePasteRowBefore'});
\r
76 sm.add({title : 'table.paste_row_after_desc', icon : 'paste', cmd : 'mceTablePasteRowAfter'});
\r
79 sm = m.addMenu({title : 'table.col'});
\r
80 sm.add({title : 'table.col_before_desc', icon : 'col_before', cmd : 'mceTableInsertColBefore'});
\r
81 sm.add({title : 'table.col_after_desc', icon : 'col_after', cmd : 'mceTableInsertColAfter'});
\r
82 sm.add({title : 'table.delete_col_desc', icon : 'delete_col', cmd : 'mceTableDeleteCol'});
\r
84 m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', ui : true});
\r
89 // Add undo level when new rows are created using the tab key
\r
90 ed.onKeyDown.add(function(ed, e) {
\r
91 if (e.keyCode == 9 && ed.dom.getParent(ed.selection.getNode(), 'TABLE'))
\r
92 ed.undoManager.add();
\r
95 ed.onNodeChange.add(function(ed, cm, n) {
\r
96 var p = ed.dom.getParent(n, 'td,th,caption');
\r
98 cm.setActive('table', !!p);
\r
99 if (p && p.nodeName === 'CAPTION')
\r
102 cm.setDisabled('delete_table', !p);
\r
103 cm.setDisabled('delete_col', !p);
\r
104 cm.setDisabled('delete_table', !p);
\r
105 cm.setDisabled('delete_row', !p);
\r
106 cm.setDisabled('col_after', !p);
\r
107 cm.setDisabled('col_before', !p);
\r
108 cm.setDisabled('row_after', !p);
\r
109 cm.setDisabled('row_before', !p);
\r
110 cm.setDisabled('row_props', !p);
\r
111 cm.setDisabled('cell_props', !p);
\r
112 cm.setDisabled('split_cells', !p || (parseInt(ed.dom.getAttrib(p, 'colspan', '1')) < 2 && parseInt(ed.dom.getAttrib(p, 'rowspan', '1')) < 2));
\r
113 cm.setDisabled('merge_cells', !p);
\r
116 // Padd empty table cells
\r
117 if (!tinymce.isIE) {
\r
118 ed.onBeforeSetContent.add(function(ed, o) {
\r
120 o.content = o.content.replace(/<(td|th)([^>]+|)>\s*<\/(td|th)>/g, tinymce.isOpera ? '<$1$2> </$1>' : '<$1$2><br mce_bogus="1" /></$1>');
\r
125 execCommand : function(cmd, ui, val) {
\r
126 var ed = this.editor, b;
\r
128 // Is table command
\r
130 case "mceInsertTable":
\r
131 case "mceTableRowProps":
\r
132 case "mceTableCellProps":
\r
133 case "mceTableSplitCells":
\r
134 case "mceTableMergeCells":
\r
135 case "mceTableInsertRowBefore":
\r
136 case "mceTableInsertRowAfter":
\r
137 case "mceTableDeleteRow":
\r
138 case "mceTableInsertColBefore":
\r
139 case "mceTableInsertColAfter":
\r
140 case "mceTableDeleteCol":
\r
141 case "mceTableCutRow":
\r
142 case "mceTableCopyRow":
\r
143 case "mceTablePasteRowBefore":
\r
144 case "mceTablePasteRowAfter":
\r
145 case "mceTableDelete":
\r
146 ed.execCommand('mceBeginUndoLevel');
\r
147 this._doExecCommand(cmd, ui, val);
\r
148 ed.execCommand('mceEndUndoLevel');
\r
153 // Pass to next handler in chain
\r
157 getInfo : function() {
\r
159 longname : 'Tables',
\r
160 author : 'Moxiecode Systems AB',
\r
161 authorurl : 'http://tinymce.moxiecode.com',
\r
162 infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/table',
\r
163 version : tinymce.majorVersion + "." + tinymce.minorVersion
\r
167 // Private plugin internal methods
\r
170 * Executes the table commands.
\r
172 _doExecCommand : function(command, user_interface, value) {
\r
173 var inst = this.editor, ed = inst, url = this.url;
\r
174 var focusElm = inst.selection.getNode();
\r
175 var trElm = inst.dom.getParent(focusElm, "tr");
\r
176 var tdElm = inst.dom.getParent(focusElm, "td,th");
\r
177 var tableElm = inst.dom.getParent(focusElm, "table");
\r
178 var doc = inst.contentWindow.document;
\r
179 var tableBorder = tableElm ? tableElm.getAttribute("border") : "";
\r
181 // Get first TD if no TD found
\r
182 if (trElm && tdElm == null)
\r
183 tdElm = trElm.cells[0];
\r
185 function inArray(ar, v) {
\r
186 for (var i=0; i<ar.length; i++) {
\r
188 if (ar[i].length > 0 && inArray(ar[i], v))
\r
199 function select(dx, dy) {
\r
202 grid = getTableGrid(tableElm);
\r
205 dx = Math.max(cpos.cellindex + dx, 0);
\r
206 dy = Math.max(cpos.rowindex + dy, 0);
\r
208 // Recalculate grid and select
\r
209 inst.execCommand('mceRepaint');
\r
210 td = getCell(grid, dy, dx);
\r
213 inst.selection.select(td.firstChild || td);
\r
214 inst.selection.collapse(1);
\r
218 function makeTD() {
\r
219 var newTD = doc.createElement("td");
\r
222 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
225 function getColRowSpan(td) {
\r
226 var colspan = inst.dom.getAttrib(td, "colspan");
\r
227 var rowspan = inst.dom.getAttrib(td, "rowspan");
\r
229 colspan = colspan == "" ? 1 : parseInt(colspan);
\r
230 rowspan = rowspan == "" ? 1 : parseInt(rowspan);
\r
232 return {colspan : colspan, rowspan : rowspan};
\r
235 function getCellPos(grid, td) {
\r
238 for (y=0; y<grid.length; y++) {
\r
239 for (x=0; x<grid[y].length; x++) {
\r
240 if (grid[y][x] == td)
\r
241 return {cellindex : x, rowindex : y};
\r
248 function getCell(grid, row, col) {
\r
249 if (grid[row] && grid[row][col])
\r
250 return grid[row][col];
\r
255 function getTableGrid(table) {
\r
256 var grid = [], rows = table.rows, x, y, td, sd, xstart, x2, y2;
\r
258 for (y=0; y<rows.length; y++) {
\r
259 for (x=0; x<rows[y].cells.length; x++) {
\r
260 td = rows[y].cells[x];
\r
261 sd = getColRowSpan(td);
\r
263 // All ready filled
\r
264 for (xstart = x; grid[y] && grid[y][xstart]; xstart++) ;
\r
267 for (y2=y; y2<y+sd['rowspan']; y2++) {
\r
271 for (x2=xstart; x2<xstart+sd['colspan']; x2++)
\r
280 function trimRow(table, tr, td, new_tr) {
\r
281 var grid = getTableGrid(table), cpos = getCellPos(grid, td);
\r
282 var cells, lastElm;
\r
284 // Time to crop away some
\r
285 if (new_tr.cells.length != tr.childNodes.length) {
\r
286 cells = tr.childNodes;
\r
289 for (var x=0; td = getCell(grid, cpos.rowindex, x); x++) {
\r
291 var sd = getColRowSpan(td);
\r
293 // Remove due to rowspan
\r
294 if (inArray(cells, td)) {
\r
295 new_tr.childNodes[x]._delete = true;
\r
296 } else if ((lastElm == null || td != lastElm) && sd.colspan > 1) { // Remove due to colspan
\r
297 for (var i=x; i<x+td.colSpan; i++)
\r
298 new_tr.childNodes[i]._delete = true;
\r
301 if ((lastElm == null || td != lastElm) && sd.rowspan > 1)
\r
302 td.rowSpan = sd.rowspan + 1;
\r
307 deleteMarked(tableElm);
\r
311 function prevElm(node, name) {
\r
312 while ((node = node.previousSibling) != null) {
\r
313 if (node.nodeName == name)
\r
320 function nextElm(node, names) {
\r
321 var namesAr = names.split(',');
\r
323 while ((node = node.nextSibling) != null) {
\r
324 for (var i=0; i<namesAr.length; i++) {
\r
325 if (node.nodeName.toLowerCase() == namesAr[i].toLowerCase() )
\r
333 function deleteMarked(tbl) {
\r
337 var tr = tbl.rows[0];
\r
339 var next = nextElm(tr, "TR");
\r
343 tr.parentNode.removeChild(tr);
\r
348 var td = tr.cells[0];
\r
349 if (td.cells > 1) {
\r
351 var nexttd = nextElm(td, "TD,TH");
\r
354 td.parentNode.removeChild(td);
\r
355 } while ((td = nexttd) != null);
\r
357 } while ((tr = next) != null);
\r
360 function addRows(td_elm, tr_elm, rowspan) {
\r
362 td_elm.rowSpan = 1;
\r
363 var trNext = nextElm(tr_elm, "TR");
\r
364 for (var i=1; i<rowspan && trNext; i++) {
\r
365 var newTD = doc.createElement("td");
\r
368 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
371 trNext.insertBefore(newTD, trNext.cells(td_elm.cellIndex));
\r
373 trNext.insertBefore(newTD, trNext.cells[td_elm.cellIndex]);
\r
375 trNext = nextElm(trNext, "TR");
\r
379 function copyRow(doc, table, tr) {
\r
380 var grid = getTableGrid(table);
\r
381 var newTR = tr.cloneNode(false);
\r
382 var cpos = getCellPos(grid, tr.cells[0]);
\r
383 var lastCell = null;
\r
384 var tableBorder = inst.dom.getAttrib(table, "border");
\r
387 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
\r
390 if (lastCell != tdElm) {
\r
391 for (var i=0; i<tr.cells.length; i++) {
\r
392 if (tdElm == tr.cells[i]) {
\r
393 newTD = tdElm.cloneNode(true);
\r
399 if (newTD == null) {
\r
400 newTD = doc.createElement("td");
\r
403 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
406 // Reset col/row span
\r
410 newTR.appendChild(newTD);
\r
418 // ---- Commands -----
\r
422 case "mceTableRowProps":
\r
426 if (user_interface) {
\r
427 inst.windowManager.open({
\r
428 url : url + '/row.htm',
\r
429 width : 400 + parseInt(inst.getLang('table.rowprops_delta_width', 0)),
\r
430 height : 295 + parseInt(inst.getLang('table.rowprops_delta_height', 0)),
\r
439 case "mceTableCellProps":
\r
443 if (user_interface) {
\r
444 inst.windowManager.open({
\r
445 url : url + '/cell.htm',
\r
446 width : 400 + parseInt(inst.getLang('table.cellprops_delta_width', 0)),
\r
447 height : 295 + parseInt(inst.getLang('table.cellprops_delta_height', 0)),
\r
456 case "mceInsertTable":
\r
457 if (user_interface) {
\r
458 inst.windowManager.open({
\r
459 url : url + '/table.htm',
\r
460 width : 400 + parseInt(inst.getLang('table.table_delta_width', 0)),
\r
461 height : 320 + parseInt(inst.getLang('table.table_delta_height', 0)),
\r
465 action : value ? value.action : 0
\r
471 case "mceTableDelete":
\r
472 var table = inst.dom.getParent(inst.selection.getNode(), "table");
\r
474 table.parentNode.removeChild(table);
\r
475 inst.execCommand('mceRepaint');
\r
479 case "mceTableSplitCells":
\r
480 case "mceTableMergeCells":
\r
481 case "mceTableInsertRowBefore":
\r
482 case "mceTableInsertRowAfter":
\r
483 case "mceTableDeleteRow":
\r
484 case "mceTableInsertColBefore":
\r
485 case "mceTableInsertColAfter":
\r
486 case "mceTableDeleteCol":
\r
487 case "mceTableCutRow":
\r
488 case "mceTableCopyRow":
\r
489 case "mceTablePasteRowBefore":
\r
490 case "mceTablePasteRowAfter":
\r
491 // No table just return (invalid command)
\r
495 // Table has a tbody use that reference
\r
496 // Changed logic by ApTest 2005.07.12 (www.aptest.com)
\r
497 // Now lookk at the focused element and take its parentNode. That will be a tbody or a table.
\r
498 if (trElm && tableElm != trElm.parentNode)
\r
499 tableElm = trElm.parentNode;
\r
501 if (tableElm && trElm) {
\r
503 case "mceTableCutRow":
\r
504 if (!trElm || !tdElm)
\r
507 inst.tableRowClipboard = copyRow(doc, tableElm, trElm);
\r
508 inst.execCommand("mceTableDeleteRow");
\r
511 case "mceTableCopyRow":
\r
512 if (!trElm || !tdElm)
\r
515 inst.tableRowClipboard = copyRow(doc, tableElm, trElm);
\r
518 case "mceTablePasteRowBefore":
\r
519 if (!trElm || !tdElm)
\r
522 var newTR = inst.tableRowClipboard.cloneNode(true);
\r
524 var prevTR = prevElm(trElm, "TR");
\r
525 if (prevTR != null)
\r
526 trimRow(tableElm, prevTR, prevTR.cells[0], newTR);
\r
528 trElm.parentNode.insertBefore(newTR, trElm);
\r
531 case "mceTablePasteRowAfter":
\r
532 if (!trElm || !tdElm)
\r
535 var nextTR = nextElm(trElm, "TR");
\r
536 var newTR = inst.tableRowClipboard.cloneNode(true);
\r
538 trimRow(tableElm, trElm, tdElm, newTR);
\r
540 if (nextTR == null)
\r
541 trElm.parentNode.appendChild(newTR);
\r
543 nextTR.parentNode.insertBefore(newTR, nextTR);
\r
547 case "mceTableInsertRowBefore":
\r
548 if (!trElm || !tdElm)
\r
551 var grid = getTableGrid(tableElm);
\r
552 var cpos = getCellPos(grid, tdElm);
\r
553 var newTR = doc.createElement("tr");
\r
554 var lastTDElm = null;
\r
557 if (cpos.rowindex < 0)
\r
561 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
\r
562 if (tdElm != lastTDElm) {
\r
563 var sd = getColRowSpan(tdElm);
\r
565 if (sd['rowspan'] == 1) {
\r
566 var newTD = doc.createElement("td");
\r
569 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
571 newTD.colSpan = tdElm.colSpan;
\r
573 newTR.appendChild(newTD);
\r
575 tdElm.rowSpan = sd['rowspan'] + 1;
\r
581 trElm.parentNode.insertBefore(newTR, trElm);
\r
585 case "mceTableInsertRowAfter":
\r
586 if (!trElm || !tdElm)
\r
589 var grid = getTableGrid(tableElm);
\r
590 var cpos = getCellPos(grid, tdElm);
\r
591 var newTR = doc.createElement("tr");
\r
592 var lastTDElm = null;
\r
595 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
\r
596 if (tdElm != lastTDElm) {
\r
597 var sd = getColRowSpan(tdElm);
\r
599 if (sd['rowspan'] == 1) {
\r
600 var newTD = doc.createElement("td");
\r
603 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
605 newTD.colSpan = tdElm.colSpan;
\r
607 newTR.appendChild(newTD);
\r
609 tdElm.rowSpan = sd['rowspan'] + 1;
\r
615 if (newTR.hasChildNodes()) {
\r
616 var nextTR = nextElm(trElm, "TR");
\r
618 nextTR.parentNode.insertBefore(newTR, nextTR);
\r
620 tableElm.appendChild(newTR);
\r
626 case "mceTableDeleteRow":
\r
627 if (!trElm || !tdElm)
\r
630 var grid = getTableGrid(tableElm);
\r
631 var cpos = getCellPos(grid, tdElm);
\r
633 // Only one row, remove whole table
\r
634 if (grid.length == 1) {
\r
635 inst.dom.remove(inst.dom.getParent(tableElm, "table"));
\r
639 // Move down row spanned cells
\r
640 var cells = trElm.cells;
\r
641 var nextTR = nextElm(trElm, "TR");
\r
642 for (var x=0; x<cells.length; x++) {
\r
643 if (cells[x].rowSpan > 1) {
\r
644 var newTD = cells[x].cloneNode(true);
\r
645 var sd = getColRowSpan(cells[x]);
\r
647 newTD.rowSpan = sd.rowspan - 1;
\r
649 var nextTD = nextTR.cells[x];
\r
651 if (nextTD == null)
\r
652 nextTR.appendChild(newTD);
\r
654 nextTR.insertBefore(newTD, nextTD);
\r
659 var lastTDElm = null;
\r
660 for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
\r
661 if (tdElm != lastTDElm) {
\r
662 var sd = getColRowSpan(tdElm);
\r
664 if (sd.rowspan > 1) {
\r
665 tdElm.rowSpan = sd.rowspan - 1;
\r
667 trElm = tdElm.parentNode;
\r
669 if (trElm.parentNode)
\r
670 trElm._delete = true;
\r
677 deleteMarked(tableElm);
\r
682 case "mceTableInsertColBefore":
\r
683 if (!trElm || !tdElm)
\r
686 var grid = getTableGrid(tableElm);
\r
687 var cpos = getCellPos(grid, tdElm);
\r
688 var lastTDElm = null;
\r
690 for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
\r
691 if (tdElm != lastTDElm) {
\r
692 var sd = getColRowSpan(tdElm);
\r
694 if (sd['colspan'] == 1) {
\r
695 var newTD = doc.createElement(tdElm.nodeName);
\r
698 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
700 newTD.rowSpan = tdElm.rowSpan;
\r
702 tdElm.parentNode.insertBefore(newTD, tdElm);
\r
713 case "mceTableInsertColAfter":
\r
714 if (!trElm || !tdElm)
\r
717 var grid = getTableGrid(tableElm);
\r
718 var cpos = getCellPos(grid, tdElm);
\r
719 var lastTDElm = null;
\r
721 for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
\r
722 if (tdElm != lastTDElm) {
\r
723 var sd = getColRowSpan(tdElm);
\r
725 if (sd['colspan'] == 1) {
\r
726 var newTD = doc.createElement(tdElm.nodeName);
\r
729 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
731 newTD.rowSpan = tdElm.rowSpan;
\r
733 var nextTD = nextElm(tdElm, "TD,TH");
\r
734 if (nextTD == null)
\r
735 tdElm.parentNode.appendChild(newTD);
\r
737 nextTD.parentNode.insertBefore(newTD, nextTD);
\r
748 case "mceTableDeleteCol":
\r
749 if (!trElm || !tdElm)
\r
752 var grid = getTableGrid(tableElm);
\r
753 var cpos = getCellPos(grid, tdElm);
\r
754 var lastTDElm = null;
\r
756 // Only one col, remove whole table
\r
757 if (grid.length > 1 && grid[0].length <= 1) {
\r
758 inst.dom.remove(inst.dom.getParent(tableElm, "table"));
\r
763 for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
\r
764 if (tdElm != lastTDElm) {
\r
765 var sd = getColRowSpan(tdElm);
\r
767 if (sd['colspan'] > 1)
\r
768 tdElm.colSpan = sd['colspan'] - 1;
\r
770 if (tdElm.parentNode)
\r
771 tdElm.parentNode.removeChild(tdElm);
\r
781 case "mceTableSplitCells":
\r
782 if (!trElm || !tdElm)
\r
785 var spandata = getColRowSpan(tdElm);
\r
787 var colspan = spandata["colspan"];
\r
788 var rowspan = spandata["rowspan"];
\r
791 if (colspan > 1 || rowspan > 1) {
\r
794 for (var i=1; i<colspan; i++) {
\r
795 var newTD = doc.createElement("td");
\r
798 newTD.innerHTML = '<br mce_bogus="1"/>';
\r
800 trElm.insertBefore(newTD, nextElm(tdElm, "TD,TH"));
\r
803 addRows(newTD, trElm, rowspan);
\r
806 addRows(tdElm, trElm, rowspan);
\r
809 // Apply visual aids
\r
810 tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
\r
813 case "mceTableMergeCells":
\r
815 var sel = inst.selection.getSel();
\r
816 var grid = getTableGrid(tableElm);
\r
818 if (tinymce.isIE || sel.rangeCount == 1) {
\r
819 if (user_interface) {
\r
821 var sp = getColRowSpan(tdElm);
\r
823 inst.windowManager.open({
\r
824 url : url + '/merge_cells.htm',
\r
825 width : 240 + parseInt(inst.getLang('table.merge_cells_delta_width', 0)),
\r
826 height : 110 + parseInt(inst.getLang('table.merge_cells_delta_height', 0)),
\r
830 numcols : sp.colspan,
\r
831 numrows : sp.rowspan,
\r
837 var numRows = parseInt(value['numrows']);
\r
838 var numCols = parseInt(value['numcols']);
\r
839 var cpos = getCellPos(grid, tdElm);
\r
841 if (("" + numRows) == "NaN")
\r
844 if (("" + numCols) == "NaN")
\r
847 // Get rows and cells
\r
848 var tRows = tableElm.rows;
\r
849 for (var y=cpos.rowindex; y<grid.length; y++) {
\r
852 for (var x=cpos.cellindex; x<grid[y].length; x++) {
\r
853 var td = getCell(grid, y, x);
\r
855 if (td && !inArray(rows, td) && !inArray(rowCells, td)) {
\r
856 var cp = getCellPos(grid, td);
\r
859 if (cp.cellindex < cpos.cellindex+numCols && cp.rowindex < cpos.rowindex+numRows)
\r
860 rowCells[rowCells.length] = td;
\r
864 if (rowCells.length > 0)
\r
865 rows[rows.length] = rowCells;
\r
867 var td = getCell(grid, cpos.rowindex, cpos.cellindex);
\r
868 each(ed.dom.select('br', td), function(e, i) {
\r
869 if (i > 0 && ed.dom.getAttrib('mce_bogus'))
\r
878 var sel = inst.selection.getSel();
\r
881 var x1 = -1, y1 = -1, x2, y2;
\r
883 // Only one cell selected, whats the point?
\r
884 if (sel.rangeCount < 2)
\r
887 // Get all selected cells
\r
888 for (var i=0; i<sel.rangeCount; i++) {
\r
889 var rng = sel.getRangeAt(i);
\r
890 var tdElm = rng.startContainer.childNodes[rng.startOffset];
\r
895 if (tdElm.nodeName == "TD")
\r
896 cells[cells.length] = tdElm;
\r
899 // Get rows and cells
\r
900 var tRows = tableElm.rows;
\r
901 for (var y=0; y<tRows.length; y++) {
\r
904 for (var x=0; x<tRows[y].cells.length; x++) {
\r
905 var td = tRows[y].cells[x];
\r
907 for (var i=0; i<cells.length; i++) {
\r
908 if (td == cells[i]) {
\r
909 rowCells[rowCells.length] = td;
\r
914 if (rowCells.length > 0)
\r
915 rows[rows.length] = rowCells;
\r
918 // Find selected cells in grid and box
\r
921 for (var y=0; y<grid.length; y++) {
\r
922 for (var x=0; x<grid[y].length; x++) {
\r
923 grid[y][x]._selected = false;
\r
925 for (var i=0; i<cells.length; i++) {
\r
926 if (grid[y][x] == cells[i]) {
\r
937 grid[y][x]._selected = true;
\r
943 // Is there gaps, if so deny
\r
944 for (var y=y1; y<=y2; y++) {
\r
945 for (var x=x1; x<=x2; x++) {
\r
946 if (!grid[y][x]._selected) {
\r
947 alert("Invalid selection for merge.");
\r
954 // Validate selection and get total rowspan and colspan
\r
955 var rowSpan = 1, colSpan = 1;
\r
957 // Validate horizontal and get total colspan
\r
958 var lastRowSpan = -1;
\r
959 for (var y=0; y<rows.length; y++) {
\r
960 var rowColSpan = 0;
\r
962 for (var x=0; x<rows[y].length; x++) {
\r
963 var sd = getColRowSpan(rows[y][x]);
\r
965 rowColSpan += sd['colspan'];
\r
967 if (lastRowSpan != -1 && sd['rowspan'] != lastRowSpan) {
\r
968 alert("Invalid selection for merge.");
\r
972 lastRowSpan = sd['rowspan'];
\r
975 if (rowColSpan > colSpan)
\r
976 colSpan = rowColSpan;
\r
981 // Validate vertical and get total rowspan
\r
982 var lastColSpan = -1;
\r
983 for (var x=0; x<rows[0].length; x++) {
\r
984 var colRowSpan = 0;
\r
986 for (var y=0; y<rows.length; y++) {
\r
987 var sd = getColRowSpan(rows[y][x]);
\r
989 colRowSpan += sd['rowspan'];
\r
991 if (lastColSpan != -1 && sd['colspan'] != lastColSpan) {
\r
992 alert("Invalid selection for merge.");
\r
996 lastColSpan = sd['colspan'];
\r
999 if (colRowSpan > rowSpan)
\r
1000 rowSpan = colRowSpan;
\r
1006 tdElm = rows[0][0];
\r
1007 tdElm.rowSpan = rowSpan;
\r
1008 tdElm.colSpan = colSpan;
\r
1011 for (var y=0; y<rows.length; y++) {
\r
1012 for (var x=0; x<rows[y].length; x++) {
\r
1013 var html = rows[y][x].innerHTML;
\r
1014 var chk = html.replace(/[ \t\r\n]/g, "");
\r
1016 if (chk != "<br/>" && chk != "<br>" && chk != '<br mce_bogus="1"/>' && (x+y > 0))
\r
1017 tdElm.innerHTML += html;
\r
1019 // Not current cell
\r
1020 if (rows[y][x] != tdElm && !rows[y][x]._deleted) {
\r
1021 var cpos = getCellPos(grid, rows[y][x]);
\r
1022 var tr = rows[y][x].parentNode;
\r
1024 tr.removeChild(rows[y][x]);
\r
1025 rows[y][x]._deleted = true;
\r
1027 // Empty TR, remove it
\r
1028 if (!tr.hasChildNodes()) {
\r
1029 tr.parentNode.removeChild(tr);
\r
1031 var lastCell = null;
\r
1032 for (var x=0; cellElm = getCell(grid, cpos.rowindex, x); x++) {
\r
1033 if (cellElm != lastCell && cellElm.rowSpan > 1)
\r
1034 cellElm.rowSpan--;
\r
1036 lastCell = cellElm;
\r
1039 if (tdElm.rowSpan > 1)
\r
1046 // Remove all but one bogus br
\r
1047 each(ed.dom.select('br', tdElm), function(e, i) {
\r
1048 if (i > 0 && ed.dom.getAttrib(e, 'mce_bogus'))
\r
1055 tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
\r
1056 inst.addVisual(tableElm);
\r
1057 inst.nodeChanged();
\r
1063 // Pass to next handler in chain
\r
1068 // Register plugin
\r
1069 tinymce.PluginManager.add('table', tinymce.plugins.TablePlugin);
\r