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

   qooxdoo - the new era of web development

   http://qooxdoo.org

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

   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:
     * Til Schneider (til132)

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

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

#module(ui_table)

// This is needed because of the instantiation at the end of this file.
// I don't think this is a good idea. (wpbasti)
#require(qx.util.format.NumberFormat)

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

/**
 * The default data cell renderer.
 */
qx.OO.defineClass("qx.ui.table.DefaultDataCellRenderer", qx.ui.table.AbstractDataCellRenderer,
function() {
  qx.ui.table.AbstractDataCellRenderer.call(this);
});


/**
 * Whether the alignment should automatically be set according to the cell value.
 * If true numbers will be right-aligned.
 */
qx.OO.addProperty({ name:"useAutoAlign", type:"boolean", defaultValue:true, allowNull:false });


// overridden
qx.Proto._getCellStyle = function(cellInfo) {
  var style = qx.ui.table.AbstractDataCellRenderer.prototype._getCellStyle(cellInfo);

  var stylesToApply = this._getStyleFlags(cellInfo);
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ALIGN_RIGHT){
    style += ";text-align:right";
  }
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_BOLD){
    style += ";font-weight:bold";
  }
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ITALIC){
    style += ";font-style:italic";
  }

  return style;
}

/**
 * Determines the styles to apply to the cell
 *
 * @param cellInfo {Object} cellInfo of the cell
 * @return the sum of any of the STYLEFLAGS defined below
 */
qx.Proto._getStyleFlags = function(cellInfo) {
  if (this.getUseAutoAlign()) {
    if (typeof cellInfo.value == "number") {
      return qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ALIGN_RIGHT;
    }
  }
}


// overridden
qx.Proto._getContentHtml = function(cellInfo) {
  return qx.html.String.escape(this._formatValue(cellInfo));
}


// overridden
qx.Proto.updateDataCellElement = function(cellInfo, cellElement) {
  var clazz = qx.ui.table.DefaultDataCellRenderer;
  var style = cellElement.style;

  var stylesToApply = this._getStyleFlags(cellInfo);
  if (stylesToApply & clazz.STYLEFLAG_ALIGN_RIGHT){
    style.textAlign = "right";
  } else {
    style.textAlign = "";
  }

  if (stylesToApply & clazz.STYLEFLAG_BOLD){
    style.fontWeight = "bold";
  } else {
    style.fontWeight = "";
  }

  if (stylesToApply & clazz.STYLEFLAG_ITALIC){
    style.fontStyle = "ital";
  } else {
    style.fontStyle = "";
  }

  var textNode = cellElement.firstChild;
  if (textNode != null) {
    textNode.nodeValue = this._formatValue(cellInfo);
  } else {
    cellElement.innerHTML = qx.html.String.escape(this._formatValue(cellInfo));
  }
}


/**
 * Formats a value.
 *
 * @param cellInfo {Map} A map containing the information about the cell to
 *        create. This map has the same structure as in
 *        {@link DataCellRenderer#createDataCell}.
 * @return {String} the formatted value.
 */
qx.Proto._formatValue = function(cellInfo) {
  var value = cellInfo.value;
  if (value == null) {
    return "";
  } else if (typeof value == "number") {
    return qx.ui.table.DefaultDataCellRenderer._numberFormat.format(value);
  } else if (value instanceof Date) {
    return qx.util.format.DateFormat.getDateInstance().format(value);
  } else {
    return value;
  }
}


qx.Proto._createCellStyle_array_join = function(cellInfo, htmlArr) {
  qx.ui.table.AbstractDataCellRenderer.prototype._createCellStyle_array_join(cellInfo, htmlArr);

  var stylesToApply = this._getStyleFlags(cellInfo);
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ALIGN_RIGHT){
    htmlArr.push(";text-align:right");
  }
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_BOLD){
    htmlArr.push(";font-weight:bold");
  }
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ITALIC){
    htmlArr.push(";font-style:italic");
  }
}


qx.Proto._createContentHtml_array_join = function(cellInfo, htmlArr) {
  htmlArr.push(qx.html.String.escape(this._formatValue(cellInfo)));
}


qx.Class._numberFormat = new qx.util.format.NumberFormat();
qx.Class._numberFormat.setMaximumFractionDigits(2);

qx.Class.STYLEFLAG_ALIGN_RIGHT = 1;
qx.Class.STYLEFLAG_BOLD = 2;
qx.Class.STYLEFLAG_ITALIC = 4;