wysihtml.commands.addTableCells={exec:function(a,b,c){if(a.tableSelection&&a.tableSelection.start&&a.tableSelection.end){var d=wysihtml.dom.table.orderSelectionEnds(a.tableSelection.start,a.tableSelection.end);"before"==c||"above"==c?wysihtml.dom.table.addCells(d.start,c):("after"==c||"below"==c)&&wysihtml.dom.table.addCells(d.end,c),setTimeout(function(){a.tableSelection.select(d.start,d.end)},0)}},state:function(a,b){return!1}},wysihtml.commands.createTable={exec:function(a,b,c){var d,e,f;if(c&&c.cols&&c.rows&&parseInt(c.cols,10)>0&&parseInt(c.rows,10)>0){for(f=c.tableStyle?'
':"",f+="",e=0;e",d=0;d
";f+=""}f+="
",a.commands.exec("insertHTML",f)}},state:function(a,b){return!1}},wysihtml.commands.deleteTableCells={exec:function(a,b,c){if(a.tableSelection&&a.tableSelection.start&&a.tableSelection.end){var d,e=wysihtml.dom.table.orderSelectionEnds(a.tableSelection.start,a.tableSelection.end),f=wysihtml.dom.table.indexOf(e.start),g=a.tableSelection.table;wysihtml.dom.table.removeCells(e.start,c),setTimeout(function(){d=wysihtml.dom.table.findCell(g,f),d||("row"==c&&(d=wysihtml.dom.table.findCell(g,{row:f.row-1,col:f.col})),"column"==c&&(d=wysihtml.dom.table.findCell(g,{row:f.row,col:f.col-1}))),d&&a.tableSelection.select(d,d)},0)}},state:function(a,b){return!1}},wysihtml.commands.mergeTableCells={exec:function(a,b){a.tableSelection&&a.tableSelection.start&&a.tableSelection.end&&(this.state(a,b)?wysihtml.dom.table.unmergeCell(a.tableSelection.start):wysihtml.dom.table.mergeCellsBetween(a.tableSelection.start,a.tableSelection.end))},state:function(a,b){if(a.tableSelection){var c=a.tableSelection.start,d=a.tableSelection.end;if(c&&d&&c==d&&(wysihtml.dom.getAttribute(c,"colspan")&&parseInt(wysihtml.dom.getAttribute(c,"colspan"),10)>1||wysihtml.dom.getAttribute(c,"rowspan")&&parseInt(wysihtml.dom.getAttribute(c,"rowspan"),10)>1))return[c]}return!1}},function(){function a(a,b){for(var c,d=[],e=0,f=a.length;f>e;e++)if(c=a[e].querySelectorAll(b))for(var g=c.length;g--;d.unshift(c[g]));return d}function b(a){a.parentNode.removeChild(a)}function d(a,b){a.parentNode.insertBefore(b,a.nextSibling)}function e(a,b){for(var c=a.nextSibling;1!=c.nodeType;)if(c=c.nextSibling,!b||b==c.tagName.toLowerCase())return c;return null}var f=wysihtml.dom,g=function(a){this.el=a,this.isColspan=!1,this.isRowspan=!1,this.firstCol=!0,this.lastCol=!0,this.firstRow=!0,this.lastRow=!0,this.isReal=!0,this.spanCollection=[],this.modified=!1},h=function(a,b){a?(this.cell=a,this.table=f.getParentElement(a,{query:"table"})):b&&(this.table=b,this.cell=this.table.querySelectorAll("th, td")[0])};h.prototype={addSpannedCellToMap:function(a,b,c,d,e,f){for(var h=[],i=c+(f?parseInt(f,10)-1:0),j=d+(e?parseInt(e,10)-1:0),k=c;i>=k;k++){"undefined"==typeof b[k]&&(b[k]=[]);for(var l=d;j>=l;l++)b[k][l]=new g(a),b[k][l].isColspan=e&&parseInt(e,10)>1,b[k][l].isRowspan=f&&parseInt(f,10)>1,b[k][l].firstCol=l==d,b[k][l].lastCol=l==j,b[k][l].firstRow=k==c,b[k][l].lastRow=k==i,b[k][l].isReal=l==d&&k==c,b[k][l].spanCollection=h,h.push(b[k][l])}},setCellAsModified:function(a){if(a.modified=!0,a.spanCollection.length>0)for(var b=0,c=a.spanCollection.length;c>b;b++)a.spanCollection[b].modified=!0},setTableMap:function(){var a,b,c,d,e,h,i,j,k=[],l=this.getTableRows();for(a=0;a0?wysihtml.lang.array(e).without(d):e;return f},getTableRows:function(){var b=this.table.querySelectorAll("table"),c=b?a(b,"tr"):[],d=this.table.querySelectorAll("tr"),e=c.length>0?wysihtml.lang.array(d).without(c):d;return e},getMapIndex:function(a){for(var b=this.map.length,c=this.map&&this.map[0]?this.map[0].length:0,d=0;b>d;d++)for(var e=0;c>e;e++)if(this.map[d][e].el===a)return{row:d,col:e};return!1},getElementAtIndex:function(a){return this.setTableMap(),this.map[a.row]&&this.map[a.row][a.col]&&this.map[a.row][a.col].el?this.map[a.row][a.col].el:null},getMapElsTo:function(a){var b=[];if(this.setTableMap(),this.idx_start=this.getMapIndex(this.cell),this.idx_end=this.getMapIndex(a),this.idx_start.row>this.idx_end.row||this.idx_start.row==this.idx_end.row&&this.idx_start.col>this.idx_end.col){var c=this.idx_start;this.idx_start=this.idx_end,this.idx_end=c}if(this.idx_start.col>this.idx_end.col){var d=this.idx_start.col;this.idx_start.col=this.idx_end.col,this.idx_end.col=d}if(null!=this.idx_start&&null!=this.idx_end)for(var e=this.idx_start.row,f=this.idx_end.row;f>=e;e++)for(var g=this.idx_start.col,h=this.idx_end.col;h>=g;g++)b.push(this.map[e][g].el);return b},orderSelectionEnds:function(a){if(this.setTableMap(),this.idx_start=this.getMapIndex(this.cell),this.idx_end=this.getMapIndex(a),this.idx_start.row>this.idx_end.row||this.idx_start.row==this.idx_end.row&&this.idx_start.col>this.idx_end.col){var b=this.idx_start;this.idx_start=this.idx_end,this.idx_end=b}if(this.idx_start.col>this.idx_end.col){var c=this.idx_start.col;this.idx_start.col=this.idx_end.col,this.idx_end.col=c}return{start:this.map[this.idx_start.row][this.idx_start.col].el,end:this.map[this.idx_end.row][this.idx_end.col].el}},createCells:function(a,b,c){for(var d,e=this.table.ownerDocument,f=e.createDocumentFragment(),g=0;b>g;g++){if(d=e.createElement(a),c)for(var h in c)c.hasOwnProperty(h)&&d.setAttribute(h,c[h]);d.appendChild(document.createTextNode(" ")),f.appendChild(d)}return f},correctColIndexForUnreals:function(a,b){for(var c=this.map[b],d=-1,e=0;a>e;e++)c[e].isReal&&d++;return d},getLastNewCellOnRow:function(a,b){for(var c,d,e=this.getRowCells(a),f=0,g=e.length;g>f;f++)if(c=e[f],d=this.getMapIndex(c),d===!1||"undefined"!=typeof b&&d.row!=b)return c;return null},removeEmptyTable:function(){var a=this.table.querySelectorAll("td, th");return a&&0!=a.length?!1:(b(this.table),!0)},splitRowToCells:function(a){if(a.isColspan){var b=parseInt(f.getAttribute(a.el,"colspan")||1,10),c=a.el.tagName.toLowerCase();if(b>1){var e=this.createCells(c,b-1);d(a.el,e)}a.el.removeAttribute("colspan")}},getRealRowEl:function(a,b){var c=null,d=null;b=b||this.idx;for(var e=0,g=this.map[b.row].length;g>e;e++)if(d=this.map[b.row][e],d.isReal&&(c=f.getParentElement(d.el,{query:"tr"})))return c;return null===c&&a&&(c=f.getParentElement(this.map[b.row][b.col].el,{query:"tr"})||null),c},injectRowAt:function(a,b,c,e,g){var h=this.getRealRowEl(!1,{row:a,col:b}),i=this.createCells(e,c);if(h){var j=this.correctColIndexForUnreals(b,a);j>=0?d(this.getRowCells(h)[j],i):h.insertBefore(i,h.firstChild)}else{var k=this.table.ownerDocument.createElement("tr");k.appendChild(i),d(f.getParentElement(g.el,{query:"tr"}),k)}},canMerge:function(a){if(this.to=a,this.setTableMap(),this.idx_start=this.getMapIndex(this.cell),this.idx_end=this.getMapIndex(this.to),this.idx_start.row>this.idx_end.row||this.idx_start.row==this.idx_end.row&&this.idx_start.col>this.idx_end.col){var b=this.idx_start;this.idx_start=this.idx_end,this.idx_end=b}if(this.idx_start.col>this.idx_end.col){var c=this.idx_start.col;this.idx_start.col=this.idx_end.col,this.idx_end.col=c}for(var d=this.idx_start.row,e=this.idx_end.row;e>=d;d++)for(var f=this.idx_start.col,g=this.idx_end.col;g>=f;f++)if(this.map[d][f].isColspan||this.map[d][f].isRowspan)return!1;return!0},decreaseCellSpan:function(a,b){var c=parseInt(f.getAttribute(a.el,b),10)-1;c>=1?a.el.setAttribute(b,c):(a.el.removeAttribute(b),"colspan"==b&&(a.isColspan=!1),"rowspan"==b&&(a.isRowspan=!1),a.firstCol=!0,a.lastCol=!0,a.firstRow=!0,a.lastRow=!0,a.isReal=!0)},removeSurplusLines:function(){var a,c,d,e,g,h,i;if(this.setTableMap(),this.map){for(d=0,e=this.map.length;e>d;d++){for(a=this.map[d],i=!0,g=0,h=a.length;h>g;g++)if(c=a[g],!(f.getAttribute(c.el,"rowspan")&&parseInt(f.getAttribute(c.el,"rowspan"),10)>1&&c.firstRow!==!0)){i=!1;break}if(i)for(g=0;h>g;g++)this.decreaseCellSpan(a[g],"rowspan")}var j=this.getTableRows();for(d=0,e=j.length;e>d;d++)a=j[d],0==a.childNodes.length&&/^\s*$/.test(a.textContent||a.innerText)&&b(a)}},fillMissingCells:function(){var a=0,b=0,c=null;if(this.setTableMap(),this.map){a=this.map.length;for(var e=0;a>e;e++)this.map[e].length>b&&(b=this.map[e].length);for(var f=0;a>f;f++)for(var h=0;b>h;h++)this.map[f]&&!this.map[f][h]&&h>0&&(this.map[f][h]=new g(this.createCells("td",1)),c=this.map[f][h-1],c&&c.el&&c.el.parent&&d(this.map[f][h-1].el,this.map[f][h].el))}},rectify:function(){return this.removeEmptyTable()?!1:(this.removeSurplusLines(),this.fillMissingCells(),!0)},unmerge:function(){if(this.rectify()&&(this.setTableMap(),this.idx=this.getMapIndex(this.cell),this.idx)){var a=this.map[this.idx.row][this.idx.col],b=f.getAttribute(a.el,"colspan")?parseInt(f.getAttribute(a.el,"colspan"),10):1,c=a.el.tagName.toLowerCase();if(a.isRowspan){var d=parseInt(f.getAttribute(a.el,"rowspan"),10);if(d>1)for(var e=1,g=d-1;g>=e;e++)this.injectRowAt(this.idx.row+e,this.idx.col,b,c,a);a.el.removeAttribute("rowspan")}this.splitRowToCells(a)}},merge:function(a){if(this.rectify())if(this.canMerge(a)){for(var c=this.idx_end.row-this.idx_start.row+1,d=this.idx_end.col-this.idx_start.col+1,e=this.idx_start.row,f=this.idx_end.row;f>=e;e++)for(var g=this.idx_start.col,h=this.idx_end.col;h>=g;g++)e==this.idx_start.row&&g==this.idx_start.col?(c>1&&this.map[e][g].el.setAttribute("rowspan",c),d>1&&this.map[e][g].el.setAttribute("colspan",d)):(/^\s*
\s*$/.test(this.map[e][g].el.innerHTML.toLowerCase())||(this.map[this.idx_start.row][this.idx_start.col].el.innerHTML+=" "+this.map[e][g].el.innerHTML),b(this.map[e][g].el));this.rectify()}else window.console&&console.log("Do not know how to merge allready merged cells.")},collapseCellToNextRow:function(a){var b=this.getMapIndex(a.el),c=b.row+1,e={row:c,col:b.col};if(c=0)d(this.getRowCells(g)[h],a.el);else{var i=this.getLastNewCellOnRow(g,c);null!==i?d(i,a.el):g.insertBefore(a.el,g.firstChild)}parseInt(f.getAttribute(a.el,"rowspan"),10)>2?a.el.setAttribute("rowspan",parseInt(f.getAttribute(a.el,"rowspan"),10)-1):a.el.removeAttribute("rowspan")}}},removeRowCell:function(a){a.isReal?a.isRowspan?this.collapseCellToNextRow(a):b(a.el):parseInt(f.getAttribute(a.el,"rowspan"),10)>2?a.el.setAttribute("rowspan",parseInt(f.getAttribute(a.el,"rowspan"),10)-1):a.el.removeAttribute("rowspan")},getRowElementsByCell:function(){var a=[];if(this.setTableMap(),this.idx=this.getMapIndex(this.cell),this.idx!==!1)for(var b=this.map[this.idx.row],c=0,d=b.length;d>c;c++)b[c].isReal&&a.push(b[c].el);return a},getColumnElementsByCell:function(){var a=[];if(this.setTableMap(),this.idx=this.getMapIndex(this.cell),this.idx!==!1)for(var b=0,c=this.map.length;c>b;b++)this.map[b][this.idx.col]&&this.map[b][this.idx.col].isReal&&a.push(this.map[b][this.idx.col].el);return a},removeRow:function(){var a=f.getParentElement(this.cell,{query:"tr"});if(a){if(this.setTableMap(),this.idx=this.getMapIndex(this.cell),this.idx!==!1)for(var c=this.map[this.idx.row],d=0,e=c.length;e>d;d++)c[d].modified||(this.setCellAsModified(c[d]),this.removeRowCell(c[d]));b(a)}},removeColCell:function(a){a.isColspan?parseInt(f.getAttribute(a.el,"colspan"),10)>2?a.el.setAttribute("colspan",parseInt(f.getAttribute(a.el,"colspan"),10)-1):a.el.removeAttribute("colspan"):a.isReal&&b(a.el)},removeColumn:function(){if(this.setTableMap(),this.idx=this.getMapIndex(this.cell),this.idx!==!1)for(var a=0,b=this.map.length;b>a;a++)this.map[a][this.idx.col].modified||(this.setCellAsModified(this.map[a][this.idx.col]),this.removeColCell(this.map[a][this.idx.col]))},remove:function(a){if(this.rectify()){switch(a){case"row":this.removeRow();break;case"column":this.removeColumn()}this.rectify()}},addRow:function(a){var b=this.table.ownerDocument;if(this.setTableMap(),this.idx=this.getMapIndex(this.cell),"below"==a&&f.getAttribute(this.cell,"rowspan")&&(this.idx.row=this.idx.row+parseInt(f.getAttribute(this.cell,"rowspan"),10)-1),this.idx!==!1){for(var c=this.map[this.idx.row],e=b.createElement("tr"),g=0,h=c.length;h>g;g++)c[g].modified||(this.setCellAsModified(c[g]),this.addRowCell(c[g],e,a));switch(a){case"below":d(this.getRealRowEl(!0),e);break;case"above":var i=f.getParentElement(this.map[this.idx.row][this.idx.col].el,{query:"tr"});i&&i.parentNode.insertBefore(e,i)}}},addRowCell:function(a,b,d){var e=a.isColspan?{colspan:f.getAttribute(a.el,"colspan")}:null;a.isReal?"above"!=d&&a.isRowspan?a.el.setAttribute("rowspan",parseInt(f.getAttribute(a.el,"rowspan"),10)+1):b.appendChild(this.createCells("td",1,e)):"above"!=d&&a.isRowspan&&a.lastRow?b.appendChild(this.createCells("td",1,e)):c.isRowspan&&a.el.attr("rowspan",parseInt(f.getAttribute(a.el,"rowspan"),10)+1)},add:function(a){this.rectify()&&(("below"==a||"above"==a)&&this.addRow(a),("before"==a||"after"==a)&&this.addColumn(a))},addColCell:function(a,b,c){var e,g=a.el.tagName.toLowerCase();switch(c){case"before":e=!a.isColspan||a.firstCol;break;case"after":e=!a.isColspan||a.lastCol||a.isColspan&&a.el==this.cell}if(e){switch(c){case"before":a.el.parentNode.insertBefore(this.createCells(g,1),a.el);break;case"after":d(a.el,this.createCells(g,1))}a.isRowspan&&this.handleCellAddWithRowspan(a,b+1,c)}else a.el.setAttribute("colspan",parseInt(f.getAttribute(a.el,"colspan"),10)+1)},addColumn:function(a){var b,c;if(this.setTableMap(),this.idx=this.getMapIndex(this.cell),"after"==a&&f.getAttribute(this.cell,"colspan")&&(this.idx.col=this.idx.col+parseInt(f.getAttribute(this.cell,"colspan"),10)-1),this.idx!==!1)for(var d=0,e=this.map.length;e>d;d++)b=this.map[d],b[this.idx.col]&&(c=b[this.idx.col],c.modified||(this.setCellAsModified(c),this.addColCell(c,d,a)))},handleCellAddWithRowspan:function(a,b,c){for(var g,h,i,j=parseInt(f.getAttribute(this.cell,"rowspan"),10)-1,k=f.getParentElement(a.el,{query:"tr"}),l=a.el.tagName.toLowerCase(),m=this.table.ownerDocument,n=0;j>n;n++)if(g=this.correctColIndexForUnreals(this.idx.col,b+n),k=e(k,"tr"))if(g>0)switch(c){case"before":h=this.getRowCells(k),g>0&&this.map[b+n][this.idx.col].el!=h[g]&&g==h.length-1?d(h[g],this.createCells(l,1)):h[g].parentNode.insertBefore(this.createCells(l,1),h[g]);break;case"after":d(this.getRowCells(k)[g],this.createCells(l,1))}else k.insertBefore(this.createCells(l,1),k.firstChild);else i=m.createElement("tr"),i.appendChild(this.createCells(l,1)),this.table.appendChild(i)}},f.table={getCellsBetween:function(a,b){var c=new h(a);return c.getMapElsTo(b)},addCells:function(a,b){var c=new h(a);c.add(b)},removeCells:function(a,b){var c=new h(a);c.remove(b)},mergeCellsBetween:function(a,b){var c=new h(a);c.merge(b)},unmergeCell:function(a){var b=new h(a);b.unmerge()},orderSelectionEnds:function(a,b){var c=new h(a);return c.orderSelectionEnds(b)},indexOf:function(a){var b=new h(a);return b.setTableMap(),b.getMapIndex(a)},findCell:function(a,b){var c=new h(null,a);return c.getElementAtIndex(b)},findRowByCell:function(a){var b=new h(a);return b.getRowElementsByCell()},findColumnByCell:function(a){var b=new h(a);return b.getColumnElementsByCell()},canMerge:function(a,b){var c=new h(a);return c.canMerge(b)}}}(),function(){var a=wysihtml.views.Composer.prototype.observe,b=function(){a.call(this),this.config.handleTables&&c.call(this)},c=function(){var a=function(){this.win.removeEventListener("load",a),this.doc.execCommand("enableObjectResizing",!1,"false"),this.doc.execCommand("enableInlineTableEditing",!1,"false")}.bind(this),b=function(){a.call(this),this.actions.removeListeners(this.sandbox.getIframe(),["focus","mouseup","mouseover"],b)}.bind(this);this.doc.execCommand&&wysihtml.browser.supportsCommand(this.doc,"enableObjectResizing")&&wysihtml.browser.supportsCommand(this.doc,"enableInlineTableEditing")&&(this.sandbox.getIframe?this.actions.addListeners(this.sandbox.getIframe(),["focus","mouseup","mouseover"],b):this.win.addEventListener("load",a)),this.tableSelection=wysihtml.quirks.tableCellsSelection(this.element,this.parent)},d=function(a,b){var c=function(){return a.addEventListener("mousedown",d),n},d=function(b){var c=wysihtml.dom.getParentElement(b.target,{query:"td, th"},!1,a);c&&e(c)},e=function(c){n.start=c,n.end=c,n.cells=[c],n.table=m.getParentElement(n.start,{query:"table"},!1,a),n.table&&(f(),m.addClass(c,o),a.addEventListener("mousemove",h),a.addEventListener("mouseup",i),b.fire("tableselectstart").fire("tableselectstart:composer"))},f=function(){if(a){var b=a.querySelectorAll("."+o);if(b.length>0)for(var c=0;c1&&b.composer.selection.deselect(),g(n.cells),n.end!==d&&b.fire("tableselectchange").fire("tableselectchange:composer")))},i=function(c){a.removeEventListener("mousemove",h),a.removeEventListener("mouseup",i),b.fire("tableselect").fire("tableselect:composer"),setTimeout(function(){k()},0)},j=function(c){a.ownerDocument.removeEventListener("click",j),m.getParentElement(c.target,{query:"table"},!1,a)!=n.table&&(f(),n.table=null,n.start=null,n.end=null,b.fire("tableunselect").fire("tableunselect:composer"))},k=function(){a.ownerDocument.addEventListener("click",j)},l=function(c,d){n.start=c,n.end=d,n.table=m.getParentElement(n.start,{query:"table"},!1,a),selectedCells=m.table.getCellsBetween(n.start,n.end),g(selectedCells),k(),b.fire("tableselect").fire("tableselect:composer")},m=wysihtml.dom,n={table:null,start:null,end:null,cells:null,select:l},o="wysiwyg-tmp-selected-cell";return c()};wysihtml.Editor.prototype.defaults.handleTables=!0,wysihtml.quirks.tableCellsSelection=d,wysihtml.views.Composer.prototype.observe=b}();