📄 kupubasetools.js
字号:
var thead = heads[i]; for (var j=0; j < thead.childNodes.length; j++) { var tr = thead.childNodes[j]; if (tr.nodeType != 1) { continue; } var currindex = 0; for (var k=0; k < tr.childNodes.length; k++) { var th = tr.childNodes[k]; if (th.nodeType != 1) { continue; } if (currindex == currcolindex) { tr.removeChild(th); break; } currindex++; } } } } // now we remove the column field, a bit harder since we need to take // colspan and rowspan into account XXX Not right, fix theads as well var bodies = currtable.getElementsByTagName('TBODY'); for (var i=0; i < bodies.length; i++) { var currtbody = bodies[i]; var relevant_rowspan = 0; for (var j=0; j < currtbody.childNodes.length; j++) { var tr = currtbody.childNodes[j]; if (tr.nodeType != 1) { continue; } var currindex = 0 for (var k=0; k < tr.childNodes.length; k++) { var cell = tr.childNodes[k]; if (cell.nodeType != 1) { continue; } var colspan = cell.getAttribute('colspan'); if (currindex == currcolindex) { tr.removeChild(cell); break; } currindex++; } } } this.editor.logMessage('Table column deleted'); }; this.delTable = function() { /* delete the current table */ var currnode = this.editor.getSelectedNode(); var table = this.editor.getNearestParentOfType(currnode, 'table'); if (!table) { this.editor.logMessage('Not inside a table!'); return; }; table.parentNode.removeChild(table); this.editor.logMessage('Table removed'); }; this.setColumnAlign = function(newalign) { /* change the alignment of a full column */ var currnode = this.editor.getSelectedNode(); var currtd = this.editor.getNearestParentOfType(currnode, "TD"); var bodytype = 'tbody'; if (!currtd) { currtd = this.editor.getNearestParentOfType(currnode, "TH"); bodytype = 'thead'; } var currcolindex = this._getColIndex(currtd); var currtable = this.editor.getNearestParentOfType(currnode, "TABLE"); // unfortunately this is not enough to make the browsers display // the align, we need to set it on individual cells as well and // mind the rowspan... for (var i=0; i < currtable.childNodes.length; i++) { var currtbody = currtable.childNodes[i]; if (currtbody.nodeType != 1 || (currtbody.nodeName.toUpperCase() != "THEAD" && currtbody.nodeName.toUpperCase() != "TBODY")) { continue; } for (var j=0; j < currtbody.childNodes.length; j++) { var row = currtbody.childNodes[j]; if (row.nodeType != 1) { continue; } var index = 0; for (var k=0; k < row.childNodes.length; k++) { var cell = row.childNodes[k]; if (cell.nodeType != 1) { continue; } if (index == currcolindex) { if (this.editor.config.use_css) { cell.style.textAlign = newalign; } else { cell.setAttribute('align', newalign); } cell.className = 'align-' + newalign; } index++; } } } }; this.setTableClass = function(sel_class) { /* set the class for the table */ var currnode = this.editor.getSelectedNode(); var currtable = this.editor.getNearestParentOfType(currnode, 'TABLE'); if (currtable) { currtable.className = sel_class; } }; this._getColIndex = function(currcell) { /* Given a node, return an integer for which column it is */ var prevsib = currcell.previousSibling; var currcolindex = 0; while (prevsib) { if (prevsib.nodeType == 1 && (prevsib.tagName.toUpperCase() == "TD" || prevsib.tagName.toUpperCase() == "TH")) { var colspan = prevsib.getAttribute('colspan'); if (colspan) { currcolindex += parseInt(colspan); } else { currcolindex++; } } prevsib = prevsib.previousSibling; if (currcolindex > 30) { alert("Recursion detected when counting column position"); return; } } return currcolindex; }; this._getColumnAlign = function(selNode) { /* return the alignment setting of the current column */ var align; var td = this.editor.getNearestParentOfType(selNode, 'td'); if (!td) { td = this.editor.getNearestParentOfType(selNode, 'th'); }; if (td) { align = td.getAttribute('align'); if (this.editor.config.use_css) { align = td.style.textAlign; }; }; return align; }; this.fixTable = function(event) { /* fix the table so it can be processed by Kupu */ // since this can be quite a nasty creature we can't just use the // helper methods // first we create a new tbody element var currnode = this.editor.getSelectedNode(); var table = this.editor.getNearestParentOfType(currnode, 'TABLE'); if (!table) { this.editor.logMessage('Not inside a table!'); return; }; this._fixTableHelper(table); }; this._fixTableHelper = function(table) { /* the code to actually fix tables */ var doc = this.editor.getInnerDocument(); var tbody = doc.createElement('tbody'); var allowed_classes = new Array('plain', 'grid', 'list', 'listing', 'data'); if (!allowed_classes.contains(table.getAttribute('class'))) { table.setAttribute('class', 'plain'); }; table.setAttribute('cellpadding', '0'); table.setAttribute('cellspacing', '0'); // now get all the rows of the table, the rows can either be // direct descendants of the table or inside a 'tbody', 'thead' // or 'tfoot' element var rows = new Array(); var parents = new Array('thead', 'tbody', 'tfoot'); for (var i=0; i < table.childNodes.length; i++) { var node = table.childNodes[i]; if (node.nodeName.toLowerCase() == 'tr') { rows.push(node); } else if (parents.contains(node.nodeName.toLowerCase())) { for (var j=0; j < node.childNodes.length; j++) { var inode = node.childNodes[j]; if (inode.nodeName.toLowerCase() == 'tr') { rows.push(inode); }; }; }; }; // now find out how many cells our rows should have var numcols = 0; for (var i=0; i < rows.length; i++) { var row = rows[i]; var currnumcols = 0; for (var j=0; j < row.childNodes.length; j++) { var node = row.childNodes[j]; if (node.nodeName.toLowerCase() == 'td' || node.nodeName.toLowerCase() == 'th') { var colspan = 1; if (node.getAttribute('colSpan')) { colspan = parseInt(node.getAttribute('colSpan')); }; currnumcols += colspan; }; }; if (currnumcols > numcols) { numcols = currnumcols; }; }; // now walk through all rows to clean them up for (var i=0; i < rows.length; i++) { var row = rows[i]; var newrow = doc.createElement('tr'); var currcolnum = 0; while (row.childNodes.length > 0) { var node = row.childNodes[0]; if (node.nodeName.toLowerCase() != 'td' && node.nodeName.toLowerCase() != 'th') { row.removeChild(node); continue; }; node.removeAttribute('colSpan'); node.removeAttribute('rowSpan'); newrow.appendChild(node); }; if (newrow.childNodes.length) { tbody.appendChild(newrow); }; }; // now make sure all rows have the correct length for (var i=0; i < tbody.childNodes.length; i++) { var row = tbody.childNodes[i]; var cellname = row.childNodes[0].nodeName; while (row.childNodes.length < numcols) { var cell = doc.createElement(cellname); var nbsp = doc.createTextNode('\u00a0'); cell.appendChild(nbsp); row.appendChild(cell); }; }; // now remove all the old stuff from the table and add the new tbody var tlength = table.childNodes.length; for (var i=0; i < tlength; i++) { table.removeChild(table.childNodes[0]); }; table.appendChild(tbody); this.editor.getDocument().getWindow().focus(); this.editor.logMessage('Table cleaned up'); }; this.fixAllTables = function() { /* fix all the tables in the document at once */ var tables = this.editor.getInnerDocument().getElementsByTagName('table'); for (var i=0; i < tables.length; i++) { this._fixTableHelper(tables[i]); }; };};TableTool.prototype = new KupuTool;function TableToolBox(addtabledivid, edittabledivid, newrowsinputid, newcolsinputid, makeheaderinputid, classselectid, alignselectid, addtablebuttonid, addrowbuttonid, delrowbuttonid, addcolbuttonid, delcolbuttonid, fixbuttonid, fixallbuttonid, toolboxid, plainclass, activeclass) { /* The table tool */ // XXX There are some awfully long methods in here!! // a lot of dependencies on html elements here, but most implementations // will use them all I guess this.addtablediv = document.getElementById(addtabledivid); this.edittablediv = document.getElementById(edittabledivid); this.newrowsinput = document.getElementById(newrowsinputid); this.newcolsinput = document.getElementById(newcolsinputid); this.makeheaderinput = document.getElementById(makeheaderinputid); this.classselect = document.getElementById(classselectid); this.alignselect = document.getElementById(alignselectid); this.addtablebutton = document.getElementById(addtablebuttonid); this.addrowbutton = document.getElementById(addrowbuttonid); this.delrowbutton = document.getElementById(delrowbuttonid); this.addcolbutton = document.getElementById(addcolbuttonid); this.delcolbutton = document.getElementById(delcolbuttonid); this.fixbutton = document.getElementById(fixbuttonid); this.fixallbutton = document.getElementById(fixallbuttonid); this.toolboxel = document.getElementById(toolboxid); this.plainclass = plainclass; this.activeclass = activeclass; // register event handlers this.initialize = function(tool, editor) { /* attach the event handlers */ this.tool = tool; this.editor = editor; // build the select list of table classes if configured if (this.editor.config.table_classes) { var classes = this.editor.config.table_classes['class']; while (this.classselect.hasChildNodes()) { this.classselect.removeChild(this.classselect.firstChild); }; for (var i=0; i < classes.length; i++) { var classname = classes[i]; var option = document.createElement('option'); var content = document.createTextNode(classname); option.appendChild(content); option.setAttribute('value', classname); this.classselect.appendChild(option); }; }; addEventHandler(this.addtablebutton, "click", this.addTable, this); addEventHandler(this.addrowbutton, "click", this.tool.addTableRow, this.tool); addEventHandler(this.delrowbutton, "click", this.tool.delTableRow, this.tool); addEventHandler(this.addcolbutton, "click", this.tool.addTableColumn, this.tool); addEventHandler(this.delcolbutton, "click", this.tool.delTableColumn, this.tool); addEventHandler(this.alignselect, "change", this.setColumnAlign, this); addEventHandler(this.classselect, "change", this.setTableClass, this); addEventHandler(this.fixbutton, "click", this.tool.fixTable, this.tool); addEventHandler(this.fixallbutton, "click", this.tool.fixAllTables, this.tool); this.addtablediv.style.display = "block"; this.edittablediv.style.display = "none"; this.editor.logMessage('Table tool initialized'); }; this.updateState = function(selNode) { /* update the state (add/edit) and update the pulldowns (if required) */ var table = this.editor.getNearestParentOfType(selNode, 'table'); if (table) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -