summaryrefslogtreecommitdiff
path: root/webapps/qooxdoo-0.6.3-sdk/frontend/framework/source/class/qx/ui/table/TablePaneModel.js
blob: d53da592516b27be8d3891ad1cb5d00f9dd5370f (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
/* ************************************************************************

   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)

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

/**
 * The model of a table pane. This model works as proxy to a
 * {@link TableColumnModel} and manages the visual order of the columns shown in
 * a {@link TablePane}.
 *
 * @param tableColumnModel {TableColumnModel} The TableColumnModel of which this
 *    model is the proxy.
 *
 * @event modelChanged {qx.event.type.Event} Fired when the model changed.
 */
qx.OO.defineClass("qx.ui.table.TablePaneModel", qx.core.Target,
function(tableColumnModel) {
  qx.core.Target.call(this);

  tableColumnModel.addEventListener("visibilityChangedPre", this._onColVisibilityChanged, this);

  this._tableColumnModel = tableColumnModel;
});


/** The visible x position of the first column this model should contain. */
qx.OO.addProperty({ name : "firstColumnX", type : "number", defaultValue : 0 });

/**
 * The maximum number of columns this model should contain. If -1 this model will
 * contain all remaining columns.
 */
qx.OO.addProperty({ name : "maxColumnCount", type : "number", defaultValue : -1 });


// property modifier
qx.Proto._modifyFirstColumnX = function(propValue, propOldValue, propData) {
  this._columnCount = null;
  this.createDispatchEvent(qx.ui.table.TablePaneModel.EVENT_TYPE_MODEL_CHANGED);
  return true;
}


// property modifier
qx.Proto._modifyMaxColumnCount = function(propValue, propOldValue, propData) {
  this._columnCount = null;
  this.createDispatchEvent(qx.ui.table.TablePaneModel.EVENT_TYPE_MODEL_CHANGED);
  return true;
}


/**
 * Event handler. Called when the visibility of a column has changed.
 *
 * @param evt {Map} the event.
 */
qx.Proto._onColVisibilityChanged = function(evt) {
  this._columnCount = null;

  // TODO: Check whether the column is in this model (This is a little bit
  //     tricky, because the column could _have been_ in this model, but is
  //     not in it after the change)
  this.createDispatchEvent(qx.ui.table.TablePaneModel.EVENT_TYPE_MODEL_CHANGED);
}


/**
 * Returns the number of columns in this model.
 *
 * @return {int} the number of columns in this model.
 */
qx.Proto.getColumnCount = function() {
  if (this._columnCount == null) {
    var firstX = this.getFirstColumnX();
    var maxColCount = this.getMaxColumnCount();
    var totalColCount = this._tableColumnModel.getVisibleColumnCount();

    if (maxColCount == -1 || (firstX + maxColCount) > totalColCount) {
      this._columnCount = totalColCount - firstX;
    } else {
      this._columnCount = maxColCount;
    }
  }
  return this._columnCount;
}


/**
 * Returns the model index of the column at the position <code>xPos</code>.
 *
 * @param xPos {int} the x postion in the table pane of the column.
 * @return {int} the model index of the column.
 */
qx.Proto.getColumnAtX = function(xPos) {
  var firstX = this.getFirstColumnX();
  return this._tableColumnModel.getVisibleColumnAtX(firstX + xPos);
}


/**
 * Returns the x position of the column <code>col</code>.
 *
 * @param col {int} the model index of the column.
 * @return {int} the x postion in the table pane of the column.
 */
qx.Proto.getX = function(col) {
  var firstX = this.getFirstColumnX();
  var maxColCount = this.getMaxColumnCount();

  var x = this._tableColumnModel.getVisibleX(col) - firstX;
  if (x >= 0 && (maxColCount == -1 || x < maxColCount)) {
    return x;
  } else {
    return -1;
  }
}


/**
 * Gets the position of the left side of a column (in pixels, relative to the
 * left side of the table pane).
 * <p>
 * This value corresponds to the sum of the widths of all columns left of the
 * column.
 *
 * @param col {int} the model index of the column.
 * @return the position of the left side of the column.
 */
qx.Proto.getColumnLeft = function(col) {
  var left = 0;
  var colCount = this.getColumnCount();
  for (var x = 0; x < colCount; x++) {
    var currCol = this.getColumnAtX(x);
    if (currCol == col) {
      return left;
    }

    left += this._tableColumnModel.getColumnWidth(currCol);
  }
  return -1;
}


/**
 * Returns the total width of all columns in the model.
 *
 * @return {int} the total width of all columns in the model.
 */
qx.Proto.getTotalWidth = function() {
  var totalWidth = 0;
  var colCount = this.getColumnCount();
  for (var x = 0; x < colCount; x++) {
    var col = this.getColumnAtX(x);
    totalWidth += this._tableColumnModel.getColumnWidth(col);
  }
  return totalWidth;
}


/** {string} The type of the event fired when the model changed. */
qx.Class.EVENT_TYPE_MODEL_CHANGED = "modelChanged";