Mercurial > public > sg101
comparison static/js/tiny_mce/plugins/table/editor_plugin_src.js @ 442:6c182ceb7147
Fixing #217; upgrade TinyMCE to 3.4.2 and enable the paste plugin.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Thu, 26 May 2011 00:43:49 +0000 |
parents | 88b2b9cb8c1f |
children |
comparison
equal
deleted
inserted
replaced
441:33d0c55e57a9 | 442:6c182ceb7147 |
---|---|
102 | 102 |
103 function getSpanVal(td, name) { | 103 function getSpanVal(td, name) { |
104 return parseInt(td.getAttribute(name) || 1); | 104 return parseInt(td.getAttribute(name) || 1); |
105 }; | 105 }; |
106 | 106 |
107 function setSpanVal(td, name, val) { | |
108 if (td) { | |
109 val = parseInt(val); | |
110 | |
111 if (val === 1) | |
112 td.removeAttribute(name, 1); | |
113 else | |
114 td.setAttribute(name, val, 1); | |
115 } | |
116 } | |
117 | |
107 function isCellSelected(cell) { | 118 function isCellSelected(cell) { |
108 return dom.hasClass(cell.elm, 'mceSelected') || cell == selectedCell; | 119 return cell && (dom.hasClass(cell.elm, 'mceSelected') || cell == selectedCell); |
109 }; | 120 }; |
110 | 121 |
111 function getSelectedRows() { | 122 function getSelectedRows() { |
112 var rows = []; | 123 var rows = []; |
113 | 124 |
153 curNode = node; | 164 curNode = node; |
154 }); | 165 }); |
155 | 166 |
156 // Add something to the inner node | 167 // Add something to the inner node |
157 if (curNode) | 168 if (curNode) |
158 curNode.innerHTML = tinymce.isIE ? ' ' : '<br _mce_bogus="1" />'; | 169 curNode.innerHTML = tinymce.isIE ? ' ' : '<br data-mce-bogus="1" />'; |
159 | 170 |
160 return false; | 171 return false; |
161 } | 172 } |
162 }, 'childNodes'); | 173 }, 'childNodes'); |
163 | 174 |
164 cell = cloneNode(cell, false); | 175 cell = cloneNode(cell, false); |
165 cell.rowSpan = cell.colSpan = 1; | 176 setSpanVal(cell, 'rowSpan', 1); |
177 setSpanVal(cell, 'colSpan', 1); | |
166 | 178 |
167 if (formatNode) { | 179 if (formatNode) { |
168 cell.appendChild(formatNode); | 180 cell.appendChild(formatNode); |
169 } else { | 181 } else { |
170 if (!tinymce.isIE) | 182 if (!tinymce.isIE) |
171 cell.innerHTML = '<br _mce_bogus="1" />'; | 183 cell.innerHTML = '<br data-mce-bogus="1" />'; |
172 } | 184 } |
173 | 185 |
174 return cell; | 186 return cell; |
175 }; | 187 }; |
176 | 188 |
248 cell = cell.elm; | 260 cell = cell.elm; |
249 colSpan = getSpanVal(cell, 'colspan'); | 261 colSpan = getSpanVal(cell, 'colspan'); |
250 rowSpan = getSpanVal(cell, 'rowspan'); | 262 rowSpan = getSpanVal(cell, 'rowspan'); |
251 | 263 |
252 if (colSpan > 1 || rowSpan > 1) { | 264 if (colSpan > 1 || rowSpan > 1) { |
253 cell.colSpan = cell.rowSpan = 1; | 265 setSpanVal(cell, 'rowSpan', 1); |
266 setSpanVal(cell, 'colSpan', 1); | |
254 | 267 |
255 // Insert cells right | 268 // Insert cells right |
256 for (i = 0; i < colSpan - 1; i++) | 269 for (i = 0; i < colSpan - 1; i++) |
257 dom.insertAfter(cloneCell(cell), cell); | 270 dom.insertAfter(cloneCell(cell), cell); |
258 | 271 |
262 }); | 275 }); |
263 }); | 276 }); |
264 }; | 277 }; |
265 | 278 |
266 function merge(cell, cols, rows) { | 279 function merge(cell, cols, rows) { |
267 var startX, startY, endX, endY, x, y, startCell, endCell, cell, children; | 280 var startX, startY, endX, endY, x, y, startCell, endCell, cell, children, count; |
268 | 281 |
269 // Use specified cell and cols/rows | 282 // Use specified cell and cols/rows |
270 if (cell) { | 283 if (cell) { |
271 pos = getPos(cell); | 284 pos = getPos(cell); |
272 startX = pos.x; | 285 startX = pos.x; |
291 split(); | 304 split(); |
292 buildGrid(); | 305 buildGrid(); |
293 | 306 |
294 // Set row/col span to start cell | 307 // Set row/col span to start cell |
295 startCell = getCell(startX, startY).elm; | 308 startCell = getCell(startX, startY).elm; |
296 startCell.colSpan = (endX - startX) + 1; | 309 setSpanVal(startCell, 'colSpan', (endX - startX) + 1); |
297 startCell.rowSpan = (endY - startY) + 1; | 310 setSpanVal(startCell, 'rowSpan', (endY - startY) + 1); |
298 | 311 |
299 // Remove other cells and add it's contents to the start cell | 312 // Remove other cells and add it's contents to the start cell |
300 for (y = startY; y <= endY; y++) { | 313 for (y = startY; y <= endY; y++) { |
301 for (x = startX; x <= endX; x++) { | 314 for (x = startX; x <= endX; x++) { |
315 if (!grid[y] || !grid[y][x]) | |
316 continue; | |
317 | |
302 cell = grid[y][x].elm; | 318 cell = grid[y][x].elm; |
303 | 319 |
304 if (cell != startCell) { | 320 if (cell != startCell) { |
305 // Move children to startCell | 321 // Move children to startCell |
306 children = tinymce.grep(cell.childNodes); | 322 children = tinymce.grep(cell.childNodes); |
307 each(children, function(node, i) { | 323 each(children, function(node) { |
308 // Jump over last BR element | 324 startCell.appendChild(node); |
309 if (node.nodeName != 'BR' || i != children.length - 1) | |
310 startCell.appendChild(node); | |
311 }); | 325 }); |
312 | 326 |
327 // Remove bogus nodes if there is children in the target cell | |
328 if (children.length) { | |
329 children = tinymce.grep(startCell.childNodes); | |
330 count = 0; | |
331 each(children, function(node) { | |
332 if (node.nodeName == 'BR' && dom.getAttrib(node, 'data-mce-bogus') && count++ < children.length - 1) | |
333 startCell.removeChild(node); | |
334 }); | |
335 } | |
336 | |
313 // Remove cell | 337 // Remove cell |
314 dom.remove(cell); | 338 dom.remove(cell); |
315 } | 339 } |
316 } | 340 } |
317 } | 341 } |
320 cleanup(); | 344 cleanup(); |
321 } | 345 } |
322 }; | 346 }; |
323 | 347 |
324 function insertRow(before) { | 348 function insertRow(before) { |
325 var posY, cell, lastCell, x, rowElm, newRow, newCell, otherCell; | 349 var posY, cell, lastCell, x, rowElm, newRow, newCell, otherCell, rowSpan; |
326 | 350 |
327 // Find first/last row | 351 // Find first/last row |
328 each(grid, function(row, y) { | 352 each(grid, function(row, y) { |
329 each(row, function(cell, x) { | 353 each(row, function(cell, x) { |
330 if (isCellSelected(cell)) { | 354 if (isCellSelected(cell)) { |
341 if (before) | 365 if (before) |
342 return !posY; | 366 return !posY; |
343 }); | 367 }); |
344 | 368 |
345 for (x = 0; x < grid[0].length; x++) { | 369 for (x = 0; x < grid[0].length; x++) { |
370 // Cell not found could be because of an invalid table structure | |
371 if (!grid[posY][x]) | |
372 continue; | |
373 | |
346 cell = grid[posY][x].elm; | 374 cell = grid[posY][x].elm; |
347 | 375 |
348 if (cell != lastCell) { | 376 if (cell != lastCell) { |
349 if (!before) { | 377 if (!before) { |
350 rowSpan = getSpanVal(cell, 'rowspan'); | 378 rowSpan = getSpanVal(cell, 'rowspan'); |
351 if (rowSpan > 1) { | 379 if (rowSpan > 1) { |
352 cell.rowSpan = rowSpan + 1; | 380 setSpanVal(cell, 'rowSpan', rowSpan + 1); |
353 continue; | 381 continue; |
354 } | 382 } |
355 } else { | 383 } else { |
356 // Check if cell above can be expanded | 384 // Check if cell above can be expanded |
357 if (posY > 0 && grid[posY - 1][x]) { | 385 if (posY > 0 && grid[posY - 1][x]) { |
358 otherCell = grid[posY - 1][x].elm; | 386 otherCell = grid[posY - 1][x].elm; |
359 rowSpan = getSpanVal(otherCell, 'rowspan'); | 387 rowSpan = getSpanVal(otherCell, 'rowSpan'); |
360 if (rowSpan > 1) { | 388 if (rowSpan > 1) { |
361 otherCell.rowSpan = rowSpan + 1; | 389 setSpanVal(otherCell, 'rowSpan', rowSpan + 1); |
362 continue; | 390 continue; |
363 } | 391 } |
364 } | 392 } |
365 } | 393 } |
366 | 394 |
367 // Insert new cell into new row | 395 // Insert new cell into new row |
368 newCell = cloneCell(cell) | 396 newCell = cloneCell(cell); |
369 newCell.colSpan = cell.colSpan; | 397 setSpanVal(newCell, 'colSpan', cell.colSpan); |
398 | |
370 newRow.appendChild(newCell); | 399 newRow.appendChild(newCell); |
371 | 400 |
372 lastCell = cell; | 401 lastCell = cell; |
373 } | 402 } |
374 } | 403 } |
398 if (before) | 427 if (before) |
399 return !posX; | 428 return !posX; |
400 }); | 429 }); |
401 | 430 |
402 each(grid, function(row, y) { | 431 each(grid, function(row, y) { |
403 var cell = row[posX].elm, rowSpan, colSpan; | 432 var cell, rowSpan, colSpan; |
404 | 433 |
434 if (!row[posX]) | |
435 return; | |
436 | |
437 cell = row[posX].elm; | |
405 if (cell != lastCell) { | 438 if (cell != lastCell) { |
406 colSpan = getSpanVal(cell, 'colspan'); | 439 colSpan = getSpanVal(cell, 'colspan'); |
407 rowSpan = getSpanVal(cell, 'rowspan'); | 440 rowSpan = getSpanVal(cell, 'rowspan'); |
408 | 441 |
409 if (colSpan == 1) { | 442 if (colSpan == 1) { |
413 } else { | 446 } else { |
414 cell.parentNode.insertBefore(cloneCell(cell), cell); | 447 cell.parentNode.insertBefore(cloneCell(cell), cell); |
415 fillLeftDown(posX, y, rowSpan - 1, colSpan); | 448 fillLeftDown(posX, y, rowSpan - 1, colSpan); |
416 } | 449 } |
417 } else | 450 } else |
418 cell.colSpan++; | 451 setSpanVal(cell, 'colSpan', cell.colSpan + 1); |
419 | 452 |
420 lastCell = cell; | 453 lastCell = cell; |
421 } | 454 } |
422 }); | 455 }); |
423 }; | 456 }; |
430 each(row, function(cell, x) { | 463 each(row, function(cell, x) { |
431 if (isCellSelected(cell) && tinymce.inArray(cols, x) === -1) { | 464 if (isCellSelected(cell) && tinymce.inArray(cols, x) === -1) { |
432 each(grid, function(row) { | 465 each(grid, function(row) { |
433 var cell = row[x].elm, colSpan; | 466 var cell = row[x].elm, colSpan; |
434 | 467 |
435 colSpan = getSpanVal(cell, 'colspan'); | 468 colSpan = getSpanVal(cell, 'colSpan'); |
436 | 469 |
437 if (colSpan > 1) | 470 if (colSpan > 1) |
438 cell.colSpan = colSpan - 1; | 471 setSpanVal(cell, 'colSpan', colSpan - 1); |
439 else | 472 else |
440 dom.remove(cell); | 473 dom.remove(cell); |
441 }); | 474 }); |
442 | 475 |
443 cols.push(x); | 476 cols.push(x); |
456 | 489 |
457 nextTr = dom.getNext(tr, 'tr'); | 490 nextTr = dom.getNext(tr, 'tr'); |
458 | 491 |
459 // Move down row spanned cells | 492 // Move down row spanned cells |
460 each(tr.cells, function(cell) { | 493 each(tr.cells, function(cell) { |
461 var rowSpan = getSpanVal(cell, 'rowspan'); | 494 var rowSpan = getSpanVal(cell, 'rowSpan'); |
462 | 495 |
463 if (rowSpan > 1) { | 496 if (rowSpan > 1) { |
464 cell.rowSpan = rowSpan - 1; | 497 setSpanVal(cell, 'rowSpan', rowSpan - 1); |
465 pos = getPos(cell); | 498 pos = getPos(cell); |
466 fillLeftDown(pos.x, pos.y, 1, 1); | 499 fillLeftDown(pos.x, pos.y, 1, 1); |
467 } | 500 } |
468 }); | 501 }); |
469 | 502 |
473 var rowSpan; | 506 var rowSpan; |
474 | 507 |
475 cell = cell.elm; | 508 cell = cell.elm; |
476 | 509 |
477 if (cell != lastCell) { | 510 if (cell != lastCell) { |
478 rowSpan = getSpanVal(cell, 'rowspan'); | 511 rowSpan = getSpanVal(cell, 'rowSpan'); |
479 | 512 |
480 if (rowSpan <= 1) | 513 if (rowSpan <= 1) |
481 dom.remove(cell); | 514 dom.remove(cell); |
482 else | 515 else |
483 cell.rowSpan = rowSpan - 1; | 516 setSpanVal(cell, 'rowSpan', rowSpan - 1); |
484 | 517 |
485 lastCell = cell; | 518 lastCell = cell; |
486 } | 519 } |
487 }); | 520 }); |
488 }; | 521 }; |
546 var cellCount = row.cells.length, cell; | 579 var cellCount = row.cells.length, cell; |
547 | 580 |
548 // Remove col/rowspans | 581 // Remove col/rowspans |
549 for (i = 0; i < cellCount; i++) { | 582 for (i = 0; i < cellCount; i++) { |
550 cell = row.cells[i]; | 583 cell = row.cells[i]; |
551 cell.colSpan = cell.rowSpan = 1; | 584 setSpanVal(cell, 'colSpan', 1); |
585 setSpanVal(cell, 'rowSpan', 1); | |
552 } | 586 } |
553 | 587 |
554 // Needs more cells | 588 // Needs more cells |
555 for (i = cellCount; i < targetCellCount; i++) | 589 for (i = cellCount; i < targetCellCount; i++) |
556 row.appendChild(cloneCell(row.cells[cellCount - 1])); | 590 row.appendChild(cloneCell(row.cells[cellCount - 1])); |
688 // Remove current selection | 722 // Remove current selection |
689 dom.removeClass(dom.select('td.mceSelected,th.mceSelected'), 'mceSelected'); | 723 dom.removeClass(dom.select('td.mceSelected,th.mceSelected'), 'mceSelected'); |
690 | 724 |
691 // Add new selection | 725 // Add new selection |
692 for (y = startY; y <= maxY; y++) { | 726 for (y = startY; y <= maxY; y++) { |
693 for (x = startX; x <= maxX; x++) | 727 for (x = startX; x <= maxX; x++) { |
694 dom.addClass(grid[y][x].elm, 'mceSelected'); | 728 if (grid[y][x]) |
729 dom.addClass(grid[y][x].elm, 'mceSelected'); | |
730 } | |
695 } | 731 } |
696 } | 732 } |
697 }; | 733 }; |
698 | 734 |
699 // Expose to public | 735 // Expose to public |
752 // Select whole table is a table border is clicked | 788 // Select whole table is a table border is clicked |
753 if (!tinymce.isIE) { | 789 if (!tinymce.isIE) { |
754 ed.onClick.add(function(ed, e) { | 790 ed.onClick.add(function(ed, e) { |
755 e = e.target; | 791 e = e.target; |
756 | 792 |
757 if (e.nodeName === 'TABLE') | 793 if (e.nodeName === 'TABLE') { |
758 ed.selection.select(e); | 794 ed.selection.select(e); |
795 ed.nodeChanged(); | |
796 } | |
759 }); | 797 }); |
760 } | 798 } |
799 | |
800 ed.onPreProcess.add(function(ed, args) { | |
801 var nodes, i, node, dom = ed.dom, value; | |
802 | |
803 nodes = dom.select('table', args.node); | |
804 i = nodes.length; | |
805 while (i--) { | |
806 node = nodes[i]; | |
807 dom.setAttrib(node, 'data-mce-style', ''); | |
808 | |
809 if ((value = dom.getAttrib(node, 'width'))) { | |
810 dom.setStyle(node, 'width', value); | |
811 dom.setAttrib(node, 'width', ''); | |
812 } | |
813 | |
814 if ((value = dom.getAttrib(node, 'height'))) { | |
815 dom.setStyle(node, 'height', value); | |
816 dom.setAttrib(node, 'height', ''); | |
817 } | |
818 } | |
819 }); | |
761 | 820 |
762 // Handle node change updates | 821 // Handle node change updates |
763 ed.onNodeChange.add(function(ed, cm, n) { | 822 ed.onNodeChange.add(function(ed, cm, n) { |
764 var p; | 823 var p; |
765 | 824 |
817 } | 876 } |
818 | 877 |
819 // Remove current selection | 878 // Remove current selection |
820 sel = ed.selection.getSel(); | 879 sel = ed.selection.getSel(); |
821 | 880 |
822 if (sel.removeAllRanges) | 881 try { |
823 sel.removeAllRanges(); | 882 if (sel.removeAllRanges) |
824 else | 883 sel.removeAllRanges(); |
825 sel.empty(); | 884 else |
885 sel.empty(); | |
886 } catch (ex) { | |
887 // IE9 might throw errors here | |
888 } | |
826 | 889 |
827 e.preventDefault(); | 890 e.preventDefault(); |
828 } | 891 } |
829 }); | 892 }); |
830 | 893 |