summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.5-sdk/frontend/framework/source/class/qx/ui/table/DefaultResizeBehavior.js
blob: 39ab740f8289adeb4f740f7e88e48f6fe7a6e5a7 (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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/* ************************************************************************

   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(table)

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

/**
 * The default resize behavior.  Until a resize model is loaded, the default
 * behavior is to:
 * <ol>
 *   <li>
 *     Upon the table initially appearing, and upon any window resize, divide
 *     the table space equally between the visible columns.
 *   </li>
 *   <li>
 *     When a column is increased in width, all columns to its right are
 *     pushed to the right with no change to their widths.  This may push some
 *     columns off the right edge of the table, causing a horizontal scroll
 *     bar to appear.
 *   </li>
 *   <li>
 *     When a column is decreased in width, if the total width of all columns
 *     is <i>greater than</i> the table width, no additional column wiidth
 *     changes are made.
 *   </li>
 *   <li>
 *     When a column is decreased in width, if the total width of all columns
 *     is <i>less than</i> the width of the table, the visible column
 *     immediately to the right of the column which decreased in width has its
 *     width increased to fill the remaining space.
 *   </li>
 * </ol>
 *
 * A resize model may be loaded to provide more guidance on how to adjust
 * column width upon each of the events: initial appear, window resize, and
 * column resize. *** TO BE FILLED IN ***
 */
qx.OO.defineClass("qx.ui.table.DefaultResizeBehavior",
                  qx.ui.table.AbstractResizeBehavior,
function()
{
  qx.ui.table.AbstractResizeBehavior.call(this);
});


/*
 * A function to instantiate a resize behavior column data object.
 */
qx.OO.addProperty(
  {
    name :
      "newResizeBehaviorColumnData",
    type :
      "function",
    setOnlyOnce :
      true,
    defaultValue:
      function(obj)
      {
        return new qx.ui.table.ResizeBehaviorColumnData();
      }
  });


/**
 * Set the width of a column.
 *
 * @param col {Integer}
 *   The column whose width is to be set
 *
 * @param width {Integer, String}
 *   The width of the specified column.  The width may be specified as integer
 *   number of pixels (e.g. 100), a string representing percentage of the
 *   inner width of the Table (e.g. "25%"), or a string representing a flex
 *   width (e.g. "1*").
 */
qx.Proto.setWidth = function(col, width)
{
  // Ensure the column is within range
  if (col >= this._resizeColumnData.length)
  {
    throw new Error("Column number out of range");
  }

  // Set the new width
  this._resizeColumnData[col].setWidth(width);
};


/**
 * Set the minimum width of a column.
 *
 * @param col {Integer}
 *   The column whose minimum width is to be set
 *
 * @param width {Integer}
 *   The minimum width of the specified column.
 */
qx.Proto.setMinWidth = function(col, width)
{
  // Ensure the column is within range
  if (col >= this._resizeColumnData.length)
  {
    throw new Error("Column number out of range");
  }

  // Set the new width
  this._resizeColumnData[col].setMinWidth(width);
};


/**
 * Set the maximum width of a column.
 *
 * @param col {Integer}
 *   The column whose maximum width is to be set
 *
 * @param width {Integer}
 *   The maximum width of the specified column.
 */
qx.Proto.setMaxWidth = function(col, width)
{
  // Ensure the column is within range
  if (col >= this._resizeColumnData.length)
  {
    throw new Error("Column number out of range");
  }

  // Set the new width
  this._resizeColumnData[col].setMaxWidth(width);
};


/**
 * Set any or all of the width, minimum width, and maximum width of a column
 * in a single call.
 *
 * @param map {Map}
 *   A map containing any or all of the property names "width", "minWidth",
 *   and "maxWidth".  The property values are as described for
 *   {@link #setWidth}, {@link #setMinWidth} and {@link #setMaxWidth}
 *   respectively.
 */
qx.Proto.set = function(col, map)
{
  for (var prop in map)
  {
    switch(prop)
    {
    case "width":
      this.setWidth(col, map[prop]);
      break;

    case "minWidth":
      this.setMinWidth(col, map[prop]);
      break;

    case "maxWidth":
      this.setMaxWidth(col, map[prop]);
      break;

    default:
      throw new Error("Unknown property: " + prop);
    }
  }
};


// overloaded
qx.Proto.onAppear = function(tableColumnModel, event)
{
  // Get the initial available width so we know whether a resize caused an
  // increase or decrease in the available space.
  this._width = this._getAvailableWidth(tableColumnModel);

  // Calculate column widths
  this._computeColumnsFlexWidth(tableColumnModel, event);
};


// overloaded
qx.Proto.onWindowResize = function(tableColumnModel, event)
{
  // Calculate column widths
  this._computeColumnsFlexWidth(tableColumnModel, event);
};


// overloaded
qx.Proto.onColumnWidthChanged = function(tableColumnModel, event)
{
  // Extend the next column to fill blank space
  this._extendNextColumn(tableColumnModel, event);
};


// overloaded
qx.Proto.onVisibilityChanged = function(tableColumnModel, event)
{
  // Extend the last column to fill blank space
  this._extendLastColumn(tableColumnModel, event);
};


// overloaded
qx.Proto._setNumColumns = function(numColumns)
{
  // Are there now fewer (or the same number of) columns than there were
  // previously?
  if (numColumns <= this._resizeColumnData.length)
  {
    // Yup.  Delete the extras.
    this._resizeColumnData.splice(numColumns);
    return;
  }

  // There are more columns than there were previously.  Allocate more.
  for (var i = this._resizeColumnData.length; i < numColumns; i++)
  {
    this._resizeColumnData[i] = this.getNewResizeBehaviorColumnData()();
    this._resizeColumnData[i]._columnNumber = i;
  }
};


/**
 * Computes the width of all flexible children (based loosely on the method of
 * the same name in HorizontalBoxLayoutImpl).
 *
 * @param tableColumnModel {qx.ui.table.ResizeTableColumnModel}
 *   The table column model in use.
 *
 * @param event
 *   The event object.
 */
qx.Proto._computeColumnsFlexWidth = function(tableColumnModel, event)
{
  // Semi-permanent configuration settings
  var debug = true;

  if (debug)
  {
    this.debug("computeColumnsFlexWidth");
  }

  var visibleColumns = tableColumnModel._visibleColumnArr;
  var visibleColumnsLength = visibleColumns.length;
  var columnData;
  var flexibleColumns = [ ];
  var widthUsed = 0;
  var i;

  // Determine the available width
  var width = this._getAvailableWidth(tableColumnModel);


  // *************************************************************
  // 1. Compute the sum of all static sized columns and find
  //    all flexible columns.
  // *************************************************************
  for (i = 0; i < visibleColumnsLength; i++)
  {
    // Get the current column's column data
    columnData = this._resizeColumnData[visibleColumns[i]];

    // Is this column width type "auto"?
    if (columnData._computedWidthTypeAuto)
    {
      // Yup.  Convert it to a Flex "1*"
      columnData._computedWidthTypeAuto = false;
      columnData._computedWidthTypeFlex = true;
      columnData._computedWidthParsed = 1;
    }

    // Is this column a flex width?
    if (columnData._computedWidthTypeFlex)
    {
      // Yup.  Save it for future processing.
      flexibleColumns.push(columnData);
    }
    else if (columnData._computedWidthTypePercent)
    {
      // We can calculate the width of a Percent type right now.  Convert it
      // to a Flex type that's already calculated (no further calculation
      // required).
      columnData._computedWidthPercentValue =
        Math.round(width * (columnData._computedWidthParsed / 100));
      widthUsed += columnData._computedWidthPercentValue;
    }
    else
    {
      // We have a fixed width.  Track width already allocated.
      widthUsed += columnData.getWidth();
    }
  }

  if (debug)
  {
    this.debug("Width: " + widthUsed + "/" + width);
    this.debug("Flexible Count: " + flexibleColumns.length);
  }


  // *************************************************************
  // 2. Compute the sum of all flexible column widths
  // *************************************************************
  var widthRemaining = width - widthUsed;
  var flexibleColumnsLength = flexibleColumns.length;
  var prioritySum = 0;

  for (i = 0; i < flexibleColumnsLength; i++)
  {
    prioritySum += flexibleColumns[i]._computedWidthParsed;
  }


  // *************************************************************
  // 3. Calculating the size of each 'part'.
  // *************************************************************
  var partWidth = widthRemaining / prioritySum;

  // *************************************************************
  // 4. Adjust flexible columns, taking min/max values into account
  // *************************************************************
  
  bSomethingChanged = true;
  for (flexibleColumnsLength = flexibleColumns.length;
       bSomethingChanged && flexibleColumnsLength > 0;
       flexibleColumnsLength = flexibleColumns.length)
  {
    // Assume nothing will change
    bSomethingChanged = false;

    for (i = flexibleColumnsLength - 1; i >= 0; i--)
    {
      columnData = flexibleColumns[i];

      computedFlexibleWidth =
        columnData._computedWidthFlexValue =
        columnData._computedWidthParsed * partWidth;

      // If the part is not within its specified min/max range, adjust it.
      var min = columnData.getMinWidthValue();
      var max = columnData.getMaxWidthValue();
      if (min && computedFlexibleWidth < min)
      {
        columnData._computedWidthFlexValue = Math.round(min);
        widthUsed += columnData._computedWidthFlexValue;
        qx.lang.Array.removeAt(flexibleColumns, i);
        bSomethingChanged = true;

        // Don't round fixed-width columns (in step 5)
        columnData = null;
      }
      else if (max && computedFlexibleWidth > max)
      {
        columnData._computedWidthFlexValue = Math.round(max);
        widthUsed += columnData._computedWidthFlexValue;
        qx.lang.Array.removeAt(flexibleColumns, i);
        bSomethingChanged = true;

        // Don't round fixed-width columns (in step 5)
        columnData = null;
      }
    }
  }

  // If any flexible columns remain, then allocate the remaining space to them
  if (flexibleColumns.length > 0)
  {
    // Recalculate the priority sum of the remaining flexible columns
    prioritySum = 0;
    for (i = 0; i < flexibleColumnsLength; i++)
    {
      prioritySum += flexibleColumns[i]._computedWidthParsed;
    }

    // Recalculate the width remaining and part width
    widthRemaining = width - widthUsed;
    partWidth = widthRemaining / prioritySum;

    // If there's no width remaining...
    if (widthRemaining <= 0)
    {
      // ... then use minimum width * priority for all remaining columns
      for (i = 0; i < flexibleColumnsLength; i++)
      {
        columnData = flexibleColumns[i];

        computedFlexibleWidth =
          columnData._computedWidthFlexValue =
          (qx.ui.table.DefaultResizeBehavior.MIN_WIDTH *
           flexibleColumns[i]._computedWidthParsed);
        columnData._computedWidthFlexValue = Math.round(computedFlexibleWidth);
        widthUsed += columnData._computedWidthFlexValue;
      }
    }
    else
    {
      // Assign widths of remaining flexible columns
      for (i = 0; i < flexibleColumnsLength; i++)
      {
        columnData = flexibleColumns[i];

        computedFlexibleWidth =
        columnData._computedWidthFlexValue =
        columnData._computedWidthParsed * partWidth;

        // If the computed width is less than our hard-coded minimum...
        if (computedFlexibleWidth <
            qx.ui.table.DefaultResizeBehavior.MIN_WIDTH)
        {
          // ... then use the hard-coded minimum
          computedFlexibleWidth = qx.ui.table.DefaultResizeBehavior.MIN_WIDTH;
        }

        columnData._computedWidthFlexValue = Math.round(computedFlexibleWidth);
        widthUsed += columnData._computedWidthFlexValue;
      }
    }
  }

  // *************************************************************
  // 5. Fix rounding errors
  // *************************************************************
  if (columnData != null && widthRemaining > 0)
  {
    columnData._computedWidthFlexValue += width - widthUsed;
  }

  // *************************************************************
  // 6. Set the column widths to what we have calculated
  // *************************************************************
  for (i = 0; i < visibleColumnsLength; i++)
  {
    var colWidth;

    // Get the current column's column data
    columnData = this._resizeColumnData[visibleColumns[i]];

    // Is this column a flex width?
    if (columnData._computedWidthTypeFlex)
    {
      // Yup.  Set the width to the calculated width value based on flex
      colWidth = columnData._computedWidthFlexValue;
    }
    else if (columnData._computedWidthTypePercent)
    {
      // Set the width to the calculated width value based on percent
      colWidth = columnData._computedWidthPercentValue;
    }
    else
    {
      colWidth = columnData.getWidth();
    }

    // Now that we've calculated the width, set it.
    tableColumnModel.setColumnWidth(visibleColumns[i], colWidth);

    if (debug)
    {
      this.debug("col " + columnData._columnNumber + ": width=" + colWidth);
    }
  }
};


/**
 * Extend the visible column to right of the column which just changed width,
 * to fill any available space within the inner width of the table.  This
 * means that if the sum of the widths of all columns exceeds the inner width
 * of the table, no change is made.  If, on the other hand, the sum of the
 * widths of all columns is less than the inner width of the table, the
 * visible column to the right of the column which just changed width is
 * extended to take up the width available within the inner width of the
 * table.
 *
 * @param tableColumnModel {qx.ui.table.ResizeTableColumnModel}
 *   The table column model in use.
 *
 * @param event
 *   The event object.
 */
qx.Proto._extendNextColumn = function(tableColumnModel, event)
{
  // Event data properties: col, oldWidth, newWidth
  var data = event.getData();

  var visibleColumns = tableColumnModel._visibleColumnArr;

  // Determine the available width
  var width = this._getAvailableWidth(tableColumnModel);

  // Determine the number of visible columns
  var numColumns = visibleColumns.length;

  // Did this column become longer than it was?
  if (data.newWidth > data.oldWidth)
  {
    // Yup.  Don't resize anything else.  The other columns will just get
    // pushed off and require scrollbars be added (if not already there).
    return;
  }

  // This column became shorter.  See if we no longer take up the full space
  // that's available to us.
  var i;
  var nextCol;
  var widthUsed = 0;
  for (i = 0; i < numColumns; i++)
  {
    widthUsed +=
      tableColumnModel.getColumnWidth(visibleColumns[i]);
  }

  // If the used width is less than the available width...
  if (widthUsed < width)
  {
    // ... then determine the next visible column
    for (i = 0; i < visibleColumns.length; i++)
    {
      if (visibleColumns[i] == data.col)
      {
        nextCol = visibleColumns[i + 1];
        break;
      }
    }

    if (nextCol)
    {
      // Make the next column take up the available space.
      var oldWidth = tableColumnModel.getColumnWidth(nextCol);
      var newWidth = (width - (widthUsed -
                               tableColumnModel.getColumnWidth(nextCol)));
      tableColumnModel.setColumnWidth(nextCol, newWidth);
    }
  }
};


/**
 * If a column was just made invisible, extend the last column to fill any
 * available space within the inner width of the table.  This means that if
 * the sum of the widths of all columns exceeds the inner width of the table,
 * no change is made.  If, on the other hand, the sum of the widths of all
 * columns is less than the inner width of the table, the last column is
 * extended to take up the width available within the inner width of the
 * table.
 *
 * @param tableColumnModel {qx.ui.table.ResizeTableColumnModel}
 *   The table column model in use.
 *
 * @param event
 *   The event object.
 */
qx.Proto._extendLastColumn = function(tableColumnModel, event)
{
  // Event data properties: col, visible
  var data = event.getData();

  // If the column just became visible, don't make any width changes
  if (data.visible)
  {
    return;
  }

  // Get the array of visible columns
  var visibleColumns = tableColumnModel._visibleColumnArr;

  // Determine the available width
  var width = this._getAvailableWidth(tableColumnModel);

  // Determine the number of visible columns
  var numColumns = visibleColumns.length;

  // See if we no longer take up the full space that's available to us.
  var i;
  var lastCol;
  var widthUsed = 0;
  for (i = 0; i < numColumns; i++)
  {
    widthUsed +=
      tableColumnModel.getColumnWidth(visibleColumns[i]);
  }

  // If the used width is less than the available width...
  if (widthUsed < width)
  {
    // ... then get the last visible column
    lastCol = visibleColumns[visibleColumns.length - 1];

    // Make the last column take up the available space.
    var oldWidth = tableColumnModel.getColumnWidth(lastCol);
    var newWidth = (width - (widthUsed -
                             tableColumnModel.getColumnWidth(lastCol)));
    tableColumnModel.setColumnWidth(lastCol, newWidth);
  }
};



qx.Class.MIN_WIDTH = 10;