PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/Source/Web/FCKeditor/editor/_source/internals/fcktablehandler.js

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