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");
62 var cell = function() {
63 return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode);
66 var table = function() {
67 return Roo.htmleditor.Block.factory(toolbar.tb.selectedNode.closest('table'));
71 var saveSel = function()
73 lr = toolbar.editorcore.getSelection().getRangeAt(0);
75 var restoreSel = function()
79 toolbar.editorcore.focus();
80 var cr = toolbar.editorcore.getSelection();
83 toolbar.editorcore.onEditorEvent();
90 var rooui = typeof(Roo.bootstrap) == 'undefined' ? Roo : Roo.bootstrap;
92 var syncValue = toolbar.editorcore.syncValue;
102 var t = toolbar.tb.selectedNode.closest('table');
103 toolbar.editorcore.selectNode(t);
104 toolbar.editorcore.onEditorEvent();
114 text : "Column Width: ",
122 click : function (_self, e)
124 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
125 cell().shrinkColumn();
127 toolbar.editorcore.onEditorEvent();
136 click : function (_self, e)
138 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
141 toolbar.editorcore.onEditorEvent();
149 text : "Vertical Align: ",
150 xns : rooui.Toolbar //Boostrap?
155 displayField : 'val',
158 triggerAction : 'all',
164 select : function (combo, r, index)
166 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
168 b.valign = r.get('val');
171 toolbar.editorcore.onEditorEvent();
176 xtype : 'SimpleStore',
180 ['bottom'] // there are afew more...
189 text : "Merge Cells: ",
199 click : function (_self, e)
201 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
203 //block().growColumn();
205 toolbar.editorcore.onEditorEvent();
215 click : function (_self, e)
217 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
219 //block().growColumn();
221 toolbar.editorcore.onEditorEvent();
237 click : function (_self, e)
239 //toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
242 toolbar.editorcore.selectNode(toolbar.tb.selectedNode);
243 toolbar.editorcore.onEditorEvent();
269 click : function (_self, e)
273 cell().deleteColumn();
275 toolbar.editorcore.selectNode(t.node);
276 toolbar.editorcore.onEditorEvent();
285 click : function (_self, e)
291 toolbar.editorcore.selectNode(t.node);
292 toolbar.editorcore.onEditorEvent();
306 click : function (_self, e)
309 var nn = t.node.nextSibling || t.node.previousSibling;
310 t.node.parentNode.removeChild(t.node);
312 toolbar.editorcore.selectNode(nn, true);
314 toolbar.editorcore.onEditorEvent();
332 * create a DomHelper friendly object - for use with
333 * Roo.DomHelper.markup / overwrite / etc..
334 * ?? should it be called with option to hide all editing features?
337 * create a DomHelper friendly object - for use with
338 * Roo.DomHelper.markup / overwrite / etc..
339 * ?? should it be called with option to hide all editing features?
341 toObject : function()
343 console.log("htmleditor.BlockTd toObject");
346 contenteditable : 'true', // this stops cell selection from picking the table.
348 valign : this.valign,
350 'text-align' : this.textAlign,
351 border : 'solid 1px rgb(0, 0, 0)', // ??? hard coded?
352 'border-collapse' : 'collapse',
353 padding : '6px', // 8 for desktop / 4 for mobile
354 'vertical-align': this.valign
358 if (this.width != '') {
359 ret.width = this.width;
360 ret.style.width = this.width;
364 if (this.colspan > 1) {
365 ret.colspan = this.colspan ;
367 if (this.rowspan > 1) {
368 ret.rowspan = this.rowspan ;
377 readElement : function(node)
379 console.log("htmleditor.BlockTd readElement");
381 node = node ? node : this.node ;
382 this.width = node.style.width;
383 this.colspan = Math.max(1,1*node.getAttribute('colspan'));
384 this.rowspan = Math.max(1,1*node.getAttribute('rowspan'));
385 this.html = node.innerHTML;
390 // the default cell object... at present...
391 emptyCell : function() {
396 html : " " // is this going to be editable now?
401 removeNode : function()
403 return this.node.closest('table');
411 toTableArray : function()
414 var tab = this.node.closest('tr').closest('table');
415 Array.from(tab.rows).forEach(function(r, ri){
421 Array.from(tab.rows).forEach(function(r, ri){
424 Array.from(r.cells).forEach(function(ce, ci){
429 colspan : ce.colSpan,
432 if (ce.isEqualNode(this.node)) {
435 // if we have been filled up by a row?
436 if (typeof(ret[rn][cn]) != 'undefined') {
437 while(typeof(ret[rn][cn]) != 'undefined') {
443 if (typeof(this.colWidths[cn]) == 'undefined') {
444 this.colWidths[cn] = ce.style.width;
445 if (this.colWidths[cn] != '') {
451 if (c.colspan < 2 && c.rowspan < 2 ) {
456 for(var j = 0; j < c.rowspan; j++) {
457 if (typeof(ret[rn+j]) == 'undefined') {
458 continue; // we have a problem..
461 for(var i = 0; i < c.colspan; i++) {
471 // initalize widths.?
472 // either all widths or no widths..
474 this.colWidths[0] = false; // no widths flag.
485 mergeRight: function()
488 // get the contents of the next cell along..
489 var tr = this.node.closest('tr');
490 var i = Array.prototype.indexOf.call(tr.childNodes, this.node);
491 if (i >= tr.childNodes.length - 1) {
492 return; // no cells on right to merge with.
494 var table = this.toTableArray();
496 if (typeof(table[this.cellData.row][this.cellData.col+this.cellData.colspan]) == 'undefined') {
497 return; // nothing right?
499 var rc = table[this.cellData.row][this.cellData.col+this.cellData.colspan];
500 // right cell - must be same rowspan and on the same row.
501 if (rc.rowspan != this.cellData.rowspan || rc.row != this.cellData.row) {
502 return; // right hand side is not same rowspan.
507 this.node.innerHTML += ' ' + rc.cell.innerHTML;
508 tr.removeChild(rc.cell);
509 this.colspan += rc.colspan;
510 this.node.setAttribute('colspan', this.colspan);
515 mergeBelow : function()
517 var table = this.toTableArray();
518 if (typeof(table[this.cellData.row+this.cellData.rowspan]) == 'undefined') {
519 return; // no row below
521 if (typeof(table[this.cellData.row+this.cellData.rowspan][this.cellData.col]) == 'undefined') {
522 return; // nothing right?
524 var rc = table[this.cellData.row+this.cellData.rowspan][this.cellData.col];
526 if (rc.colspan != this.cellData.colspan || rc.col != this.cellData.col) {
527 return; // right hand side is not same rowspan.
529 this.node.innerHTML = this.node.innerHTML + rc.cell.innerHTML ;
530 rc.cell.parentNode.removeChild(rc.cell);
531 this.rowspan += rc.rowspan;
532 this.node.setAttribute('rowspan', this.rowspan);
537 if (this.node.rowSpan < 2 && this.node.colSpan < 2) {
540 var table = this.toTableArray();
541 var cd = this.cellData;
545 for(var r = cd.row; r < cd.row + cd.rowspan; r++) {
549 for(var c = cd.col; c < cd.col + cd.colspan; c++) {
550 if (r == cd.row && c == cd.col) {
551 this.node.removeAttribute('rowspan');
552 this.node.removeAttribute('colspan');
556 var ntd = this.node.cloneNode(); // which col/row should be 0..
557 ntd.removeAttribute('id'); //
558 //ntd.style.width = '';
560 table[r][c] = { cell : ntd, col : c, row: r , colspan : 1 , rowspan : 1 };
564 this.redrawAllCells(table);
572 redrawAllCells: function(table)
576 var tab = this.node.closest('tr').closest('table');
577 var ctr = tab.rows[0].parentNode;
578 Array.from(tab.rows).forEach(function(r, ri){
580 Array.from(r.cells).forEach(function(ce, ci){
581 ce.parentNode.removeChild(ce);
583 r.parentNode.removeChild(r);
585 for(var r = 0 ; r < table.length; r++) {
586 var re = tab.rows[r];
588 var re = tab.ownerDocument.createElement('tr');
590 for(var c = 0 ; c < table[r].length; c++) {
591 if (table[r][c].cell === false) {
595 re.appendChild(table[r][c].cell);
597 table[r][c].cell = false;
602 updateWidths : function(table)
604 for(var r = 0 ; r < table.length; r++) {
606 for(var c = 0 ; c < table[r].length; c++) {
607 if (table[r][c].cell === false) {
611 if (this.colWidths[0] != false && table[r][c].colspan < 2) {
612 var el = Roo.htmleditor.Block.factory(table[r][c].cell);
613 el.width = Math.floor(this.colWidths[c]) +'%';
614 el.updateElement(el.node);
616 table[r][c].cell = false; // done
620 normalizeWidths : function(table)
623 if (this.colWidths[0] === false) {
624 var nw = 100.0 / this.colWidths.length;
625 this.colWidths.forEach(function(w,i) {
626 this.colWidths[i] = nw;
631 var t = 0, missing = [];
633 this.colWidths.forEach(function(w,i) {
635 this.colWidths[i] = this.colWidths[i] == '' ? 0 : (this.colWidths[i]+'').replace(/[^0-9]+/g,'')*1;
636 var add = this.colWidths[i];
645 var nc = this.colWidths.length;
646 if (missing.length) {
647 var mult = (nc - missing.length) / (1.0 * nc);
649 var ew = (100 -t) / (1.0 * missing.length);
650 this.colWidths.forEach(function(w,i) {
652 this.colWidths[i] = w * mult;
656 this.colWidths[i] = ew;
658 // have to make up numbers..
661 // now we should have all the widths..
666 shrinkColumn : function()
668 var table = this.toTableArray();
669 this.normalizeWidths(table);
670 var col = this.cellData.col;
671 var nw = this.colWidths[col] * 0.8;
675 var otherAdd = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
676 this.colWidths.forEach(function(w,i) {
678 this.colWidths[i] = nw;
681 this.colWidths[i] += otherAdd
683 this.updateWidths(table);
686 growColumn : function()
688 var table = this.toTableArray();
689 this.normalizeWidths(table);
690 var col = this.cellData.col;
691 var nw = this.colWidths[col] * 1.2;
695 var otherSub = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
696 this.colWidths.forEach(function(w,i) {
698 this.colWidths[i] = nw;
701 this.colWidths[i] -= otherSub
703 this.updateWidths(table);
706 deleteRow : function()
708 // delete this rows 'tr'
709 // if any of the cells in this row have a rowspan > 1 && row!= this row..
710 // then reduce the rowspan.
711 var table = this.toTableArray();
712 // this.cellData.row;
713 for (var i =0;i< table[this.cellData.row].length ; i++) {
714 var c = table[this.cellData.row][i];
715 if (c.row != this.cellData.row) {
718 c.cell.setAttribute('rowspan', c.rowspan);
723 c.cell.setAttribute('rowspan', c.rowspan);
726 table.splice(this.cellData.row,1);
727 this.redrawAllCells(table);
730 deleteColumn : function()
732 var table = this.toTableArray();
734 for (var i =0;i< table.length ; i++) {
735 var c = table[i][this.cellData.col];
736 if (c.col != this.cellData.col) {
737 table[i][this.cellData.col].colspan--;
738 } else if (c.colspan > 1) {
740 c.cell.setAttribute('colspan', c.colspan);
742 table[i].splice(this.cellData.col,1);
745 this.redrawAllCells(table);