summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/ui/table/SelectionModel.js
blob: fb0f6b731720f79e96bfd3517ecd8877c8728c8c (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* ************************************************************************

   qooxdoo - the new era of web development

   http://qooxdoo.org

   Copyright:
     2006 by STZ-IDA, Germany, http://www.stz-ida.de

   License:
     LGPL 2.1: http://www.gnu.org/licenses/lgpl.html

   Authors:
     * Til Schneider (til132)

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

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

#module(ui_table)

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

/**
 * A selection model.
 *
 * @event changeSelection {qx.event.type.Event} Fired when the selection has
 *        changed.
 */
qx.OO.defineClass("qx.ui.table.SelectionModel", qx.core.Target,
function() {
  qx.core.Target.call(this);

  this._selectedRangeArr = [];
  this._anchorSelectionIndex = -1;
  this._leadSelectionIndex = -1;
  this.hasBatchModeRefCount = 0;
  this._hadChangeEventInBatchMode = false;
});


/** {int} The selection mode "none". Nothing can ever be selected. */
qx.Class.NO_SELECTION = 1;

/** {int} The selection mode "single". This mode only allows one selected item. */
qx.Class.SINGLE_SELECTION = 2;

/**
 * (int) The selection mode "single interval". This mode only allows one
 * continuous interval of selected items.
 */
qx.Class.SINGLE_INTERVAL_SELECTION = 3;

/**
 * (int) The selection mode "multiple interval". This mode only allows any
 * selection.
 */
qx.Class.MULTIPLE_INTERVAL_SELECTION = 4;


/**
 * (int) the selection mode.
 */
qx.OO.addProperty({ name:"selectionMode", type:"number",
  defaultValue:qx.Class.SINGLE_SELECTION,
  allowNull:false,
  possibleValues:[ qx.Class.NO_SELECTION,
           qx.Class.SINGLE_SELECTION,
           qx.Class.SINGLE_INTERVAL_SELECTION,
           qx.Class.MULTIPLE_INTERVAL_SELECTION  ] });

// selectionMode property modifier
qx.Proto._modifySelectionMode = function(selectionMode) {
  if (selectionMode == qx.ui.table.SelectionModel.NO_SELECTION) {
    this.clearSelection();
  }
  return true;
}


/**
 * <p>Activates / Deactivates batch mode. In batch mode, no change events will be thrown but
 * will be collected instead. When batch mode is turned off again and any events have
 * been collected, one event is thrown to inform the listeners.</p>
 *
 * <p>This method supports nested calling, i. e. batch mode can be turned more than once.
 * In this case, batch mode will not end until it has been turned off once for each
 * turning on.</p>
 *
 * @param batchMode {boolean} true to activate batch mode, false to deactivate
 * @return {boolean} true if batch mode is active, false otherwise
 * @throws Error if batch mode is turned off once more than it has been turned on
 */
qx.Proto.setBatchMode = function(batchMode) {
  if (batchMode){
    this.hasBatchModeRefCount += 1;
  } else {
    if (this.hasBatchModeRefCount == 0){
      throw new Error("Try to turn off batch mode althoug it was not turned on.")
    }
    this.hasBatchModeRefCount -= 1;
    if (this._hadChangeEventInBatchMode){
      this._hadChangeEventInBatchMode = false;
      this._fireChangeSelection();
    }
  }
  return this.hasBatchMode();
}


/**
 * <p>Returns whether batch mode is active. See setter for a description of batch mode.</p>
 *
 * @return {boolean} true if batch mode is active, false otherwise
 */
qx.Proto.hasBatchMode = function() {
  return this.hasBatchModeRefCount > 0;
}


/**
 * Returns the first argument of the last call to {@link #setSelectionInterval()},
 * {@link #addSelectionInterval()} or {@link #removeSelectionInterval()}.
 *
 * @return {int} the ancor selection index.
 */
qx.Proto.getAnchorSelectionIndex = function() {
  return this._anchorSelectionIndex;
}


/**
 * Returns the second argument of the last call to {@link #setSelectionInterval()},
 * {@link #addSelectionInterval()} or {@link #removeSelectionInterval()}.
 *
 * @return {int} the lead selection index.
 */
qx.Proto.getLeadSelectionIndex = function() {
  return this._leadSelectionIndex;
}


/**
 * Clears the selection.
 */
qx.Proto.clearSelection = function() {
  if (! this.isSelectionEmpty()) {
    this._clearSelection();
    this._fireChangeSelection();
  }
}


/**
 * Returns whether the selection is empty.
 *
 * @return {boolean} whether the selection is empty.
 */
qx.Proto.isSelectionEmpty = function() {
  return this._selectedRangeArr.length == 0;
}


/**
 * Returns the number of selected items.
 *
 * @return {int} the number of selected items.
 */
qx.Proto.getSelectedCount = function() {
  var selectedCount = 0;
  for (var i = 0; i < this._selectedRangeArr.length; i++) {
    var range = this._selectedRangeArr[i];
    selectedCount += range.maxIndex - range.minIndex + 1;
  }

  return selectedCount;
}


/**
 * Returns whether a index is selected.
 *
 * @param index {int} the index to check.
 * @return {boolean} whether the index is selected.
 */
qx.Proto.isSelectedIndex = function(index) {
  for (var i = 0; i < this._selectedRangeArr.length; i++) {
    var range = this._selectedRangeArr[i];

    if (index >= range.minIndex && index <= range.maxIndex) {
      return true;
    }
  }

  return false;
}


/**
 * Returns the selected ranges as an array. Each array element has a
 * <code>minIndex</code> and a <code>maxIndex</code> property.
 *
 * @return {Map[]} the selected ranges.
 */
qx.Proto.getSelectedRanges = function() {
  // clone the selection array and the individual elements - this prevents the
  // caller from messing with the internal model
  var retVal = [];
  for (var i = 0; i < this._selectedRangeArr.length; i++) {
    retVal.push({minIndex: this._selectedRangeArr[i].minIndex,
                 maxIndex: this._selectedRangeArr[i].maxIndex});
  }
  return retVal;
}


/**
 * Calls a iterator function for each selected index.
 * <p>
 * Usage Example:
 * <pre>
 * var selectedRowData = [];
 * mySelectionModel.iterateSelection(function(index) {
 *   selectedRowData.push(myTableModel.getRowData(index));
 * });
 * </pre>
 *
 * @param iterator {Function} the function to call for each selected index.
 *        Gets the current index as parameter.
 * @param object {var ? null} the object to use when calling the handler.
 *        (this object will be available via "this" in the iterator)
 */
qx.Proto.iterateSelection = function(iterator, object) {
  for (var i = 0; i < this._selectedRangeArr.length; i++) {
    for (var j = this._selectedRangeArr[i].minIndex; j <= this._selectedRangeArr[i].maxIndex; j++) {
      iterator.call(object, j);
    }
  }
};


/**
 * Sets the selected interval. This will clear the former selection.
 *
 * @param fromIndex {int} the first index of the selection (including).
 * @param toIndex {int} the last index of the selection (including).
 */
qx.Proto.setSelectionInterval = function(fromIndex, toIndex) {
  var SelectionModel = qx.ui.table.SelectionModel;

  switch(this.getSelectionMode()) {
    case SelectionModel.NO_SELECTION:
      return;
    case SelectionModel.SINGLE_SELECTION:
      fromIndex = toIndex;
      break;
  }

  this._clearSelection();
  this._addSelectionInterval(fromIndex, toIndex);

  this._fireChangeSelection();
}


/**
 * Adds a selection interval to the current selection.
 *
 * @param fromIndex {int} the first index of the selection (including).
 * @param toIndex {int} the last index of the selection (including).
 */
qx.Proto.addSelectionInterval = function(fromIndex, toIndex) {
  var SelectionModel = qx.ui.table.SelectionModel;
  switch (this.getSelectionMode()) {
    case SelectionModel.NO_SELECTION:
      return;
    case SelectionModel.MULTIPLE_INTERVAL_SELECTION:
      this._addSelectionInterval(fromIndex, toIndex);
      this._fireChangeSelection();
      break;
    default:
      this.setSelectionInterval(fromIndex, toIndex);
      break;
  }
}


/**
 * Removes a interval from the current selection.
 *
 * @param fromIndex {int} the first index of the interval (including).
 * @param toIndex {int} the last index of the interval (including).
 */
qx.Proto.removeSelectionInterval = function(fromIndex, toIndex) {
  this._anchorSelectionIndex = fromIndex;
  this._leadSelectionIndex = toIndex;

  var minIndex = Math.min(fromIndex, toIndex);
  var maxIndex = Math.max(fromIndex, toIndex);

  // Crop the affected ranges
  for (var i = 0; i < this._selectedRangeArr.length; i++) {
    var range = this._selectedRangeArr[i];

    if (range.minIndex > maxIndex) {
      // We are done
      break;
    } else if (range.maxIndex >= minIndex) {
      // This range is affected
      var minIsIn = (range.minIndex >= minIndex) && (range.minIndex <= maxIndex);
      var maxIsIn = (range.maxIndex >= minIndex) && (range.maxIndex <= maxIndex);

      if (minIsIn && maxIsIn) {
        // This range is removed completely
        this._selectedRangeArr.splice(i, 1);

        // Check this index another time
        i--;
      } else if (minIsIn) {
        // The range is cropped from the left
        range.minIndex = maxIndex + 1;
      } else if (maxIsIn) {
        // The range is cropped from the right
        range.maxIndex = minIndex - 1;
      } else {
        // The range is split
        var newRange = { minIndex:maxIndex + 1, maxIndex:range.maxIndex }
        this._selectedRangeArr.splice(i + 1, 0, newRange);

        range.maxIndex = minIndex - 1;

        // We are done
        break;
      }
    }
  }

  //this._dumpRanges();

  this._fireChangeSelection();
}


/**
 * Clears the selection, but doesn't inform the listeners.
 */
qx.Proto._clearSelection = function() {
  this._selectedRangeArr = [];
}


/**
 * Adds a selection interval to the current selection, but doesn't inform
 * the listeners.
 *
 * @param fromIndex {int} the first index of the selection (including).
 * @param toIndex {int} the last index of the selection (including).
 */
qx.Proto._addSelectionInterval = function(fromIndex, toIndex) {
  this._anchorSelectionIndex = fromIndex;
  this._leadSelectionIndex = toIndex;

  var minIndex = Math.min(fromIndex, toIndex);
  var maxIndex = Math.max(fromIndex, toIndex);

  // Find the index where the new range should be inserted
  var newRangeIndex = 0;
  for (; newRangeIndex < this._selectedRangeArr.length; newRangeIndex++) {
    var range = this._selectedRangeArr[newRangeIndex];
    if (range.minIndex > minIndex) {
      break;
    }
  }

  // Add the new range
  this._selectedRangeArr.splice(newRangeIndex, 0, { minIndex:minIndex, maxIndex:maxIndex });

  // Merge overlapping ranges
  var lastRange = this._selectedRangeArr[0];
  for (var i = 1; i < this._selectedRangeArr.length; i++) {
    var range = this._selectedRangeArr[i];

    if (lastRange.maxIndex + 1 >= range.minIndex) {
      // The ranges are overlapping -> merge them
      lastRange.maxIndex = Math.max(lastRange.maxIndex, range.maxIndex);

      // Remove the current range
      this._selectedRangeArr.splice(i, 1);

      // Check this index another time
      i--;
    } else {
      lastRange = range;
    }
  }

  //this._dumpRanges();
}


/**
 * Logs the current ranges for debug perposes.
 */
qx.Proto._dumpRanges = function() {
  var text = "Ranges:";
  for (var i = 0; i < this._selectedRangeArr.length; i++) {
    var range = this._selectedRangeArr[i];
    text += " [" + range.minIndex + ".." + range.maxIndex + "]";
  }
  this.debug(text);
}


/**
 * Fires the "changeSelection" event to all registered listeners. If the selection model
 * currently is in batch mode, only one event will be thrown when batch mode is ended.
 */
qx.Proto._fireChangeSelection = function() {
  //In batch mode, remember event but do not throw (yet)
  if (this.hasBatchMode()){
    this._hadChangeEventInBatchMode = true;

  //If not in batch mode, throw event
  } else if (this.hasEventListeners("changeSelection")) {
    this.dispatchEvent(new qx.event.type.Event("changeSelection"), true);
  }
}