UU跑腿标准版

table.js 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*******************************************************************************
  2. * KindEditor - WYSIWYG HTML Editor for Internet
  3. * Copyright (C) 2006-2011 kindsoft.net
  4. *
  5. * @author Roddy <luolonghao@gmail.com>
  6. * @site http://www.kindsoft.net/
  7. * @licence http://www.kindsoft.net/license.php
  8. *******************************************************************************/
  9. KindEditor.plugin('table', function(K) {
  10. var self = this, name = 'table', lang = self.lang(name + '.'), zeroborder = 'ke-zeroborder';
  11. // 设置颜色
  12. function _setColor(box, color) {
  13. color = color.toUpperCase();
  14. box.css('background-color', color);
  15. box.css('color', color === '#000000' ? '#FFFFFF' : '#000000');
  16. box.html(color);
  17. }
  18. // 初始化取色器
  19. var pickerList = [];
  20. function _initColorPicker(dialogDiv, colorBox) {
  21. colorBox.bind('click,mousedown', function(e){
  22. e.stopPropagation();
  23. });
  24. function removePicker() {
  25. K.each(pickerList, function() {
  26. this.remove();
  27. });
  28. pickerList = [];
  29. K(document).unbind('click,mousedown', removePicker);
  30. dialogDiv.unbind('click,mousedown', removePicker);
  31. }
  32. colorBox.click(function(e) {
  33. removePicker();
  34. var box = K(this),
  35. pos = box.pos();
  36. var picker = K.colorpicker({
  37. x : pos.x,
  38. y : pos.y + box.height(),
  39. z : 811214,
  40. selectedColor : K(this).html(),
  41. colors : self.colorTable,
  42. noColor : self.lang('noColor'),
  43. shadowMode : self.shadowMode,
  44. click : function(color) {
  45. _setColor(box, color);
  46. removePicker();
  47. }
  48. });
  49. pickerList.push(picker);
  50. K(document).bind('click,mousedown', removePicker);
  51. dialogDiv.bind('click,mousedown', removePicker);
  52. });
  53. }
  54. // 取得下一行cell的index
  55. function _getCellIndex(table, row, cell) {
  56. var rowSpanCount = 0;
  57. for (var i = 0, len = row.cells.length; i < len; i++) {
  58. if (row.cells[i] == cell) {
  59. break;
  60. }
  61. rowSpanCount += row.cells[i].rowSpan - 1;
  62. }
  63. return cell.cellIndex - rowSpanCount;
  64. }
  65. self.plugin.table = {
  66. //insert or modify table
  67. prop : function(isInsert) {
  68. var html = [
  69. '<div style="padding:10px 20px;">',
  70. //rows, cols
  71. '<div class="ke-dialog-row">',
  72. '<label for="keRows" style="width:90px;">' + lang.cells + '</label>',
  73. lang.rows + ' <input type="text" id="keRows" class="ke-input-text ke-input-number" name="rows" value="" maxlength="4" /> &nbsp; ',
  74. lang.cols + ' <input type="text" class="ke-input-text ke-input-number" name="cols" value="" maxlength="4" />',
  75. '</div>',
  76. //width, height
  77. '<div class="ke-dialog-row">',
  78. '<label for="keWidth" style="width:90px;">' + lang.size + '</label>',
  79. lang.width + ' <input type="text" id="keWidth" class="ke-input-text ke-input-number" name="width" value="" maxlength="4" /> &nbsp; ',
  80. '<select name="widthType">',
  81. '<option value="%">' + lang.percent + '</option>',
  82. '<option value="px">' + lang.px + '</option>',
  83. '</select> &nbsp; ',
  84. lang.height + ' <input type="text" class="ke-input-text ke-input-number" name="height" value="" maxlength="4" /> &nbsp; ',
  85. '<select name="heightType">',
  86. '<option value="%">' + lang.percent + '</option>',
  87. '<option value="px">' + lang.px + '</option>',
  88. '</select>',
  89. '</div>',
  90. //space, padding
  91. '<div class="ke-dialog-row">',
  92. '<label for="kePadding" style="width:90px;">' + lang.space + '</label>',
  93. lang.padding + ' <input type="text" id="kePadding" class="ke-input-text ke-input-number" name="padding" value="" maxlength="4" /> &nbsp; ',
  94. lang.spacing + ' <input type="text" class="ke-input-text ke-input-number" name="spacing" value="" maxlength="4" />',
  95. '</div>',
  96. //align
  97. '<div class="ke-dialog-row">',
  98. '<label for="keAlign" style="width:90px;">' + lang.align + '</label>',
  99. '<select id="keAlign" name="align">',
  100. '<option value="">' + lang.alignDefault + '</option>',
  101. '<option value="left">' + lang.alignLeft + '</option>',
  102. '<option value="center">' + lang.alignCenter + '</option>',
  103. '<option value="right">' + lang.alignRight + '</option>',
  104. '</select>',
  105. '</div>',
  106. //border
  107. '<div class="ke-dialog-row">',
  108. '<label for="keBorder" style="width:90px;">' + lang.border + '</label>',
  109. lang.borderWidth + ' <input type="text" id="keBorder" class="ke-input-text ke-input-number" name="border" value="" maxlength="4" /> &nbsp; ',
  110. lang.borderColor + ' <span class="ke-inline-block ke-input-color"></span>',
  111. '</div>',
  112. //background color
  113. '<div class="ke-dialog-row">',
  114. '<label for="keBgColor" style="width:90px;">' + lang.backgroundColor + '</label>',
  115. '<span class="ke-inline-block ke-input-color"></span>',
  116. '</div>',
  117. '</div>'
  118. ].join('');
  119. var dialog = self.createDialog({
  120. name : name,
  121. width : 500,
  122. height : 300,
  123. title : self.lang(name),
  124. body : html,
  125. beforeRemove : function() {
  126. colorBox.unbind();
  127. },
  128. yesBtn : {
  129. name : self.lang('yes'),
  130. click : function(e) {
  131. var rows = rowsBox.val(),
  132. cols = colsBox.val(),
  133. width = widthBox.val(),
  134. height = heightBox.val(),
  135. widthType = widthTypeBox.val(),
  136. heightType = heightTypeBox.val(),
  137. padding = paddingBox.val(),
  138. spacing = spacingBox.val(),
  139. align = alignBox.val(),
  140. border = borderBox.val(),
  141. borderColor = K(colorBox[0]).html() || '',
  142. bgColor = K(colorBox[1]).html() || '';
  143. if (rows == 0 || !/^\d+$/.test(rows)) {
  144. alert(self.lang('invalidRows'));
  145. rowsBox[0].focus();
  146. return;
  147. }
  148. if (cols == 0 || !/^\d+$/.test(cols)) {
  149. alert(self.lang('invalidRows'));
  150. colsBox[0].focus();
  151. return;
  152. }
  153. if (!/^\d*$/.test(width)) {
  154. alert(self.lang('invalidWidth'));
  155. widthBox[0].focus();
  156. return;
  157. }
  158. if (!/^\d*$/.test(height)) {
  159. alert(self.lang('invalidHeight'));
  160. heightBox[0].focus();
  161. return;
  162. }
  163. if (!/^\d*$/.test(padding)) {
  164. alert(self.lang('invalidPadding'));
  165. paddingBox[0].focus();
  166. return;
  167. }
  168. if (!/^\d*$/.test(spacing)) {
  169. alert(self.lang('invalidSpacing'));
  170. spacingBox[0].focus();
  171. return;
  172. }
  173. if (!/^\d*$/.test(border)) {
  174. alert(self.lang('invalidBorder'));
  175. borderBox[0].focus();
  176. return;
  177. }
  178. //modify table
  179. if (table) {
  180. if (width !== '') {
  181. table.width(width + widthType);
  182. } else {
  183. table.css('width', '');
  184. }
  185. if (table[0].width !== undefined) {
  186. table.removeAttr('width');
  187. }
  188. if (height !== '') {
  189. table.height(height + heightType);
  190. } else {
  191. table.css('height', '');
  192. }
  193. if (table[0].height !== undefined) {
  194. table.removeAttr('height');
  195. }
  196. table.css('background-color', bgColor);
  197. if (table[0].bgColor !== undefined) {
  198. table.removeAttr('bgColor');
  199. }
  200. if (padding !== '') {
  201. table[0].cellPadding = padding;
  202. } else {
  203. table.removeAttr('cellPadding');
  204. }
  205. if (spacing !== '') {
  206. table[0].cellSpacing = spacing;
  207. } else {
  208. table.removeAttr('cellSpacing');
  209. }
  210. if (align !== '') {
  211. table[0].align = align;
  212. } else {
  213. table.removeAttr('align');
  214. }
  215. if (border !== '') {
  216. table.attr('border', border);
  217. } else {
  218. table.removeAttr('border');
  219. }
  220. if (border === '' || border === '0') {
  221. table.addClass(zeroborder);
  222. } else {
  223. table.removeClass(zeroborder);
  224. }
  225. if (borderColor !== '') {
  226. table.attr('borderColor', borderColor);
  227. } else {
  228. table.removeAttr('borderColor');
  229. }
  230. self.hideDialog().focus();
  231. return;
  232. }
  233. //insert new table
  234. var style = '';
  235. if (width !== '') {
  236. style += 'width:' + width + widthType + ';';
  237. }
  238. if (height !== '') {
  239. style += 'height:' + height + heightType + ';';
  240. }
  241. if (bgColor !== '') {
  242. style += 'background-color:' + bgColor + ';';
  243. }
  244. var html = '<table';
  245. if (style !== '') {
  246. html += ' style="' + style + '"';
  247. }
  248. if (padding !== '') {
  249. html += ' cellpadding="' + padding + '"';
  250. }
  251. if (spacing !== '') {
  252. html += ' cellspacing="' + spacing + '"';
  253. }
  254. if (align !== '') {
  255. html += ' align="' + align + '"';
  256. }
  257. if (border !== '') {
  258. html += ' border="' + border + '"';
  259. }
  260. if (border === '' || border === '0') {
  261. html += ' class="' + zeroborder + '"';
  262. }
  263. if (borderColor !== '') {
  264. html += ' bordercolor="' + borderColor + '"';
  265. }
  266. html += '>';
  267. for (var i = 0; i < rows; i++) {
  268. html += '<tr>';
  269. for (var j = 0; j < cols; j++) {
  270. html += '<td>' + (K.IE ? '&nbsp;' : '<br />') + '</td>';
  271. }
  272. html += '</tr>';
  273. }
  274. html += '</table>';
  275. if (!K.IE) {
  276. html += '<br />';
  277. }
  278. self.insertHtml(html);
  279. self.select().hideDialog().focus();
  280. self.addBookmark();
  281. }
  282. }
  283. }),
  284. div = dialog.div,
  285. rowsBox = K('[name="rows"]', div).val(3),
  286. colsBox = K('[name="cols"]', div).val(2),
  287. widthBox = K('[name="width"]', div).val(100),
  288. heightBox = K('[name="height"]', div),
  289. widthTypeBox = K('[name="widthType"]', div),
  290. heightTypeBox = K('[name="heightType"]', div),
  291. paddingBox = K('[name="padding"]', div).val(2),
  292. spacingBox = K('[name="spacing"]', div).val(0),
  293. alignBox = K('[name="align"]', div),
  294. borderBox = K('[name="border"]', div).val(1),
  295. colorBox = K('.ke-input-color', div);
  296. _initColorPicker(div, colorBox.eq(0));
  297. _initColorPicker(div, colorBox.eq(1));
  298. _setColor(colorBox.eq(0), '#000000');
  299. _setColor(colorBox.eq(1), '');
  300. // foucs and select
  301. rowsBox[0].focus();
  302. rowsBox[0].select();
  303. var table;
  304. if (isInsert) {
  305. return;
  306. }
  307. //get selected table node
  308. table = self.plugin.getSelectedTable();
  309. if (table) {
  310. rowsBox.val(table[0].rows.length);
  311. colsBox.val(table[0].rows.length > 0 ? table[0].rows[0].cells.length : 0);
  312. rowsBox.attr('disabled', true);
  313. colsBox.attr('disabled', true);
  314. var match,
  315. tableWidth = table[0].style.width || table[0].width,
  316. tableHeight = table[0].style.height || table[0].height;
  317. if (tableWidth !== undefined && (match = /^(\d+)((?:px|%)*)$/.exec(tableWidth))) {
  318. widthBox.val(match[1]);
  319. widthTypeBox.val(match[2]);
  320. } else {
  321. widthBox.val('');
  322. }
  323. if (tableHeight !== undefined && (match = /^(\d+)((?:px|%)*)$/.exec(tableHeight))) {
  324. heightBox.val(match[1]);
  325. heightTypeBox.val(match[2]);
  326. }
  327. paddingBox.val(table[0].cellPadding || '');
  328. spacingBox.val(table[0].cellSpacing || '');
  329. alignBox.val(table[0].align || '');
  330. borderBox.val(table[0].border === undefined ? '' : table[0].border);
  331. _setColor(colorBox.eq(0), K.toHex(table.attr('borderColor') || ''));
  332. _setColor(colorBox.eq(1), K.toHex(table[0].style.backgroundColor || table[0].bgColor || ''));
  333. widthBox[0].focus();
  334. widthBox[0].select();
  335. }
  336. },
  337. //modify cell
  338. cellprop : function() {
  339. var html = [
  340. '<div style="padding:10px 20px;">',
  341. //width, height
  342. '<div class="ke-dialog-row">',
  343. '<label for="keWidth" style="width:90px;">' + lang.size + '</label>',
  344. lang.width + ' <input type="text" id="keWidth" class="ke-input-text ke-input-number" name="width" value="" maxlength="4" /> &nbsp; ',
  345. '<select name="widthType">',
  346. '<option value="%">' + lang.percent + '</option>',
  347. '<option value="px">' + lang.px + '</option>',
  348. '</select> &nbsp; ',
  349. lang.height + ' <input type="text" class="ke-input-text ke-input-number" name="height" value="" maxlength="4" /> &nbsp; ',
  350. '<select name="heightType">',
  351. '<option value="%">' + lang.percent + '</option>',
  352. '<option value="px">' + lang.px + '</option>',
  353. '</select>',
  354. '</div>',
  355. //align
  356. '<div class="ke-dialog-row">',
  357. '<label for="keAlign" style="width:90px;">' + lang.align + '</label>',
  358. lang.textAlign + ' <select id="keAlign" name="textAlign">',
  359. '<option value="">' + lang.alignDefault + '</option>',
  360. '<option value="left">' + lang.alignLeft + '</option>',
  361. '<option value="center">' + lang.alignCenter + '</option>',
  362. '<option value="right">' + lang.alignRight + '</option>',
  363. '</select> ',
  364. lang.verticalAlign + ' <select name="verticalAlign">',
  365. '<option value="">' + lang.alignDefault + '</option>',
  366. '<option value="top">' + lang.alignTop + '</option>',
  367. '<option value="middle">' + lang.alignMiddle + '</option>',
  368. '<option value="bottom">' + lang.alignBottom + '</option>',
  369. '<option value="baseline">' + lang.alignBaseline + '</option>',
  370. '</select>',
  371. '</div>',
  372. //border
  373. '<div class="ke-dialog-row">',
  374. '<label for="keBorder" style="width:90px;">' + lang.border + '</label>',
  375. lang.borderWidth + ' <input type="text" id="keBorder" class="ke-input-text ke-input-number" name="border" value="" maxlength="4" /> &nbsp; ',
  376. lang.borderColor + ' <span class="ke-inline-block ke-input-color"></span>',
  377. '</div>',
  378. //background color
  379. '<div class="ke-dialog-row">',
  380. '<label for="keBgColor" style="width:90px;">' + lang.backgroundColor + '</label>',
  381. '<span class="ke-inline-block ke-input-color"></span>',
  382. '</div>',
  383. '</div>'
  384. ].join('');
  385. var dialog = self.createDialog({
  386. name : name,
  387. width : 500,
  388. height : 220,
  389. title : self.lang('tablecell'),
  390. body : html,
  391. beforeRemove : function() {
  392. colorBox.unbind();
  393. },
  394. yesBtn : {
  395. name : self.lang('yes'),
  396. click : function(e) {
  397. var width = widthBox.val(),
  398. height = heightBox.val(),
  399. widthType = widthTypeBox.val(),
  400. heightType = heightTypeBox.val(),
  401. padding = paddingBox.val(),
  402. spacing = spacingBox.val(),
  403. textAlign = textAlignBox.val(),
  404. verticalAlign = verticalAlignBox.val(),
  405. border = borderBox.val(),
  406. borderColor = K(colorBox[0]).html() || '',
  407. bgColor = K(colorBox[1]).html() || '';
  408. if (!/^\d*$/.test(width)) {
  409. alert(self.lang('invalidWidth'));
  410. widthBox[0].focus();
  411. return;
  412. }
  413. if (!/^\d*$/.test(height)) {
  414. alert(self.lang('invalidHeight'));
  415. heightBox[0].focus();
  416. return;
  417. }
  418. if (!/^\d*$/.test(border)) {
  419. alert(self.lang('invalidBorder'));
  420. borderBox[0].focus();
  421. return;
  422. }
  423. cell.css({
  424. width : width !== '' ? (width + widthType) : '',
  425. height : height !== '' ? (height + heightType) : '',
  426. 'background-color' : bgColor,
  427. 'text-align' : textAlign,
  428. 'vertical-align' : verticalAlign,
  429. 'border-width' : border,
  430. 'border-style' : border !== '' ? 'solid' : '',
  431. 'border-color' : borderColor
  432. });
  433. self.hideDialog().focus();
  434. self.addBookmark();
  435. }
  436. }
  437. }),
  438. div = dialog.div,
  439. widthBox = K('[name="width"]', div).val(100),
  440. heightBox = K('[name="height"]', div),
  441. widthTypeBox = K('[name="widthType"]', div),
  442. heightTypeBox = K('[name="heightType"]', div),
  443. paddingBox = K('[name="padding"]', div).val(2),
  444. spacingBox = K('[name="spacing"]', div).val(0),
  445. textAlignBox = K('[name="textAlign"]', div),
  446. verticalAlignBox = K('[name="verticalAlign"]', div),
  447. borderBox = K('[name="border"]', div).val(1),
  448. colorBox = K('.ke-input-color', div);
  449. _initColorPicker(div, colorBox.eq(0));
  450. _initColorPicker(div, colorBox.eq(1));
  451. _setColor(colorBox.eq(0), '#000000');
  452. _setColor(colorBox.eq(1), '');
  453. // foucs and select
  454. widthBox[0].focus();
  455. widthBox[0].select();
  456. // get selected cell
  457. var cell = self.plugin.getSelectedCell();
  458. var match,
  459. cellWidth = cell[0].style.width || cell[0].width || '',
  460. cellHeight = cell[0].style.height || cell[0].height || '';
  461. if ((match = /^(\d+)((?:px|%)*)$/.exec(cellWidth))) {
  462. widthBox.val(match[1]);
  463. widthTypeBox.val(match[2]);
  464. } else {
  465. widthBox.val('');
  466. }
  467. if ((match = /^(\d+)((?:px|%)*)$/.exec(cellHeight))) {
  468. heightBox.val(match[1]);
  469. heightTypeBox.val(match[2]);
  470. }
  471. textAlignBox.val(cell[0].style.textAlign || '');
  472. verticalAlignBox.val(cell[0].style.verticalAlign || '');
  473. var border = cell[0].style.borderWidth || '';
  474. if (border) {
  475. border = parseInt(border);
  476. }
  477. borderBox.val(border);
  478. _setColor(colorBox.eq(0), K.toHex(cell[0].style.borderColor || ''));
  479. _setColor(colorBox.eq(1), K.toHex(cell[0].style.backgroundColor || ''));
  480. widthBox[0].focus();
  481. widthBox[0].select();
  482. },
  483. insert : function() {
  484. this.prop(true);
  485. },
  486. 'delete' : function() {
  487. var table = self.plugin.getSelectedTable();
  488. self.cmd.range.setStartBefore(table[0]).collapse(true);
  489. self.cmd.select();
  490. table.remove();
  491. self.addBookmark();
  492. },
  493. colinsert : function(offset) {
  494. var table = self.plugin.getSelectedTable()[0],
  495. row = self.plugin.getSelectedRow()[0],
  496. cell = self.plugin.getSelectedCell()[0],
  497. index = cell.cellIndex + offset;
  498. for (var i = 0, len = table.rows.length; i < len; i++) {
  499. var newRow = table.rows[i],
  500. newCell = newRow.insertCell(index);
  501. newCell.innerHTML = K.IE ? '' : '<br />';
  502. // 调整下一行的单元格index
  503. index = _getCellIndex(table, newRow, newCell);
  504. }
  505. self.cmd.range.selectNodeContents(cell).collapse(true);
  506. self.cmd.select();
  507. self.addBookmark();
  508. },
  509. colinsertleft : function() {
  510. this.colinsert(0);
  511. },
  512. colinsertright : function() {
  513. this.colinsert(1);
  514. },
  515. rowinsert : function(offset) {
  516. var table = self.plugin.getSelectedTable()[0],
  517. row = self.plugin.getSelectedRow()[0],
  518. cell = self.plugin.getSelectedCell()[0],
  519. newRow;
  520. if (offset === 1) {
  521. newRow = table.insertRow(row.rowIndex + (cell.rowSpan - 1) + offset);
  522. } else {
  523. newRow = table.insertRow(row.rowIndex);
  524. }
  525. for (var i = 0, len = row.cells.length; i < len; i++) {
  526. var newCell = newRow.insertCell(i);
  527. // copy colspan
  528. if (offset === 1 && row.cells[i].colSpan > 1) {
  529. newCell.colSpan = row.cells[i].colSpan;
  530. }
  531. newCell.innerHTML = K.IE ? '' : '<br />';
  532. }
  533. self.cmd.range.selectNodeContents(cell).collapse(true);
  534. self.cmd.select();
  535. self.addBookmark();
  536. },
  537. rowinsertabove : function() {
  538. this.rowinsert(0);
  539. },
  540. rowinsertbelow : function() {
  541. this.rowinsert(1);
  542. },
  543. rowmerge : function() {
  544. var table = self.plugin.getSelectedTable()[0],
  545. row = self.plugin.getSelectedRow()[0],
  546. cell = self.plugin.getSelectedCell()[0],
  547. rowIndex = row.rowIndex, // 当前行的index
  548. nextRowIndex = rowIndex + cell.rowSpan, // 下一行的index
  549. nextRow = table.rows[nextRowIndex]; // 下一行
  550. // 最后一行不能合并
  551. if (table.rows.length <= nextRowIndex) {
  552. return;
  553. }
  554. var cellIndex = _getCellIndex(table, row, cell); // 下一行单元格的index
  555. if (nextRow.cells.length <= cellIndex) {
  556. return;
  557. }
  558. var nextCell = nextRow.cells[cellIndex]; // 下一行单元格
  559. // 上下行的colspan不一致时不能合并
  560. if (cell.colSpan !== nextCell.colSpan) {
  561. return;
  562. }
  563. cell.rowSpan += nextCell.rowSpan;
  564. nextRow.deleteCell(cellIndex);
  565. self.cmd.range.selectNodeContents(cell).collapse(true);
  566. self.cmd.select();
  567. self.addBookmark();
  568. },
  569. colmerge : function() {
  570. var table = self.plugin.getSelectedTable()[0],
  571. row = self.plugin.getSelectedRow()[0],
  572. cell = self.plugin.getSelectedCell()[0],
  573. rowIndex = row.rowIndex, // 当前行的index
  574. cellIndex = cell.cellIndex,
  575. nextCellIndex = cellIndex + 1;
  576. // 最后一列不能合并
  577. if (row.cells.length <= nextCellIndex) {
  578. return;
  579. }
  580. var nextCell = row.cells[nextCellIndex];
  581. // 左右列的rowspan不一致时不能合并
  582. if (cell.rowSpan !== nextCell.rowSpan) {
  583. return;
  584. }
  585. cell.colSpan += nextCell.colSpan;
  586. row.deleteCell(nextCellIndex);
  587. self.cmd.range.selectNodeContents(cell).collapse(true);
  588. self.cmd.select();
  589. self.addBookmark();
  590. },
  591. rowsplit : function() {
  592. var table = self.plugin.getSelectedTable()[0],
  593. row = self.plugin.getSelectedRow()[0],
  594. cell = self.plugin.getSelectedCell()[0],
  595. rowIndex = row.rowIndex;
  596. // 不是可分割单元格
  597. if (cell.rowSpan === 1) {
  598. return;
  599. }
  600. var cellIndex = _getCellIndex(table, row, cell);
  601. for (var i = 1, len = cell.rowSpan; i < len; i++) {
  602. var newRow = table.rows[rowIndex + i],
  603. newCell = newRow.insertCell(cellIndex);
  604. if (cell.colSpan > 1) {
  605. newCell.colSpan = cell.colSpan;
  606. }
  607. newCell.innerHTML = K.IE ? '' : '<br />';
  608. // 调整下一行的单元格index
  609. cellIndex = _getCellIndex(table, newRow, newCell);
  610. }
  611. K(cell).removeAttr('rowSpan');
  612. self.cmd.range.selectNodeContents(cell).collapse(true);
  613. self.cmd.select();
  614. self.addBookmark();
  615. },
  616. colsplit : function() {
  617. var table = self.plugin.getSelectedTable()[0],
  618. row = self.plugin.getSelectedRow()[0],
  619. cell = self.plugin.getSelectedCell()[0],
  620. cellIndex = cell.cellIndex;
  621. // 不是可分割单元格
  622. if (cell.colSpan === 1) {
  623. return;
  624. }
  625. for (var i = 1, len = cell.colSpan; i < len; i++) {
  626. var newCell = row.insertCell(cellIndex + i);
  627. if (cell.rowSpan > 1) {
  628. newCell.rowSpan = cell.rowSpan;
  629. }
  630. newCell.innerHTML = K.IE ? '' : '<br />';
  631. }
  632. K(cell).removeAttr('colSpan');
  633. self.cmd.range.selectNodeContents(cell).collapse(true);
  634. self.cmd.select();
  635. self.addBookmark();
  636. },
  637. coldelete : function() {
  638. var table = self.plugin.getSelectedTable()[0],
  639. row = self.plugin.getSelectedRow()[0],
  640. cell = self.plugin.getSelectedCell()[0],
  641. index = cell.cellIndex;
  642. for (var i = 0, len = table.rows.length; i < len; i++) {
  643. var newRow = table.rows[i],
  644. newCell = newRow.cells[index];
  645. if (newCell.colSpan > 1) {
  646. newCell.colSpan -= 1;
  647. if (newCell.colSpan === 1) {
  648. K(newCell).removeAttr('colSpan');
  649. }
  650. } else {
  651. newRow.deleteCell(index);
  652. }
  653. // 跳过不需要删除的行
  654. if (newCell.rowSpan > 1) {
  655. i += newCell.rowSpan - 1;
  656. }
  657. }
  658. if (row.cells.length === 0) {
  659. self.cmd.range.setStartBefore(table).collapse(true);
  660. self.cmd.select();
  661. K(table).remove();
  662. } else {
  663. self.cmd.selection(true);
  664. }
  665. self.addBookmark();
  666. },
  667. rowdelete : function() {
  668. var table = self.plugin.getSelectedTable()[0],
  669. row = self.plugin.getSelectedRow()[0],
  670. cell = self.plugin.getSelectedCell()[0],
  671. rowIndex = row.rowIndex;
  672. // 从下到上删除
  673. for (var i = cell.rowSpan - 1; i >= 0; i--) {
  674. table.deleteRow(rowIndex + i);
  675. }
  676. if (table.rows.length === 0) {
  677. self.cmd.range.setStartBefore(table).collapse(true);
  678. self.cmd.select();
  679. K(table).remove();
  680. } else {
  681. self.cmd.selection(true);
  682. }
  683. self.addBookmark();
  684. }
  685. };
  686. self.clickToolbar(name, self.plugin.table.prop);
  687. });