insert.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*jslint nomen: false*/
  2. /*global Trex, $tom, $tx, _FALSE, _NULL, _TRUE */
  3. Trex.Table.Insert = Trex.Class.create({
  4. COL_DIRECTION: {
  5. LEFT: "left",
  6. RIGHT: "right"
  7. },
  8. initialize: function (editor/*, config*/) {
  9. var canvas;
  10. canvas = editor.getCanvas();
  11. this.wysiwygPanel = canvas.getPanel(Trex.Canvas.__WYSIWYG_MODE);
  12. },
  13. /**
  14. * insertRowAbove
  15. * @param {Trex.Table.Selector} tableSelector
  16. */
  17. insertRowAbove: function (tableSelector) {
  18. var boundary, indexer;
  19. tableSelector.reloadIndexer();
  20. boundary = tableSelector.getSelected();
  21. if (boundary.isValid()) {
  22. indexer = tableSelector.getIndexer();
  23. this.insertRowAboveByBoundary(boundary, indexer);
  24. tableSelector.reset();
  25. }
  26. },
  27. /**
  28. * @private
  29. * @param {Trex.TableUtil.Boundary} boundary
  30. * @param {Trex.TableUtil.Indexer} indexer
  31. */
  32. insertRowAboveByBoundary: function (boundary, indexer) {
  33. var table, rowCount, insertIndex, tdArrAtBoundaryLine, tdArrForClone;
  34. table = indexer.table;
  35. rowCount = boundary.bottom - boundary.top + 1;
  36. insertIndex = boundary.top;
  37. tdArrAtBoundaryLine = indexer.getTdArr(new Trex.TableUtil.Boundary({
  38. top: boundary.top,
  39. right: indexer.getColSize() - 1,
  40. bottom: boundary.top,
  41. left: 0
  42. }));
  43. tdArrForClone = indexer.getTdArrHasTop(boundary.top);
  44. this.addRow(table, rowCount, insertIndex, tdArrAtBoundaryLine, tdArrForClone);
  45. },
  46. /**
  47. * addRow
  48. * @param {Element} table
  49. * @param {number} rowCount
  50. * @param {number} insertIndex
  51. * @param {Array} tdArrAtBoundaryLine
  52. * @param {Array} tdArrForClone
  53. */
  54. addRow: function (table, rowCount, insertIndex, tdArrAtBoundaryLine, tdArrForClone) {
  55. var fn, i, tr_closure;
  56. fn = function (td) {
  57. var newTd;
  58. if (tdArrForClone.contains(td)) {
  59. newTd = Trex.TableUtil.cloneNodeForEmptyTd(td);
  60. Trex.TableUtil.splitHeightByRowSpan(newTd);
  61. //TODO.azki left / top 보더 세팅이 필요할지도.?
  62. newTd.rowSpan = 1;
  63. tr_closure.appendChild(newTd); //tr_closure is closure variable.
  64. } else {
  65. td.rowSpan += 1;
  66. }
  67. };
  68. for (i = 0; i < rowCount; i += 1) {
  69. tr_closure = table.insertRow(insertIndex);
  70. tdArrAtBoundaryLine.each(fn);
  71. }
  72. },
  73. /**
  74. * insertRowBelow
  75. * @param {Trex.Table.Selector} tableSelector
  76. */
  77. insertRowBelow: function (tableSelector) {
  78. var boundary, indexer;
  79. tableSelector.reloadIndexer();
  80. boundary = tableSelector.getSelected();
  81. if (boundary.isValid()) {
  82. indexer = tableSelector.getIndexer();
  83. this.insertRowBelowByBoundary(boundary, indexer);
  84. tableSelector.reset();
  85. }
  86. },
  87. /**
  88. * @private
  89. * @param {Trex.TableUtil.Boundary} boundary
  90. * @param {Trex.TableUtil.Indexer} indexer
  91. */
  92. insertRowBelowByBoundary: function (boundary, indexer) {
  93. var table, rowCount, insertIndex, tdArrAtBoundaryLine, tdArrForClone;
  94. table = indexer.table;
  95. rowCount = boundary.bottom - boundary.top + 1;
  96. insertIndex = boundary.bottom + 1;
  97. tdArrAtBoundaryLine = indexer.getTdArr(new Trex.TableUtil.Boundary({
  98. top: boundary.bottom,
  99. right: indexer.getColSize() - 1,
  100. bottom: boundary.bottom,
  101. left: 0
  102. }));
  103. tdArrForClone = indexer.getTdArrHasBottom(boundary.bottom);
  104. this.addRow(table, rowCount, insertIndex, tdArrAtBoundaryLine, tdArrForClone);
  105. },
  106. /**
  107. * insertColLeft
  108. * @param {Trex.Table.Selector} tableSelector
  109. */
  110. insertColLeft: function (tableSelector) {
  111. var boundary, indexer;
  112. tableSelector.reloadIndexer();
  113. boundary = tableSelector.getSelected();
  114. if (boundary.isValid()) {
  115. indexer = tableSelector.getIndexer();
  116. this.insertColLeftByBoundary(boundary, indexer);
  117. tableSelector.reset();
  118. }
  119. },
  120. /**
  121. * @private
  122. * @param {Trex.TableUtil.Boundary} boundary
  123. * @param {Trex.TableUtil.Indexer} indexer
  124. */
  125. insertColLeftByBoundary: function (boundary, indexer) {
  126. var colCount, tdArrAtBoundaryLine, tdArrForClone;
  127. colCount = boundary.right - boundary.left + 1;
  128. tdArrAtBoundaryLine = indexer.getTdArr(new Trex.TableUtil.Boundary({
  129. top: 0,
  130. right: boundary.left,
  131. bottom: indexer.getRowSize() - 1,
  132. left: boundary.left
  133. }));
  134. tdArrForClone = indexer.getTdArrHasLeft(boundary.left);
  135. this.addCol(colCount, tdArrAtBoundaryLine, tdArrForClone, this.COL_DIRECTION.LEFT);
  136. },
  137. /**
  138. * addCol
  139. * @param {number} colCount
  140. * @param {Array} tdArrAtBoundaryLine
  141. * @param {Array} tdArrForClone
  142. * @param {Trex.Table.Insert.COL_DIRECTION} direction
  143. */
  144. addCol: function (colCount, tdArrAtBoundaryLine, tdArrForClone, direction) {
  145. var self, fn, i;
  146. self = this;
  147. fn = function (td) {
  148. var newTd;
  149. if (tdArrForClone.contains(td)) {
  150. newTd = Trex.TableUtil.cloneNodeForEmptyTd(td);
  151. Trex.TableUtil.splitWidthByColSpan(newTd);
  152. //TODO.azki left / top 보더 세팅이 필요할지도.?
  153. newTd.colSpan = 1;
  154. if (direction === self.COL_DIRECTION.LEFT) {
  155. $tom.insertAt(newTd, td);
  156. } else {
  157. $tom.insertNext(newTd, td);
  158. }
  159. } else {
  160. td.colSpan += 1;
  161. }
  162. };
  163. for (i = 0; i < colCount; i += 1) {
  164. tdArrAtBoundaryLine.each(fn);
  165. }
  166. },
  167. /**
  168. * insertColRight
  169. * @param {Trex.Table.Selector} tableSelector
  170. */
  171. insertColRight: function (tableSelector) {
  172. var boundary, indexer;
  173. tableSelector.reloadIndexer();
  174. boundary = tableSelector.getSelected();
  175. if (boundary.isValid()) {
  176. indexer = tableSelector.getIndexer();
  177. this.insertColRightByBoundary(boundary, indexer);
  178. tableSelector.reset();
  179. }
  180. },
  181. /**
  182. * @private
  183. * @param {Trex.TableUtil.Boundary} boundary
  184. * @param {Trex.TableUtil.Indexer} indexer
  185. */
  186. insertColRightByBoundary: function (boundary, indexer) {
  187. var colCount, tdArrAtBoundaryLine, tdArrForClone;
  188. colCount = boundary.right - boundary.left + 1;
  189. tdArrAtBoundaryLine = indexer.getTdArr(new Trex.TableUtil.Boundary({
  190. top: 0,
  191. right: boundary.right,
  192. bottom: indexer.getRowSize() - 1,
  193. left: boundary.right
  194. }));
  195. tdArrForClone = indexer.getTdArrHasRight(boundary.right);
  196. this.addCol(colCount, tdArrAtBoundaryLine, tdArrForClone, this.COL_DIRECTION.RIGHT);
  197. }
  198. });