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()
415 var tab = this.node.closest('tr').closest('table');
416 Array.from(tab.rows).forEach(function(r, ri){
422 Array.from(tab.rows).forEach(function(r, ri){
425 Array.from(r.cells).forEach(function(ce, ci){
430 colspan : ce.colSpan,
433 if (ce.isEqualNode(this.node)) {
436 // if we have been filled up by a row?
437 if (typeof(ret[rn][cn]) != 'undefined') {
438 while(typeof(ret[rn][cn]) != 'undefined') {
444 if (typeof(this.colWidths[cn]) == 'undefined') {
445 this.colWidths[cn] = ce.style.width;
446 if (this.colWidths[cn] != '') {
452 if (c.colspan < 2 && c.rowspan < 2 ) {
457 for(var j = 0; j < c.rowspan; j++) {
458 if (typeof(ret[rn+j]) == 'undefined') {
459 continue; // we have a problem..
462 for(var i = 0; i < c.colspan; i++) {
472 // initalize widths.?
473 // either all widths or no widths..
475 this.colWidths[0] = false; // no widths flag.
486 mergeRight: function()
489 // get the contents of the next cell along..
490 var tr = this.node.closest('tr');
491 var i = Array.prototype.indexOf.call(tr.childNodes, this.node);
492 if (i >= tr.childNodes.length - 1) {
493 return; // no cells on right to merge with.
495 var table = this.toTableArray();
497 if (typeof(table[this.cellData.row][this.cellData.col+this.cellData.colspan]) == 'undefined') {
498 return; // nothing right?
500 var rc = table[this.cellData.row][this.cellData.col+this.cellData.colspan];
501 // right cell - must be same rowspan and on the same row.
502 if (rc.rowspan != this.cellData.rowspan || rc.row != this.cellData.row) {
503 return; // right hand side is not same rowspan.
508 this.node.innerHTML += ' ' + rc.cell.innerHTML;
509 tr.removeChild(rc.cell);
510 this.colspan += rc.colspan;
511 this.node.setAttribute('colspan', this.colspan);
516 mergeBelow : function()
518 var table = this.toTableArray();
519 if (typeof(table[this.cellData.row+this.cellData.rowspan]) == 'undefined') {
520 return; // no row below
522 if (typeof(table[this.cellData.row+this.cellData.rowspan][this.cellData.col]) == 'undefined') {
523 return; // nothing right?
525 var rc = table[this.cellData.row+this.cellData.rowspan][this.cellData.col];
527 if (rc.colspan != this.cellData.colspan || rc.col != this.cellData.col) {
528 return; // right hand side is not same rowspan.
530 this.node.innerHTML = this.node.innerHTML + rc.cell.innerHTML ;
531 rc.cell.parentNode.removeChild(rc.cell);
532 this.rowspan += rc.rowspan;
533 this.node.setAttribute('rowspan', this.rowspan);
538 if (this.node.rowSpan < 2 && this.node.colSpan < 2) {
541 var table = this.toTableArray();
542 var cd = this.cellData;
546 for(var r = cd.row; r < cd.row + cd.rowspan; r++) {
550 for(var c = cd.col; c < cd.col + cd.colspan; c++) {
551 if (r == cd.row && c == cd.col) {
552 this.node.removeAttribute('rowspan');
553 this.node.removeAttribute('colspan');
557 var ntd = this.node.cloneNode(); // which col/row should be 0..
558 ntd.removeAttribute('id'); //
559 //ntd.style.width = '';
561 table[r][c] = { cell : ntd, col : c, row: r , colspan : 1 , rowspan : 1 };
565 this.redrawAllCells(table);
573 redrawAllCells: function(table)
577 var tab = this.node.closest('tr').closest('table');
578 var ctr = tab.rows[0].parentNode;
579 Array.from(tab.rows).forEach(function(r, ri){
581 Array.from(r.cells).forEach(function(ce, ci){
582 ce.parentNode.removeChild(ce);
584 r.parentNode.removeChild(r);
586 for(var r = 0 ; r < table.length; r++) {
587 var re = tab.rows[r];
589 var re = tab.ownerDocument.createElement('tr');
591 for(var c = 0 ; c < table[r].length; c++) {
592 if (table[r][c].cell === false) {
596 re.appendChild(table[r][c].cell);
598 table[r][c].cell = false;
603 updateWidths : function(table)
605 for(var r = 0 ; r < table.length; r++) {
607 for(var c = 0 ; c < table[r].length; c++) {
608 if (table[r][c].cell === false) {
612 if (this.colWidths[0] != false && table[r][c].colspan < 2) {
613 var el = Roo.htmleditor.Block.factory(table[r][c].cell);
614 el.width = Math.floor(this.colWidths[c]) +'%';
615 el.updateElement(el.node);
617 table[r][c].cell = false; // done
621 normalizeWidths : function(table)
624 if (this.colWidths[0] === false) {
625 var nw = 100.0 / this.colWidths.length;
626 this.colWidths.forEach(function(w,i) {
627 this.colWidths[i] = nw;
632 var t = 0, missing = [];
634 this.colWidths.forEach(function(w,i) {
636 this.colWidths[i] = this.colWidths[i] == '' ? 0 : (this.colWidths[i]+'').replace(/[^0-9]+/g,'')*1;
637 var add = this.colWidths[i];
646 var nc = this.colWidths.length;
647 if (missing.length) {
648 var mult = (nc - missing.length) / (1.0 * nc);
650 var ew = (100 -t) / (1.0 * missing.length);
651 this.colWidths.forEach(function(w,i) {
653 this.colWidths[i] = w * mult;
657 this.colWidths[i] = ew;
659 // have to make up numbers..
662 // now we should have all the widths..
667 shrinkColumn : function()
669 var table = this.toTableArray();
670 this.normalizeWidths(table);
671 var col = this.cellData.col;
672 var nw = this.colWidths[col] * 0.8;
676 var otherAdd = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
677 this.colWidths.forEach(function(w,i) {
679 this.colWidths[i] = nw;
682 this.colWidths[i] += otherAdd
684 this.updateWidths(table);
687 growColumn : function()
689 var table = this.toTableArray();
690 this.normalizeWidths(table);
691 var col = this.cellData.col;
692 var nw = this.colWidths[col] * 1.2;
696 var otherSub = (this.colWidths[col] * 0.2) / (this.colWidths.length -1);
697 this.colWidths.forEach(function(w,i) {
699 this.colWidths[i] = nw;
702 this.colWidths[i] -= otherSub
704 this.updateWidths(table);
707 deleteRow : function()
709 // delete this rows 'tr'
710 // if any of the cells in this row have a rowspan > 1 && row!= this row..
711 // then reduce the rowspan.
712 var table = this.toTableArray();
713 // this.cellData.row;
714 for (var i =0;i< table[this.cellData.row].length ; i++) {
715 var c = table[this.cellData.row][i];
716 if (c.row != this.cellData.row) {
719 c.cell.setAttribute('rowspan', c.rowspan);
724 c.cell.setAttribute('rowspan', c.rowspan);
727 table.splice(this.cellData.row,1);
728 this.redrawAllCells(table);
731 deleteColumn : function()
733 var table = this.toTableArray();
735 for (var i =0;i< table.length ; i++) {
736 var c = table[i][this.cellData.col];
737 if (c.col != this.cellData.col) {
738 table[i][this.cellData.col].colspan--;
739 } else if (c.colspan > 1) {
741 c.cell.setAttribute('colspan', c.colspan);
743 table[i].splice(this.cellData.col,1);
746 this.redrawAllCells(table);