Changed asp and dotnet examples to use css sprites instead of images. Changed dotnet...
[infodrom/rico3] / minsrc / ricoGridCommon.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
16 if(typeof Rico=='undefined') throw("GridCommon requires the Rico JavaScript framework");
17
18 /**
19  * Define methods that are common to both SimpleGrid and LiveGrid
20  */
21 Rico.GridCommon = {
22
23   baseInit: function() {
24     this.options = {
25       saveColumnInfo   : {width:true, filter:false, sort:false},  // save info in cookies?
26       cookiePrefix     : 'RicoGrid.',
27       allowColResize   : true,      // allow user to resize columns
28       windowResize     : true,      // Resize grid on window.resize event? Set to false when embedded in an accordian.
29       click            : null,
30       dblclick         : null,
31       contextmenu      : null,
32       menuEvent        : 'dblclick',  // event that triggers menus - click, dblclick, contextmenu, or none (no menus)
33       defaultWidth     : -1,          // if -1, then use unformatted column width
34       scrollBarWidth   : 19,          // this is the value used in positioning calculations, it does not actually change the width of the scrollbar
35       minScrollWidth   : 100,         // min scroll area width when width of frozen columns exceeds window width
36       frozenColumns    : 0,
37       exportWindow     : "height=400,width=500,scrollbars=1,menubar=1,resizable=1,location=0,toolbar=0,status=0",
38       exportStyleList  : ['background-color','color','text-align','font-weight','font-size','font-family'],
39       exportImgTags    : false,       // applies to grid header and to SimpleGrid cells (not LiveGrid cells)
40       exportFormFields : true,
41       FilterLocation   : null,        // heading row number to place filters. -1=add a new heading row.
42       FilterAllToken   : '___ALL___', // select box value to use to indicate ALL
43       columnSpecs      : []
44     };
45     this.colWidths = [];
46     this.hdrCells=[];
47     this.headerColCnt=0;
48     this.headerRowIdx=0;       // row in header which gets resizers (no colspan's in this row)
49     this.tabs=new Array(2);
50     this.thead=new Array(2);
51     this.tbody=new Array(2);
52   },
53
54   attachMenuEvents: function() {
55     var i;
56     if (!this.options.menuEvent || this.options.menuEvent=='none') return;
57     this.hideScroll=navigator.userAgent.match(/Macintosh\b.*\b(Firefox|Camino)\b/i) || (Rico.isOpera && parseFloat(window.opera.version())<9.5);
58     this.options[this.options.menuEvent]=Rico.eventHandle(this,'handleMenuClick');
59     if (this.highlightDiv) {
60       switch (this.options.highlightElem) {
61         case 'cursorRow':
62           this.attachMenu(this.highlightDiv[0]);
63           break;
64         case 'cursorCell':
65           for (i=0; i<2; i++) {
66             this.attachMenu(this.highlightDiv[i]);
67           }
68           break;
69       }
70     }
71     for (i=0; i<2; i++) {
72       this.attachMenu(this.tbody[i]);
73     }
74   },
75
76   attachMenu: function(elem) {
77     if (this.options.click)
78       Rico.eventBind(elem, 'click', this.options.click, false);
79     if (this.options.dblclick) {
80       if (Rico.isWebKit || Rico.isOpera)
81         Rico.eventBind(elem, 'click', Rico.eventHandle(this,'handleDblClick'), false);
82       else
83         Rico.eventBind(elem, 'dblclick', this.options.dblclick, false);
84     }
85     if (this.options.contextmenu) {
86       if (Rico.isOpera || Rico.isKonqueror)
87         Rico.eventBind(elem, 'click', Rico.eventHandle(this,'handleContextMenu'), false);
88       else
89         Rico.eventBind(elem, 'contextmenu', this.options.contextmenu, false);
90     }
91   },
92
93 /**
94  * implement double-click for browsers that don't support a double-click event (e.g. Safari)
95  */
96   handleDblClick: function(e) {
97     var elem=Rico.eventElement(e);
98     if (this.dblClickElem == elem) {
99       this.options.dblclick(e);
100     } else {
101       this.dblClickElem = elem;
102       this.safariTimer=Rico.runLater(300,this,'clearDblClick');
103     }
104   },
105
106   clearDblClick: function() {
107     this.dblClickElem=null;
108   },
109
110 /**
111  * implement right-click for browsers that don't support contextmenu event (e.g. Opera, Konqueror)
112  * use control-click instead
113  */
114   handleContextMenu: function(e) {
115     var b;
116     if( typeof( e.which ) == 'number' )
117       b = e.which; //Netscape compatible
118     else if( typeof( e.button ) == 'number' )
119       b = e.button; //DOM
120     else
121       return;
122     if (b==1 && e.ctrlKey) {
123       this.options.contextmenu(e);
124     }
125   },
126
127   cancelMenu: function() {
128     if (this.menu) this.menu.cancelmenu();
129   },
130
131 /**
132  * gather info from original headings
133  */
134   getColumnInfo: function(hdrSrc) {
135     Rico.log('getColumnInfo: len='+hdrSrc.length);
136     if (hdrSrc.length == 0) return 0;
137     this.headerRowCnt=hdrSrc.length;
138     var r,c,colcnt;
139     for (r=0; r<this.headerRowCnt; r++) {
140       var headerRow = hdrSrc[r];
141       var headerCells=headerRow.cells;
142       if (r >= this.hdrCells.length) this.hdrCells[r]=[];
143       for (c=0; c<headerCells.length; c++) {
144         var obj={};
145         obj.cell=headerCells[c];
146         obj.colSpan=headerCells[c].colSpan || 1;  // Safari & Konqueror return default colspan of 0
147         if (this.options.defaultWidth < 0) obj.initWidth=headerCells[c].offsetWidth;
148         this.hdrCells[r].push(obj);
149       }
150       if (headerRow.id.slice(-5)=='_main') {
151         colcnt=this.hdrCells[r].length;
152         this.headerRowIdx=r;
153       }
154     }
155     if (!colcnt) {
156       this.headerRowIdx=this.headerRowCnt-1;
157       colcnt=this.hdrCells[this.headerRowIdx].length;
158     }
159     Rico.log("getColumnInfo: colcnt="+colcnt);
160     return colcnt;
161   },
162
163   addHeadingRow: function(className) {
164     var r=this.headerRowCnt++;
165     this.hdrCells[r]=[];
166     for( var h=0; h < 2; h++ ) {
167       var row = this.thead[h].insertRow(-1);
168       var newClass='ricoLG_hdg '+this.tableId+'_hdg'+r;
169       if (className) newClass+=' '+className;
170       row.className=newClass;
171       var limit= h==0 ? this.options.frozenColumns : this.headerColCnt-this.options.frozenColumns;
172       for( var c=0; c < limit; c++ ) {
173         var hdrCell=row.insertCell(-1);
174         var colDiv=Rico.wrapChildren(hdrCell,'ricoLG_col');
175         Rico.wrapChildren(colDiv,'ricoLG_cell');
176         this.hdrCells[r].push({cell:hdrCell,colSpan:1});
177       }
178     }
179     return r;
180   },
181   
182 /**
183  * create column array
184  */
185   createColumnArray: function(columnType) {
186     this.direction=Rico.getStyle(this.outerDiv,'direction').toLowerCase();  // ltr or rtl
187     this.align=this.direction=='rtl' ? ['right','left'] : ['left','right'];
188     Rico.log('createColumnArray: dir='+this.direction);
189     this.columns = [];
190     for (var c=0; c < this.headerColCnt; c++) {
191       Rico.log("createColumnArray: c="+c);
192       var tabidx=c<this.options.frozenColumns ? 0 : 1;
193       var col=new Rico[columnType](this, c, this.hdrCells[this.headerRowIdx][c], tabidx);
194       this.columns.push(col);
195       if (c > 0) this.columns[c-1].next=col;
196     }
197     this.getCookie();
198     Rico.runLater(100,this,'insertResizers');  // avoids peek-a-boo bug in column 1 in IE6/7
199   },
200
201 /**
202  * Insert resizing handles
203  */
204   insertResizers: function() {
205     if (!this.options.allowColResize) return;
206     for (var x=0;x<this.columns.length;x++) {
207       this.columns[x].insertResizer();
208     }
209   },
210
211 /**
212  * Create div structure
213  */
214   createDivs: function() {
215     Rico.log("createDivs start");
216     this.outerDiv   = this.createDiv("outer");
217     if (Rico.theme.widget) Rico.addClass(this.outerDiv,Rico.theme.widget);
218     if (this.outerDiv.firstChild && this.outerDiv.firstChild.tagName && this.outerDiv.firstChild.tagName.toUpperCase()=='TABLE') {
219       this.structTab=this.outerDiv.firstChild;
220       this.structTabLeft=this.structTab.rows[0].cells[0];
221       this.structTabUR=this.structTab.rows[0].cells[1];
222       this.structTabLR=this.structTab.rows[1].cells[0];
223     } else {
224       this.structTab = document.createElement("table");
225       this.structTab.border=0;
226       this.structTab.cellPadding=0;
227       this.structTab.cellSpacing=0;
228       var tr1=this.structTab.insertRow(-1);
229       tr1.vAlign='top';
230       this.structTabLeft=tr1.insertCell(-1);
231       this.structTabLeft.rowSpan=2;
232       var tr2=this.structTab.insertRow(-1);
233       tr2.vAlign='top';
234       this.structTabUR=tr1.insertCell(-1);
235       this.structTabLR=tr2.insertCell(-1);
236       this.outerDiv.appendChild(this.structTab);
237     }
238     //this.structTabLR.style.overflow='hidden';
239     //if (Rico.isOpera) this.outerDiv.style.overflow="hidden";
240     this.frozenTabs = this.createDiv("frozenTabs",this.structTabLeft);
241     this.innerDiv   = this.createDiv("inner",this.structTabUR);
242     this.scrollDiv  = this.createDiv("scroll",this.structTabLR);
243     this.resizeDiv  = this.createDiv("resize",this.outerDiv,true);
244
245     this.messagePopup=new Rico.Popup();
246     this.messagePopup.createContainer({hideOnEscape:false, hideOnClick:false, parent:this.outerDiv});
247     this.messagePopup.content.className='ricoLG_messageDiv';
248     if (Rico.theme.gridMessage) Rico.addClass(this.messagePopup.content,Rico.theme.gridMessage);
249
250     this.keywordPopup=new Rico.Window('', {zIndex:-1, parent:this.outerDiv});
251     Rico.addClass(this.keywordPopup.container, 'ricoLG_keywordDiv');
252     var instructions=this.keywordPopup.contentDiv.appendChild(document.createElement("p"));
253     instructions.innerHTML=Rico.getPhraseById("keywordPrompt");
254     this.keywordBox=this.keywordPopup.contentDiv.appendChild(document.createElement("input"));
255     this.keywordBox.size=20;
256     Rico.eventBind(this.keywordBox,"keypress", Rico.eventHandle(this,'keywordKey'), false);
257     this.keywordPopup.contentDiv.appendChild(Rico.floatButton('Checkmark', Rico.eventHandle(this,'processKeyword')));
258     var s=this.keywordPopup.contentDiv.appendChild(document.createElement("p"));
259     Rico.setStyle(s,{clear:'both'});
260
261     //this.frozenTabs.style[this.align[0]]='0px';
262     //this.innerDiv.style[this.align[0]]='0px';
263     Rico.log("createDivs end");
264   },
265   
266   keywordKey: function(e) {
267     switch (Rico.eventKey(e)) {
268       case 27: this.closeKeyword(); Rico.eventStop(e); return false;
269       case 13: this.processKeyword(); Rico.eventStop(e); return false;
270     }
271     return true;
272   },
273   
274   openKeyword: function(colnum) {
275     this.keywordCol=colnum;
276     this.keywordBox.value='';
277     this.keywordPopup.setTitle(this.columns[colnum].displayName);
278     this.keywordPopup.centerPopup();
279     this.keywordBox.focus();
280   },
281   
282   closeKeyword: function() {
283     this.keywordPopup.closePopup();
284     this.cancelMenu();
285   },
286   
287   processKeyword: function() {
288     var keyword=this.keywordBox.value;
289     this.closeKeyword();
290     this.columns[this.keywordCol].setFilterKW(keyword);
291   },
292
293 /**
294  * Create a div and give it a standardized id and class name.
295  * If the div already exists, then just assign the class name.
296  */
297   createDiv: function(elemName,elemParent,hidden) {
298     var id=this.tableId+"_"+elemName+"Div";
299     var newdiv=document.getElementById(id);
300     if (!newdiv) {
301       newdiv = document.createElement("div");
302       newdiv.id = id;
303       if (elemParent) elemParent.appendChild(newdiv);
304     }
305     newdiv.className = "ricoLG_"+elemName+"Div";
306     if (hidden) Rico.hide(newdiv);
307     return newdiv;
308   },
309
310 /**
311  * Common code used to size & position divs in both SimpleGrid & LiveGrid
312  */
313   baseSizeDivs: function() {
314     this.setOtherHdrCellWidths();
315
316     if (this.options.frozenColumns) {
317       Rico.show(this.tabs[0]);
318       Rico.show(this.frozenTabs);
319       // order of next 3 lines is critical in IE6
320       this.hdrHt=Math.max(Rico.nan2zero(this.thead[0].offsetHeight),this.thead[1].offsetHeight);
321       this.dataHt=Math.max(Rico.nan2zero(this.tbody[0].offsetHeight),this.tbody[1].offsetHeight);
322       this.frzWi=this.borderWidth(this.tabs[0]);
323     } else {
324       Rico.hide(this.tabs[0]);
325       Rico.hide(this.frozenTabs);
326       this.frzWi=0;
327       this.hdrHt=this.thead[1].offsetHeight;
328       this.dataHt=this.tbody[1].offsetHeight;
329     }
330
331     var wiLimit,i;
332     var borderWi=this.borderWidth(this.columns[0].dataCell);
333     Rico.log('baseSizeDivs '+this.tableId+': hdrHt='+this.hdrHt+' dataHt='+this.dataHt);
334     Rico.log(this.tableId+' frzWi='+this.frzWi+' borderWi='+borderWi);
335     for (i=0; i<this.options.frozenColumns; i++) {
336       if (this.columns[i].visible) this.frzWi+=parseInt(this.columns[i].colWidth,10)+borderWi;
337     }
338     this.scrTabWi=this.borderWidth(this.tabs[1]);
339     this.scrTabWi0=this.scrTabWi;
340     Rico.log('scrTabWi: '+this.scrTabWi);
341     for (i=this.options.frozenColumns; i<this.columns.length; i++) {
342       if (this.columns[i].visible) this.scrTabWi+=parseInt(this.columns[i].colWidth,10)+borderWi;
343     }
344     this.scrWi=this.scrTabWi+this.options.scrollBarWidth;
345     if (this.sizeTo=='parent') {
346       if (Rico.isIE) Rico.hide(this.outerDiv);
347       wiLimit=this.outerDiv.parentNode.offsetWidth;
348       if (Rico.isIE) Rico.show(this.outerDiv);
349     }  else {
350       wiLimit=Rico.windowWidth()-this.options.scrollBarWidth-8;
351     }
352     if (this.outerDiv.parentNode.clientWidth > 0)
353       wiLimit=Math.min(this.outerDiv.parentNode.clientWidth, wiLimit);
354     var overage=this.frzWi+this.scrWi-wiLimit;
355     Rico.log('baseSizeDivs '+this.tableId+': scrWi='+this.scrWi+' wiLimit='+wiLimit+' overage='+overage+' clientWidth='+this.outerDiv.parentNode.clientWidth);
356     if (overage > 0 && this.options.frozenColumns < this.columns.length)
357       this.scrWi=Math.max(this.scrWi-overage, this.options.minScrollWidth);
358     this.scrollDiv.style.width=this.scrWi+'px';
359     //this.scrollDiv.style.top=this.hdrHt+'px';
360     //this.frozenTabs.style.width=this.scrollDiv.style[this.align[0]]=this.innerDiv.style[this.align[0]]=this.frzWi+'px';
361     this.frozenTabs.style.width=this.frzWi+'px';
362     this.outerDiv.style.width=(this.frzWi+this.scrWi)+'px';
363   },
364
365 /**
366  * Returns the sum of the left & right border widths of an element
367  */
368   borderWidth: function(elem) {
369     var l=Rico.nan2zero(Rico.getStyle(elem,'borderLeftWidth'));
370     var r=Rico.nan2zero(Rico.getStyle(elem,'borderRightWidth'));
371     Rico.log((elem.id || elem.tagName)+' borderWidth: L='+l+', R='+r);
372     return l + r;
373 //    return Rico.nan2zero(Rico.getStyle(elem,'borderLeftWidth')) + Rico.nan2zero(Rico.getStyle(elem,'borderRightWidth'));
374   },
375
376   setOtherHdrCellWidths: function() {
377     var c,i,j,r,w,hdrcell,cell,origSpan,newSpan,divs;
378     for (r=0; r<this.hdrCells.length; r++) {
379       if (r==this.headerRowIdx) continue;
380       Rico.log('setOtherHdrCellWidths: r='+r);
381       c=i=0;
382       while (i<this.headerColCnt && c<this.hdrCells[r].length) {
383         hdrcell=this.hdrCells[r][c];
384         cell=hdrcell.cell;
385         origSpan=newSpan=hdrcell.colSpan;
386         for (w=j=0; j<origSpan; j++, i++) {
387           if (this.columns[i].hdrCell.style.display=='none')
388             newSpan--;
389           else if (this.columns[i].hdrColDiv.style.display!='none')
390             w+=parseInt(this.columns[i].colWidth,10);
391         }
392         if (!hdrcell.hdrColDiv || !hdrcell.hdrCellDiv) {
393           divs=cell.getElementsByTagName('div');
394           hdrcell.hdrColDiv=(divs.length<1) ? Rico.wrapChildren(cell,'ricoLG_col') : divs[0];
395           hdrcell.hdrCellDiv=(divs.length<2) ? Rico.wrapChildren(hdrcell.hdrColDiv,'ricoLG_cell') : divs[1];
396         }
397         if (newSpan==0) {
398           cell.style.display='none';
399         } else if (w==0) {
400           hdrcell.hdrColDiv.style.display='none';
401           cell.colSpan=newSpan;
402         } else {
403           cell.style.display='';
404           hdrcell.hdrColDiv.style.display='';
405           cell.colSpan=newSpan;
406           hdrcell.hdrColDiv.style.width=w+'px';
407         }
408         c++;
409       }
410     }
411   },
412
413   initFilterImage: function(filterRowNum){
414     this.filterAnchor=document.getElementById(this.tableId+'_filterLink');
415     if (!this.filterAnchor) return;
416     this.filterRows=Rico.select('tr.'+this.tableId+'_hdg'+filterRowNum);
417     if (this.filterRows.length!=2) return;
418     for (var i=0, r=[]; i<2; i++) r[i]=Rico.select('.ricoLG_cell',this.filterRows[i]);
419     this.filterElements=r[0].concat(r[1]);
420     this.saveHeight = this.filterElements[0].offsetHeight;
421     var pt=Rico.getStyle(this.filterElements[0],'paddingTop');
422     var pb=Rico.getStyle(this.filterElements[0],'paddingBottom');
423     if (pt) this.saveHeight-=parseInt(pt,10);
424     if (pb) this.saveHeight-=parseInt(pb,10);
425     this.rowNum = filterRowNum;
426     this.setFilterImage(false);
427     //Rico.eventBind(this.filterAnchor, 'click', Rico.eventHandle(this,'toggleFilterRow'), false);
428   },
429
430   toggleFilterRow: function() {
431     if ( Rico.visible(this.filterRows[0]) )
432       this.slideFilterUp();
433     else
434       this.slideFilterDown();
435   },
436
437   setFilterImage: function(expandFlag) {
438     var altText=Rico.getPhraseById((expandFlag ? 'show' : 'hide')+'FilterRow');
439     this.filterAnchor.innerHTML = '<img src="'+Rico.imgDir+'tableFilter'+(expandFlag ? 'Expand' : 'Collapse')+'.gif" alt="'+altText+'" border="0">';
440   },
441
442 /**
443  * Returns a div for the cell at the specified row and column index.
444  * In SimpleGrid, r can refer to any row in the grid.
445  * In LiveGrid, r refers to a visible row (row 0 is the first visible row).
446  */
447   cell: function(r,c) {
448     return (0<=c && c<this.columns.length && r>=0) ? this.columns[c].cell(r) : null;
449   },
450
451 /**
452  * Returns the screen height available for a grid
453  */
454   availHt: function() {
455     var divPos=Rico.cumulativeOffset(this.outerDiv);
456     return Rico.windowHeight()-divPos.top-2*this.options.scrollBarWidth-15;  // allow for scrollbar and some margin
457   },
458
459   setHorizontalScroll: function() {
460     var newLeft=(-this.scrollDiv.scrollLeft)+'px';
461     this.hdrTabs[1].style.marginLeft=newLeft;
462   },
463
464   pluginScroll: function() {
465      if (this.scrollPluggedIn) return;
466      Rico.eventBind(this.scrollDiv,"scroll",this.scrollEventFunc, false);
467      this.scrollPluggedIn=true;
468   },
469
470   unplugScroll: function() {
471      Rico.eventUnbind(this.scrollDiv,"scroll", this.scrollEventFunc , false);
472      this.scrollPluggedIn=false;
473   },
474
475   hideMsg: function() {
476     this.messagePopup.closePopup();
477   },
478
479   showMsg: function(msg) {
480     this.messagePopup.setContent(msg);
481     this.messagePopup.centerPopup();
482     Rico.log("showMsg: "+msg);
483   },
484
485 /**
486  * @return array of column objects which have invisible status
487  */
488   listInvisible: function(attr) {
489     var hiddenColumns=[];
490     for (var x=0;x<this.columns.length;x++) {
491       if (!this.columns[x].visible)
492         hiddenColumns.push(attr ? this.columns[x][attr] : this.columns[x]);
493     }
494     return hiddenColumns;
495   },
496
497 /**
498  * @return index of left-most visibile column, or -1 if there are no visible columns
499  */
500   firstVisible: function() {
501     for (var x=0;x<this.columns.length;x++) {
502       if (this.columns[x].visible) return x;
503     }
504     return -1;
505   },
506
507 /**
508  * Show all columns
509  */
510   showAll: function() {
511     var invisible=this.listInvisible();
512     for (var x=0;x<invisible.length;x++)
513       invisible[x].showColumn();
514   },
515   
516   chooseColumns: function() {
517     this.menu.cancelmenu();
518     var x,z,col,itemDiv,span,contentDiv;\r
519     if (!this.columnChooser) {
520       Rico.log('creating columnChooser');
521       z=Rico.getStyle(this.outerDiv.offsetParent,'zIndex');
522       if (typeof z!='number') z=0;
523       this.columnChooser=new Rico.Window(Rico.getPhraseById('gridChooseCols'), {zIndex:z+2, parent:this.outerDiv});
524       Rico.addClass(this.columnChooser.container, 'ricoLG_chooserDiv');
525       contentDiv=this.columnChooser.contentDiv;
526       for (x=0;x<this.columns.length;x++) {
527         col=this.columns[x];
528         itemDiv=contentDiv.appendChild(document.createElement('div'));
529         col.ChooserBox=Rico.createFormField(itemDiv,'input','checkbox');
530         span=itemDiv.appendChild(document.createElement('span'));
531         span.innerHTML=col.displayName;
532         Rico.eventBind(col.ChooserBox, 'click', Rico.eventHandle(col,'chooseColumn'), false);
533       }
534     }
535     Rico.log('opening columnChooser');
536     this.columnChooser.openPopup(1,this.hdrHt);
537     for (x=0;x<this.columns.length;x++) {
538       this.columns[x].ChooserBox.checked=this.columns[x].visible;
539       this.columns[x].ChooserBox.disabled = !this.columns[x].canHideShow();
540     }
541   },
542
543   blankRow: function(r) {
544     for (var c=0; c < this.columns.length; c++) {
545       this.columns[c].clearCell(r);
546     }
547   },
548   
549   getExportStyles: function(chkelem) {
550     var exportStyles=this.options.exportStyleList;
551     var bgImg=Rico.getStyle(chkelem,'backgroundImage');
552     if (!bgImg || bgImg=='none') return exportStyles;
553     for (var styles=[],i=0; i<exportStyles.length; i++)
554       if (exportStyles[i]!='background-color' && exportStyles[i]!='color') styles.push(exportStyles[i]);
555     return styles;
556   },
557
558 /**
559  * Support function for printVisible()
560  */
561   exportStart: function() {
562     var r,c,i,j,hdrcell,newSpan,divs,cell;
563     var exportStyles=this.getExportStyles(this.thead[0]);
564     //alert(exportStyles.join('\n'));
565     this.exportRows=[];
566     this.exportText="<html><head></head><body><table border='1' cellspacing='0'>";\r
567     for (c=0; c<this.columns.length; c++) {\r
568       if (this.columns[c].visible) this.exportText+="<col width='"+parseInt(this.columns[c].colWidth,10)+"'>";
569     }\r
570     this.exportText+="<thead style='display: table-header-group;'>";
571     if (this.exportHeader) this.exportText+=this.exportHeader;
572     for (r=0; r<this.hdrCells.length; r++) {
573       if (this.hdrCells[r].length==0 || !Rico.visible(this.hdrCells[r][0].cell.parentNode)) continue;
574       this.exportText+="<tr>";
575       for (c=0,i=0; c<this.hdrCells[r].length; c++) {
576         hdrcell=this.hdrCells[r][c];
577         newSpan=hdrcell.colSpan;
578         for (j=0; j<hdrcell.colSpan; j++, i++) {
579           if (!this.columns[i].visible) newSpan--;
580         }
581         if (newSpan > 0) {
582           divs=Rico.select('.ricoLG_cell',hdrcell.cell);
583           cell=divs && divs.length>0 ? divs[0] : hdrcell.cell;
584           this.exportText+="<td style='"+this.exportStyle(cell,exportStyles)+"'";
585           if (hdrcell.colSpan > 1) this.exportText+=" colspan='"+newSpan+"'";
586           this.exportText+=">"+Rico.getInnerText(cell,!this.options.exportImgTags, !this.options.exportFormFields, 'NoExport')+"</td>";
587         }
588       }
589       this.exportText+="</tr>";
590     }
591     this.exportText+="</thead><tbody>";
592   },
593
594 /**
595  * Support function for printVisible().
596  */
597   exportFinish: function() {
598     if (this.hideMsg) this.hideMsg();
599     window.status=Rico.getPhraseById('exportComplete');
600     if (this.exportRows.length > 0) this.exportText+='<tr>'+this.exportRows.join('</tr><tr>')+'</tr>';
601     if (this.exportFooter) this.exportText+=this.exportFooter;
602     this.exportText+="</tbody></table></body></html>";
603     if (this.cancelMenu) this.cancelMenu();
604     var w=window.open('','_blank',this.options.exportWindow);
605     if (w == null) {
606       alert(Rico.getPhraseById('disableBlocker'));
607     } else {
608       w.document.open();
609       w.document.write(this.exportText);
610       w.document.close();
611     }
612     this.exportText=undefined;
613     this.exportRows=undefined;
614   },
615
616 /**
617  * Support function for printVisible()
618  */
619   exportStyle: function(elem,styleList) {
620     for (var i=0,s=''; i < styleList.length; i++) {
621       try {
622         var curstyle=Rico.getStyle(elem,styleList[i]);
623         if (curstyle) s+=styleList[i]+':'+curstyle+';';
624       } catch(e) {};
625     }
626     return s;
627   },
628
629 /**
630  * Gets the value of the grid cookie and interprets the contents.
631  * All information for a particular grid is stored in a single cookie.
632  * This may include column widths, column hide/show status, current sort, and any column filters.
633  */
634   getCookie: function() {
635     var c=Rico.getCookie(this.options.cookiePrefix+this.tableId);
636     if (!c) return;
637     var cookieVals=c.split(',');
638     for (var i=0; i<cookieVals.length; i++) {
639       var v=cookieVals[i].split(':');
640       if (v.length!=2) continue;
641       var colnum=parseInt(v[0].slice(1),10);
642       if (colnum < 0 || colnum >= this.columns.length) continue;
643       var col=this.columns[colnum];
644       switch (v[0].charAt(0)) {
645         case 'w':
646           col.setColWidth(v[1]);
647           col.customWidth=true;
648           break;
649         case 'h':
650           if (v[1].toLowerCase()=='true')
651             col.hideshow(true,true);
652           else
653             col.hideshow(false,true);
654           break;
655         case 's':
656           if (!this.options.saveColumnInfo.sort || !col.sortable) break;
657           col.setSorted(v[1]);
658           break;
659         case 'f':
660           if (!this.options.saveColumnInfo.filter || !col.filterable) break;
661           var filterTemp=v[1].split('~');
662           col.filterOp=filterTemp.shift();
663           col.filterValues = [];
664           col.filterType = Rico.ColumnConst.USERFILTER;
665           for (var j=0; j<filterTemp.length; j++)
666             col.filterValues.push(unescape(filterTemp[j]));
667           break;
668       }
669     }
670   },
671
672 /**
673  * Sets the grid cookie.
674  * All information for a particular grid is stored in a single cookie.
675  * This may include column widths, column hide/show status, current sort, and any column filters.
676  */
677   setCookie: function() {
678     var cookieVals=[];
679     for (var i=0; i<this.columns.length; i++) {
680       var col=this.columns[i];
681       if (this.options.saveColumnInfo.width) {
682         if (col.customWidth) cookieVals.push('w'+i+':'+col.colWidth);
683         if (col.customVisible) cookieVals.push('h'+i+':'+col.visible);
684       }
685       if (this.options.saveColumnInfo.sort) {
686         if (col.currentSort != Rico.ColumnConst.UNSORTED)
687           cookieVals.push('s'+i+':'+col.currentSort);
688       }
689       if (this.options.saveColumnInfo.filter && col.filterType == Rico.ColumnConst.USERFILTER) {
690         var filterTemp=[col.filterOp];
691         for (var j=0; j<col.filterValues.length; j++)
692           filterTemp.push(escape(col.filterValues[j]));
693         cookieVals.push('f'+i+':'+filterTemp.join('~'));
694       }
695     }
696     Rico.setCookie(this.options.cookiePrefix+this.tableId, cookieVals.join(','), this.options.cookieDays, this.options.cookiePath, this.options.cookieDomain);
697   }
698
699 };
700
701
702 Rico.ColumnConst = {
703   UNFILTERED:   0,
704   SYSTEMFILTER: 1,
705   USERFILTER:   2,
706
707   UNSORTED:  0,
708   SORT_ASC:  "ASC",
709   SORT_DESC: "DESC",
710
711   MINWIDTH: 10,
712   DOLLAR:  {type:'number', prefix:'$', decPlaces:2, ClassName:'alignright'},
713   EURO:    {type:'number', prefix:'&euro;', decPlaces:2, ClassName:'alignright'},
714   PERCENT: {type:'number', suffix:'%', decPlaces:2, multiplier:100, ClassName:'alignright'},
715   QTY:     {type:'number', decPlaces:0, ClassName:'alignright'},
716   DEFAULT: {type:"showTags"}
717 }
718
719
720 /**
721  * @class Define methods that are common to columns in both SimpleGrid and LiveGrid
722  */
723 Rico.TableColumnBase = function() {};
724
725 Rico.TableColumnBase.prototype = {
726
727 /**
728  * Common code used to initialize the column in both SimpleGrid & LiveGrid
729  */
730   baseInit: function(liveGrid,colIdx,hdrInfo,tabIdx) {
731     Rico.log("TableColumnBase.init index="+colIdx+" tabIdx="+tabIdx);
732     this.liveGrid  = liveGrid;
733     this.index     = colIdx;
734     this.hideWidth = Rico.isKonqueror || Rico.isWebKit || liveGrid.headerRowCnt>1 ? 5 : 2;  // column width used for "hidden" columns. Anything less than 5 causes problems with Konqueror. Best to keep this greater than padding used inside cell.
735     this.options   = liveGrid.options;
736     this.tabIdx    = tabIdx;
737     this.hdrCell   = hdrInfo.cell;
738     this.body = document.getElementsByTagName("body")[0];
739     this.displayName  = this.getDisplayName(this.hdrCell);
740     var divs=this.hdrCell.getElementsByTagName('div');
741     this.hdrColDiv=(divs.length<1) ? Rico.wrapChildren(this.hdrCell,'ricoLG_col') : divs[0];
742     this.hdrCellDiv=(divs.length<2) ? Rico.wrapChildren(this.hdrColDiv,'ricoLG_cell') : divs[1];
743     var sectionIndex= tabIdx==0 ? colIdx : colIdx-liveGrid.options.frozenColumns;
744     this.dataCell = liveGrid.tbody[tabIdx].rows[0].cells[sectionIndex];
745     divs=this.dataCell.getElementsByTagName('div');
746     this.dataColDiv=(divs.length<1) ? Rico.wrapChildren(this.dataCell,'ricoLG_col') : divs[0];
747
748     this.mouseDownHandler= Rico.eventHandle(this,'handleMouseDown');
749     this.mouseMoveHandler= Rico.eventHandle(this,'handleMouseMove');
750     this.mouseUpHandler  = Rico.eventHandle(this,'handleMouseUp');
751     this.mouseOutHandler = Rico.eventHandle(this,'handleMouseOut');
752
753     this.fieldName = 'col'+this.index;
754     var spec = liveGrid.options.columnSpecs[colIdx];
755     this.format=Rico.extend( {}, Rico.ColumnConst.DEFAULT);
756     switch (typeof spec) {
757       case 'object':
758         if (typeof spec.format=='string') Rico.extend(this.format, Rico.ColumnConst[spec.format.toUpperCase()]);
759         Rico.extend(this.format, spec);
760         break;
761       case 'string':
762         if (spec.slice(0,4)=='spec') spec=spec.slice(4).toUpperCase();  // for backwards compatibility
763         if (typeof Rico.ColumnConst[spec]=='object') Rico.extend(this.format, Rico.ColumnConst[spec]);
764         break;
765     }
766     Rico.addClass(this.dataColDiv, this.colClassName());
767     this.visible=true;
768     if (typeof this.format.visible=='boolean') this.visible=this.format.visible;
769     Rico.log("TableColumn.init index="+colIdx+" fieldName="+this.fieldName);
770     this.sortable     = typeof this.format.canSort=='boolean' ? this.format.canSort : liveGrid.options.canSortDefault;
771     this.currentSort  = Rico.ColumnConst.UNSORTED;
772     this.filterable   = typeof this.format.canFilter=='boolean' ? this.format.canFilter : liveGrid.options.canFilterDefault;
773     this.filterType   = Rico.ColumnConst.UNFILTERED;
774     this.hideable     = typeof this.format.canHide=='boolean' ? this.format.canHide : liveGrid.options.canHideDefault;
775
776     var wi;
777     switch (typeof this.format.width) {
778       case 'number': wi=this.format.width; break;
779       case 'string': wi=parseInt(this.format.width,10); break;
780       default:       wi=hdrInfo.initWidth; break;
781     }
782     wi=(typeof(wi)=='number') ? Math.max(wi,Rico.ColumnConst.MINWIDTH) : liveGrid.options.defaultWidth;
783     this.setColWidth(wi);
784     if (!this.visible) this.setDisplay('none');
785   },
786   
787   colClassName: function() {
788     return this.format.ClassName ? this.format.ClassName : this.liveGrid.tableId+'_col'+this.index;
789   },
790
791   insertResizer: function() {
792     //this.hdrCell.style.width='';
793     if (this.format.noResize) return;
794     var resizer=document.createElement('div');
795     resizer.className='ricoLG_Resize';
796     resizer.style[this.liveGrid.align[1]]='0px';
797     this.hdrCellDiv.appendChild(resizer);
798     Rico.eventBind(resizer,"mousedown", this.mouseDownHandler, false);
799   },
800
801 /**
802  * get the display name of a column
803  */
804   getDisplayName: function(el) {
805     var anchors=el.getElementsByTagName("A");
806     //Check the existance of A tags
807     if (anchors.length > 0)
808       return anchors[0].innerHTML;
809     else
810       return Rico.stripTags(el.innerHTML);
811   },
812
813   _clear: function(gridCell) {
814     gridCell.innerHTML='&nbsp;';
815   },
816
817   clearCell: function(rowIndex) {
818     var gridCell=this.cell(rowIndex);
819     this._clear(gridCell,rowIndex);
820     if (!this.liveGrid.buffer) return;
821     var acceptAttr=this.liveGrid.buffer.options.acceptAttr;
822     for (var k=0; k<acceptAttr.length; k++) {
823       switch (acceptAttr[k]) {
824         case 'style': gridCell.style.cssText=''; break;
825         case 'class': gridCell.className=''; break;
826         default:      gridCell['_'+acceptAttr[k]]=''; break;
827       }
828     }
829   },
830
831   dataTable: function() {
832     return this.liveGrid.tabs[this.tabIdx];
833   },
834
835   numRows: function() {
836     return this.dataColDiv.childNodes.length;
837   },
838
839   clearColumn: function() {
840     var childCnt=this.numRows();
841     for (var r=0; r<childCnt; r++)
842       this.clearCell(r);
843   },
844
845   cell: function(r) {
846     return this.dataColDiv.childNodes[r];
847   },
848
849   getFormattedValue: function(r,xImg,xForm,xClass) {
850     return Rico.getInnerText(this.cell(r),xImg,xForm,xClass);
851   },
852
853   setColWidth: function(wi) {
854     if (typeof wi=='number') {
855       wi=parseInt(wi,10);
856       if (wi < Rico.ColumnConst.MINWIDTH) return;
857       wi=wi+'px';
858     }
859     Rico.log('setColWidth '+this.index+': '+wi);
860     this.colWidth=wi;
861     this.hdrColDiv.style.width=wi;
862     this.dataColDiv.style.width=wi;
863   },
864
865   pluginMouseEvents: function() {
866     if (this.mousePluggedIn==true) return;
867     Rico.eventBind(this.body,"mousemove", this.mouseMoveHandler, false);
868     Rico.eventBind(this.body,"mouseup",   this.mouseUpHandler  , false);
869     Rico.eventBind(this.body,"mouseout",  this.mouseOutHandler , false);
870     this.mousePluggedIn=true;
871   },
872
873   unplugMouseEvents: function() {
874     Rico.eventUnbind(this.body,"mousemove", this.mouseMoveHandler, false);
875     Rico.eventUnbind(this.body,"mouseup",   this.mouseUpHandler  , false);
876     Rico.eventUnbind(this.body,"mouseout",  this.mouseOutHandler , false);
877     this.mousePluggedIn=false;
878   },
879
880   handleMouseDown: function(e) {
881     this.resizeStart=Rico.eventClient(e).x;
882     this.origWidth=parseInt(this.colWidth,10);
883     var p=Rico.positionedOffset(this.hdrCell);
884     if (this.liveGrid.direction=='rtl') {
885       this.edge=p.left+this.liveGrid.options.scrollBarWidth;
886       switch (this.tabIdx) {
887         case 0: this.edge+=this.liveGrid.innerDiv.offsetWidth; break;
888         case 1: this.edge-=this.liveGrid.scrollDiv.scrollLeft; break;
889       }
890     } else {
891       this.edge=p.left+this.hdrCell.offsetWidth;
892       if (this.tabIdx>0) this.edge+=Rico.nan2zero(this.liveGrid.tabs[0].offsetWidth);
893     }
894     this.liveGrid.resizeDiv.style.left=this.edge+"px";
895     this.liveGrid.resizeDiv.style.display="";
896     this.liveGrid.outerDiv.style.cursor='e-resize';
897     this.tmpHighlight=this.liveGrid.highlightEnabled;
898     this.liveGrid.highlightEnabled=false;
899     this.pluginMouseEvents();
900     Rico.eventStop(e);
901   },
902
903   handleMouseMove: function(e) {
904     var delta=Rico.eventClient(e).x-this.resizeStart;
905     var newWidth=(this.liveGrid.direction=='rtl') ? this.origWidth-delta : this.origWidth+delta;
906     if (newWidth < Rico.ColumnConst.MINWIDTH) return;
907     this.liveGrid.resizeDiv.style.left=(this.edge+delta)+"px";
908     this.colWidth=newWidth;
909     Rico.eventStop(e);
910   },
911
912   handleMouseUp: function(e) {
913     this.unplugMouseEvents();
914     Rico.log('handleMouseUp '+this.liveGrid.tableId);
915     this.liveGrid.outerDiv.style.cursor='';
916     this.liveGrid.resizeDiv.style.display="none";
917     this.setColWidth(this.colWidth);
918     this.customWidth=true;
919     this.liveGrid.setCookie();
920     this.liveGrid.highlightEnabled=this.tmpHighlight;
921     this.liveGrid.sizeDivs();
922     Rico.eventStop(e);
923   },
924
925   handleMouseOut: function(e) {
926     var reltg = Rico.eventRelatedTarget(e) || e.toElement;
927     while (reltg != null && reltg.nodeName.toLowerCase() != 'body')
928       reltg=reltg.parentNode;
929     if (reltg!=null && reltg.nodeName.toLowerCase() == 'body') return true;
930     this.handleMouseUp(e);
931     return true;
932   },
933
934   setDisplay: function(d) {
935     this.hdrCell.style.display=d;
936     this.hdrColDiv.style.display=d;
937     this.dataCell.style.display=d;
938     this.dataColDiv.style.display=d;
939   },
940   
941   hideshow: function(visible,noresize) {
942     this.setDisplay(visible ? '' : 'none');
943     this.liveGrid.cancelMenu();
944     this.visible=visible;
945     this.customVisible=true;
946     if (noresize) return;
947     this.liveGrid.setCookie();
948     this.liveGrid.sizeDivs();
949   },
950
951   hideColumn: function() {
952     Rico.log('hideColumn '+this.liveGrid.tableId);
953     this.hideshow(false,false);
954   },
955
956   showColumn: function() {
957     Rico.log('showColumn '+this.liveGrid.tableId);
958     this.hideshow(true,false);
959   },
960
961   chooseColumn: function(e) {
962     var elem=Rico.eventElement(e);
963     this.hideshow(elem.checked,false);
964   },
965
966   setImage: function() {
967     if ( this.currentSort == Rico.ColumnConst.SORT_ASC ) {
968        this.imgSort.style.display='inline-block';
969        this.imgSort.className=Rico.theme.sortAsc || 'rico-icon ricoLG_sortAsc';
970     } else if ( this.currentSort == Rico.ColumnConst.SORT_DESC ) {
971        this.imgSort.style.display='inline-block';
972        this.imgSort.className=Rico.theme.sortDesc || 'rico-icon ricoLG_sortDesc';
973     } else {
974        this.imgSort.style.display='none';
975     }
976     if (this.filterType == Rico.ColumnConst.USERFILTER) {
977        this.imgFilter.style.display='inline-block';
978        this.imgFilter.title=this.getFilterText();
979     } else {
980        this.imgFilter.style.display='none';
981     }
982   },
983
984   canHideShow: function() {
985     return this.hideable;
986   }
987
988 };