r20517: re-add cleaned-up webapps
[sfrench/samba-autobuild/.git] / webapps / qooxdoo-0.6.3-sdk / frontend / framework / source / class / qx / ui / table / IconDataCellRenderer.js
1 /* ************************************************************************
2
3    qooxdoo - the new era of web development
4
5    http://qooxdoo.org
6
7    Copyright:
8      2006 by STZ-IDA, Germany, http://www.stz-ida.de
9
10    License:
11      LGPL 2.1: http://www.gnu.org/licenses/lgpl.html
12
13    Authors:
14      * Til Schneider (til132)
15      * Carsten Lergenmueller (carstenl)
16
17 ************************************************************************ */
18
19 /* ************************************************************************
20
21 #module(ui_table)
22
23 ************************************************************************ */
24
25 /**
26  * A data cell renderer for boolean values.
27  */
28 qx.OO.defineClass("qx.ui.table.IconDataCellRenderer", qx.ui.table.AbstractDataCellRenderer,
29 function() {
30   qx.ui.table.AbstractDataCellRenderer.call(this);
31   this.IMG_BLANK_URL = qx.manager.object.AliasManager.getInstance().resolvePath("static/image/blank.gif");
32 });
33
34
35 /**
36  * Identifies the Image to show.
37  *
38  * @param cellInfo {Map} The information about the cell.
39  *        See {@link #createDataCellHtml}.
40  * @return {Map} A map having the following attributes:
41  *         <ul>
42  *         <li>"url": (type string) must be the URL of the image to show.</li>
43  *         <li>"imageWidth": (type int) the width of the image in pixels.</li>
44  *         <li>"imageHeight": (type int) the height of the image in pixels.</li>
45  *         <li>"tooltip": (type string) must be the image tooltip text.</li>
46  *         </ul>
47  */
48 qx.Proto._identifyImage = function(cellInfo) {
49   throw new Error("_identifyImage is abstract");
50 }
51
52
53 /**
54  * Retrieves the image infos.
55  *
56  * @param cellInfo {Map} The information about the cell.
57  *        See {@link #createDataCellHtml}.
58  * @return {Map} Map with an "url" attribute (type string)
59  *               holding the URL of the image to show
60  *               and a "tooltip" attribute
61  *               (type string) being the tooltip text (or null if none was specified)
62  *
63  */
64 qx.Proto._getImageInfos= function(cellInfo) {
65   // Query the subclass about image and tooltip
66   var urlAndTooltipMap = this._identifyImage(cellInfo);
67
68   // If subclass refuses to give map, construct it
69   if (urlAndTooltipMap == null || typeof urlAndTooltipMap == "string"){
70     urlAndTooltipMap = {url:urlAndTooltipMap, tooltip:null};
71   }
72
73   // If subclass gave null as url, replace with url to empty image
74   if (urlAndTooltipMap.url == null){
75     urlAndTooltipMap.url = this.IMG_BLANK_URL;
76   }
77
78   return urlAndTooltipMap;
79 }
80
81 // overridden
82 qx.Proto._getCellStyle = function(cellInfo) {
83   var style = qx.ui.table.AbstractDataCellRenderer.prototype._getCellStyle(cellInfo);
84   style += qx.ui.table.IconDataCellRenderer.MAIN_DIV_STYLE;
85   return style;
86 }
87
88
89 // overridden
90 qx.Proto._getContentHtml = function(cellInfo) {
91   var IconDataCellRenderer = qx.ui.table.IconDataCellRenderer;
92
93   var urlAndToolTip = this._getImageInfos(cellInfo);
94   var html = IconDataCellRenderer.IMG_START;
95   if (qx.sys.Client.getInstance().isMshtml() && /\.png$/i.test(urlAndToolTip.url)) {
96     html += qx.manager.object.AliasManager.getInstance().resolvePath("static/image/blank.gif")
97       + '" style="filter:' + "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + urlAndToolTip.url + "',sizingMethod='scale')";
98   } else {
99     html += urlAndToolTip.url + '" style="';
100   }
101
102   if (urlAndToolTip.imageWidth && urlAndToolTip.imageHeight) {
103     html += ';width:' + urlAndToolTip.imageWidth + 'px'
104          +  ';height:' + urlAndToolTip.imageHeight + 'px';
105   }
106
107   var tooltip = urlAndToolTip.tooltip;
108   if (tooltip != null){
109     html += IconDataCellRenderer.IMG_TITLE_START + tooltip;
110   }
111   html += IconDataCellRenderer.IMG_END;
112   return html;
113 }
114
115
116 // overridden
117 qx.Proto.updateDataCellElement = function(cellInfo, cellElement) {
118   // Set image and tooltip text
119   var urlAndToolTip = this._getImageInfos(cellInfo);
120   var img = cellElement.firstChild;
121   if (qx.sys.Client.getInstance().isMshtml()) {
122     if (/\.png$/i.test(urlAndToolTip.url)) {
123       img.src = qx.manager.object.AliasManager.getInstance().resolvePath("static/image/blank.gif");
124       img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + urlAndToolTip.url + "',sizingMethod='scale')";
125     } else {
126       img.src = urlAndToolTip.url;
127       img.style.filter = "";
128     }
129   } else {
130     img.src = urlAndToolTip.url;
131   }
132
133   if (urlAndToolTip.imageWidth && urlAndToolTip.imageHeight) {
134     img.style.width = urlAndToolTip.imageWidth + "px";
135     img.style.height = urlAndToolTip.imageHeight + "px";
136   }
137
138   if (urlAndToolTip.tooltip != null){
139     img.text = urlAndToolTip.tooltip;
140   }
141 }
142
143
144 // overridden
145 qx.Proto._createCellStyle_array_join = function(cellInfo, htmlArr) {
146   qx.ui.table.AbstractDataCellRenderer.prototype._createCellStyle_array_join(cellInfo, htmlArr);
147
148   htmlArr.push(qx.ui.table.IconDataCellRenderer.MAIN_DIV_STYLE);
149 }
150
151 qx.Proto._createContentHtml_array_join = function(cellInfo, htmlArr) {
152   var IconDataCellRenderer = qx.ui.table.IconDataCellRenderer;
153
154   if (qx.ui.table.TablePane.USE_TABLE) {
155     htmlArr.push(IconDataCellRenderer.TABLE_DIV);
156     htmlArr.push(cellInfo.styleHeight - 2); // -1 for the border, -1 for the padding
157     htmlArr.push(IconDataCellRenderer.TABLE_DIV_CLOSE);
158   }
159
160   htmlArr.push(IconDataCellRenderer.IMG_START);
161   var urlAndToolTip = this._getImageInfos(cellInfo);
162   htmlArr.push(urlAndToolTip.url);
163   var tooltip = urlAndToolTip.tooltip;
164   if (tooltip != null){
165     IconDataCellRenderer.IMG_TITLE_START;
166     htmlArr.push(tooltip);
167   }
168   htmlArr.push(IconDataCellRenderer.IMG_END);
169
170   if (qx.ui.table.TablePane.USE_TABLE) {
171     htmlArr.push(IconDataCellRenderer.TABLE_DIV_END);
172   }
173 }
174
175 qx.Class.MAIN_DIV_STYLE = ';text-align:center;padding-top:1px;';
176 qx.Class.IMG_START = '<img src="';
177 qx.Class.IMG_END = '"/>';
178 qx.Class.IMG_TITLE_START = '" title="';
179 qx.Class.TABLE_DIV = '<div style="overflow:hidden;height:';
180 qx.Class.TABLE_DIV_CLOSE = 'px">';
181 qx.Class.TABLE_DIV_END = '</div>';
182