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)
61 var cell = function() {
62 return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode);
65 var table = function() {
66 return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode.closest('table'));
70 var saveSel = function()
72 lr = toolbar.editorcore.getSelection().getRangeAt(0);
74 var restoreSel = function()
78 toolbar.editorcore.focus();
79 var cr = toolbar.editorcore.getSelection();
82 toolbar.editorcore.onEditorEvent();
89 var rooui = typeof(Roo.bootstrap) == 'undefined' ? Roo : Roo.bootstrap;
91 var syncValue = toolbar.editorcore.syncValue;
101 var t = toolbar.tb.selectedNode.closest('table');
102 toolbar.editorcore.selectNode(t);
103 toolbar.editorcore.onEditorEvent();
113 text : "Column Width: ",
121 click : function (_self, e)
123 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
124 cell().shrinkColumn();
126 toolbar.editorcore.onEditorEvent();
135 click : function (_self, e)
137 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
140 toolbar.editorcore.onEditorEvent();
148 text : "Vertical Align: ",
149 xns : rooui.Toolbar //Boostrap?
154 displayField : 'val',
157 triggerAction : 'all',
163 select : function (combo, r, index)
165 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
167 b.valign = r.get('val');
170 toolbar.editorcore.onEditorEvent();
175 xtype : 'SimpleStore',
179 ['bottom'] // there are afew more...
188 text : "Merge Cells: ",
198 click : function (_self, e)
200 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
202 //block().growColumn();
204 toolbar.editorcore.onEditorEvent();
214 click : function (_self, e)
216 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
218 //block().growColumn();
220 toolbar.editorcore.onEditorEvent();
236 click : function (_self, e)
238 //toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
241 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
242 toolbar.editorcore.onEditorEvent();
268 click : function (_self, e)
272 cell().deleteColumn();
274 toolbar.editorcore.selectNode(t.node);
275 toolbar.editorcore.onEditorEvent();
284 click : function (_self, e)
290 toolbar.editorcore.selectNode(t.node);
291 toolbar.editorcore.onEditorEvent();
305 click : function (_self, e)
308 var nn = t.node.nextSibling || t.node.previousSibling;
309 t.node.parentNode.removeChild(t.node);
311 toolbar.editorcore.selectNode(nn, true);
313 toolbar.editorcore.onEditorEvent();
331 * create a DomHelper friendly object - for use with
332 * Roo.DomHelper.markup / overwrite / etc..
333 * ?? should it be called with option to hide all editing features?
336 * create a DomHelper friendly object - for use with
337 * Roo.DomHelper.markup / overwrite / etc..
338 * ?? should it be called with option to hide all editing features?
340 toObject : function()
345 contenteditable : 'true', // this stops cell selection from picking the table.
347 valign : this.valign,
349 'text-align' : this.textAlign,
350 border : 'solid 1px rgb(0, 0, 0)', // ??? hard coded?
351 'border-collapse' : 'collapse',
352 padding : '6px', // 8 for desktop / 4 for mobile
353 'vertical-align': this.valign
357 if (this.width != '') {
358 ret.width = this.width;
359 ret.style.width = this.width;
363 if (this.colspan > 1) {
364 ret.colspan = this.colspan ;
366 if (this.rowspan > 1) {
367 ret.rowspan = this.rowspan ;
376 readElement : function(node)
378 node = node ? node : this.node ;
379 this.width = node.style.width;
380 this.colspan = Math.max(1,1*node.getAttribute('colspan'));
381 this.rowspan = Math.max(1,1*node.getAttribute('rowspan'));
382 this.html = node.innerHTML;
387 // the default cell object... at present...
388 emptyCell : function() {
393 html : " " // is this going to be editable now?
398 removeNode : function()
400 return this.node.closest('table');
408 toTableArray : function()
411 var tab = this.node.closest('tr').closest('table');
412 Array.from(tab.rows).forEach(function(r, ri){
418 Array.from(tab.rows).forEach(function(r, ri){
421 Array.from(r.cells).forEach(function(ce, ci){
426 colspan : ce.colSpan,
429 if (ce.isEqualNode(this.node)) {
432 // if we have been filled up by a row?
433 if (typeof(ret[rn][cn]) != 'undefined') {
434 while(typeof(ret[rn][cn]) != 'undefined') {
440 if (typeof(this.colWidths[cn]) == 'undefined') {
441 this.colWidths[cn] = ce.style.width;
442 if (this.colWidths[cn] != '') {
448 if (c.colspan < 2 && c.rowspan < 2 ) {
453 for(var j = 0; j < c.rowspan; j++) {
454 if (typeof(ret[rn+j]) == 'undefined') {
455 continue; // we have a problem..
458 for(var i = 0; i < c.colspan; i++) {
468 // initalize widths.?
469 // either all widths or no widths..
471 this.colWidths[0] = false; // no widths flag.
482 mergeRight: function()
485 // get the contents of the next cell along..
486 var tr = this.node.closest('tr');
487 var i = Array.prototype.indexOf.call(tr.childNodes, this.node);
488 if (i >= tr.childNodes.length - 1) {
489 return; // no cells on right to merge with.
491 var table = this.toTableArray();
493 if (typeof(table[this.cellData.row][this.cellData.col+this.cellData.colspan]) == 'undefined') {
494 return; // nothing right?
496 var rc = table[this.cellData.row][this.cellData.col+this.cellData.colspan];
497 // right cell - must be same rowspan and on the same row.
498 if (rc.rowspan != this.cellData.rowspan || rc.row != this.cellData.row) {
499 return; // right hand side is not same rowspan.
504 this.node.innerHTML += ' ' + rc.cell.innerHTML;
505 tr.removeChild(rc.cell);
506 this.colspan += rc.colspan;
507 this.node.setAttribute('colspan', this.colspan);
512 mergeBelow : function()
514 var table = this.toTableArray();
515 if (typeof(table[this.cellData.row+this.cellData.rowspan]) == 'undefined') {
516 return; // no row below
518 if (typeof(table[this.cellData.row+this.cellData.rowspan][this.cellData.col]) == 'undefined') {
519 return; // nothing right?
521 var rc = table[this.cellData.row+this.cellData.rowspan][this.cellData.col];
523 if (rc.colspan != this.cellData.colspan || rc.col != this.cellData.col) {
524 return; // right hand side is not same rowspan.
526 this.node.innerHTML = this.node.innerHTML + rc.cell.innerHTML ;
527 rc.cell.parentNode.removeChild(rc.cell);
528 this.rowspan += rc.rowspan;
529 this.node.setAttribute('rowspan', this.rowspan);
534 if (this.node.rowSpan < 2 && this.node.colSpan < 2) {
537 var table = this.toTableArray();
538 var cd = this.cellData;
542 for(var r = cd.row; r < cd.row + cd.rowspan; r++) {
545 for(var c = cd.col; c < cd.col + cd.colspan; c++) {
546 if (r == cd.row && c == cd.col) {
547 this.node.removeAttribute('rowspan');
548 this.node.removeAttribute('colspan');
552 var ntd = this.node.cloneNode(); // which col/row should be 0..
553 ntd.removeAttribute('id'); //
554 //ntd.style.width = '';
556 table[r][c] = { cell : ntd, col : c, row: r , colspan : 1 , rowspan : 1 };
560 this.redrawAllCells(table);
568 redrawAllCells: function(table)
572 var tab = this.node.closest('tr').closest('table');
573 var ctr = tab.rows[0].parentNode;
574 Array.from(tab.rows).forEach(function(r, ri){
576 Array.from(r.cells).forEach(function(ce, ci){
577 ce.parentNode.removeChild(ce);
579 r.parentNode.removeChild(r);
581 for(var r = 0 ; r < table.length; r++) {
582 var re = tab.rows[r];
584 var re = tab.ownerDocument.createElement('tr');
586 for(var c = 0 ; c < table[r].length; c++) {
587 if (table[r][c].cell === false) {
591 re.appendChild(table[r][c].cell);
593 table[r][c].cell = false;
598 updateWidths : function(table)
600 for(var r = 0 ; r < table.length; r++) {
602 for(var c = 0 ; c < table[r].length; c++) {
603 if (table[r][c].cell === false) {
607 if (this.colWidths[0] != false && table[r][c].colspan < 2) {
608 var el = Roo.htmleditor.Block.factory(table[r][c].cell);
609 el.width = Math.floor(this.colWidths[c]) +'%';
610 el.updateElement(el.node);
612 table[r][c].cell = false; // done
616 normalizeWidths : function(table)
619 if (this.colWidths[0] === false) {
620 var nw = 100.0 / this.colWidths.length;
621 this.colWidths.forEach(function(w,i) {
622 this.colWidths[i] = nw;
627 var t = 0, missing = [];
629 this.colWidths.forEach(function(w,i) {
631 this.colWidths[i] = this.colWidths[i] == '' ? 0 : (this.colWidths[i]+'').replace(/[^0-9]+/g,'')*1;
632 var add = this.colWidths[i];
641 var nc = this.colWidths.length;
642 if (missing.length) {
643 var mult = (nc - missing.length) / (1.0 * nc);
645 var ew = (100 -t) / (1.0 * missing.length);
646 this.colWidths.forEach(function(w,i) {
648 this.colWidths[i] = w * mult;
652 this.colWidths[i] = ew;
654 // have to make up numbers..
657 // now we should have all the widths..
662 shrinkColumn : function()
664 var table = this.toTableArray();
665 this.normalizeWidths(table);
666 var col = this.cellData.col;
667 var nw = this.colWidths[col] * 0.8;
671 var otherAdd = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
672 this.colWidths.forEach(function(w,i) {
674 this.colWidths[i] = nw;
677 this.colWidths[i] += otherAdd
679 this.updateWidths(table);
682 growColumn : function()
684 var table = this.toTableArray();
685 this.normalizeWidths(table);
686 var col = this.cellData.col;
687 var nw = this.colWidths[col] * 1.2;
691 var otherSub = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
692 this.colWidths.forEach(function(w,i) {
694 this.colWidths[i] = nw;
697 this.colWidths[i] -= otherSub
699 this.updateWidths(table);
702 deleteRow : function()
704 // delete this rows 'tr'
705 // if any of the cells in this row have a rowspan > 1 && row!= this row..
706 // then reduce the rowspan.
707 var table = this.toTableArray();
708 // this.cellData.row;
709 for (var i =0;i< table[this.cellData.row].length ; i++) {
710 var c = table[this.cellData.row][i];
711 if (c.row != this.cellData.row) {
714 c.cell.setAttribute('rowspan', c.rowspan);
719 c.cell.setAttribute('rowspan', c.rowspan);
722 table.splice(this.cellData.row,1);
723 this.redrawAllCells(table);
726 deleteColumn : function()
728 var table = this.toTableArray();
730 for (var i =0;i< table.length ; i++) {
731 var c = table[i][this.cellData.col];
732 if (c.col != this.cellData.col) {
733 table[i][this.cellData.col].colspan--;
734 } else if (c.colspan > 1) {
736 c.cell.setAttribute('colspan', c.colspan);
738 table[i].splice(this.cellData.col,1);
741 this.redrawAllCells(table);