summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/ui/treevirtual/SelectionManager.js
blob: 94e5154545b13198f7e95de308dbcc9420f55153 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2007 Derrell Lipman

   License:
     LGPL: http://www.gnu.org/licenses/lgpl.html
     EPL: http://www.eclipse.org/org/documents/epl-v10.php
     See the LICENSE file in the project's top-level directory for details.

   Authors:
     * Derrell Lipman (derrell)

************************************************************************ */

/* ************************************************************************

#module(treevirtual)

************************************************************************ */

/**
 * A selection manager. This is a helper class that handles all selection
 * related events and updates a SelectionModel.
 * <p>
 * This Selection Manager differs from its superclass in that we do not want
 * rows to be selected when moving around with the keyboard.
 */
qx.OO.defineClass("qx.ui.treevirtual.SelectionManager",
                  qx.ui.table.SelectionManager,
function(table)
{
  qx.ui.table.SelectionManager.call(this);

  this._table = table;
});


/**
 * Handles a key down event that moved the focus (E.g. up, down, home, end, ...).
 *
 * @param index {Integer} the index that is currently focused.
 * @param evt {Map} the key event.
 */
qx.Proto.handleMoveKeyDown = function(index, evt)
{
  var selectionModel = this.getSelectionModel();

  switch (evt.getModifiers())
  {
  case 0:
    break;

  case qx.event.type.DomEvent.SHIFT_MASK:
    var anchor = selectionModel.getAnchorSelectionIndex();
    
    if (anchor == -1)
    {
      selectionModel.setSelectionInterval(index, index);
    }
    else
    {
      selectionModel.setSelectionInterval(anchor, index);
    }
    break;
  }
};


/**
 * Handles a select event.  First we determine if the click was on the
 * open/close button and toggle the opened/closed state as necessary.  Then,
 * if the click was not on the open/close button or if the table's
 * "openCloseClickSelectsRow" property so indicates, call our superclass to
 * handle the actual row selection.
 *
 * @param index {Integer} the index the event is pointing at.
 * @param evt {Map} the mouse event.
 */
qx.Proto._handleSelectEvent = function(index, evt)
{
  function handleOpenCloseClick(table, index, evt)
  {
    // Get the node to which this event applies
    var node = table.getTableModel().getValue(table.getFocusedColumn(),
                                              table.getFocusedRow());
    if (! node)
    {
      return false;
    }

    // Was this a mouse event?
    if (evt instanceof qx.event.type.MouseEvent)
    {
      // Yup.  Get the order of the columns
      var tcm = table.getTableColumnModel();
      var columnPositions = tcm._getColToXPosMap();

      // Calculate the position of the beginning of the tree column
      var treeCol = table.getTableModel().getTreeColumn();
      var left = qx.html.Location.getClientBoxLeft(table.getElement());
      for (i = 0; i < columnPositions[treeCol].visX; i++)
      {
        left += tcm.getColumnWidth(columnPositions[i].visX);
      }

      // Was the click on the open/close button?  That button begins at
      // (node.level - 1) * 19 + 2 (the latter for padding), and has width 19.
      // We add a bit of latitude to that.
      var x = evt.getClientX();
      var latitude = 2;

      var buttonPos = left + (node.level - 1) * 19 + 2;

      if (x >= buttonPos - latitude && x <= buttonPos + 19 + latitude)
      {
        // Yup.  Toggle the opened state for this node.
        table.toggleOpened(node);
        return table.openCloseClickSelectsRow() ? false : true;
      }
      else
      {
        return false;
      }
    }
    else
    {
      // See which key generated the event
      var identifier = evt.getKeyIdentifier();
      switch (identifier)
      {
      case "Space":
        // This should only select the row, not toggle the opened state
        return false;

      case "Enter":
        // Toggle the open state if open/close is allowed
        if (! node.bHideOpenClose)
        {
          table.toggleOpened(node);
        }
        return table.openCloseClickSelectsRow() ? false : true;

      default:
        // Unrecognized key.  Ignore it.
        return true;
      }
    }
  }

  // Call our local method to toggle the open/close state, if necessary
  var bNoSelect = handleOpenCloseClick(this._table, index, evt);

  // If we haven't been told not to do the selection...
  if (! bNoSelect)
  {
    // then call the Selection Manager's method to do it.
    var Sm = qx.ui.table.SelectionManager;
    Sm.prototype._handleSelectEvent.call(this, index, evt);
  }
};