1 | (function() { |
---|
2 | var __slice = Array.prototype.slice; |
---|
3 | CKEDITOR.on('instanceCreated', function(_arg) { |
---|
4 | var editor; |
---|
5 | editor = _arg.editor; |
---|
6 | return editor.on('pluginsLoaded', function() { |
---|
7 | var $$, DISABLED, ENABLED, MenuElement, OffsetsByLevel, RedrawSubMenuName, cellNodeRegex, cmd, col, colItems, getCellColIndex, getColumnsIndices, getSelectedCells, isFirstColumn, isFirstRow, isLastColumn, isLastRow, key, moveColumn, moveColumnAfter, moveColumnBefore, moveRowAfter, moveRowBefore, redrawContextMenu, row, rowItems, subMenuName, _fn, _i, _len, _ref; |
---|
8 | ENABLED = CKEDITOR.TRISTATE_OFF, DISABLED = CKEDITOR.TRISTATE_DISABLED; |
---|
9 | RedrawSubMenuName = null; |
---|
10 | OffsetsByLevel = []; |
---|
11 | row = editor.getMenuItem('tablerow'); |
---|
12 | rowItems = row.getItems(); |
---|
13 | row.getItems = function() { |
---|
14 | var sel; |
---|
15 | sel = editor.getSelection(); |
---|
16 | return $.extend({ |
---|
17 | tablerow_moveBefore: isFirstRow(sel) ? DISABLED : ENABLED, |
---|
18 | tablerow_moveAfter: isLastRow(sel) ? DISABLED : ENABLED |
---|
19 | }, rowItems); |
---|
20 | }; |
---|
21 | col = editor.getMenuItem('tablecolumn'); |
---|
22 | colItems = col.getItems(); |
---|
23 | col.getItems = function() { |
---|
24 | var sel; |
---|
25 | sel = editor.getSelection(); |
---|
26 | return $.extend({ |
---|
27 | tablecolumn_moveBefore: isFirstColumn(sel) ? DISABLED : ENABLED, |
---|
28 | tablecolumn_moveAfter: isLastColumn(sel) ? DISABLED : ENABLED |
---|
29 | }, colItems); |
---|
30 | }; |
---|
31 | editor.addMenuItems({ |
---|
32 | tablerow_moveBefore: { |
---|
33 | label: 'Move Row Before', |
---|
34 | group: 'tablerow', |
---|
35 | command: 'rowMoveBefore', |
---|
36 | order: 11 |
---|
37 | }, |
---|
38 | tablerow_moveAfter: { |
---|
39 | label: 'Move Row After', |
---|
40 | group: 'tablerow', |
---|
41 | command: 'rowMoveAfter', |
---|
42 | order: 12 |
---|
43 | }, |
---|
44 | tablecolumn_moveBefore: { |
---|
45 | label: 'Move Column Before', |
---|
46 | group: 'tablecolumn', |
---|
47 | command: 'columnMoveBefore', |
---|
48 | order: 11 |
---|
49 | }, |
---|
50 | tablecolumn_moveAfter: { |
---|
51 | label: 'Move Column After', |
---|
52 | group: 'tablecolumn', |
---|
53 | command: 'columnMoveAfter', |
---|
54 | order: 12 |
---|
55 | } |
---|
56 | }); |
---|
57 | editor.addCommand("rowMoveBefore", { |
---|
58 | exec: function(editor) { |
---|
59 | return moveRowBefore(editor.getSelection()); |
---|
60 | } |
---|
61 | }); |
---|
62 | editor.addCommand("rowMoveAfter", { |
---|
63 | exec: function(editor) { |
---|
64 | return moveRowAfter(editor.getSelection()); |
---|
65 | } |
---|
66 | }); |
---|
67 | editor.addCommand("columnMoveBefore", { |
---|
68 | exec: function(editor) { |
---|
69 | return moveColumnBefore(editor.getSelection()); |
---|
70 | } |
---|
71 | }); |
---|
72 | editor.addCommand("columnMoveAfter", { |
---|
73 | exec: function(editor) { |
---|
74 | return moveColumnAfter(editor.getSelection()); |
---|
75 | } |
---|
76 | }); |
---|
77 | _ref = ['rowInsertBefore', 'rowInsertAfter', 'columnInsertBefore', 'columnInsertAfter']; |
---|
78 | _fn = function(cmd, subMenuName) { |
---|
79 | cmd._origExec = cmd.exec; |
---|
80 | return cmd.exec = function() { |
---|
81 | var args, rv; |
---|
82 | args = 1 <= arguments.length ? __slice.call(arguments, 0) : []; |
---|
83 | rv = this._origExec.apply(this, args); |
---|
84 | redrawContextMenu(subMenuName); |
---|
85 | return rv; |
---|
86 | }; |
---|
87 | }; |
---|
88 | for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
---|
89 | key = _ref[_i]; |
---|
90 | cmd = editor._.commands[key]; |
---|
91 | if (cmd._origExec) { |
---|
92 | continue; |
---|
93 | } |
---|
94 | subMenuName = "table" + (key.replace(/Insert.*/, '')); |
---|
95 | _fn(cmd, subMenuName); |
---|
96 | } |
---|
97 | MenuElement = null; |
---|
98 | CKEDITOR.ui.on('ready', function(_arg2) { |
---|
99 | var data, _ref2; |
---|
100 | data = _arg2.data; |
---|
101 | if ((_ref2 = data._) != null ? _ref2.panel : void 0) { |
---|
102 | return MenuElement = data; |
---|
103 | } |
---|
104 | }); |
---|
105 | editor.on('menuShow', function(_arg2) { |
---|
106 | var data, level, panel; |
---|
107 | data = _arg2.data; |
---|
108 | if (!RedrawSubMenuName) { |
---|
109 | return; |
---|
110 | } |
---|
111 | panel = data[0]; |
---|
112 | level = panel._.definition.level; |
---|
113 | return setTimeout((function() { |
---|
114 | var idx, item, _fn2, _len2, _ref2; |
---|
115 | panel.element.setStyles(OffsetsByLevel[level]); |
---|
116 | _ref2 = MenuElement.items; |
---|
117 | _fn2 = function(MenuElement, idx) { |
---|
118 | return setTimeout((function() { |
---|
119 | return MenuElement._.showSubMenu(idx); |
---|
120 | }), 100); |
---|
121 | }; |
---|
122 | for (idx = 0, _len2 = _ref2.length; idx < _len2; idx++) { |
---|
123 | item = _ref2[idx]; |
---|
124 | if (item.name !== RedrawSubMenuName) { |
---|
125 | continue; |
---|
126 | } |
---|
127 | _fn2(MenuElement, idx); |
---|
128 | RedrawSubMenuName = null; |
---|
129 | return; |
---|
130 | } |
---|
131 | }), 1); |
---|
132 | }); |
---|
133 | isFirstRow = function(selection) { |
---|
134 | var cells, firstCell, mapCell, maxRowSpan, rowCells, startRow, startRowIndex, table; |
---|
135 | cells = getSelectedCells(selection); |
---|
136 | firstCell = cells[0]; |
---|
137 | startRow = firstCell.getParent(); |
---|
138 | startRowIndex = startRow.$.rowIndex; |
---|
139 | if (startRowIndex === 0) { |
---|
140 | return true; |
---|
141 | } |
---|
142 | table = firstCell.getAscendant('table'); |
---|
143 | rowCells = table.$.rows[0].cells; |
---|
144 | maxRowSpan = Math.max.apply(Math, (function() { |
---|
145 | var _j, _len2, _results; |
---|
146 | _results = []; |
---|
147 | for (_j = 0, _len2 = rowCells.length; _j < _len2; _j++) { |
---|
148 | mapCell = rowCells[_j]; |
---|
149 | _results.push(mapCell.rowSpan); |
---|
150 | } |
---|
151 | return _results; |
---|
152 | })()); |
---|
153 | return startRowIndex <= (maxRowSpan - 1); |
---|
154 | }; |
---|
155 | isLastRow = function(selection) { |
---|
156 | var cells, endRow, endRowIndex, lastCell, mapCell, maxRowSpan, rowCells, table; |
---|
157 | cells = getSelectedCells(selection); |
---|
158 | lastCell = cells[cells.length - 1]; |
---|
159 | table = lastCell.getAscendant('table'); |
---|
160 | endRow = lastCell.getParent(); |
---|
161 | rowCells = endRow.$.cells; |
---|
162 | endRowIndex = endRow.$.rowIndex; |
---|
163 | maxRowSpan = Math.max.apply(Math, (function() { |
---|
164 | var _j, _len2, _results; |
---|
165 | _results = []; |
---|
166 | for (_j = 0, _len2 = rowCells.length; _j < _len2; _j++) { |
---|
167 | mapCell = rowCells[_j]; |
---|
168 | _results.push(mapCell.rowSpan); |
---|
169 | } |
---|
170 | return _results; |
---|
171 | })()); |
---|
172 | return (endRowIndex + maxRowSpan) >= table.$.rows.length; |
---|
173 | }; |
---|
174 | isFirstColumn = function(selection) { |
---|
175 | var cells, startColIndex; |
---|
176 | cells = getSelectedCells(selection); |
---|
177 | startColIndex = getColumnsIndices(cells, 1); |
---|
178 | return startColIndex === 0; |
---|
179 | }; |
---|
180 | isLastColumn = function(selection) { |
---|
181 | var cells, colIndex, endColIndex, lastRow, mapCell, rowCells, _j, _len2; |
---|
182 | cells = getSelectedCells(selection); |
---|
183 | endColIndex = getColumnsIndices(cells); |
---|
184 | lastRow = cells[cells.length - 1].getParent(); |
---|
185 | rowCells = lastRow.$.cells; |
---|
186 | colIndex = -1; |
---|
187 | for (_j = 0, _len2 = rowCells.length; _j < _len2; _j++) { |
---|
188 | mapCell = rowCells[_j]; |
---|
189 | colIndex += mapCell.colSpan; |
---|
190 | if (colIndex > endColIndex) { |
---|
191 | return false; |
---|
192 | } |
---|
193 | } |
---|
194 | return true; |
---|
195 | }; |
---|
196 | $$ = function(elm) { |
---|
197 | return new CKEDITOR.dom.element(elm); |
---|
198 | }; |
---|
199 | moveRowBefore = function(selection) { |
---|
200 | var cells, endRow, firstCell, prevRow, startRowIndex, table; |
---|
201 | cells = getSelectedCells(selection); |
---|
202 | endRow = cells[cells.length - 1].getParent(); |
---|
203 | firstCell = cells[0]; |
---|
204 | startRowIndex = firstCell.getParent().$.rowIndex; |
---|
205 | table = firstCell.getAscendant('table'); |
---|
206 | prevRow = table.$.rows[startRowIndex - 1]; |
---|
207 | $$(prevRow).insertAfter(endRow); |
---|
208 | return redrawContextMenu('tablerow'); |
---|
209 | }; |
---|
210 | moveRowAfter = function(selection) { |
---|
211 | var cells, endRowIndex, lastCell, nextRow, startRow, table; |
---|
212 | cells = getSelectedCells(selection); |
---|
213 | startRow = cells[0].getParent(); |
---|
214 | lastCell = cells[cells.length - 1]; |
---|
215 | endRowIndex = lastCell.getParent().$.rowIndex + lastCell.$.rowSpan - 1; |
---|
216 | table = lastCell.getAscendant('table'); |
---|
217 | nextRow = table.$.rows[endRowIndex + 1]; |
---|
218 | $$(nextRow).insertBefore(startRow); |
---|
219 | return redrawContextMenu('tablerow'); |
---|
220 | }; |
---|
221 | moveColumn = function(selection, isBefore) { |
---|
222 | var cells, endColIndex, row, rowCells, startColIndex, table, _j, _len2, _ref2; |
---|
223 | cells = getSelectedCells(selection); |
---|
224 | table = cells[0].getAscendant('table'); |
---|
225 | startColIndex = getColumnsIndices(cells, 1); |
---|
226 | endColIndex = getColumnsIndices(cells); |
---|
227 | _ref2 = table.$.rows; |
---|
228 | for (_j = 0, _len2 = _ref2.length; _j < _len2; _j++) { |
---|
229 | row = _ref2[_j]; |
---|
230 | rowCells = row.cells; |
---|
231 | if (isBefore) { |
---|
232 | $$(rowCells[startColIndex - 1]).insertAfter($$(rowCells[endColIndex])); |
---|
233 | } else { |
---|
234 | $$(rowCells[endColIndex + 1]).insertBefore($$(rowCells[startColIndex])); |
---|
235 | } |
---|
236 | } |
---|
237 | return redrawContextMenu('tablecolumn'); |
---|
238 | }; |
---|
239 | moveColumnBefore = function(selection) { |
---|
240 | return moveColumn(selection, true); |
---|
241 | }; |
---|
242 | moveColumnAfter = function(selection) { |
---|
243 | return moveColumn(selection, false); |
---|
244 | }; |
---|
245 | redrawContextMenu = function(subMenuName) { |
---|
246 | var $parent, elm, idx, _len2, _ref2; |
---|
247 | RedrawSubMenuName = subMenuName; |
---|
248 | _ref2 = $('.cke_contextmenu').get(); |
---|
249 | for (idx = 0, _len2 = _ref2.length; idx < _len2; idx++) { |
---|
250 | elm = _ref2[idx]; |
---|
251 | $parent = $(elm).parent(); |
---|
252 | OffsetsByLevel[idx] = { |
---|
253 | top: $parent.css('top'), |
---|
254 | left: $parent.css('left') |
---|
255 | }; |
---|
256 | } |
---|
257 | return editor.execCommand('contextMenu'); |
---|
258 | }; |
---|
259 | cellNodeRegex = /^(?:td|th)$/; |
---|
260 | getSelectedCells = function(selection) { |
---|
261 | var bookmarks, database, i, moveOutOfCellGuard, nearestCell, node, parent, range, ranges, retval, startNode, walker, _j, _len2; |
---|
262 | bookmarks = selection.createBookmarks(); |
---|
263 | ranges = selection.getRanges(); |
---|
264 | retval = []; |
---|
265 | database = {}; |
---|
266 | i = 0; |
---|
267 | moveOutOfCellGuard = function(node) { |
---|
268 | if (retval.length > 0) { |
---|
269 | return; |
---|
270 | } |
---|
271 | if (node.type === CKEDITOR.NODE_ELEMENT && cellNodeRegex.test(node.getName()) && !node.getCustomData("selected_cell")) { |
---|
272 | CKEDITOR.dom.element.setMarker(database, node, "selected_cell", true); |
---|
273 | retval.push(node); |
---|
274 | } |
---|
275 | }; |
---|
276 | for (_j = 0, _len2 = ranges.length; _j < _len2; _j++) { |
---|
277 | range = ranges[_j]; |
---|
278 | if (range.collapsed) { |
---|
279 | startNode = range.getCommonAncestor(); |
---|
280 | nearestCell = startNode.getAscendant("td", true) || startNode.getAscendant("th", true); |
---|
281 | if (nearestCell) { |
---|
282 | retval.push(nearestCell); |
---|
283 | } |
---|
284 | } else { |
---|
285 | walker = new CKEDITOR.dom.walker(range); |
---|
286 | node = void 0; |
---|
287 | walker.guard = moveOutOfCellGuard; |
---|
288 | while ((node = walker.next())) { |
---|
289 | parent = node.getAscendant("td") || node.getAscendant("th"); |
---|
290 | if (parent && !parent.getCustomData("selected_cell")) { |
---|
291 | CKEDITOR.dom.element.setMarker(database, parent, "selected_cell", true); |
---|
292 | retval.push(parent); |
---|
293 | } |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | CKEDITOR.dom.element.clearAllMarkers(database); |
---|
298 | selection.selectBookmarks(bookmarks); |
---|
299 | return retval; |
---|
300 | }; |
---|
301 | getColumnsIndices = function(cells, isStart) { |
---|
302 | var cell, colIndex, retval, _j, _len2; |
---|
303 | retval = (isStart ? Infinity : 0); |
---|
304 | for (_j = 0, _len2 = cells.length; _j < _len2; _j++) { |
---|
305 | cell = cells[_j]; |
---|
306 | colIndex = getCellColIndex(cell, isStart); |
---|
307 | if (isStart) { |
---|
308 | if (colIndex < retval) { |
---|
309 | retval = colIndex; |
---|
310 | } |
---|
311 | } else { |
---|
312 | if (colIndex > retval) { |
---|
313 | retval = colIndex; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | return retval; |
---|
318 | }; |
---|
319 | return getCellColIndex = function(cell, isStart) { |
---|
320 | var colIndex, mapCell, rowCells, _j, _len2; |
---|
321 | row = cell.getParent(); |
---|
322 | rowCells = row.$.cells; |
---|
323 | colIndex = 0; |
---|
324 | for (_j = 0, _len2 = rowCells.length; _j < _len2; _j++) { |
---|
325 | mapCell = rowCells[_j]; |
---|
326 | colIndex += (isStart ? 1 : mapCell.colSpan); |
---|
327 | if (mapCell === cell.$) { |
---|
328 | break; |
---|
329 | } |
---|
330 | } |
---|
331 | return colIndex - 1; |
---|
332 | }; |
---|
333 | }); |
---|
334 | }); |
---|
335 | }).call(this); |
---|