/**
* Plugin.js
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This class contains all core logic for the table plugin.
*
* @class tinymce.tableplugin.Plugin
* @private
*/
define("tinymce/tableplugin/Plugin", [
"tinymce/tableplugin/TableGrid",
"tinymce/tableplugin/Quirks",
"tinymce/tableplugin/CellSelection",
"tinymce/util/Tools",
"tinymce/dom/TreeWalker",
"tinymce/Env",
"tinymce/PluginManager"
], function(TableGrid, Quirks, CellSelection, Tools, TreeWalker, Env, PluginManager) {
var each = Tools.each;
function Plugin(editor) {
var winMan, clipboardRows, self = this; // Might be selected cells on reload
function removePxSuffix(size) {
return size ? size.replace(/px$/, '') : "";
}
function addSizeSuffix(size) {
if (/^[0-9]+$/.test(size)) {
size += "px";
}
return size;
}
function unApplyAlign(elm) {
each('left center right'.split(' '), function(name) {
editor.formatter.remove('align' + name, {}, elm);
});
}
function tableDialog() {
var dom = editor.dom, tableElm, data;
tableElm = dom.getParent(editor.selection.getStart(), 'table');
data = {
width: removePxSuffix(dom.getStyle(tableElm, 'width') || dom.getAttrib(tableElm, 'width')),
height: removePxSuffix(dom.getStyle(tableElm, 'height') || dom.getAttrib(tableElm, 'height')),
cellspacing: dom.getAttrib(tableElm, 'cellspacing'),
cellpadding: dom.getAttrib(tableElm, 'cellpadding'),
border: dom.getAttrib(tableElm, 'border'),
caption: !!dom.select('caption', tableElm)[0]
};
each('left center right'.split(' '), function(name) {
if (editor.formatter.matchNode(tableElm, 'align' + name)) {
data.align = name;
}
});
editor.windowManager.open({
title: "Table properties",
items: {
type: 'form',
layout: 'grid',
columns: 2,
data: data,
defaults: {
type: 'textbox',
maxWidth: 50
},
items: [
{label: 'Width', name: 'width'},
{label: 'Height', name: 'height'},
{label: 'Cell spacing', name: 'cellspacing'},
{label: 'Cell padding', name: 'cellpadding'},
{label: 'Border', name: 'border'},
{label: 'Caption', name: 'caption', type: 'checkbox'},
{
label: 'Alignment',
minWidth: 90,
name: 'align',
type: 'listbox',
text: 'None',
maxWidth: null,
values: [
{text: 'None', value: ''},
{text: 'Left', value: 'left'},
{text: 'Center', value: 'center'},
{text: 'Right', value: 'right'}
]
}
]
},
onsubmit: function() {
var data = this.toJSON(), captionElm;
editor.undoManager.transact(function() {
editor.dom.setAttribs(tableElm, {
cellspacing: data.cellspacing,
cellpadding: data.cellpadding,
border: data.border
});
editor.dom.setStyles(tableElm, {
width: addSizeSuffix(data.width),
height: addSizeSuffix(data.height)
});
// Toggle caption on/off
captionElm = dom.select('caption', tableElm)[0];
if (captionElm && !data.caption) {
dom.remove(captionElm);
}
if (!captionElm && data.caption) {
captionElm = dom.create('caption');
captionElm.innerHTML = !Env.ie ? '
' : '\u00a0';
tableElm.insertBefore(captionElm, tableElm.firstChild);
}
unApplyAlign(tableElm);
if (data.align) {
editor.formatter.apply('align' + data.align, {}, tableElm);
}
editor.focus();
editor.addVisual();
});
}
});
}
function mergeDialog(grid, cell) {
editor.windowManager.open({
title: "Merge cells",
body: [
{label: 'Cols', name: 'cols', type: 'textbox', size: 10},
{label: 'Rows', name: 'rows', type: 'textbox', size: 10}
],
onsubmit: function() {
var data = this.toJSON();
editor.undoManager.transact(function() {
grid.merge(cell, data.cols, data.rows);
});
}
});
}
function cellDialog() {
var dom = editor.dom, cellElm, data, cells = [];
// Get selected cells or the current cell
cells = editor.dom.select('td.mce-item-selected,th.mce-item-selected');
cellElm = editor.dom.getParent(editor.selection.getStart(), 'td,th');
if (!cells.length && cellElm) {
cells.push(cellElm);
}
cellElm = cellElm || cells[0];
if (!cellElm) {
// If this element is null, return now to avoid crashing.
return;
}
data = {
width: removePxSuffix(dom.getStyle(cellElm, 'width') || dom.getAttrib(cellElm, 'width')),
height: removePxSuffix(dom.getStyle(cellElm, 'height') || dom.getAttrib(cellElm, 'height')),
scope: dom.getAttrib(cellElm, 'scope')
};
data.type = cellElm.nodeName.toLowerCase();
each('left center right'.split(' '), function(name) {
if (editor.formatter.matchNode(cellElm, 'align' + name)) {
data.align = name;
}
});
editor.windowManager.open({
title: "Cell properties",
items: {
type: 'form',
data: data,
layout: 'grid',
columns: 2,
defaults: {
type: 'textbox',
maxWidth: 50
},
items: [
{label: 'Width', name: 'width'},
{label: 'Height', name: 'height'},
{
label: 'Cell type',
name: 'type',
type: 'listbox',
text: 'None',
minWidth: 90,
maxWidth: null,
menu: [
{text: 'Cell', value: 'td'},
{text: 'Header cell', value: 'th'}
]
},
{
label: 'Scope',
name: 'scope',
type: 'listbox',
text: 'None',
minWidth: 90,
maxWidth: null,
menu: [
{text: 'None', value: ''},
{text: 'Row', value: 'row'},
{text: 'Column', value: 'col'},
{text: 'Row group', value: 'rowgroup'},
{text: 'Column group', value: 'colgroup'}
]
},
{
label: 'Alignment',
name: 'align',
type: 'listbox',
text: 'None',
minWidth: 90,
maxWidth: null,
values: [
{text: 'None', value: ''},
{text: 'Left', value: 'left'},
{text: 'Center', value: 'center'},
{text: 'Right', value: 'right'}
]
}
]
},
onsubmit: function() {
var data = this.toJSON();
editor.undoManager.transact(function() {
each(cells, function(cellElm) {
editor.dom.setAttrib(cellElm, 'scope', data.scope);
editor.dom.setStyles(cellElm, {
width: addSizeSuffix(data.width),
height: addSizeSuffix(data.height)
});
// Switch cell type
if (data.type && cellElm.nodeName.toLowerCase() != data.type) {
cellElm = dom.rename(cellElm, data.type);
}
// Apply/remove alignment
unApplyAlign(cellElm);
if (data.align) {
editor.formatter.apply('align' + data.align, {}, cellElm);
}
});
editor.focus();
});
}
});
}
function rowDialog() {
var dom = editor.dom, tableElm, cellElm, rowElm, data, rows = [];
tableElm = editor.dom.getParent(editor.selection.getStart(), 'table');
cellElm = editor.dom.getParent(editor.selection.getStart(), 'td,th');
each(tableElm.rows, function(row) {
each(row.cells, function(cell) {
if (dom.hasClass(cell, 'mce-item-selected') || cell == cellElm) {
rows.push(row);
return false;
}
});
});
rowElm = rows[0];
if (!rowElm) {
// If this element is null, return now to avoid crashing.
return;
}
data = {
height: removePxSuffix(dom.getStyle(rowElm, 'height') || dom.getAttrib(rowElm, 'height')),
scope: dom.getAttrib(rowElm, 'scope')
};
data.type = rowElm.parentNode.nodeName.toLowerCase();
each('left center right'.split(' '), function(name) {
if (editor.formatter.matchNode(rowElm, 'align' + name)) {
data.align = name;
}
});
editor.windowManager.open({
title: "Row properties",
items: {
type: 'form',
data: data,
columns: 2,
defaults: {
type: 'textbox'
},
items: [
{
type: 'listbox',
name: 'type',
label: 'Row type',
text: 'None',
maxWidth: null,
menu: [
{text: 'Header', value: 'thead'},
{text: 'Body', value: 'tbody'},
{text: 'Footer', value: 'tfoot'}
]
},
{
type: 'listbox',
name: 'align',
label: 'Alignment',
text: 'None',
maxWidth: null,
menu: [
{text: 'None', value: ''},
{text: 'Left', value: 'left'},
{text: 'Center', value: 'center'},
{text: 'Right', value: 'right'}
]
},
{label: 'Height', name: 'height'}
]
},
onsubmit: function() {
var data = this.toJSON(), tableElm, oldParentElm, parentElm;
editor.undoManager.transact(function() {
var toType = data.type;
each(rows, function(rowElm) {
editor.dom.setAttrib(rowElm, 'scope', data.scope);
editor.dom.setStyles(rowElm, {
height: addSizeSuffix(data.height)
});
if (toType != rowElm.parentNode.nodeName.toLowerCase()) {
tableElm = dom.getParent(rowElm, 'table');
oldParentElm = rowElm.parentNode;
parentElm = dom.select(toType, tableElm)[0];
if (!parentElm) {
parentElm = dom.create(toType);
if (tableElm.firstChild) {
tableElm.insertBefore(parentElm, tableElm.firstChild);
} else {
tableElm.appendChild(parentElm);
}
}
parentElm.appendChild(rowElm);
if (!oldParentElm.hasChildNodes()) {
dom.remove(oldParentElm);
}
}
// Apply/remove alignment
unApplyAlign(rowElm);
if (data.align) {
editor.formatter.apply('align' + data.align, {}, rowElm);
}
});
editor.focus();
});
}
});
}
function cmd(command) {
return function() {
editor.execCommand(command);
};
}
function insertTable(cols, rows) {
var y, x, html;
html = '
' + (Env.ie ? " " : ' ') + ' | ';
}
html += '
'; } html += ' |