table.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. *☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
  3. *★ ★
  4. *☆ eWebEditor - eWebSoft在线文本编辑器飞鱼修改版 ☆
  5. *★ ★
  6. *☆ 版权所有: eWebSoft.com ☆
  7. *★ ★
  8. *☆ 程序制作: eWeb开发团队 ☆
  9. *★ email:webmaster@webasp.net ★
  10. *☆ QQ:589808 ☆
  11. *★ ★
  12. *☆ 相关网址: [原版地址]http://www.eWebSoft.com/Product/eWebEditor/ ☆
  13. *★ [支持论坛]http://bbs.eWebSoft.com/ ★
  14. *☆ ☆
  15. *★ 主页地址: http://www.fiyu.net/ 飞鱼论坛 ★
  16. *☆ 有什么问题欢迎到飞鱼论坛提出! ☆
  17. *★ ★
  18. *★ ★
  19. *☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆★☆
  20. */
  21. // 表格相关全局变量
  22. var selectedTD
  23. var selectedTR
  24. var selectedTBODY
  25. var selectedTable
  26. // 插入表格
  27. function TableInsert(){
  28. if (!isTableSelected()){
  29. ShowDialog('dialog/table.htm', 350, 380, true);
  30. }
  31. }
  32. // 修改表格属性
  33. function TableProp(){
  34. if (isTableSelected()||isCursorInTableCell()){
  35. ShowDialog('dialog/table.htm?action=modify', 350, 380, true);
  36. }
  37. }
  38. // 修改单元格属性
  39. function TableCellProp(){
  40. if (isCursorInTableCell()){
  41. ShowDialog('dialog/tablecell.htm', 350, 280, true);
  42. }
  43. }
  44. // 拆分单元格
  45. function TableCellSplit(){
  46. if (isCursorInTableCell()){
  47. ShowDialog('dialog/tablecellsplit.htm', 200, 150, true);
  48. }
  49. }
  50. // 修改表格行属性
  51. function TableRowProp(){
  52. if (isCursorInTableCell()){
  53. ShowDialog('dialog/tablecell.htm?action=row', 350, 280, true);
  54. }
  55. }
  56. // 插入行(在上方)
  57. function TableRowInsertAbove() {
  58. if (isCursorInTableCell()){
  59. var numCols = 0
  60. allCells = selectedTR.cells
  61. for (var i=0;i<allCells.length;i++) {
  62. numCols = numCols + allCells[i].getAttribute('colSpan')
  63. }
  64. var newTR = selectedTable.insertRow(selectedTR.rowIndex)
  65. for (i = 0; i < numCols; i++) {
  66. newTD = newTR.insertCell()
  67. newTD.innerHTML = "&nbsp;"
  68. if (borderShown == "yes") {
  69. newTD.runtimeStyle.border = "1px dotted #BFBFBF"
  70. }
  71. }
  72. }
  73. }
  74. // 插入行(在下方)
  75. function TableRowInsertBelow() {
  76. if (isCursorInTableCell()){
  77. var numCols = 0
  78. allCells = selectedTR.cells
  79. for (var i=0;i<allCells.length;i++) {
  80. numCols = numCols + allCells[i].getAttribute('colSpan')
  81. }
  82. var newTR = selectedTable.insertRow(selectedTR.rowIndex+1)
  83. for (i = 0; i < numCols; i++) {
  84. newTD = newTR.insertCell()
  85. newTD.innerHTML = "&nbsp;"
  86. if (borderShown == "yes") {
  87. newTD.runtimeStyle.border = "1px dotted #BFBFBF"
  88. }
  89. }
  90. }
  91. }
  92. // 合并行(向下方)
  93. function TableRowMerge() {
  94. if (isCursorInTableCell()) {
  95. var rowSpanTD = selectedTD.getAttribute('rowSpan')
  96. allRows = selectedTable.rows
  97. if (selectedTR.rowIndex +1 != allRows.length) {
  98. var allCellsInNextRow = allRows[selectedTR.rowIndex+selectedTD.rowSpan].cells
  99. var addRowSpan = allCellsInNextRow[selectedTD.cellIndex].getAttribute('rowSpan')
  100. var moveTo = selectedTD.rowSpan
  101. if (!addRowSpan) addRowSpan = 1;
  102. selectedTD.rowSpan = selectedTD.rowSpan + addRowSpan
  103. allRows[selectedTR.rowIndex + moveTo].deleteCell(selectedTD.cellIndex)
  104. }
  105. }
  106. }
  107. // 拆分行
  108. function TableRowSplit(nRows){
  109. if (!isCursorInTableCell()) return;
  110. if (nRows<2) return;
  111. var addRows = nRows - 1;
  112. var addRowsNoSpan = addRows;
  113. var nsLeftColSpan = 0;
  114. for (var i=0; i<selectedTD.cellIndex; i++){
  115. nsLeftColSpan += selectedTR.cells[i].colSpan;
  116. }
  117. var allRows = selectedTable.rows;
  118. // rowspan>1时
  119. while (selectedTD.rowSpan > 1 && addRowsNoSpan > 0){
  120. var nextRow = allRows[selectedTR.rowIndex+selectedTD.rowSpan-1];
  121. selectedTD.rowSpan -= 1;
  122. var ncLeftColSpan = 0;
  123. var position = -1;
  124. for (var n=0; n<nextRow.cells.length; n++){
  125. ncLeftColSpan += nextRow.cells[n].getAttribute('colSpan');
  126. if (ncLeftColSpan>nsLeftColSpan){
  127. position = n;
  128. break;
  129. }
  130. }
  131. var newTD=nextRow.insertCell(position);
  132. newTD.innerHTML = "&nbsp;";
  133. if (borderShown == "yes") {
  134. newTD.runtimeStyle.border = "1px dotted #BFBFBF";
  135. }
  136. addRowsNoSpan -= 1;
  137. }
  138. // rowspan=1时
  139. for (var n=0; n<addRowsNoSpan; n++){
  140. var numCols = 0
  141. allCells = selectedTR.cells
  142. for (var i=0;i<allCells.length;i++) {
  143. numCols = numCols + allCells[i].getAttribute('colSpan')
  144. }
  145. var newTR = selectedTable.insertRow(selectedTR.rowIndex+1)
  146. // 上方行的rowspan达到这行
  147. for (var j=0; j<selectedTR.rowIndex; j++){
  148. for (var k=0; k<allRows[j].cells.length; k++){
  149. if ((allRows[j].cells[k].rowSpan>1)&&(allRows[j].cells[k].rowSpan>=selectedTR.rowIndex-allRows[j].rowIndex+1)){
  150. allRows[j].cells[k].rowSpan += 1;
  151. }
  152. }
  153. }
  154. // 当前行
  155. for (i = 0; i < allCells.length; i++) {
  156. if (i!=selectedTD.cellIndex){
  157. selectedTR.cells[i].rowSpan += 1;
  158. }else{
  159. newTD = newTR.insertCell();
  160. newTD.colSpan = selectedTD.colSpan;
  161. newTD.innerHTML = "&nbsp;";
  162. if (borderShown == "yes") {
  163. newTD.runtimeStyle.border = "1px dotted #BFBFBF";
  164. }
  165. }
  166. }
  167. }
  168. }
  169. // 删除行
  170. function TableRowDelete() {
  171. if (isCursorInTableCell()) {
  172. selectedTable.deleteRow(selectedTR.rowIndex)
  173. }
  174. }
  175. // 插入列(在左侧)
  176. function TableColInsertLeft() {
  177. if (isCursorInTableCell()) {
  178. moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex)
  179. allRows = selectedTable.rows
  180. for (i=0;i<allRows.length;i++) {
  181. rowCount = allRows[i].cells.length - 1
  182. position = rowCount - moveFromEnd
  183. if (position < 0) {
  184. position = 0
  185. }
  186. newCell = allRows[i].insertCell(position)
  187. newCell.innerHTML = "&nbsp;"
  188. if (borderShown == "yes") {
  189. newCell.runtimeStyle.border = "1px dotted #BFBFBF"
  190. }
  191. }
  192. }
  193. }
  194. // 插入列(在右侧)
  195. function TableColInsertRight() {
  196. if (isCursorInTableCell()) {
  197. moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex)
  198. allRows = selectedTable.rows
  199. for (i=0;i<allRows.length;i++) {
  200. rowCount = allRows[i].cells.length - 1
  201. position = rowCount - moveFromEnd
  202. if (position < 0) {
  203. position = 0
  204. }
  205. newCell = allRows[i].insertCell(position+1)
  206. newCell.innerHTML = "&nbsp;"
  207. if (borderShown == "yes") {
  208. newCell.runtimeStyle.border = "1px dotted #BFBFBF"
  209. }
  210. }
  211. }
  212. }
  213. // 合并列
  214. function TableColMerge() {
  215. if (isCursorInTableCell()) {
  216. var colSpanTD = selectedTD.getAttribute('colSpan')
  217. allCells = selectedTR.cells
  218. if (selectedTD.cellIndex + 1 != selectedTR.cells.length) {
  219. var addColspan = allCells[selectedTD.cellIndex+1].getAttribute('colSpan')
  220. selectedTD.colSpan = colSpanTD + addColspan
  221. selectedTR.deleteCell(selectedTD.cellIndex+1)
  222. }
  223. }
  224. }
  225. // 删除列
  226. function TableColDelete() {
  227. if (isCursorInTableCell()) {
  228. moveFromEnd = (selectedTR.cells.length-1) - (selectedTD.cellIndex)
  229. allRows = selectedTable.rows
  230. for (var i=0;i<allRows.length;i++) {
  231. endOfRow = allRows[i].cells.length - 1
  232. position = endOfRow - moveFromEnd
  233. if (position < 0) {
  234. position = 0
  235. }
  236. allCellsInRow = allRows[i].cells
  237. if (allCellsInRow[position].colSpan > 1) {
  238. allCellsInRow[position].colSpan = allCellsInRow[position].colSpan - 1
  239. } else {
  240. allRows[i].deleteCell(position)
  241. }
  242. }
  243. }
  244. }
  245. // 拆分列
  246. function TableColSplit(nCols){
  247. if (!isCursorInTableCell()) return;
  248. if (nCols<2) return;
  249. var addCols = nCols - 1;
  250. var addColsNoSpan = addCols;
  251. var newCell;
  252. var nsLeftColSpan = 0;
  253. var nsLeftRowSpanMoreOne = 0;
  254. for (var i=0; i<selectedTD.cellIndex; i++){
  255. nsLeftColSpan += selectedTR.cells[i].colSpan;
  256. if (selectedTR.cells[i].rowSpan > 1){
  257. nsLeftRowSpanMoreOne += 1;
  258. }
  259. }
  260. var allRows = selectedTable.rows
  261. // colSpan>1时
  262. while (selectedTD.colSpan > 1 && addColsNoSpan > 0) {
  263. newCell = selectedTR.insertCell(selectedTD.cellIndex+1);
  264. newCell.innerHTML = "&nbsp;"
  265. if (borderShown == "yes") {
  266. newCell.runtimeStyle.border = "1px dotted #BFBFBF"
  267. }
  268. selectedTD.colSpan -= 1;
  269. addColsNoSpan -= 1;
  270. }
  271. // colSpan=1时
  272. for (i=0;i<allRows.length;i++) {
  273. var ncLeftColSpan = 0;
  274. var position = -1;
  275. for (var n=0; n<allRows[i].cells.length; n++){
  276. ncLeftColSpan += allRows[i].cells[n].getAttribute('colSpan');
  277. if (ncLeftColSpan+nsLeftRowSpanMoreOne>nsLeftColSpan){
  278. position = n;
  279. break;
  280. }
  281. }
  282. if (selectedTR.rowIndex!=i){
  283. if (position!=-1){
  284. allRows[i].cells[position+nsLeftRowSpanMoreOne].colSpan += addColsNoSpan;
  285. }
  286. }else{
  287. for (var n=0; n<addColsNoSpan; n++){
  288. newCell = allRows[i].insertCell(selectedTD.cellIndex+1)
  289. newCell.innerHTML = "&nbsp;"
  290. newCell.rowSpan = selectedTD.rowSpan;
  291. if (borderShown == "yes") {
  292. newCell.runtimeStyle.border = "1px dotted #BFBFBF"
  293. }
  294. }
  295. }
  296. }
  297. }
  298. // 是否选中表格
  299. function isTableSelected() {
  300. if (eWebEditor.document.selection.type == "Control") {
  301. var oControlRange = eWebEditor.document.selection.createRange();
  302. if (oControlRange(0).tagName.toUpperCase() == "TABLE") {
  303. selectedTable = eWebEditor.document.selection.createRange()(0);
  304. return true;
  305. }
  306. }
  307. }
  308. // 光标是否在表格中
  309. function isCursorInTableCell() {
  310. if (eWebEditor.document.selection.type != "Control") {
  311. var elem = eWebEditor.document.selection.createRange().parentElement()
  312. while (elem.tagName.toUpperCase() != "TD" && elem.tagName.toUpperCase() != "TH"){
  313. elem = elem.parentElement
  314. if (elem == null)
  315. break
  316. }
  317. if (elem) {
  318. selectedTD = elem
  319. selectedTR = selectedTD.parentElement
  320. selectedTBODY = selectedTR.parentElement
  321. selectedTable = selectedTBODY.parentElement
  322. return true
  323. }
  324. }
  325. }