fcktablehandler.js
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:10k
源码类别:

OA系统

开发平台:

C#

  1. /*
  2.  * FCKeditor - The text editor for internet
  3.  * Copyright (C) 2003-2006 Frederico Caldeira Knabben
  4.  * 
  5.  * Licensed under the terms of the GNU Lesser General Public License:
  6.  *  http://www.opensource.org/licenses/lgpl-license.php
  7.  * 
  8.  * For further information visit:
  9.  *  http://www.fckeditor.net/
  10.  * 
  11.  * "Support Open Source software. What about a donation today?"
  12.  * 
  13.  * File Name: fcktablehandler.js
  14.  *  Manage table operations.
  15.  * 
  16.  * File Authors:
  17.  *  Frederico Caldeira Knabben (fredck@fckeditor.net)
  18.  */
  19. var FCKTableHandler = new Object() ;
  20. FCKTableHandler.InsertRow = function()
  21. {
  22. // Get the row where the selection is placed in.
  23. var oRow = FCKSelection.MoveToAncestorNode("TR") ;
  24. if ( !oRow ) return ;
  25. // Create a clone of the row.
  26. var oNewRow = oRow.cloneNode( true ) ;
  27. // Insert the new row (copy) before of it.
  28. oRow.parentNode.insertBefore( oNewRow, oRow ) ;
  29. // Clean the row (it seems that the new row has been added after it).
  30. FCKTableHandler.ClearRow( oRow ) ;
  31. }
  32. FCKTableHandler.DeleteRows = function( row )
  33. {
  34. // If no row has been passed as a parameer,
  35. // then get the row where the selection is placed in.
  36. if ( !row )
  37. row = FCKSelection.MoveToAncestorNode("TR") ;
  38. if ( !row ) return ;
  39. // Get the row's table.
  40. var oTable = FCKTools.GetElementAscensor( row, 'TABLE' ) ;
  41. // If just one row is available then delete the entire table.
  42. if ( oTable.rows.length == 1 ) 
  43. {
  44. FCKTableHandler.DeleteTable( oTable ) ;
  45. return ;
  46. }
  47. // Delete the row.
  48. row.parentNode.removeChild( row ) ;
  49. }
  50. FCKTableHandler.DeleteTable = function( table )
  51. {
  52. // If no table has been passed as a parameer,
  53. // then get the table where the selection is placed in.
  54. if ( !table )
  55. {
  56. var table = FCKSelection.GetSelectedElement() ;
  57. if ( !table || table.tagName != 'TABLE' )
  58. table = FCKSelection.MoveToAncestorNode("TABLE") ;
  59. }
  60. if ( !table ) return ;
  61. // Delete the table.
  62. FCKSelection.SelectNode( table ) ;
  63. FCKSelection.Collapse();
  64. table.parentNode.removeChild( table ) ;
  65. }
  66. FCKTableHandler.InsertColumn = function()
  67. {
  68. // Get the cell where the selection is placed in.
  69. var oCell = FCKSelection.MoveToAncestorNode("TD") ;
  70. if ( !oCell )
  71.     oCell =  FCKSelection.MoveToAncestorNode("TH") ;
  72. if ( !oCell ) return ;
  73. // Get the cell's table.
  74. var oTable = FCKTools.GetElementAscensor( oCell, 'TABLE' ) ;
  75. // Get the index of the column to be created (based on the cell).
  76. var iIndex = oCell.cellIndex + 1 ;
  77. // Loop throw all rows available in the table.
  78. for ( var i = 0 ; i < oTable.rows.length ; i++ )
  79. {
  80. // Get the row.
  81. var oRow = oTable.rows[i] ;
  82. // If the row doens't have enought cells, ignore it.
  83. if ( oRow.cells.length < iIndex )
  84. continue ;
  85. oCell = oRow.cells[iIndex-1].cloneNode(false) ;
  86. if ( FCKBrowserInfo.IsGecko )
  87. oCell.innerHTML = GECKO_BOGUS ;
  88. // Get the cell that is placed in the new cell place.
  89. var oBaseCell = oRow.cells[iIndex] ;
  90. // If the cell is available (we are not in the last cell of the row).
  91. if ( oBaseCell )
  92. oRow.insertBefore( oCell, oBaseCell ) ; // Insert the new cell just before of it.
  93. else
  94. oRow.appendChild( oCell ) ; // Append the cell at the end of the row.
  95. }
  96. }
  97. FCKTableHandler.DeleteColumns = function()
  98. {
  99. // Get the cell where the selection is placed in.
  100. var oCell = FCKSelection.MoveToAncestorNode('TD') || FCKSelection.MoveToAncestorNode('TH') ;
  101. if ( !oCell ) return ;
  102. // Get the cell's table.
  103. var oTable = FCKTools.GetElementAscensor( oCell, 'TABLE' ) ;
  104. // Get the cell index.
  105. var iIndex = oCell.cellIndex ;
  106. // Loop throw all rows (from down to up, because it's possible that some
  107. // rows will be deleted).
  108. for ( var i = oTable.rows.length - 1 ; i >= 0 ; i-- )
  109. {
  110. // Get the row.
  111. var oRow = oTable.rows[i] ;
  112. // If the cell to be removed is the first one and the row has just one cell.
  113. if ( iIndex == 0 && oRow.cells.length == 1 )
  114. {
  115. // Remove the entire row.
  116. FCKTableHandler.DeleteRows( oRow ) ;
  117. continue ;
  118. }
  119. // If the cell to be removed exists the delete it.
  120. if ( oRow.cells[iIndex] )
  121. oRow.removeChild( oRow.cells[iIndex] ) ;
  122. }
  123. }
  124. FCKTableHandler.InsertCell = function( cell )
  125. {
  126. // Get the cell where the selection is placed in.
  127. var oCell = cell ? cell : FCKSelection.MoveToAncestorNode("TD") ;
  128. if ( !oCell ) return ;
  129. // Create the new cell element to be added.
  130. var oNewCell = FCK.EditorDocument.createElement("TD");
  131. if ( FCKBrowserInfo.IsGecko )
  132. oNewCell.innerHTML = GECKO_BOGUS ;
  133. // oNewCell.innerHTML = "&nbsp;" ;
  134. // If it is the last cell in the row.
  135. if ( oCell.cellIndex == oCell.parentNode.cells.length - 1 )
  136. {
  137. // Add the new cell at the end of the row.
  138. oCell.parentNode.appendChild( oNewCell ) ;
  139. }
  140. else
  141. {
  142. // Add the new cell before the next cell (after the active one).
  143. oCell.parentNode.insertBefore( oNewCell, oCell.nextSibling ) ;
  144. }
  145. return oNewCell ;
  146. }
  147. FCKTableHandler.DeleteCell = function( cell )
  148. {
  149. // If this is the last cell in the row.
  150. if ( cell.parentNode.cells.length == 1 )
  151. {
  152. // Delete the entire row.
  153. FCKTableHandler.DeleteRows( FCKTools.GetElementAscensor( cell, 'TR' ) ) ;
  154. return ;
  155. }
  156. // Delete the cell from the row.
  157. cell.parentNode.removeChild( cell ) ;
  158. }
  159. FCKTableHandler.DeleteCells = function()
  160. {
  161. var aCells = FCKTableHandler.GetSelectedCells() ;
  162. for ( var i = aCells.length - 1 ; i >= 0  ; i-- )
  163. {
  164. FCKTableHandler.DeleteCell( aCells[i] ) ;
  165. }
  166. }
  167. FCKTableHandler.MergeCells = function()
  168. {
  169. // Get all selected cells.
  170. var aCells = FCKTableHandler.GetSelectedCells() ;
  171. // At least 2 cells must be selected.
  172. if ( aCells.length < 2 )
  173. return ;
  174. // The merge can occour only if the selected cells are from the same row.
  175. if ( aCells[0].parentNode != aCells[aCells.length-1].parentNode )
  176. return ;
  177. // Calculate the new colSpan for the first cell.
  178. var iColSpan = isNaN( aCells[0].colSpan ) ? 1 : aCells[0].colSpan ;
  179. var sHtml = '' ;
  180. var oCellsContents = FCK.EditorDocument.createDocumentFragment() ;
  181. for ( var i = aCells.length - 1 ; i >= 0 ; i-- )
  182. {
  183. var eCell = aCells[i] ;
  184. // Move its contents to the document fragment.
  185. for ( var c = eCell.childNodes.length - 1 ; c >= 0 ; c-- )
  186. {
  187. var eChild = eCell.removeChild( eCell.childNodes[c] ) ;
  188. if ( ( eChild.hasAttribute && eChild.hasAttribute('_moz_editor_bogus_node') ) || ( eChild.getAttribute && eChild.getAttribute( 'type', 2 ) == '_moz' ) )
  189. continue ;
  190. oCellsContents.insertBefore( eChild, oCellsContents.firstChild ) ;
  191. }
  192. if ( i > 0 )
  193. {
  194. // Accumulate the colspan of the cell.
  195. iColSpan += isNaN( eCell.colSpan ) ? 1 : eCell.colSpan ;
  196. // Delete the cell.
  197. FCKTableHandler.DeleteCell( eCell ) ;
  198. }
  199. }
  200. // Set the innerHTML of the remaining cell (the first one).
  201. aCells[0].colSpan = iColSpan ;
  202. if ( FCKBrowserInfo.IsGecko && oCellsContents.childNodes.length == 0 )
  203. aCells[0].innerHTML = GECKO_BOGUS ;
  204. else
  205. aCells[0].appendChild( oCellsContents ) ;
  206. }
  207. FCKTableHandler.SplitCell = function()
  208. {
  209. // Check that just one cell is selected, otherwise return.
  210. var aCells = FCKTableHandler.GetSelectedCells() ;
  211. if ( aCells.length != 1 )
  212. return ;
  213. var aMap = this._CreateTableMap( aCells[0].parentNode.parentNode ) ;
  214. var iCellIndex = FCKTableHandler._GetCellIndexSpan( aMap, aCells[0].parentNode.rowIndex , aCells[0] ) ;
  215. var aCollCells = this._GetCollumnCells( aMap, iCellIndex ) ;
  216. for ( var i = 0 ; i < aCollCells.length ; i++ )
  217. {
  218. if ( aCollCells[i] == aCells[0] )
  219. {
  220. var oNewCell = this.InsertCell( aCells[0] ) ;
  221. if ( !isNaN( aCells[0].rowSpan ) && aCells[0].rowSpan > 1 )
  222. oNewCell.rowSpan = aCells[0].rowSpan ;
  223. }
  224. else
  225. {
  226. if ( isNaN( aCollCells[i].colSpan ) )
  227. aCollCells[i].colSpan = 2 ;
  228. else
  229. aCollCells[i].colSpan += 1 ;
  230. }
  231. }
  232. }
  233. // Get the cell index from a TableMap.
  234. FCKTableHandler._GetCellIndexSpan = function( tableMap, rowIndex, cell )
  235. {
  236. if ( tableMap.length < rowIndex + 1 )
  237. return null ;
  238. var oRow = tableMap[ rowIndex ] ;
  239. for ( var c = 0 ; c < oRow.length ; c++ )
  240. {
  241. if ( oRow[c] == cell )
  242. return c ;
  243. }
  244. return null ;
  245. }
  246. // Get the cells available in a collumn of a TableMap.
  247. FCKTableHandler._GetCollumnCells = function( tableMap, collumnIndex )
  248. {
  249. var aCollCells = new Array() ;
  250. for ( var r = 0 ; r < tableMap.length ; r++ )
  251. {
  252. var oCell = tableMap[r][collumnIndex] ;
  253. if ( oCell && ( aCollCells.length == 0 || aCollCells[ aCollCells.length - 1 ] != oCell ) )
  254. aCollCells[ aCollCells.length ] = oCell ;
  255. }
  256. return aCollCells ;
  257. }
  258. // This function is quite hard to explain. It creates a matrix representing all cells in a table.
  259. // The difference here is that the "spanned" cells (colSpan and rowSpan) are duplicated on the matrix
  260. // cells that are "spanned". For example, a row with 3 cells where the second cell has colSpan=2 and rowSpan=3
  261. // will produce a bi-dimensional matrix with the following values (representing the cells):
  262. // Cell1, Cell2, Cell2, Cell 3
  263. // Cell4, Cell2, Cell2, Cell 5
  264. FCKTableHandler._CreateTableMap = function( table )
  265. {
  266. var aRows = table.rows ;
  267. // Row and Collumn counters.
  268. var r = -1 ;
  269. var aMap = new Array() ;
  270. for ( var i = 0 ; i < aRows.length ; i++ )
  271. {
  272. r++ ;
  273. if ( !aMap[r] )
  274. aMap[r] = new Array() ;
  275. var c = -1 ;
  276. for ( var j = 0 ; j < aRows[i].cells.length ; j++ )
  277. {
  278. var oCell = aRows[i].cells[j] ;
  279. c++ ;
  280. while ( aMap[r][c] )
  281. c++ ;
  282. var iColSpan = isNaN( oCell.colSpan ) ? 1 : oCell.colSpan ;
  283. var iRowSpan = isNaN( oCell.rowSpan ) ? 1 : oCell.rowSpan ;
  284. for ( var rs = 0 ; rs < iRowSpan ; rs++ )
  285. {
  286. if ( !aMap[r + rs] )
  287. aMap[r + rs] = new Array() ;
  288. for ( var cs = 0 ; cs < iColSpan ; cs++ )
  289. {
  290. aMap[r + rs][c + cs] = aRows[i].cells[j] ;
  291. }
  292. }
  293. c += iColSpan - 1 ;
  294. }
  295. }
  296. return aMap ;
  297. }
  298. FCKTableHandler.ClearRow = function( tr )
  299. {
  300. // Get the array of row's cells.
  301. var aCells = tr.cells ;
  302. // Replace the contents of each cell with "nothing".
  303. for ( var i = 0 ; i < aCells.length ; i++ ) 
  304. {
  305. if ( FCKBrowserInfo.IsGecko )
  306. aCells[i].innerHTML = GECKO_BOGUS ;
  307. else
  308. aCells[i].innerHTML = '' ;
  309. }
  310. }