Fixes based on recent testing. Key changes: EntryType DT (datetime) has been replaced...
[infodrom/rico3] / minsrc / ricoLiveGridControls.js
1 /*
2  *  (c) 2005-2009 Richard Cowin (http://openrico.org)
3  *  (c) 2005-2009 Matt Brown (http://dowdybrown.com)
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
6  *  file except in compliance with the License. You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software distributed under the
11  *  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
12  *  either express or implied. See the License for the specific language governing permissions
13  *  and limitations under the License.
14  */
15 \r
16 // -----------------------------------------------------\r
17 //\r
18 // Custom formatting for LiveGrid columns\r
19 //\r
20 // columnSpecs Usage: { type:'control', control:new Rico.TableColumn.CONTROLNAME() }\r
21 //\r
22 // -----------------------------------------------------\r
23
24 Rico.TableColumn = {};
25 \r
26 Rico.TableColumn.checkboxKey = function(showKey) {
27   this.initialize(showKey);
28 }
29
30 Rico.TableColumn.checkboxKey.prototype = {
31 /**
32  * @class Custom formatting for a LiveGrid column.
33  * Display unique key column as: <checkbox> <key value>
34  * and keep track of which keys the user selects
35  * Key values should not contain <, >, or &
36  * @constructs
37  */
38   initialize: function(showKey) {
39     this._checkboxes=[];
40     this._spans=[];
41     this._KeyHash={};
42     this._showKey=showKey;\r
43   },
44
45   _create: function(gridCell,windowRow) {
46     this._checkboxes[windowRow]=Rico.createFormField(gridCell,'input','checkbox',this.liveGrid.tableId+'_chkbox_'+this.index+'_'+windowRow);
47     this._spans[windowRow]=Rico.createFormField(gridCell,'span',null,this.liveGrid.tableId+'_desc_'+this.index+'_'+windowRow);
48     this._clear(gridCell,windowRow);
49     Rico.eventBind(this._checkboxes[windowRow], 'click', Rico.eventHandle(this,'_onclick'));
50   },
51
52   _onclick: function(e) {
53     var elem=Event.element(e);
54     var windowRow=parseInt(elem.id.substr((elem.id.lastIndexOf('_',elem.id.length)+1)));  //faster than split
55     var v=this.getValue(windowRow);
56     if (elem.checked)
57       this._addChecked(v);
58     else
59       this._remChecked(v);
60   },
61
62   _clear: function(gridCell,windowRow) {
63     var box=this._checkboxes[windowRow];
64     box.checked=false;
65     box.style.display='none';
66     this._spans[windowRow].innerHTML='';
67   },
68
69   _display: function(v,gridCell,windowRow) {
70     var box=this._checkboxes[windowRow];
71     box.style.display='';
72     box.checked=this._KeyHash[v];
73     if (this._showKey) this._spans[windowRow].innerHTML=v;
74   },
75
76   _SelectedKeys: function() {
77     return Rico.keys(this._KeyHash);
78   },
79
80   _addChecked: function(k){\r
81     this._KeyHash[k]=1;\r
82   },\r
83 \r
84   _remChecked: function(k){\r
85     delete this._KeyHash[k];\r
86   }\r
87 }
88
89
90 Rico.TableColumn.checkbox = function(checkedValue, uncheckedValue, defaultValue, readOnly)
91 {
92   this.initialize(checkedValue, uncheckedValue, defaultValue, readOnly);
93 }
94
95 Rico.TableColumn.checkbox.prototype = {
96 /**
97  * @class display checkboxes for two-valued column (e.g. yes/no)
98  * @constructs
99  */
100   initialize: function(checkedValue, uncheckedValue, defaultValue, readOnly) {
101     this._checkedValue=checkedValue;
102     this._uncheckedValue=uncheckedValue;
103     this._defaultValue=defaultValue || false;
104     this._readOnly=readOnly || false;
105     this._checkboxes=[];
106   },
107
108   _create: function(gridCell,windowRow) {
109     this._checkboxes[windowRow]=Rico.createFormField(gridCell,'input','checkbox',this.liveGrid.tableId+'_chkbox_'+this.index+'_'+windowRow);
110     this._clear(gridCell,windowRow);
111     if (this._readOnly)
112       this._checkboxes[windowRow].disabled=true;
113     else
114       Rico.eventBind(this._checkboxes[windowRow], 'click', Rico.eventHandle(this,'_onclick'));
115   },
116
117   _onclick: function(e) {
118     var elem=Event.element(e);
119     var windowRow=parseInt(elem.id.substr((elem.id.lastIndexOf('_',elem.id.length)+1)));  //faster than split
120     var newval=elem.checked ? this._checkedValue : this._uncheckedValue;
121     this.setValue(windowRow,newval);
122   },
123
124   _clear: function(gridCell,windowRow) {
125     var box=this._checkboxes[windowRow];
126     box.checked=this._defaultValue;
127     box.style.display='none';
128   },
129
130   _display: function(v,gridCell,windowRow) {
131     var box=this._checkboxes[windowRow];
132     box.style.display='';
133     box.checked=(v==this._checkedValue);
134   }
135
136 }
137
138
139 Rico.TableColumn.textbox = function(boxSize, boxMaxLen, readOnly) {
140   this.initialize(boxSize, boxMaxLen, readOnly);
141 }
142
143 Rico.TableColumn.textbox.prototype = {
144 /**
145  * @class display value in a text box
146  * @constructs
147  */
148   initialize: function(boxSize, boxMaxLen, readOnly) {
149     this._boxSize=boxSize;
150     this._boxMaxLen=boxMaxLen;
151     this._readOnly=readOnly || false;
152     this._textboxes=[];
153   },
154
155   _create: function(gridCell,windowRow) {
156     var box=Rico.createFormField(gridCell,'input','text',this.liveGrid.tableId+'_txtbox_'+this.index+'_'+windowRow);
157     box.size=this._boxSize;
158     box.maxLength=this._boxMaxLen;
159     this._textboxes[windowRow]=box;
160     this._clear(gridCell,windowRow);
161     if (this._readOnly)
162       box.disabled=true;
163     else
164       Rico.eventBind(box, 'change', Rico.eventHandle(this,'_onchange'));
165   },
166
167   _onchange: function(e) {
168     var elem=Event.element(e);
169     var windowRow=parseInt(elem.id.substr((elem.id.lastIndexOf('_',elem.id.length)+1)));  //faster than split
170     this.setValue(windowRow,elem.value);
171   },
172
173   _clear: function(gridCell,windowRow) {
174     var box=this._textboxes[windowRow];
175     box.value='';
176     box.style.display='none';
177   },
178
179   _display: function(v,gridCell,windowRow) {
180     var box=this._textboxes[windowRow];
181     box.style.display='';
182     box.value=v;
183   }
184
185 }
186
187
188 Rico.TableColumn.bgColor = function() {
189 }
190
191 Rico.TableColumn.bgColor.prototype = {
192 /**
193  * @class database value contains a css color name/value
194  */
195  
196  _clear: function(gridCell,windowRow) {
197     gridCell.style.backgroundColor='';
198   },
199
200   _display: function(v,gridCell,windowRow) {
201     gridCell.style.backgroundColor=v;
202   }
203
204 }
205
206
207 Rico.TableColumn.link = function(href,target,linktext) {
208   this.initialize(href,target,linktext);
209 }
210
211 Rico.TableColumn.link.prototype = {
212 /**
213  * @class database value contains a url to another page
214  * @constructs
215  */
216   initialize: function(href,target,linktext) {
217     this._href=href;
218     this._target=target;
219     this._linktext=linktext;
220     this._anchors=[];
221   },
222
223   _create: function(gridCell,windowRow) {
224     var a = gridCell.appendChild(document.createElement('a'));
225     if (this._target) a.target=this._target;
226     a.href='';
227     a.innerHTML=Rico.isIE ? ' ' : '';
228     this._anchors[windowRow] = a;
229   },
230
231   _clear: function(gridCell,windowRow) {
232     this._anchors[windowRow].style.display='none';
233   },
234
235   _display: function(v,gridCell,windowRow) {
236     var buf=this.liveGrid.buffer;
237     var href=this._href=='self' ? v : this._href.replace(/\{\d+\}/g,
238       function ($1) {
239         var colIdx=parseInt($1.substr(1),10);
240         return encodeURIComponent(buf.getWindowValue(windowRow,colIdx));
241       }
242     );
243     var desc=this._linktext || v;
244     if (href && desc) {
245       this._anchors[windowRow].href=href;
246       this._anchors[windowRow].innerHTML=desc;
247       this._anchors[windowRow].style.display='';
248     } else {
249       this._clear(gridCell,windowRow);
250     }
251   }
252
253 };
254
255
256 Rico.TableColumn.image = function(prefix,suffix) {
257   this.initialize(prefix,suffix);
258 };
259
260 Rico.TableColumn.image.prototype = {
261 /**
262  * @class database value contains a url to an image
263  * @constructs
264  */
265   initialize: function(prefix,suffix) {
266     this._img=[];
267     this._prefix=prefix || '';
268     this._suffix=suffix || '';
269   },
270
271   _create: function(gridCell,windowRow) {
272     this._img[windowRow]=Rico.createFormField(gridCell,'img',null,this.liveGrid.tableId+'_img_'+this.index+'_'+windowRow);
273     this._clear(gridCell,windowRow);
274   },
275
276   _clear: function(gridCell,windowRow) {
277     var img=this._img[windowRow];
278     img.style.display='none';
279     img.src='';
280   },
281
282   _display: function(v,gridCell,windowRow) {
283     var img=this._img[windowRow];
284     this._img[windowRow].src=this._prefix+v+this._suffix;
285     img.style.display='';
286   }
287
288 };
289
290
291 Rico.TableColumn.lookup = function(map, defaultCode, defaultDesc) {
292   this.initialize(map, defaultCode, defaultDesc);
293 };
294
295 Rico.TableColumn.lookup.prototype = {
296 /**
297  * @class map a database value to a display value
298  * @constructs
299  */
300   initialize: function(map, defaultCode, defaultDesc) {
301     this._map=map;
302     this._defaultCode=defaultCode || '';
303     this._defaultDesc=defaultDesc || ' ';
304     var self=this;
305     this._sortfunc=function(v) { return self._sortvalue(v); };
306     this._codes=[];
307     this._descriptions=[];
308   },
309
310   _create: function(gridCell,windowRow) {
311     this._descriptions[windowRow]=Rico.createFormField(gridCell,'span',null,this.liveGrid.tableId+'_desc_'+this.index+'_'+windowRow);
312     this._codes[windowRow]=Rico.createFormField(gridCell,'input','hidden',this.liveGrid.tableId+'_code_'+this.index+'_'+windowRow);
313     this._clear(gridCell,windowRow);
314   },
315
316   _clear: function(gridCell,windowRow) {
317     this._codes[windowRow].value=this._defaultCode;
318     this._descriptions[windowRow].innerHTML=this._defaultDesc;
319   },
320
321   _sortvalue: function(v) {
322     return this._getdesc(v).replace(/&amp;/g, '&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&nbsp;/g,' ');
323   },
324
325   _getdesc: function(v) {
326     var desc=this._map[v];
327     return (typeof desc=='string') ? desc : this._defaultDesc;
328   },
329
330   _export: function(v) {
331     return this._getdesc(v);
332   },
333
334   _display: function(v,gridCell,windowRow) {
335     this._codes[windowRow].value=v;
336     this._descriptions[windowRow].innerHTML=this._getdesc(v);
337   }
338
339 };
340
341
342
343 Rico.TableColumn.MultiLine = function() {
344 };
345
346 Rico.TableColumn.MultiLine.prototype = {
347 /**
348  * @class Fix issues with multiline content in IE
349  */
350   _display: function(v,gridCell,windowRow) {\r
351     var newdiv = document.createElement("div");\r
352     newdiv.innerHTML = this._format(v);\r
353     newdiv.style.height='100%';\r
354     if (gridCell.firstChild)\r
355       gridCell.replaceChild(newdiv, gridCell.firstChild);\r
356     else\r
357       gridCell.appendChild(newdiv);\r
358   }\r
359
360 };