4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
12 * This class handles table cell selection by faking it using a css class that gets applied
13 * to cells when dragging the mouse from one cell to another.
15 * @class tinymce.tableplugin.CellSelection
18 define("tinymce/tableplugin/CellSelection", [
19 "tinymce/tableplugin/TableGrid",
20 "tinymce/dom/TreeWalker",
22 ], function(TableGrid, TreeWalker, Tools) {
23 return function(editor) {
24 var dom = editor.dom, tableGrid, startCell, startTable, hasCellSelection = true;
27 // Restore selection possibilities
28 editor.getBody().style.webkitUserSelect = '';
30 if (hasCellSelection) {
31 editor.dom.removeClass(
32 editor.dom.select('td.mce-item-selected,th.mce-item-selected'),
36 hasCellSelection = false;
40 // Add cell selection logic
41 editor.on('MouseDown', function(e) {
45 startCell = dom.getParent(e.target, 'td,th');
46 startTable = dom.getParent(startCell, 'table');
50 dom.bind(editor.getDoc(), 'mouseover', function(e) {
51 var sel, table, target = e.target;
53 if (startCell && (tableGrid || target != startCell) && (target.nodeName == 'TD' || target.nodeName == 'TH')) {
54 table = dom.getParent(target, 'table');
55 if (table == startTable) {
57 tableGrid = new TableGrid(editor, table);
58 tableGrid.setStartCell(startCell);
60 editor.getBody().style.webkitUserSelect = 'none';
63 tableGrid.setEndCell(target);
64 hasCellSelection = true;
67 // Remove current selection
68 sel = editor.selection.getSel();
71 if (sel.removeAllRanges) {
72 sel.removeAllRanges();
77 // IE9 might throw errors here
84 editor.on('MouseUp', function() {
85 var rng, sel = editor.selection, selectedCells, walker, node, lastNode, endNode;
87 function setPoint(node, start) {
88 var walker = new TreeWalker(node, node);
92 if (node.nodeType == 3 && Tools.trim(node.nodeValue).length !== 0) {
94 rng.setStart(node, 0);
96 rng.setEnd(node, node.nodeValue.length);
103 if (node.nodeName == 'BR') {
105 rng.setStartBefore(node);
107 rng.setEndBefore(node);
112 } while ((node = (start ? walker.next() : walker.prev())));
115 // Move selection to startCell
118 editor.getBody().style.webkitUserSelect = '';
121 // Try to expand text selection as much as we can only Gecko supports cell selection
122 selectedCells = dom.select('td.mce-item-selected,th.mce-item-selected');
123 if (selectedCells.length > 0) {
124 rng = dom.createRng();
125 node = selectedCells[0];
126 endNode = selectedCells[selectedCells.length - 1];
127 rng.setStartBefore(node);
128 rng.setEndAfter(node);
131 walker = new TreeWalker(node, dom.getParent(selectedCells[0], 'table'));
134 if (node.nodeName == 'TD' || node.nodeName == 'TH') {
135 if (!dom.hasClass(node, 'mce-item-selected')) {
141 } while ((node = walker.next()));
148 editor.nodeChanged();
149 startCell = tableGrid = startTable = null;
153 editor.on('KeyUp', function() {