5 * since selections really work on the table cell, then editing really should work from there
7 * The original plan was to support merging etc... - but that may not be needed yet..
9 * So this simple version will support:
11 * adjust the width +/-
21 * @class Roo.htmleditor.BlockTable
22 * Block that manages a table
25 * Create a new Filter.
26 * @param {Object} config Configuration options
29 Roo.htmleditor.BlockTd = function(cfg)
32 this.readElement(cfg.node);
33 this.updateElement(cfg.node);
40 Roo.extend(Roo.htmleditor.BlockTd, Roo.htmleditor.Block, {
52 // used by context menu
53 friendly_name : 'Table Cell',
54 deleteTitle : false, // use our customer delete
56 // context menu is drawn once..
58 contextMenu : function(toolbar)
60 console.log("htmleditor.BlockTd contextMenu");
63 var cell = function() {
64 return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode);
67 var table = function() {
68 return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode.closest('table'));
72 var saveSel = function()
74 lr = toolbar.editorcore.getSelection().getRangeAt(0);
76 var restoreSel = function()
80 toolbar.editorcore.focus();
81 var cr = toolbar.editorcore.getSelection();
84 toolbar.editorcore.onEditorEvent();
91 var rooui = typeof(Roo.bootstrap) == 'undefined' ? Roo : Roo.bootstrap;
93 var syncValue = toolbar.editorcore.syncValue;
103 var t = toolbar.tb.selectedNode.closest('table');
104 toolbar.editorcore.selectNode(t);
105 toolbar.editorcore.onEditorEvent();
115 text : "Column Width: ",
123 click : function (_self, e)
125 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
126 cell().shrinkColumn();
128 toolbar.editorcore.onEditorEvent();
137 click : function (_self, e)
139 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
142 toolbar.editorcore.onEditorEvent();
150 text : "Vertical Align: ",
151 xns : rooui.Toolbar //Boostrap?
156 displayField : 'val',
159 triggerAction : 'all',
165 select : function (combo, r, index)
167 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
169 b.valign = r.get('val');
172 toolbar.editorcore.onEditorEvent();
177 xtype : 'SimpleStore',
181 ['bottom'] // there are afew more...
190 text : "Merge Cells: ",
200 click : function (_self, e)
202 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
204 //block().growColumn();
206 toolbar.editorcore.onEditorEvent();
216 click : function (_self, e)
218 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
220 //block().growColumn();
222 toolbar.editorcore.onEditorEvent();
238 click : function (_self, e)
240 //toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
243 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
244 toolbar.editorcore.onEditorEvent();
270 click : function (_self, e)
274 cell().deleteColumn();
276 toolbar.editorcore.selectNode(t.node);
277 toolbar.editorcore.onEditorEvent();
286 click : function (_self, e)
292 toolbar.editorcore.selectNode(t.node);
293 toolbar.editorcore.onEditorEvent();
307 click : function (_self, e)
310 var nn = t.node.nextSibling || t.node.previousSibling;
311 t.node.parentNode.removeChild(t.node);
313 toolbar.editorcore.selectNode(nn, true);
315 toolbar.editorcore.onEditorEvent();
333 * create a DomHelper friendly object - for use with
334 * Roo.DomHelper.markup / overwrite / etc..
335 * ?? should it be called with option to hide all editing features?
338 * create a DomHelper friendly object - for use with
339 * Roo.DomHelper.markup / overwrite / etc..
340 * ?? should it be called with option to hide all editing features?
342 toObject : function()
344 console.log("htmleditor.BlockTd toObject");
347 contenteditable : 'true', // this stops cell selection from picking the table.
349 valign : this.valign,
351 'text-align' : this.textAlign,
352 border : 'solid 1px rgb(0, 0, 0)', // ??? hard coded?
353 'border-collapse' : 'collapse',
354 padding : '6px', // 8 for desktop / 4 for mobile
355 'vertical-align': this.valign
359 if (this.width != '') {
360 ret.width = this.width;
361 ret.style.width = this.width;
365 if (this.colspan > 1) {
366 ret.colspan = this.colspan ;
368 if (this.rowspan > 1) {
369 ret.rowspan = this.rowspan ;
378 readElement : function(node)
380 console.log("htmleditor.BlockTd readElement");
382 node = node ? node : this.node ;
383 this.width = node.style.width;
384 this.colspan = Math.max(1,1*node.getAttribute('colspan'));
385 this.rowspan = Math.max(1,1*node.getAttribute('rowspan'));
386 this.html = node.innerHTML;
391 // the default cell object... at present...
392 emptyCell : function() {
397 html : " " // is this going to be editable now?
402 removeNode : function()
404 return this.node.closest('table');
412 toTableArray : function()
414 console.log("htmleditor.BlockTd toTableArray");
416 var tab = this.node.closest('tr').closest('table');
417 Array.from(tab.rows).forEach(function(r, ri){
423 Array.from(tab.rows).forEach(function(r, ri){
426 Array.from(r.cells).forEach(function(ce, ci){
431 colspan : ce.colSpan,
434 if (ce.isEqualNode(this.node)) {
437 // if we have been filled up by a row?
438 if (typeof(ret[rn][cn]) != 'undefined') {
439 while(typeof(ret[rn][cn]) != 'undefined') {
445 if (typeof(this.colWidths[cn]) == 'undefined') {
446 this.colWidths[cn] = ce.style.width;
447 if (this.colWidths[cn] != '') {
453 if (c.colspan < 2 && c.rowspan < 2 ) {
458 for(var j = 0; j < c.rowspan; j++) {
459 if (typeof(ret[rn+j]) == 'undefined') {
460 continue; // we have a problem..
463 for(var i = 0; i < c.colspan; i++) {
473 // initalize widths.?
474 // either all widths or no widths..
476 this.colWidths[0] = false; // no widths flag.
487 mergeRight: function()
490 // get the contents of the next cell along..
491 var tr = this.node.closest('tr');
492 var i = Array.prototype.indexOf.call(tr.childNodes, this.node);
493 if (i >= tr.childNodes.length - 1) {
494 return; // no cells on right to merge with.
496 var table = this.toTableArray();
498 if (typeof(table[this.cellData.row][this.cellData.col+this.cellData.colspan]) == 'undefined') {
499 return; // nothing right?
501 var rc = table[this.cellData.row][this.cellData.col+this.cellData.colspan];
502 // right cell - must be same rowspan and on the same row.
503 if (rc.rowspan != this.cellData.rowspan || rc.row != this.cellData.row) {
504 return; // right hand side is not same rowspan.
509 this.node.innerHTML += ' ' + rc.cell.innerHTML;
510 tr.removeChild(rc.cell);
511 this.colspan += rc.colspan;
512 this.node.setAttribute('colspan', this.colspan);
517 mergeBelow : function()
519 var table = this.toTableArray();
520 if (typeof(table[this.cellData.row+this.cellData.rowspan]) == 'undefined') {
521 return; // no row below
523 if (typeof(table[this.cellData.row+this.cellData.rowspan][this.cellData.col]) == 'undefined') {
524 return; // nothing right?
526 var rc = table[this.cellData.row+this.cellData.rowspan][this.cellData.col];
528 if (rc.colspan != this.cellData.colspan || rc.col != this.cellData.col) {
529 return; // right hand side is not same rowspan.
531 this.node.innerHTML = this.node.innerHTML + rc.cell.innerHTML ;
532 rc.cell.parentNode.removeChild(rc.cell);
533 this.rowspan += rc.rowspan;
534 this.node.setAttribute('rowspan', this.rowspan);
539 if (this.node.rowSpan < 2 && this.node.colSpan < 2) {
542 var table = this.toTableArray();
543 var cd = this.cellData;
547 for(var r = cd.row; r < cd.row + cd.rowspan; r++) {
551 for(var c = cd.col; c < cd.col + cd.colspan; c++) {
552 if (r == cd.row && c == cd.col) {
553 this.node.removeAttribute('rowspan');
554 this.node.removeAttribute('colspan');
558 var ntd = this.node.cloneNode(); // which col/row should be 0..
559 ntd.removeAttribute('id'); //
560 //ntd.style.width = '';
562 table[r][c] = { cell : ntd, col : c, row: r , colspan : 1 , rowspan : 1 };
566 this.redrawAllCells(table);
574 redrawAllCells: function(table)
578 var tab = this.node.closest('tr').closest('table');
579 var ctr = tab.rows[0].parentNode;
580 Array.from(tab.rows).forEach(function(r, ri){
582 Array.from(r.cells).forEach(function(ce, ci){
583 ce.parentNode.removeChild(ce);
585 r.parentNode.removeChild(r);
587 for(var r = 0 ; r < table.length; r++) {
588 var re = tab.rows[r];
590 var re = tab.ownerDocument.createElement('tr');
592 for(var c = 0 ; c < table[r].length; c++) {
593 if (table[r][c].cell === false) {
597 re.appendChild(table[r][c].cell);
599 table[r][c].cell = false;
604 updateWidths : function(table)
606 for(var r = 0 ; r < table.length; r++) {
608 for(var c = 0 ; c < table[r].length; c++) {
609 if (table[r][c].cell === false) {
613 if (this.colWidths[0] != false && table[r][c].colspan < 2) {
614 var el = Roo.htmleditor.Block.factory(table[r][c].cell);
615 el.width = Math.floor(this.colWidths[c]) +'%';
616 el.updateElement(el.node);
618 table[r][c].cell = false; // done
622 normalizeWidths : function(table)
625 if (this.colWidths[0] === false) {
626 var nw = 100.0 / this.colWidths.length;
627 this.colWidths.forEach(function(w,i) {
628 this.colWidths[i] = nw;
633 var t = 0, missing = [];
635 this.colWidths.forEach(function(w,i) {
637 this.colWidths[i] = this.colWidths[i] == '' ? 0 : (this.colWidths[i]+'').replace(/[^0-9]+/g,'')*1;
638 var add = this.colWidths[i];
647 var nc = this.colWidths.length;
648 if (missing.length) {
649 var mult = (nc - missing.length) / (1.0 * nc);
651 var ew = (100 -t) / (1.0 * missing.length);
652 this.colWidths.forEach(function(w,i) {
654 this.colWidths[i] = w * mult;
658 this.colWidths[i] = ew;
660 // have to make up numbers..
663 // now we should have all the widths..
668 shrinkColumn : function()
670 console.log("htmleditor.BlockTd shrinkColumn");
671 var table = this.toTableArray();
673 this.normalizeWidths(table);
674 var col = this.cellData.col;
675 var nw = this.colWidths[col] * 0.8;
679 var otherAdd = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
680 this.colWidths.forEach(function(w,i) {
682 this.colWidths[i] = nw;
685 this.colWidths[i] += otherAdd
687 this.updateWidths(table);
690 growColumn : function()
692 var table = this.toTableArray();
693 this.normalizeWidths(table);
694 var col = this.cellData.col;
695 var nw = this.colWidths[col] * 1.2;
699 var otherSub = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
700 this.colWidths.forEach(function(w,i) {
702 this.colWidths[i] = nw;
705 this.colWidths[i] -= otherSub
707 this.updateWidths(table);
710 deleteRow : function()
712 // delete this rows 'tr'
713 // if any of the cells in this row have a rowspan > 1 && row!= this row..
714 // then reduce the rowspan.
715 var table = this.toTableArray();
716 // this.cellData.row;
717 for (var i =0;i< table[this.cellData.row].length ; i++) {
718 var c = table[this.cellData.row][i];
719 if (c.row != this.cellData.row) {
722 c.cell.setAttribute('rowspan', c.rowspan);
727 c.cell.setAttribute('rowspan', c.rowspan);
730 table.splice(this.cellData.row,1);
731 this.redrawAllCells(table);
734 deleteColumn : function()
736 var table = this.toTableArray();
738 for (var i =0;i< table.length ; i++) {
739 var c = table[i][this.cellData.col];
740 if (c.col != this.cellData.col) {
741 table[i][this.cellData.col].colspan--;
742 } else if (c.colspan > 1) {
744 c.cell.setAttribute('colspan', c.colspan);
746 table[i].splice(this.cellData.col,1);
749 this.redrawAllCells(table);