/**
* $Id: editor_plugin_src.js 953 2008-11-04 10:16:50Z spocke $
*
* @author Moxiecode
* @copyright Copyright © 2004-2008, Moxiecode Systems AB, All rights reserved.
*/
(function() {
var each = tinymce.each;
tinymce.create('tinymce.plugins.TablePlugin', {
init : function(ed, url) {
var t = this;
t.editor = ed;
t.url = url;
// Register buttons
each([
['table', 'table.desc', 'mceInsertTable', true],
['delete_table', 'table.del', 'mceTableDelete'],
['delete_col', 'table.delete_col_desc', 'mceTableDeleteCol'],
['delete_row', 'table.delete_row_desc', 'mceTableDeleteRow'],
['col_after', 'table.col_after_desc', 'mceTableInsertColAfter'],
['col_before', 'table.col_before_desc', 'mceTableInsertColBefore'],
['row_after', 'table.row_after_desc', 'mceTableInsertRowAfter'],
['row_before', 'table.row_before_desc', 'mceTableInsertRowBefore'],
['row_props', 'table.row_desc', 'mceTableRowProps', true],
['cell_props', 'table.cell_desc', 'mceTableCellProps', true],
['split_cells', 'table.split_cells_desc', 'mceTableSplitCells', true],
['merge_cells', 'table.merge_cells_desc', 'mceTableMergeCells', true]
], function(c) {
ed.addButton(c[0], {title : c[1], cmd : c[2], ui : c[3]});
});
if (ed.getParam('inline_styles')) {
// Force move of attribs to styles in strict mode
ed.onPreProcess.add(function(ed, o) {
var dom = ed.dom;
each(dom.select('table', o.node), function(n) {
var v;
if (v = dom.getAttrib(n, 'width')) {
dom.setStyle(n, 'width', v);
dom.setAttrib(n, 'width');
}
if (v = dom.getAttrib(n, 'height')) {
dom.setStyle(n, 'height', v);
dom.setAttrib(n, 'height');
}
});
});
}
ed.onInit.add(function() {
if (ed && ed.plugins.contextmenu) {
ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
var sm, se = ed.selection, el = se.getNode() || ed.getBody();
if (ed.dom.getParent(e, 'td') || ed.dom.getParent(e, 'th')) {
m.removeAll();
if (el.nodeName == 'A' && !ed.dom.getAttrib(el, 'name')) {
m.add({title : 'advanced.link_desc', icon : 'link', cmd : ed.plugins.advlink ? 'mceAdvLink' : 'mceLink', ui : true});
m.add({title : 'advanced.unlink_desc', icon : 'unlink', cmd : 'UnLink'});
m.addSeparator();
}
if (el.nodeName == 'IMG' && el.className.indexOf('mceItem') == -1) {
m.add({title : 'advanced.image_desc', icon : 'image', cmd : ed.plugins.advimage ? 'mceAdvImage' : 'mceImage', ui : true});
m.addSeparator();
}
m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', ui : true, value : {action : 'insert'}});
m.add({title : 'table.props_desc', icon : 'table_props', cmd : 'mceInsertTable', ui : true});
m.add({title : 'table.del', icon : 'delete_table', cmd : 'mceTableDelete', ui : true});
m.addSeparator();
// Cell menu
sm = m.addMenu({title : 'table.cell'});
sm.add({title : 'table.cell_desc', icon : 'cell_props', cmd : 'mceTableCellProps', ui : true});
sm.add({title : 'table.split_cells_desc', icon : 'split_cells', cmd : 'mceTableSplitCells', ui : true});
sm.add({title : 'table.merge_cells_desc', icon : 'merge_cells', cmd : 'mceTableMergeCells', ui : true});
// Row menu
sm = m.addMenu({title : 'table.row'});
sm.add({title : 'table.row_desc', icon : 'row_props', cmd : 'mceTableRowProps', ui : true});
sm.add({title : 'table.row_before_desc', icon : 'row_before', cmd : 'mceTableInsertRowBefore'});
sm.add({title : 'table.row_after_desc', icon : 'row_after', cmd : 'mceTableInsertRowAfter'});
sm.add({title : 'table.delete_row_desc', icon : 'delete_row', cmd : 'mceTableDeleteRow'});
sm.addSeparator();
sm.add({title : 'table.cut_row_desc', icon : 'cut', cmd : 'mceTableCutRow'});
sm.add({title : 'table.copy_row_desc', icon : 'copy', cmd : 'mceTableCopyRow'});
sm.add({title : 'table.paste_row_before_desc', icon : 'paste', cmd : 'mceTablePasteRowBefore'});
sm.add({title : 'table.paste_row_after_desc', icon : 'paste', cmd : 'mceTablePasteRowAfter'});
// Column menu
sm = m.addMenu({title : 'table.col'});
sm.add({title : 'table.col_before_desc', icon : 'col_before', cmd : 'mceTableInsertColBefore'});
sm.add({title : 'table.col_after_desc', icon : 'col_after', cmd : 'mceTableInsertColAfter'});
sm.add({title : 'table.delete_col_desc', icon : 'delete_col', cmd : 'mceTableDeleteCol'});
} else
m.add({title : 'table.desc', icon : 'table', cmd : 'mceInsertTable', ui : true});
});
}
});
// Add undo level when new rows are created using the tab key
ed.onKeyDown.add(function(ed, e) {
if (e.keyCode == 9 && ed.dom.getParent(ed.selection.getNode(), 'TABLE')) {
if (!tinymce.isGecko && !tinymce.isOpera) {
tinyMCE.execInstanceCommand(ed.editorId, "mceTableMoveToNextRow", true);
return tinymce.dom.Event.cancel(e);
}
ed.undoManager.add();
}
});
// Select whole table is a table border is clicked
if (!tinymce.isIE) {
if (ed.getParam('table_selection', true)) {
ed.onClick.add(function(ed, e) {
e = e.target;
if (e.nodeName === 'TABLE')
ed.selection.select(e);
});
}
}
ed.onNodeChange.add(function(ed, cm, n) {
var p = ed.dom.getParent(n, 'td,th,caption');
cm.setActive('table', n.nodeName === 'TABLE' || !!p);
if (p && p.nodeName === 'CAPTION')
p = null;
cm.setDisabled('delete_table', !p);
cm.setDisabled('delete_col', !p);
cm.setDisabled('delete_table', !p);
cm.setDisabled('delete_row', !p);
cm.setDisabled('col_after', !p);
cm.setDisabled('col_before', !p);
cm.setDisabled('row_after', !p);
cm.setDisabled('row_before', !p);
cm.setDisabled('row_props', !p);
cm.setDisabled('cell_props', !p);
cm.setDisabled('split_cells', !p || (parseInt(ed.dom.getAttrib(p, 'colspan', '1')) < 2 && parseInt(ed.dom.getAttrib(p, 'rowspan', '1')) < 2));
cm.setDisabled('merge_cells', !p);
});
// Padd empty table cells
if (!tinymce.isIE) {
ed.onBeforeSetContent.add(function(ed, o) {
if (o.initial)
o.content = o.content.replace(/<(td|th)([^>]+|)>\s*<\/(td|th)>/g, tinymce.isOpera ? '<$1$2> $1>' : '<$1$2>
$1>');
});
}
},
execCommand : function(cmd, ui, val) {
var ed = this.editor, b;
// Is table command
switch (cmd) {
case "mceTableMoveToNextRow":
case "mceInsertTable":
case "mceTableRowProps":
case "mceTableCellProps":
case "mceTableSplitCells":
case "mceTableMergeCells":
case "mceTableInsertRowBefore":
case "mceTableInsertRowAfter":
case "mceTableDeleteRow":
case "mceTableInsertColBefore":
case "mceTableInsertColAfter":
case "mceTableDeleteCol":
case "mceTableCutRow":
case "mceTableCopyRow":
case "mceTablePasteRowBefore":
case "mceTablePasteRowAfter":
case "mceTableDelete":
ed.execCommand('mceBeginUndoLevel');
this._doExecCommand(cmd, ui, val);
ed.execCommand('mceEndUndoLevel');
return true;
}
// Pass to next handler in chain
return false;
},
getInfo : function() {
return {
longname : 'Tables',
author : 'Moxiecode Systems AB',
authorurl : 'http://tinymce.moxiecode.com',
infourl : 'http://wiki.moxiecode.com/index.php/TinyMCE:Plugins/table',
version : tinymce.majorVersion + "." + tinymce.minorVersion
};
},
// Private plugin internal methods
/**
* Executes the table commands.
*/
_doExecCommand : function(command, user_interface, value) {
var inst = this.editor, ed = inst, url = this.url;
var focusElm = inst.selection.getNode();
var trElm = inst.dom.getParent(focusElm, "tr");
var tdElm = inst.dom.getParent(focusElm, "td,th");
var tableElm = inst.dom.getParent(focusElm, "table");
var doc = inst.contentWindow.document;
var tableBorder = tableElm ? tableElm.getAttribute("border") : "";
// Get first TD if no TD found
if (trElm && tdElm == null)
tdElm = trElm.cells[0];
function inArray(ar, v) {
for (var i=0; i 0 && inArray(ar[i], v))
return true;
// Found value
if (ar[i] == v)
return true;
}
return false;
}
function select(dx, dy) {
var td;
grid = getTableGrid(tableElm);
dx = dx || 0;
dy = dy || 0;
dx = Math.max(cpos.cellindex + dx, 0);
dy = Math.max(cpos.rowindex + dy, 0);
// Recalculate grid and select
inst.execCommand('mceRepaint');
td = getCell(grid, dy, dx);
if (td) {
inst.selection.select(td.firstChild || td);
inst.selection.collapse(1);
}
};
function makeTD() {
var newTD = doc.createElement("td");
if (!tinymce.isIE)
newTD.innerHTML = '
';
}
function getColRowSpan(td) {
var colspan = inst.dom.getAttrib(td, "colspan");
var rowspan = inst.dom.getAttrib(td, "rowspan");
colspan = colspan == "" ? 1 : parseInt(colspan);
rowspan = rowspan == "" ? 1 : parseInt(rowspan);
return {colspan : colspan, rowspan : rowspan};
}
function getCellPos(grid, td) {
var x, y;
for (y=0; y 1) { // Remove due to colspan
for (var i=x; i 1)
td.rowSpan = sd.rowspan + 1;
lastElm = td;
}
deleteMarked(tableElm);
}
}
function prevElm(node, name) {
while ((node = node.previousSibling) != null) {
if (node.nodeName == name)
return node;
}
return null;
}
function nextElm(node, names) {
var namesAr = names.split(',');
while ((node = node.nextSibling) != null) {
for (var i=0; i 1) {
do {
var nexttd = nextElm(td, "TD,TH");
if (td._delete)
td.parentNode.removeChild(td);
} while ((td = nexttd) != null);
}
} while ((tr = next) != null);
}
function addRows(td_elm, tr_elm, rowspan) {
// Add rows
td_elm.rowSpan = 1;
var trNext = nextElm(tr_elm, "TR");
for (var i=1; i 1) {
var newTD = cells[x].cloneNode(true);
var sd = getColRowSpan(cells[x]);
newTD.rowSpan = sd.rowspan - 1;
var nextTD = nextTR.cells[x];
if (nextTD == null)
nextTR.appendChild(newTD);
else
nextTR.insertBefore(newTD, nextTD);
}
}
// Delete cells
var lastTDElm = null;
for (var x=0; tdElm = getCell(grid, cpos.rowindex, x); x++) {
if (tdElm != lastTDElm) {
var sd = getColRowSpan(tdElm);
if (sd.rowspan > 1) {
tdElm.rowSpan = sd.rowspan - 1;
} else {
trElm = tdElm.parentNode;
if (trElm.parentNode)
trElm._delete = true;
}
lastTDElm = tdElm;
}
}
deleteMarked(tableElm);
select(0, -1);
break;
case "mceTableInsertColBefore":
if (!trElm || !tdElm)
return true;
var grid = getTableGrid(inst.dom.getParent(tableElm, "table"));
var cpos = getCellPos(grid, tdElm);
var lastTDElm = null;
for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
if (tdElm != lastTDElm) {
var sd = getColRowSpan(tdElm);
if (sd['colspan'] == 1) {
var newTD = doc.createElement(tdElm.nodeName);
if (!tinymce.isIE)
newTD.innerHTML = '
';
newTD.rowSpan = tdElm.rowSpan;
tdElm.parentNode.insertBefore(newTD, tdElm);
} else
tdElm.colSpan++;
lastTDElm = tdElm;
}
}
select();
break;
case "mceTableInsertColAfter":
if (!trElm || !tdElm)
return true;
var grid = getTableGrid(inst.dom.getParent(tableElm, "table"));
var cpos = getCellPos(grid, tdElm);
var lastTDElm = null;
for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
if (tdElm != lastTDElm) {
var sd = getColRowSpan(tdElm);
if (sd['colspan'] == 1) {
var newTD = doc.createElement(tdElm.nodeName);
if (!tinymce.isIE)
newTD.innerHTML = '
';
newTD.rowSpan = tdElm.rowSpan;
var nextTD = nextElm(tdElm, "TD,TH");
if (nextTD == null)
tdElm.parentNode.appendChild(newTD);
else
nextTD.parentNode.insertBefore(newTD, nextTD);
} else
tdElm.colSpan++;
lastTDElm = tdElm;
}
}
select(1);
break;
case "mceTableDeleteCol":
if (!trElm || !tdElm)
return true;
var grid = getTableGrid(tableElm);
var cpos = getCellPos(grid, tdElm);
var lastTDElm = null;
// Only one col, remove whole table
if ((grid.length > 1 && grid[0].length <= 1) && tableElm.nodeName == 'TBODY') {
inst.dom.remove(inst.dom.getParent(tableElm, "table"));
return true;
}
// Delete cells
for (var y=0; tdElm = getCell(grid, y, cpos.cellindex); y++) {
if (tdElm != lastTDElm) {
var sd = getColRowSpan(tdElm);
if (sd['colspan'] > 1)
tdElm.colSpan = sd['colspan'] - 1;
else {
if (tdElm.parentNode)
tdElm.parentNode.removeChild(tdElm);
}
lastTDElm = tdElm;
}
}
select(-1);
break;
case "mceTableSplitCells":
if (!trElm || !tdElm)
return true;
var spandata = getColRowSpan(tdElm);
var colspan = spandata["colspan"];
var rowspan = spandata["rowspan"];
// Needs splitting
if (colspan > 1 || rowspan > 1) {
// Generate cols
tdElm.colSpan = 1;
for (var i=1; i 1)
addRows(newTD, trElm, rowspan);
}
addRows(tdElm, trElm, rowspan);
}
// Apply visual aids
tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
break;
case "mceTableMergeCells":
var rows = [];
var sel = inst.selection.getSel();
var grid = getTableGrid(tableElm);
if (tinymce.isIE || sel.rangeCount == 1) {
if (user_interface) {
// Setup template
var sp = getColRowSpan(tdElm);
inst.windowManager.open({
url : url + '/merge_cells.htm',
width : 240 + parseInt(inst.getLang('table.merge_cells_delta_width', 0)),
height : 110 + parseInt(inst.getLang('table.merge_cells_delta_height', 0)),
inline : 1
}, {
action : "update",
numcols : sp.colspan,
numrows : sp.rowspan,
plugin_url : url
});
return true;
} else {
var numRows = parseInt(value['numrows']);
var numCols = parseInt(value['numcols']);
var cpos = getCellPos(grid, tdElm);
if (("" + numRows) == "NaN")
numRows = 1;
if (("" + numCols) == "NaN")
numCols = 1;
// Get rows and cells
var tRows = tableElm.rows;
for (var y=cpos.rowindex; y 0)
rows[rows.length] = rowCells;
var td = getCell(grid, cpos.rowindex, cpos.cellindex);
each(ed.dom.select('br', td), function(e, i) {
if (i > 0 && ed.dom.getAttrib('mce_bogus'))
ed.dom.remove(e);
});
}
//return true;
}
} else {
var cells = [];
var sel = inst.selection.getSel();
var lastTR = null;
var curRow = null;
var x1 = -1, y1 = -1, x2, y2;
// Only one cell selected, whats the point?
if (sel.rangeCount < 2)
return true;
// Get all selected cells
for (var i=0; i 0)
rows[rows.length] = rowCells;
}
// Find selected cells in grid and box
var curRow = [];
var lastTR = null;
for (var y=0; y colSpan)
colSpan = rowColSpan;
lastRowSpan = -1;
}
// Validate vertical and get total rowspan
var lastColSpan = -1;
for (var x=0; x rowSpan)
rowSpan = colRowSpan;
lastColSpan = -1;
}
// Setup td
tdElm = rows[0][0];
tdElm.rowSpan = rowSpan;
tdElm.colSpan = colSpan;
// Merge cells
for (var y=0; y" && chk != "
" && chk != '
' && (x+y > 0))
tdElm.innerHTML += html;
// Not current cell
if (rows[y][x] != tdElm && !rows[y][x]._deleted) {
var cpos = getCellPos(grid, rows[y][x]);
var tr = rows[y][x].parentNode;
tr.removeChild(rows[y][x]);
rows[y][x]._deleted = true;
// Empty TR, remove it
if (!tr.hasChildNodes()) {
tr.parentNode.removeChild(tr);
var lastCell = null;
for (var x=0; cellElm = getCell(grid, cpos.rowindex, x); x++) {
if (cellElm != lastCell && cellElm.rowSpan > 1)
cellElm.rowSpan--;
lastCell = cellElm;
}
if (tdElm.rowSpan > 1)
tdElm.rowSpan--;
}
}
}
}
// Remove all but one bogus br
each(ed.dom.select('br', tdElm), function(e, i) {
if (i > 0 && ed.dom.getAttrib(e, 'mce_bogus'))
ed.dom.remove(e);
});
break;
}
tableElm = inst.dom.getParent(inst.selection.getNode(), "table");
inst.addVisual(tableElm);
inst.nodeChanged();
}
return true;
}
// Pass to next handler in chain
return false;
}
});
// Register plugin
tinymce.PluginManager.add('table', tinymce.plugins.TablePlugin);
})();