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 console.log("colWidths " + cn);
448 console.log(ce.style.width);
449 if (this.colWidths[cn] != '') {
455 if (c.colspan < 2 && c.rowspan < 2 ) {
460 for(var j = 0; j < c.rowspan; j++) {
461 if (typeof(ret[rn+j]) == 'undefined') {
462 continue; // we have a problem..
465 for(var i = 0; i < c.colspan; i++) {
475 // initalize widths.?
476 // either all widths or no widths..
478 this.colWidths[0] = false; // no widths flag.
489 mergeRight: function()
492 // get the contents of the next cell along..
493 var tr = this.node.closest('tr');
494 var i = Array.prototype.indexOf.call(tr.childNodes, this.node);
495 if (i >= tr.childNodes.length - 1) {
496 return; // no cells on right to merge with.
498 var table = this.toTableArray();
500 if (typeof(table[this.cellData.row][this.cellData.col+this.cellData.colspan]) == 'undefined') {
501 return; // nothing right?
503 var rc = table[this.cellData.row][this.cellData.col+this.cellData.colspan];
504 // right cell - must be same rowspan and on the same row.
505 if (rc.rowspan != this.cellData.rowspan || rc.row != this.cellData.row) {
506 return; // right hand side is not same rowspan.
511 this.node.innerHTML += ' ' + rc.cell.innerHTML;
512 tr.removeChild(rc.cell);
513 this.colspan += rc.colspan;
514 this.node.setAttribute('colspan', this.colspan);
519 mergeBelow : function()
521 var table = this.toTableArray();
522 if (typeof(table[this.cellData.row+this.cellData.rowspan]) == 'undefined') {
523 return; // no row below
525 if (typeof(table[this.cellData.row+this.cellData.rowspan][this.cellData.col]) == 'undefined') {
526 return; // nothing right?
528 var rc = table[this.cellData.row+this.cellData.rowspan][this.cellData.col];
530 if (rc.colspan != this.cellData.colspan || rc.col != this.cellData.col) {
531 return; // right hand side is not same rowspan.
533 this.node.innerHTML = this.node.innerHTML + rc.cell.innerHTML ;
534 rc.cell.parentNode.removeChild(rc.cell);
535 this.rowspan += rc.rowspan;
536 this.node.setAttribute('rowspan', this.rowspan);
541 if (this.node.rowSpan < 2 && this.node.colSpan < 2) {
544 var table = this.toTableArray();
545 var cd = this.cellData;
549 for(var r = cd.row; r < cd.row + cd.rowspan; r++) {
553 for(var c = cd.col; c < cd.col + cd.colspan; c++) {
554 if (r == cd.row && c == cd.col) {
555 this.node.removeAttribute('rowspan');
556 this.node.removeAttribute('colspan');
560 var ntd = this.node.cloneNode(); // which col/row should be 0..
561 ntd.removeAttribute('id'); //
562 //ntd.style.width = '';
564 table[r][c] = { cell : ntd, col : c, row: r , colspan : 1 , rowspan : 1 };
568 this.redrawAllCells(table);
576 redrawAllCells: function(table)
580 var tab = this.node.closest('tr').closest('table');
581 var ctr = tab.rows[0].parentNode;
582 Array.from(tab.rows).forEach(function(r, ri){
584 Array.from(r.cells).forEach(function(ce, ci){
585 ce.parentNode.removeChild(ce);
587 r.parentNode.removeChild(r);
589 for(var r = 0 ; r < table.length; r++) {
590 var re = tab.rows[r];
592 var re = tab.ownerDocument.createElement('tr');
594 for(var c = 0 ; c < table[r].length; c++) {
595 if (table[r][c].cell === false) {
599 re.appendChild(table[r][c].cell);
601 table[r][c].cell = false;
606 updateWidths : function(table)
608 for(var r = 0 ; r < table.length; r++) {
610 for(var c = 0 ; c < table[r].length; c++) {
611 if (table[r][c].cell === false) {
615 if (this.colWidths[0] != false && table[r][c].colspan < 2) {
616 var el = Roo.htmleditor.Block.factory(table[r][c].cell);
617 el.width = Math.floor(this.colWidths[c]) +'%';
618 el.updateElement(el.node);
620 table[r][c].cell = false; // done
624 normalizeWidths : function(table)
626 console.log("htmleditor.BlockTd normalizeWidths");
627 console.log("colWidths 0");
628 console.log(col.Widths[0]);
629 console.log("colWidths 1");
630 console.log(col.Wdiths[1]);
632 if (this.colWidths[0] === false) {
633 var nw = 100.0 / this.colWidths.length;
634 this.colWidths.forEach(function(w,i) {
635 this.colWidths[i] = nw;
640 var t = 0, missing = [];
642 this.colWidths.forEach(function(w,i) {
644 this.colWidths[i] = this.colWidths[i] == '' ? 0 : (this.colWidths[i]+'').replace(/[^0-9]+/g,'')*1;
645 var add = this.colWidths[i];
654 var nc = this.colWidths.length;
655 if (missing.length) {
656 var mult = (nc - missing.length) / (1.0 * nc);
658 var ew = (100 -t) / (1.0 * missing.length);
659 this.colWidths.forEach(function(w,i) {
661 this.colWidths[i] = w * mult;
665 this.colWidths[i] = ew;
667 // have to make up numbers..
670 // now we should have all the widths..
675 shrinkColumn : function()
677 console.log("htmleditor.BlockTd shrinkColumn");
678 var table = this.toTableArray();
680 this.normalizeWidths(table);
681 var col = this.cellData.col;
682 var nw = this.colWidths[col] * 0.8;
686 var otherAdd = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
687 this.colWidths.forEach(function(w,i) {
689 this.colWidths[i] = nw;
692 this.colWidths[i] += otherAdd
694 this.updateWidths(table);
697 growColumn : function()
699 var table = this.toTableArray();
700 this.normalizeWidths(table);
701 var col = this.cellData.col;
702 var nw = this.colWidths[col] * 1.2;
706 var otherSub = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
707 this.colWidths.forEach(function(w,i) {
709 this.colWidths[i] = nw;
712 this.colWidths[i] -= otherSub
714 this.updateWidths(table);
717 deleteRow : function()
719 // delete this rows 'tr'
720 // if any of the cells in this row have a rowspan > 1 && row!= this row..
721 // then reduce the rowspan.
722 var table = this.toTableArray();
723 // this.cellData.row;
724 for (var i =0;i< table[this.cellData.row].length ; i++) {
725 var c = table[this.cellData.row][i];
726 if (c.row != this.cellData.row) {
729 c.cell.setAttribute('rowspan', c.rowspan);
734 c.cell.setAttribute('rowspan', c.rowspan);
737 table.splice(this.cellData.row,1);
738 this.redrawAllCells(table);
741 deleteColumn : function()
743 var table = this.toTableArray();
745 for (var i =0;i< table.length ; i++) {
746 var c = table[i][this.cellData.col];
747 if (c.col != this.cellData.col) {
748 table[i][this.cellData.col].colspan--;
749 } else if (c.colspan > 1) {
751 c.cell.setAttribute('colspan', c.colspan);
753 table[i].splice(this.cellData.col,1);
756 this.redrawAllCells(table);