2 * (c) 2005-2007 Richard Cowin (http://openrico.org)
3 * (c) 2005-2007 Matt Brown (http://dowdybrown.com)
5 * Rico is 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 * http://www.apache.org/licenses/LICENSE-2.0
11 if(typeof Rico=='undefined') throw("SimpleGrid requires the Rico JavaScript framework");
12 if(typeof RicoUtil=='undefined') throw("SimpleGrid requires the RicoUtil Library");
13 if(typeof RicoTranslate=='undefined') throw("SimpleGrid requires the RicoTranslate Library");
15 Rico.SimpleGrid = Class.create();
17 Rico.SimpleGrid.prototype = {
19 initialize: function( tableId, options ) {
20 Object.extend(this, new Rico.GridCommon);
21 this.baseInit(tableId);
22 Rico.setDebugArea(tableId+"_debugmsgs"); // if used, this should be a textarea
23 Object.extend(this.options, options || {});
24 this.tableId = tableId;
26 this.hdrTabs=new Array(2);
27 for (var i=0; i<2; i++) {
28 this.tabs[i]=$(tableId+'_tab'+i);
29 this.hdrTabs[i]=$(tableId+'_tab'+i+'h');
30 if (i==0) this.tabs[i].style.position='absolute';
31 if (i==0) this.tabs[i].style.left='0px';
32 this.hdrTabs[i].style.position='absolute';
33 this.hdrTabs[i].style.top='0px';
34 this.hdrTabs[i].style.zIndex=1;
35 this.thead[i]=this.hdrTabs[i];
36 this.tbody[i]=this.tabs[i];
37 this.headerColCnt = this.getColumnInfo(this.hdrTabs[i].rows);
38 if (i==0) this.options.frozenColumns=this.headerColCnt;
40 if (this.headerColCnt==0) {
41 alert('ERROR: no columns found in "'+this.tableId+'"');
44 this.hdrHt=Math.max(RicoUtil.nan2zero(this.hdrTabs[0].offsetHeight),this.hdrTabs[1].offsetHeight);
45 for (var i=0; i<2; i++)
46 if (i==0) this.tabs[i].style.top=this.hdrHt+'px';
47 this.createColumnArray();
48 this.pageSize=this.columns[0].dataColDiv.childNodes.length;
50 this.attachMenuEvents();
51 this.scrollEventFunc=this.handleScroll.bindAsEventListener(this);
53 if (this.options.windowResize)
54 Event.observe(window,"resize", this.sizeDivs.bindAsEventListener(this), false);
58 * Register a menu that will only be used in the scrolling part of the grid.
59 * If submenus are used, they must be registered after the main menu.
61 registerScrollMenu: function(menu) {
62 if (!this.menu) this.menu=menu;
64 menu.showmenu=menu.showSimpleMenu;
65 menu.showSubMenu=menu.showSimpleSubMenu;
66 menu.createDiv(this.scrollDiv);
69 handleMenuClick: function(e) {
71 this.menuCell=RicoUtil.getParentByTagName(Event.element(e),'div');
72 this.highlightEnabled=false;
73 if (this.hideScroll) this.scrollDiv.style.overflow="hidden";
74 if (this.menu.buildGridMenu) this.menu.buildGridMenu(this.menuCell);
75 this.menu.showmenu(e,this.closeMenu.bind(this));
78 closeMenu: function() {
79 if (this.hideScroll) this.scrollDiv.style.overflow="";
80 this.highlightEnabled=true;
83 sizeDivs: function() {
84 if (this.outerDiv.offsetParent.style.display=='none') return;
86 var maxHt=Math.max(this.options.maxHt || this.availHt(), 50);
87 var totHt=Math.min(this.hdrHt+this.dataHt, maxHt);
88 Rico.writeDebugMsg('sizeDivs '+this.tableId+': hdrHt='+this.hdrHt+' dataHt='+this.dataHt);
89 this.dataHt=totHt-this.hdrHt;
90 if (this.scrWi>0) this.dataHt+=this.options.scrollBarWidth;
91 this.scrollDiv.style.height=this.dataHt+'px';
93 this.innerDiv.style.width=(this.scrWi-this.options.scrollBarWidth+divAdjust)+'px';
94 this.innerDiv.style.height=this.hdrHt+'px';
96 this.resizeDiv.style.height=this.frozenTabs.style.height=totHt+'px';
97 this.outerDiv.style.height=(totHt+this.options.scrollBarWidth)+'px';
98 this.setHorizontalScroll();
104 Object.extend(Rico.Menu.prototype, {
106 showSimpleMenu: function(e,hideFunc) {
108 this.hideFunc=hideFunc;
109 if (this.div.childNodes.length==0) {
113 this.clientX=Event.pointerX(e);
114 this.clientY=Event.pointerY(e);
115 var elem=Event.element(e);
116 while (elem && !Element.hasClassName(elem,'ricoLG_cell'))
117 elem=elem.parentNode;
118 if (!elem) return false;
119 var td=RicoUtil.getParentByTagName(elem,'td');
121 var newLeft=Math.floor(td.offsetLeft+td.offsetWidth/2);
122 if (this.direction == 'rtl') {
123 if (newLeft > this.width) newLeft-=this.width;
125 if (newLeft+this.width+this.options.margin > this.grid.scrollDiv.scrollLeft+this.grid.scrollDiv.clientWidth) newLeft-=this.width;
127 this.div.style.visibility="hidden";
128 this.div.style.display="block";
129 var contentHt=this.div.offsetHeight;
130 var newTop=Math.floor(elem.offsetTop+elem.offsetHeight/2);
131 if (newTop+contentHt+this.options.margin > this.grid.scrollDiv.scrollTop+this.grid.scrollDiv.clientHeight)
132 newTop=Math.max(newTop-contentHt,0);
133 this.openPopup(newLeft,newTop);
134 this.div.style.visibility ="visible";
138 showSimpleSubMenu: function(a,submenu) {
139 if (this.openSubMenu) this.hideSubMenu();
140 this.openSubMenu=submenu;
141 this.openMenuAnchor=a;
142 if (a.className=='ricoSubMenu') a.className='ricoSubMenuOpen';
143 var top=parseInt(this.div.style.top);
144 var left=parseInt(this.div.style.left);
145 submenu.openPopup(left+a.offsetWidth,top+a.offsetTop);
146 submenu.div.style.visibility ="visible";
152 Object.extend(Rico.TableColumn.prototype, {
154 initialize: function(grid,colIdx,hdrInfo,tabIdx) {
155 this.baseInit(grid,colIdx,hdrInfo,tabIdx);
160 Rico.includeLoaded('ricoSimpleGrid.js');