-pg电子官方网址入口

/******************************************************************************* * kindeditor - wysiwyg html editor for internet * pg电子平台网站 copyright (c) 2006-2011 kindsoft.net * * @author roddy * @site http://www.kindsoft.net/ * @licence http://www.kindsoft.net/license.php *******************************************************************************/ kindeditor.plugin('table', function(k) { var self = this, name = 'table', lang = self.lang(name '.'), zeroborder = 'ke-zeroborder'; // 设置颜色 function _setcolor(box, color) { color = color.touppercase(); box.css('background-color', color); box.css('color', color === '#000000' ? '#ffffff' : '#000000'); box.html(color); } // 初始化取色器 var pickerlist = []; function _initcolorpicker(dialogdiv, colorbox) { colorbox.bind('click,mousedown', function(e){ e.stoppropagation(); }); function removepicker() { k.each(pickerlist, function() { this.remove(); }); pickerlist = []; k(document).unbind('click,mousedown', removepicker); dialogdiv.unbind('click,mousedown', removepicker); } colorbox.click(function(e) { removepicker(); var box = k(this), pos = box.pos(); var picker = k.colorpicker({ x : pos.x, y : pos.y box.height(), z : 811214, selectedcolor : k(this).html(), colors : self.colortable, nocolor : self.lang('nocolor'), shadowmode : self.shadowmode, click : function(color) { _setcolor(box, color); removepicker(); } }); pickerlist.push(picker); k(document).bind('click,mousedown', removepicker); dialogdiv.bind('click,mousedown', removepicker); }); } // 取得下一行cell的index function _getcellindex(table, row, cell) { var rowspancount = 0; for (var i = 0, len = row.cells.length; i < len; i ) { if (row.cells[i] == cell) { break; } rowspancount = row.cells[i].rowspan - 1; } return cell.cellindex - rowspancount; } self.plugin.table = { //insert or modify table prop : function(isinsert) { var html = [ '
', //rows, cols '
', '', lang.rows '   ', lang.cols ' ', '
', //width, height '
', '', lang.width '   ', '   ', lang.height '   ', '', '
', //space, padding '
', '', lang.padding '   ', lang.spacing ' ', '
', //align '
', '', '', '
', //border '
', '', lang.borderwidth '   ', lang.bordercolor ' ', '
', //background color '
', '', '', '
', '
' ].join(''); var bookmark = self.cmd.range.createbookmark(); var dialog = self.createdialog({ name : name, width : 500, title : self.lang(name), body : html, beforeremove : function() { colorbox.unbind(); }, yesbtn : { name : self.lang('yes'), click : function(e) { var rows = rowsbox.val(), cols = colsbox.val(), width = widthbox.val(), height = heightbox.val(), widthtype = widthtypebox.val(), heighttype = heighttypebox.val(), padding = paddingbox.val(), spacing = spacingbox.val(), align = alignbox.val(), border = borderbox.val(), bordercolor = k(colorbox[0]).html() || '', bgcolor = k(colorbox[1]).html() || ''; if (rows == 0 || !/^\d $/.test(rows)) { alert(self.lang('invalidrows')); rowsbox[0].focus(); return; } if (cols == 0 || !/^\d $/.test(cols)) { alert(self.lang('invalidrows')); colsbox[0].focus(); return; } if (!/^\d*$/.test(width)) { alert(self.lang('invalidwidth')); widthbox[0].focus(); return; } if (!/^\d*$/.test(height)) { alert(self.lang('invalidheight')); heightbox[0].focus(); return; } if (!/^\d*$/.test(padding)) { alert(self.lang('invalidpadding')); paddingbox[0].focus(); return; } if (!/^\d*$/.test(spacing)) { alert(self.lang('invalidspacing')); spacingbox[0].focus(); return; } if (!/^\d*$/.test(border)) { alert(self.lang('invalidborder')); borderbox[0].focus(); return; } //modify table if (table) { if (width !== '') { table.width(width widthtype); } else { table.css('width', ''); } if (table[0].width !== undefined) { table.removeattr('width'); } if (height !== '') { table.height(height heighttype); } else { table.css('height', ''); } if (table[0].height !== undefined) { table.removeattr('height'); } table.css('background-color', bgcolor); if (table[0].bgcolor !== undefined) { table.removeattr('bgcolor'); } if (padding !== '') { table[0].cellpadding = padding; } else { table.removeattr('cellpadding'); } if (spacing !== '') { table[0].cellspacing = spacing; } else { table.removeattr('cellspacing'); } if (align !== '') { table[0].align = align; } else { table.removeattr('align'); } if (border !== '') { table.attr('border', border); } else { table.removeattr('border'); } if (border === '' || border === '0') { table.addclass(zeroborder); } else { table.removeclass(zeroborder); } if (bordercolor !== '') { table.attr('bordercolor', bordercolor); } else { table.removeattr('bordercolor'); } self.hidedialog().focus(); self.cmd.range.movetobookmark(bookmark); self.cmd.select(); self.addbookmark(); return; } //insert new table var style = ''; if (width !== '') { style = 'width:' width widthtype ';'; } if (height !== '') { style = 'height:' height heighttype ';'; } if (bgcolor !== '') { style = 'background-color:' bgcolor ';'; } var html = '') ''; } html = ''; } html = ''; if (!k.ie) { html = '
'; } self.inserthtml(html); self.select().hidedialog().focus(); self.addbookmark(); } } }), div = dialog.div, rowsbox = k('[name="rows"]', div).val(3), colsbox = k('[name="cols"]', div).val(2), widthbox = k('[name="width"]', div).val(100), heightbox = k('[name="height"]', div), widthtypebox = k('[name="widthtype"]', div), heighttypebox = k('[name="heighttype"]', div), paddingbox = k('[name="padding"]', div).val(2), spacingbox = k('[name="spacing"]', div).val(0), alignbox = k('[name="align"]', div), borderbox = k('[name="border"]', div).val(1), colorbox = k('.ke-input-color', div); _initcolorpicker(div, colorbox.eq(0)); _initcolorpicker(div, colorbox.eq(1)); _setcolor(colorbox.eq(0), '#000000'); _setcolor(colorbox.eq(1), ''); // foucs and select rowsbox[0].focus(); rowsbox[0].select(); var table; if (isinsert) { return; } //get selected table node table = self.plugin.getselectedtable(); if (table) { rowsbox.val(table[0].rows.length); colsbox.val(table[0].rows.length > 0 ? table[0].rows[0].cells.length : 0); rowsbox.attr('disabled', true); colsbox.attr('disabled', true); var match, tablewidth = table[0].style.width || table[0].width, tableheight = table[0].style.height || table[0].height; if (tablewidth !== undefined && (match = /^(\d )((?:px|%)*)$/.exec(tablewidth))) { widthbox.val(match[1]); widthtypebox.val(match[2]); } else { widthbox.val(''); } if (tableheight !== undefined && (match = /^(\d )((?:px|%)*)$/.exec(tableheight))) { heightbox.val(match[1]); heighttypebox.val(match[2]); } paddingbox.val(table[0].cellpadding || ''); spacingbox.val(table[0].cellspacing || ''); alignbox.val(table[0].align || ''); borderbox.val(table[0].border === undefined ? '' : table[0].border); _setcolor(colorbox.eq(0), k.tohex(table.attr('bordercolor') || '')); _setcolor(colorbox.eq(1), k.tohex(table[0].style.backgroundcolor || table[0].bgcolor || '')); widthbox[0].focus(); widthbox[0].select(); } }, //modify cell cellprop : function() { var html = [ '
', //width, height '
', '', lang.width '   ', '   ', lang.height '   ', '', '
', //align '
', '', lang.textalign ' ', lang.verticalalign ' ', '
', //border '
', '', lang.borderwidth '   ', lang.bordercolor ' ', '
', //background color '
', '', '', '
', '
' ].join(''); var bookmark = self.cmd.range.createbookmark(); var dialog = self.createdialog({ name : name, width : 500, title : self.lang('tablecell'), body : html, beforeremove : function() { colorbox.unbind(); }, yesbtn : { name : self.lang('yes'), click : function(e) { var width = widthbox.val(), height = heightbox.val(), widthtype = widthtypebox.val(), heighttype = heighttypebox.val(), padding = paddingbox.val(), spacing = spacingbox.val(), textalign = textalignbox.val(), verticalalign = verticalalignbox.val(), border = borderbox.val(), bordercolor = k(colorbox[0]).html() || '', bgcolor = k(colorbox[1]).html() || ''; if (!/^\d*$/.test(width)) { alert(self.lang('invalidwidth')); widthbox[0].focus(); return; } if (!/^\d*$/.test(height)) { alert(self.lang('invalidheight')); heightbox[0].focus(); return; } if (!/^\d*$/.test(border)) { alert(self.lang('invalidborder')); borderbox[0].focus(); return; } cell.css({ width : width !== '' ? (width widthtype) : '', height : height !== '' ? (height heighttype) : '', 'background-color' : bgcolor, 'text-align' : textalign, 'vertical-align' : verticalalign, 'border-width' : border, 'border-style' : border !== '' ? 'solid' : '', 'border-color' : bordercolor }); self.hidedialog().focus(); self.cmd.range.movetobookmark(bookmark); self.cmd.select(); self.addbookmark(); } } }), div = dialog.div, widthbox = k('[name="width"]', div).val(100), heightbox = k('[name="height"]', div), widthtypebox = k('[name="widthtype"]', div), heighttypebox = k('[name="heighttype"]', div), paddingbox = k('[name="padding"]', div).val(2), spacingbox = k('[name="spacing"]', div).val(0), textalignbox = k('[name="textalign"]', div), verticalalignbox = k('[name="verticalalign"]', div), borderbox = k('[name="border"]', div).val(1), colorbox = k('.ke-input-color', div); _initcolorpicker(div, colorbox.eq(0)); _initcolorpicker(div, colorbox.eq(1)); _setcolor(colorbox.eq(0), '#000000'); _setcolor(colorbox.eq(1), ''); // foucs and select widthbox[0].focus(); widthbox[0].select(); // get selected cell var cell = self.plugin.getselectedcell(); var match, cellwidth = cell[0].style.width || cell[0].width || '', cellheight = cell[0].style.height || cell[0].height || ''; if ((match = /^(\d )((?:px|%)*)$/.exec(cellwidth))) { widthbox.val(match[1]); widthtypebox.val(match[2]); } else { widthbox.val(''); } if ((match = /^(\d )((?:px|%)*)$/.exec(cellheight))) { heightbox.val(match[1]); heighttypebox.val(match[2]); } textalignbox.val(cell[0].style.textalign || ''); verticalalignbox.val(cell[0].style.verticalalign || ''); var border = cell[0].style.borderwidth || ''; if (border) { border = parseint(border); } borderbox.val(border); _setcolor(colorbox.eq(0), k.tohex(cell[0].style.bordercolor || '')); _setcolor(colorbox.eq(1), k.tohex(cell[0].style.backgroundcolor || '')); widthbox[0].focus(); widthbox[0].select(); }, insert : function() { this.prop(true); }, 'delete' : function() { var table = self.plugin.getselectedtable(); self.cmd.range.setstartbefore(table[0]).collapse(true); self.cmd.select(); table.remove(); self.addbookmark(); }, colinsert : function(offset) { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], index = cell.cellindex offset; // 取得第一行的index index = table.rows[0].cells.length - row.cells.length; for (var i = 0, len = table.rows.length; i < len; i ) { var newrow = table.rows[i], newcell = newrow.insertcell(index); newcell.innerhtml = k.ie ? '' : '
'; // 调整下一行的单元格index index = _getcellindex(table, newrow, newcell); } self.cmd.range.selectnodecontents(cell).collapse(true); self.cmd.select(); self.addbookmark(); }, colinsertleft : function() { this.colinsert(0); }, colinsertright : function() { this.colinsert(1); }, rowinsert : function(offset) { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0]; var rowindex = row.rowindex; if (offset === 1) { rowindex = row.rowindex (cell.rowspan - 1) offset; } var newrow = table.insertrow(rowindex); for (var i = 0, len = row.cells.length; i < len; i ) { // 调整cell个数 if (row.cells[i].rowspan > 1) { len -= row.cells[i].rowspan - 1; } var newcell = newrow.insertcell(i); // copy colspan if (offset === 1 && row.cells[i].colspan > 1) { newcell.colspan = row.cells[i].colspan; } newcell.innerhtml = k.ie ? '' : '
'; } // 调整rowspan for (var j = rowindex; j >= 0; j--) { var cells = table.rows[j].cells; if (cells.length > i) { for (var k = cell.cellindex; k >= 0; k--) { if (cells[k].rowspan > 1) { cells[k].rowspan = 1; } } break; } } self.cmd.range.selectnodecontents(cell).collapse(true); self.cmd.select(); self.addbookmark(); }, rowinsertabove : function() { this.rowinsert(0); }, rowinsertbelow : function() { this.rowinsert(1); }, rowmerge : function() { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], rowindex = row.rowindex, // 当前行的index nextrowindex = rowindex cell.rowspan, // 下一行的index nextrow = table.rows[nextrowindex]; // 下一行 // 最后一行不能合并 if (table.rows.length <= nextrowindex) { return; } var cellindex = cell.cellindex; // 下一行单元格的index if (nextrow.cells.length <= cellindex) { return; } var nextcell = nextrow.cells[cellindex]; // 下一行单元格 // 上下行的colspan不一致时不能合并 if (cell.colspan !== nextcell.colspan) { return; } cell.rowspan = nextcell.rowspan; nextrow.deletecell(cellindex); self.cmd.range.selectnodecontents(cell).collapse(true); self.cmd.select(); self.addbookmark(); }, colmerge : function() { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], rowindex = row.rowindex, // 当前行的index cellindex = cell.cellindex, nextcellindex = cellindex 1; // 最后一列不能合并 if (row.cells.length <= nextcellindex) { return; } var nextcell = row.cells[nextcellindex]; // 左右列的rowspan不一致时不能合并 if (cell.rowspan !== nextcell.rowspan) { return; } cell.colspan = nextcell.colspan; row.deletecell(nextcellindex); self.cmd.range.selectnodecontents(cell).collapse(true); self.cmd.select(); self.addbookmark(); }, rowsplit : function() { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], rowindex = row.rowindex; // 不是可分割单元格 if (cell.rowspan === 1) { return; } var cellindex = _getcellindex(table, row, cell); for (var i = 1, len = cell.rowspan; i < len; i ) { var newrow = table.rows[rowindex i], newcell = newrow.insertcell(cellindex); if (cell.colspan > 1) { newcell.colspan = cell.colspan; } newcell.innerhtml = k.ie ? '' : '
'; // 调整下一行的单元格index cellindex = _getcellindex(table, newrow, newcell); } k(cell).removeattr('rowspan'); self.cmd.range.selectnodecontents(cell).collapse(true); self.cmd.select(); self.addbookmark(); }, colsplit : function() { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], cellindex = cell.cellindex; // 不是可分割单元格 if (cell.colspan === 1) { return; } for (var i = 1, len = cell.colspan; i < len; i ) { var newcell = row.insertcell(cellindex i); if (cell.rowspan > 1) { newcell.rowspan = cell.rowspan; } newcell.innerhtml = k.ie ? '' : '
'; } k(cell).removeattr('colspan'); self.cmd.range.selectnodecontents(cell).collapse(true); self.cmd.select(); self.addbookmark(); }, coldelete : function() { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], index = cell.cellindex; for (var i = 0, len = table.rows.length; i < len; i ) { var newrow = table.rows[i], newcell = newrow.cells[index]; if (newcell.colspan > 1) { newcell.colspan -= 1; if (newcell.colspan === 1) { k(newcell).removeattr('colspan'); } } else { newrow.deletecell(index); } // 跳过不需要删除的行 if (newcell.rowspan > 1) { i = newcell.rowspan - 1; } } if (row.cells.length === 0) { self.cmd.range.setstartbefore(table).collapse(true); self.cmd.select(); k(table).remove(); } else { self.cmd.selection(true); } self.addbookmark(); }, rowdelete : function() { var table = self.plugin.getselectedtable()[0], row = self.plugin.getselectedrow()[0], cell = self.plugin.getselectedcell()[0], rowindex = row.rowindex; // 从下到上删除 for (var i = cell.rowspan - 1; i >= 0; i--) { table.deleterow(rowindex i); } if (table.rows.length === 0) { self.cmd.range.setstartbefore(table).collapse(true); self.cmd.select(); k(table).remove(); } else { self.cmd.selection(true); } self.addbookmark(); } }; self.clicktoolbar(name, self.plugin.table.prop); });
网站地图