c742f036e7ee23fe3ffadbbb373f5ed4280d8a0a
[infodrom/rico3] / plugins / dotnet / SimpleGrid.ascx.vb
1 Imports System.Data\r
2 \r
3 Partial Class SimpleGrid\r
4 Inherits System.Web.UI.UserControl\r
5 \r
6 Private _gridHeading As ITemplate = Nothing\r
7 Protected HdgContainer As New GridContainer()\r
8 \r
9 Public columns as New ArrayList()\r
10 Public gridVar as String\r
11 Public optionsVar as String   ' name of grid options js var\r
12 Public FilterLocation as Integer = -2\r
13 Public FilterAllToken as String\r
14 Public FilterBoxMaxLen as Integer = -1\r
15 Public FilterAnchorLeft as Boolean = false  ' when matching text box values, should they match beginning of string (true) or anywhere in string (false)?
16 Public UsingMinRico as Boolean = True       ' using minified version of Rico?\r
17 Public defaultWidth as Integer = -1         ' if -1, then use unformatted column width, otherwise this is the default width in pixels\r
18 Public menuEvent as String\r
19 public rows as New ArrayList()\r
20 public FrozenCols as Integer\r
21 private LastRow,LastHeadingRow,ResizeRowIdx\r
22 \r
23 \r
24 \r
25 Sub Page_Init()\r
26   gridVar=Me.UniqueId & "['grid']"\r
27   optionsVar=Me.UniqueId & "['options']"\r
28 \r
29   If Not (_gridHeading Is Nothing) Then\r
30     _gridHeading.InstantiateIn(HdgContainer)\r
31     For Each ctrl As Control In HdgContainer.Controls\r
32       If TypeOf(ctrl) is GridColumn then\r
33         columns.Add(ctrl)\r
34       end if\r
35     Next\r
36   End If\r
37 End Sub\r
38 \r
39 \r
40 Public Class HeadingCellClass\r
41   Public content As String, span As Integer\r
42 \r
43   Public Sub New(Optional contentParm As String = "", Optional spanParm As Integer = 1)\r
44     content=contentParm\r
45     span=spanParm\r
46   End Sub\r
47 End Class\r
48 \r
49 class SimpleGridCell\r
50   public content as String\r
51   private attr As New Hashtable()\r
52 \r
53   Public Function HeadingCell() as object\r
54     Dim s as String, span as Integer\r
55     s="<td"\r
56     span=1\r
57     If attr.contains("colspan") Then\r
58       span=CInt(attr("colspan"))\r
59       s &= " colspan='" & span & "'"\r
60     End If\r
61     dim content as String=s & "><div class='ricoLG_col'>" & DataCell("") & "</div></td>"\r
62     dim result() as object = {content,span}\r
63     HeadingCell = result\r
64   End Function\r
65 \r
66   Public Function DataCell(rowclass as String) as String\r
67     dim s as String, k as String\r
68     s = "<div"\r
69     attr("class")=trim("ricoLG_cell " & attr("class") & " " & rowclass)\r
70     for each k in attr.keys\r
71       If k<>"colspan" Then s=s & " " & k & "='" & attr(k) & "'"\r
72     next\r
73     s=s & ">" & content & "</div>"\r
74     DataCell=s\r
75   End Function\r
76 \r
77   Public Function HtmlCell()\r
78     dim s as String="", k as String\r
79     for each k in attr.keys\r
80       s &= " " & k & "='" & attr(k) & "'"\r
81     next\r
82     HtmlCell="<td" & s & ">" & content & "</td>"\r
83   End Function\r
84 \r
85   Public Sub SetAttr(name as String, value as String)\r
86     attr(name)=value\r
87   End Sub\r
88 End class\r
89 \r
90 \r
91 class SimpleGridRow\r
92   public cells as New ArrayList()\r
93   private attr As New Hashtable()\r
94   private CurrentCell as SimpleGridCell\r
95 \r
96   Public Sub AddCell(ByVal content as String)\r
97     CurrentCell=new SimpleGridCell()\r
98     cells.Add(CurrentCell)\r
99     CurrentCell.content=content\r
100   End Sub\r
101   \r
102   Public Function HeadingRow(ByVal c1 as Integer, ByVal c2 as Integer) as String\r
103     dim s as String, a\r
104     dim cellidx as Integer=0\r
105     dim colidx as Integer=0\r
106     while colidx < c1 and cellidx < cells.count\r
107       a=cells(cellidx).HeadingCell()\r
108       colidx+=CInt(a(1))\r
109       cellidx+=1\r
110     end while\r
111     while (colidx <= c2 or c2=-1) and cellidx < cells.count\r
112       a=cells(cellidx).HeadingCell()\r
113       s &= a(0)\r
114       colidx+=CInt(a(1))\r
115       cellidx+=1\r
116     end while\r
117     HeadingRow = s\r
118   End Function\r
119   \r
120   Public Function HeadingClass()\r
121     HeadingClass=trim("ricoLG_hdg " & attr("class"))\r
122   End Function\r
123   \r
124   Public Function CellCount()\r
125     CellCount=cells.count\r
126   End Function\r
127 \r
128   Public Function GetRowAttr(ByVal name)\r
129     GetRowAttr=attr(name)\r
130   End Function\r
131 \r
132   Public Sub SetRowAttr(ByVal name, ByVal value)\r
133     attr(name)=value\r
134   End Sub\r
135 \r
136   Public Sub SetCellAttr(ByVal name, ByVal value)\r
137     CurrentCell.SetAttr(name,value)\r
138   End Sub\r
139 end class\r
140 \r
141 \r
142 Public Function AddHeadingRow(ResizeRowFlag as Boolean)\r
143   LastHeadingRow=AddDataRow()\r
144   if ResizeRowFlag then ResizeRowIdx=LastHeadingRow\r
145   AddHeadingRow=LastHeadingRow\r
146 End Function\r
147 \r
148 Public Function AddDataRow()\r
149   rows.Add(new SimpleGridRow())\r
150   LastRow=rows.count-1\r
151   AddDataRow=LastRow\r
152 End Function\r
153 \r
154 Public Function HeadingRowCount()\r
155   if IsNothing(LastHeadingRow) then\r
156     HeadingRowCount=0\r
157   else\r
158     HeadingRowCount=LastHeadingRow+1\r
159   end if\r
160 End Function\r
161 \r
162 Public Function DataRowCount()\r
163   if IsNothing(LastRow) then\r
164     DataRowCount=0\r
165   else\r
166     DataRowCount=LastRow+1-HeadingRowCount()\r
167   end if\r
168 End Function\r
169 \r
170 ' returns # of cells in the current row\r
171 Public Function CellCount()\r
172   CellCount=rows(LastRow).CellCount\r
173 End Function\r
174 \r
175 Public Sub AddCell(ByVal content as String)\r
176   rows(LastRow).AddCell(content)\r
177 End Sub\r
178 \r
179 Public Sub AddCellToRow(ByVal RowIdx as Integer, ByVal content as String)\r
180   LastRow=RowIdx\r
181   AddCell(content)\r
182 End Sub\r
183 \r
184 Public Sub SetRowAttr(ByVal name as String, ByVal value as String)\r
185   rows(LastRow).SetRowAttr(name,value)\r
186 End Sub\r
187 \r
188 Public Sub SetCellAttr(ByVal name as String, ByVal value as String)\r
189   rows(LastRow).SetCellAttr(name,value)\r
190 End Sub\r
191 \r
192 Private Function RenderColumns(writer as HTMLTextWriter, c1 as Integer, c2 as Integer)\r
193   dim r as Integer, c as Integer\r
194   for c=c1 to c2\r
195     writer.Write("<td><div class='ricoLG_col'>")\r
196     for r=LastHeadingRow+1 to rows.count-1\r
197       writer.Write(rows(r).cells(c).DataCell(rows(r).GetRowAttr("class")))\r
198     next\r
199     writer.WriteLine("</div></td>")\r
200   next\r
201 End Function\r
202 \r
203 <TemplateContainer(GetType(GridContainer))> _\r
204 Public Property GridColumns() As ITemplate\r
205   Get\r
206     Return _gridHeading\r
207   End Get\r
208   Set\r
209     _gridHeading = value\r
210   End Set\r
211 End Property\r
212 \r
213 Protected Overrides Sub Render(writer as HTMLTextWriter)\r
214   dim colcnt as Integer, r as Integer, c as Integer\r
215   if IsNothing(ResizeRowIdx) then exit sub\r
216   colcnt=rows(ResizeRowIdx).CellCount\r
217   \r
218   writer.WriteLine("<script type='text/javascript'>")\r
219   if not UsingMinRico then writer.WriteLine("Rico.loadModule('SimpleGrid');")\r
220   writer.WriteLine("var " & Me.UniqueId & " = {};")\r
221   writer.WriteLine("Rico.onLoad( function() {")\r
222   writer.WriteLine("  " & optionsVar & " = {")\r
223   if FilterLocation >= -1       then writer.WriteLine("    FilterLocation: " & FilterLocation & ",")\r
224   if not IsNothing(FilterAllToken) then writer.WriteLine("    FilterAllToken: '" & FilterAllToken & "',")\r
225   if FilterBoxMaxLen >= 0       then writer.WriteLine("    FilterBoxMaxLen: " & FilterBoxMaxLen & ",")\r
226   if FilterAnchorLeft           then writer.WriteLine("    FilterAnchorLeft: " & lcase(FilterAnchorLeft) & ",")\r
227   if defaultWidth > 0           then writer.WriteLine("    defaultWidth: " & defaultWidth & ",")\r
228   if not IsNothing(menuEvent)   then writer.WriteLine("    menuEvent: '" & menuEvent & "',")\r
229   writer.WriteLine("    columnSpecs: [")\r
230   for c=0 to columns.count-1\r
231     if c > 0 then writer.WriteLine(",")\r
232     writer.Write(CType(columns(c),GridColumn).script)\r
233   next\r
234   writer.WriteLine(vbCrLf & "    ]")\r
235   writer.WriteLine("  };")\r
236   writer.WriteLine("  " & gridVar & "=new Rico.SimpleGrid('" & Me.UniqueId & "', " & optionsVar & ");")\r
237   writer.WriteLine("  if(typeof " & Me.UniqueId & "_InitComplete=='function') " & Me.UniqueId & "_InitComplete();" & vbCrLf)\r
238   writer.WriteLine("});")\r
239   writer.WriteLine("</script>")\r
240   \r
241   writer.Write("<div id='" & Me.UniqueId & "_outerDiv'>")\r
242 \r
243   '-------------------\r
244   ' frozen columns\r
245   '-------------------\r
246   writer.WriteLine("<div id='" & Me.UniqueId & "_frozenTabsDiv'>")\r
247 \r
248   ' upper left\r
249   writer.WriteLine("<table id='" & Me.UniqueId & "_tab0h' class='ricoLG_table ricoLG_top ricoLG_left' cellspacing='0' cellpadding='0'><thead>")\r
250   for r=0 to LastHeadingRow\r
251     writer.Write("<tr class='" & rows(r).HeadingClass() & "'")\r
252     if r=ResizeRowIdx then writer.Write(" id='" & Me.UniqueId & "_tab0h_main'")\r
253     writer.WriteLine(">")\r
254     writer.Write(rows(r).HeadingRow(0,FrozenCols-1))\r
255     writer.Write("</tr>")\r
256   next\r
257   writer.WriteLine("</thead></table>")\r
258 \r
259   ' lower left\r
260   writer.Write("<table id='" & Me.UniqueId & "_tab0' class='ricoLG_table ricoLG_bottom ricoLG_left' cellspacing='0' cellpadding='0'>")\r
261   writer.WriteLine("<tr>")\r
262   RenderColumns(writer,0,FrozenCols-1)\r
263   writer.Write("</tr>")\r
264   writer.WriteLine("</table>")\r
265 \r
266   writer.WriteLine("</div>")\r
267 \r
268 \r
269   '-------------------\r
270   ' scrolling columns\r
271   '-------------------\r
272 \r
273   ' upper right\r
274   writer.Write("<div id='" & Me.UniqueId & "_innerDiv'>")\r
275   writer.Write("<div id='" & Me.UniqueId & "_scrollTabsDiv'>")\r
276   writer.WriteLine("<table id='" & Me.UniqueId & "_tab1h' class='ricoLG_table ricoLG_top ricoLG_right' cellspacing='0' cellpadding='0'><thead>")\r
277   for r=0 to LastHeadingRow\r
278     writer.Write("<tr class='" & rows(r).HeadingClass & "'")\r
279     if r=ResizeRowIdx then writer.Write(" id='" & Me.UniqueId & "_tab1h_main'")\r
280     writer.Write(">")\r
281     writer.Write(rows(r).HeadingRow(FrozenCols,-1))\r
282     writer.Write("</tr>")\r
283   next\r
284   writer.Write("</thead></table>")\r
285   writer.Write("</div>")\r
286   writer.WriteLine("</div>")\r
287 \r
288   ' lower right\r
289   writer.Write("<div id='" & Me.UniqueId & "_scrollDiv'>")\r
290   writer.Write("<table id='" & Me.UniqueId & "_tab1' class='ricoLG_table ricoLG_bottom ricoLG_right' cellspacing='0' cellpadding='0'>")\r
291   writer.WriteLine("<tr>")\r
292   RenderColumns(writer,FrozenCols,colcnt-1)\r
293   writer.Write("</tr>")\r
294   writer.Write("</table>")\r
295   writer.Write("</div>")\r
296 \r
297   writer.WriteLine("</div>")\r
298 End Sub\r
299 \r
300 ' Response.Buffer must be true\r
301 Public Sub RenderExcel(fileName)\r
302   Dim r as Integer, c as Integer\r
303   Dim sw As New System.IO.StringWriter\r
304 \r
305   HttpContext.Current.Response.Clear()\r
306   if fileName<>"" then HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & fileName)\r
307   HttpContext.Current.Response.ContentType = "application/ms-excel"\r
308 \r
309   sw.WriteLine("<table>")\r
310   for r=0 to rows.count-1\r
311     sw.WriteLine("<tr>")\r
312     for c=0 to rows(r).CellCount()-1\r
313       sw.Write(rows(r).cells(c).HtmlCell())\r
314     next\r
315     sw.WriteLine("</tr>")\r
316   next\r
317   sw.WriteLine("</table>")\r
318   HttpContext.Current.Response.Write(sw.ToString)\r
319   HttpContext.Current.Response.End()\r
320 End Sub\r
321 \r
322 ' Response.Buffer must be true\r
323 Public Sub RenderDelimited(fileName,delim,SubstituteChar)\r
324   Dim r as Integer, c as Integer\r
325   Dim sw As New System.IO.StringWriter\r
326 \r
327   HttpContext.Current.Response.Clear()\r
328   if fileName<>"" then HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & fileName)\r
329   HttpContext.Current.Response.ContentType = "text/csv"\r
330 \r
331   for r=0 to rows.count-1\r
332     for c=0 to rows(r).CellCount()-1\r
333       if c > 0 then sw.Write(delim)\r
334       sw.Write(replace(rows(r).cells(c).content,delim,SubstituteChar))\r
335     next\r
336     sw.WriteLine("")\r
337   next\r
338   HttpContext.Current.Response.Write(sw.ToString)\r
339   HttpContext.Current.Response.End()\r
340 End Sub\r
341 \r
342 Public Class GridContainer\r
343   Inherits Control\r
344   Implements INamingContainer\r
345 End Class\r
346 \r
347 End Class\r