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

   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)

// 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.ui.table.DefaultDataCellRenderer.escapeHtml(this._formatValue(cellInfo));
}


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

  var stylesToApply = this._getStyleFlags(cellInfo);
  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ALIGN_RIGHT){
    cellElement.style.textAlign = "right";
  } else {
    cellElement.style.textAlign = "";
  }

  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_BOLD){
    cellElement.style.fontWeight = "bold";
  } else {
    cellElement.style.fontWeight = "";
  }

  if (stylesToApply & qx.ui.table.DefaultDataCellRenderer.STYLEFLAG_ITALIC){
    cellElement.style.fontStyle = "ital";
  } else {
    cellElement.style.fontStyle = "";
  }

  var textNode = cellElement.firstChild;
  if (textNode != null) {
    textNode.nodeValue = this._formatValue(cellInfo);
  } else {
    cellElement.innerHTML = qx.ui.table.DefaultDataCellRenderer.escapeHtml(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.ui.table.DefaultDataCellRenderer.escapeHtml(this._formatValue(cellInfo)));
}


/**
 * Escapes special HTML characters by their entities.
 *
 * @param html {string} The HTML to escape.
 * @return {string} The escaped string showing HTML code as plain text.
 */
qx.Class.escapeHtml = function(html) {
  return html.replace(/[<>&]/gi, qx.ui.table.DefaultDataCellRenderer._escapeHtmlReplacer);
}


/**
 * Helper method for {@link #escapeHtml}.
 */
qx.Class._escapeHtmlReplacer = function(str) {
  switch(str) {
    case "<": return "&lt;";
    case ">": return "&gt;";
    case "&": return "&amp;";
  }
}


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;