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