Replaced .net user controls under rico3/plugins/dotnet with a single .net server...
[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.HighlightCell = function(chkcol,chkval,highlightColor,highlightBackground,chkop) {
189   this.initialize(chkcol,chkval,highlightColor,highlightBackground,chkop);
190 }
191
192 Rico.TableColumn.HighlightCell.prototype = {
193 /**
194  * @class highlight a grid cell when a particular value is present in the specified column
195  * @constructs
196  */
197   initialize: function(chkcol,chkval,highlightColor,highlightBackground,chkop) {\r
198     this._chkcol=chkcol;\r
199     this._chkval=chkval;
200     this._chkop=chkop;\r
201     this._highlightColor=highlightColor;\r
202     this._highlightBackground=highlightBackground;\r
203   },\r
204 \r
205   _clear: function(gridCell,windowRow) {\r
206     gridCell.style.color='';\r
207     gridCell.style.backgroundColor='';\r
208     gridCell.innerHTML=' ';\r
209   },\r
210 \r
211   _display: function(v,gridCell,windowRow) {\r
212     var gridval=this.liveGrid.buffer.getWindowValue(windowRow,this._chkcol);\r
213     var match;\r
214     switch(this._chkop){
215         case '!=':
216           match=(gridval!=this._chkval);
217           break;
218         case '>':
219           match=(gridval>this._chkval);
220           break;
221         case '<':
222           match=(gridval<this._chkval);
223           break;
224         case '>=':
225           match=(gridval>=this._chkval);
226           break;
227         case '<=':
228           match=(gridval<=this._chkval);
229           break;
230         case 'abs>':
231           match=(Math.abs(gridval)>this._chkval);
232           break;
233         case 'abs<':
234           match=(Math.abs(gridval)<this._chkval);
235           break;
236         case 'abs>=':
237           match=(Math.abs(gridval)>=this._chkval);
238           break;
239         case 'abs<=':
240           match=(Math.abs(gridval)<=this._chkval);
241           break;
242         default:
243           match=(gridval==this._chkval);
244           break;
245     }
246     gridCell.style.color=match ? this._highlightColor : '';\r
247     gridCell.style.backgroundColor=match ? this._highlightBackground : '';\r
248     gridCell.innerHTML=this._format(v);\r
249   }\r
250 }
251
252
253 Rico.TableColumn.bgColor = function() {
254 }
255
256 Rico.TableColumn.bgColor.prototype = {
257 /**
258  * @class database value contains a css color name/value
259  */
260  
261  _clear: function(gridCell,windowRow) {
262     gridCell.style.backgroundColor='';
263   },
264
265   _display: function(v,gridCell,windowRow) {
266     gridCell.style.backgroundColor=v;
267   }
268
269 }
270
271
272 Rico.TableColumn.link = function(href,target,linktext) {
273   this.initialize(href,target,linktext);
274 }
275
276 Rico.TableColumn.link.prototype = {
277 /**
278  * @class database value contains a url to another page
279  * @constructs
280  */
281   initialize: function(href,target,linktext) {
282     this._href=href;
283     this._target=target;
284     this._linktext=linktext;
285     this._anchors=[];
286   },
287
288   _create: function(gridCell,windowRow) {
289     this._anchors[windowRow]=Rico.createFormField(gridCell,'a',null,this.liveGrid.tableId+'_a_'+this.index+'_'+windowRow);
290     if (this._target) this._anchors[windowRow].target=this._target;
291     this._clear(gridCell,windowRow);
292   },
293
294   _clear: function(gridCell,windowRow) {
295     this._anchors[windowRow].href='';
296     this._anchors[windowRow].innerHTML='';
297   },
298
299   _display: function(v,gridCell,windowRow) {
300     var buf=this.liveGrid.buffer;
301     var href=this._href=='self' ? v : this._href.replace(/\{\d+\}/g,
302       function ($1) {
303         var colIdx=parseInt($1.substr(1),10);
304         return encodeURIComponent(buf.getWindowValue(windowRow,colIdx));
305       }
306     );
307     var desc=this._linktext || v;
308     if (href && desc) {
309       this._anchors[windowRow].href=href;
310       this._anchors[windowRow].innerHTML=desc;
311     } else {
312       this._clear(gridCell,windowRow);
313     }
314   }
315
316 };
317
318
319 Rico.TableColumn.image = function(prefix,suffix) {
320   this.initialize(prefix,suffix);
321 };
322
323 Rico.TableColumn.image.prototype = {
324 /**
325  * @class database value contains a url to an image
326  * @constructs
327  */
328   initialize: function(prefix,suffix) {
329     this._img=[];
330     this._prefix=prefix || '';
331     this._suffix=suffix || '';
332   },
333
334   _create: function(gridCell,windowRow) {
335     this._img[windowRow]=Rico.createFormField(gridCell,'img',null,this.liveGrid.tableId+'_img_'+this.index+'_'+windowRow);
336     this._clear(gridCell,windowRow);
337   },
338
339   _clear: function(gridCell,windowRow) {
340     var img=this._img[windowRow];
341     img.style.display='none';
342     img.src='';
343   },
344
345   _display: function(v,gridCell,windowRow) {
346     var img=this._img[windowRow];
347     this._img[windowRow].src=this._prefix+v+this._suffix;
348     img.style.display='';
349   }
350
351 };
352
353
354 Rico.TableColumn.lookup = function(map, defaultCode, defaultDesc) {
355   this.initialize(map, defaultCode, defaultDesc);
356 };
357
358 Rico.TableColumn.lookup.prototype = {
359 /**
360  * @class map a database value to a display value
361  * @constructs
362  */
363   initialize: function(map, defaultCode, defaultDesc) {
364     this._map=map;
365     this._defaultCode=defaultCode || '';
366     this._defaultDesc=defaultDesc || '&nbsp;';
367     var self=this;
368     this._sortfunc=function(v) { return self._sortvalue(v); };
369     this._codes=[];
370     this._descriptions=[];
371   },
372
373   _create: function(gridCell,windowRow) {
374     this._descriptions[windowRow]=Rico.createFormField(gridCell,'span',null,this.liveGrid.tableId+'_desc_'+this.index+'_'+windowRow);
375     this._codes[windowRow]=Rico.createFormField(gridCell,'input','hidden',this.liveGrid.tableId+'_code_'+this.index+'_'+windowRow);
376     this._clear(gridCell,windowRow);
377   },
378
379   _clear: function(gridCell,windowRow) {
380     this._codes[windowRow].value=this._defaultCode;
381     this._descriptions[windowRow].innerHTML=this._defaultDesc;
382   },
383
384   _sortvalue: function(v) {
385     return this._getdesc(v).replace(/&amp;/g, '&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&nbsp;/g,' ');
386   },
387
388   _getdesc: function(v) {
389     var desc=this._map[v];
390     return (typeof desc=='string') ? desc : this._defaultDesc;
391   },
392
393   _export: function(v) {
394     return this._getdesc(v);
395   },
396
397   _display: function(v,gridCell,windowRow) {
398     this._codes[windowRow].value=v;
399     this._descriptions[windowRow].innerHTML=this._getdesc(v);
400   }
401
402 };
403
404
405
406 Rico.TableColumn.MultiLine = function() {
407 };
408
409 Rico.TableColumn.MultiLine.prototype = {
410 /**
411  * @class Fix issues with multiline content in IE
412  */
413   _display: function(v,gridCell,windowRow) {\r
414     var newdiv = document.createElement("div");\r
415     newdiv.innerHTML = this._format(v);\r
416     newdiv.style.height='100%';\r
417     if (gridCell.firstChild)\r
418       gridCell.replaceChild(newdiv, gridCell.firstChild);\r
419     else\r
420       gridCell.appendChild(newdiv);\r
421   }\r
422
423 };