forked from webgrid/WebGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridSetup.cs
428 lines (363 loc) · 21.8 KB
/
GridSetup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/*
Copyright © Olav Christian Botterli.
Dual licensed under the MIT or GPL Version 2 licenses.
Date: 30.08.2011, Norway.
http://www.webgrid.com
*/
#region Header
/*
Copyright © Olav Christian Botterli.
Dual licensed under the MIT or GPL Version 2 licenses.
Date: 30.08.2011, Norway.
http://www.webgrid.com
*/
#endregion Header
namespace WebGrid
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using Data.Database;
using Design;
using Enums;
using Util;
public partial class Grid
{
#region Methods
/// <summary>
/// Inserts a client script resource into the Html header of the page rather
/// than into the body as RegisterClientScriptInclude does.
///
/// Scripts references are embedded at the bottom of the Html Header after
/// any manually added header scripts.
/// </summary>
/// <param name="page"></param>
/// <param name="type"></param>
/// <param name="resourceName"></param>
/// <param name="scriptName"></param>
internal void RegisterClientScriptResourceInHeader(Page page, Type type, string resourceName, string scriptName)
{
if (page == null)
return;
// Can't do this if there's no header to work with - degrade
if (page.Header == null || !GotHttpContext)
{
page.ClientScript.RegisterClientScriptInclude(scriptName,
page.ClientScript.GetWebResourceUrl(type.GetType(),
resourceName).Replace(
"&", "&"));
return;
}
// *** Keep duplicates from getting written
const string identifier = "headerscript_";
if (HttpContext.Current.Items.Contains(identifier + resourceName))
return;
HttpContext.Current.Items.Add(identifier + resourceName, string.Empty);
object val = HttpContext.Current.Items["__ScriptResourceIndex"];
int index = 0;
if (val != null)
index = (int)val;
else if (Scripts != null && page.Header.Controls.Count - 1 > Scripts.HeaderStartIndex)
index = Scripts.HeaderStartIndex;
string script = page.ClientScript.GetWebResourceUrl(type, resourceName).Replace("&", "&");
StringBuilder sb = new StringBuilder(300);
sb.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>\r\n", script);
if (page.Header == null)
throw new InvalidOperationException(
"Can't add resources to page: missing <head runat=\"server\"> tag in the page.");
page.Header.Controls.AddAt(index, new LiteralControl(sb.ToString()));
index++;
HttpContext.Current.Items["__ScriptResourceIndex"] = index;
}
internal void SetupFilterByColumns()
{
if (DesignMode || DisplayView != DisplayView.Grid)
return;
foreach (Column column in MasterTable.Columns)
{
if (!column.FilterByColumn)
continue;
if (MasterTable.DataSourceType != DataSourceControlType.InternalDataSource)
throw new GridException(
"FilterByColumn property is only supported when the Grid.DataSourceID has reference to a database table.");
string where = Interface.BuildFilter(MasterTable, true);
string sowhere = BuildFilterByColumnSql();
if (string.IsNullOrEmpty(sowhere) == false)
where = where.Replace(where, string.Empty);
switch (column.ColumnType)
{
case ColumnType.Foreignkey:
{
where = null;
if (string.IsNullOrEmpty(((Foreignkey)column).FilterExpression) == false)
where = string.Format(" WHERE {0}", ((Foreignkey)column).FilterExpression);
if (column.m_Table.m_Grid.Page != null)
foreach (Column foreignkeycolumn in ((Foreignkey)column).Table.Columns)
{
if (column.m_Table.m_Grid.Page.Session[string.Format("WG{0}", column.ColumnId)] !=
null)
if (!string.IsNullOrEmpty(where))
where +=
string.Format(" AND {0} = '{1}'",
Interface.BuildTableElement(true,
foreignkeycolumn.
DataSourceId,
column.ColumnId),
column.m_Table.m_Grid.Page.Session[
string.Format("WG{0}", column.ColumnId)]);
else
where =
string.Format(" WHERE {0} = '{1}'",
Interface.BuildTableElement(true,
foreignkeycolumn.
DataSourceId,
column.ColumnId),
column.m_Table.m_Grid.Page.Session[
string.Format("WG{0}", column.ColumnId)]);
}
string sqlwhere =
string.Format(@"SELECT DISTINCT {0} , {1} as fk_text FROM [{2}]{3}", column.ColumnId,
((Foreignkey)column).ValueColumn, column.DataSourceId, where);
if (Debug)
m_DebugString.AppendFormat("<b>{0} - FilterByColumn (Foreignkey)</b> - {1}<br/>",
column.ColumnId, sqlwhere);
Query getData =
Query.ExecuteReader(sqlwhere, ActiveConnectionString, DatabaseConnectionType);
column.FilterByColumnCollection.Clear();
if (!string.IsNullOrEmpty(column.NullText))
column.SetFilterByColumn(column.NullText, "is null");
while (getData.Read())
{
if (getData[column.ColumnId] == null ||
string.IsNullOrEmpty(getData[column.ColumnId].ToString()))
continue;
if (!column.HasDataSourceQuote)
column.SetFilterByColumn(getData["fk_text"].ToString(),
string.Format("= {0}", getData[column.ColumnId]));
else
column.SetFilterByColumn(getData["fk_text"].ToString(),
string.Format("= '{0}'", getData[column.ColumnId]));
}
getData.Close();
}
break;
case ColumnType.DateTime:
//It is triggered by 'SearchFilter' in WebGrid.DateTime.cs
column.SetFilterByColumn("active", "active");
break;
default:
{
string datasource = column.DataSourceId;
if (string.IsNullOrEmpty(datasource))
datasource = DataSourceId;
if (string.IsNullOrEmpty(datasource))
throw new GridException(
string.Format(
"'{0}' is missing database table reference. You should set 'DataSourceID' in the column settings '{0}'.",
column.ColumnId));
string sqlWhere =
string.Format(@"SELECT DISTINCT {0} FROM [{1}]{2}", column.ColumnId, datasource, where);
if (Debug)
m_DebugString.AppendFormat("<b>{0} - FilterByColumn</b> - {1}<br/>", column.ColumnId,
sqlWhere);
Query getData =
Query.ExecuteReader(sqlWhere, ActiveConnectionString, DatabaseConnectionType);
column.FilterByColumnCollection.Clear();
while (getData.Read())
{
if (getData[column.ColumnId] == null || getData[column.ColumnId] == DBNull.Value)
continue;
if (getData[column.ColumnId] is Boolean)
{
if ((bool)getData[column.ColumnId])
column.SetFilterByColumn(getData[column.ColumnId].ToString(), "= 1");
else column.SetFilterByColumn(getData[column.ColumnId].ToString(), "= 0");
}
else if (!column.HasDataSourceQuote)
column.SetFilterByColumn(getData[column.ColumnId].ToString(),
string.Format("= {0}", getData[column.ColumnId]));
else
column.SetFilterByColumn(getData[column.ColumnId].ToString(),
string.Format("= '{0}'", getData[column.ColumnId]));
}
getData.Close();
}
break;
}
}
}
private void SetupGridMessages()
{
if (SystemMessage.CriticalCount > 0 && SystemMessageCritical != null)
{
SystemMessage.Clear();
SystemMessage.Add(SystemMessageCritical, true);
}
if (m_IsRecordUpdate && m_SystemMessageUpdate != null)
SystemMessage.Add(m_SystemMessageUpdate);
else if (m_IsRecordNew && SystemMessageInsert != null)
SystemMessage.Add(SystemMessageInsert);
else if (m_IsRecordDelete && SystemMessageDelete != null)
SystemMessage.Add(SystemMessageDelete);
else if (Equals(m_IsRecordUpdateRows, true) && SystemMessageUpdateRows != null)
SystemMessage.Add(SystemMessageUpdateRows);
if (Page != null && MailForm != null)
if (m_IsRecordNew && !string.IsNullOrEmpty(MailForm.EmailAdresses))
{
string email = MailForm.EmailAdresses;
if (email.IndexOf("[") > -1)
{
foreach (Column column in MasterTable.Columns)
{
string format = String.Format("[{0}]", column.ColumnId);
if (email.IndexOf(format, StringComparison.OrdinalIgnoreCase) > -1)
email = email.Replace(format, column.ColumnId);
}
}
Mail.SendEmail(email.Split(';'), null, this);
}
if (SystemMessage.Count <= 0) return;
if (SystemMessage.CriticalCount > 0 && Debug == false && SystemMessageCritical == null)
SystemMessage.Add(
"Set debug=\"true\" to retrieve debug information, or replace critical system messages by setting 'SystemMessageCritical' property.<br/>(technical support is available at <a href=\"http://forums.webgrid.com\" target=\"webgrid\">forums.webgrid.com</a>)");
}
private void SetupGridRelations()
{
// DETECT one-to-one relation
if (MasterWebGrid == null || MasterWebGrid.DisplayView != DisplayView.Detail ||
MasterTable.Columns.Primarykeys.Count == 0 ||
MasterTable.Columns.Primarykeys.Count !=
MasterWebGrid.MasterTable.Columns.Primarykeys.Count && !m_AllowCancel)
return;
bool isOneToOneGrid = true;
List<Column> masterWebGridPrimaryKeys = MasterWebGrid.MasterTable.Columns.Primarykeys;
foreach (Column column in MasterTable.Columns.Primarykeys)
{
Column column1 = column;
if (column.Identity || !masterWebGridPrimaryKeys.Exists(
mastercolumn => mastercolumn.ColumnId.Equals(column1.ColumnId)))
{
// one-to-one relation is when there are no identity in table.
// and all primarykeys exist in masterwebgrid.
isOneToOneGrid = false;
}
}
if (Debug)
m_DebugString.AppendFormat("<b>{0}: Detected one-to-one relation for grid: {1}</b><br/>", ID,
isOneToOneGrid);
if (!isOneToOneGrid) return;
if (m_AllowCancel == false)
{
m_IsOneToOneRelationGrid = true;
return;
}
DisplayView = DisplayView.Detail;
MasterTable.m_GotPostBackData = true;
MasterTable.m_GotData = false;
InternalId = MasterWebGrid.InternalId;
m_IsOneToOneRelationGrid = true;
if (Trace.IsTracing)
Trace.Trace("One To One Relation enabled.");
MasterTable.Columns.Primarykeys.ForEach(delegate(Column column)
{
MasterTable.Rows[0][column.ColumnId].Value =
MasterWebGrid.MasterTable.Rows[0][column.ColumnId].
Value;
MasterTable.Rows[0][column.ColumnId].DataSourceValue =
MasterWebGrid.MasterTable.Rows[0][column.ColumnId].
Value;
});
}
private void SetupGridScripts()
{
if ((Page == null || !GotHttpContext) && !DesignMode)
return;
if (Scripts == null || !Scripts.DisableJQuery)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery-1.3.2.min.js", "JQuery");
if (Scripts == null || !Scripts.DisableJQueryUI)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery-ui-1.7.2.custom.min.js", "JQueryUI");
if (Scripts == null || !Scripts.DisableBookmark)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery-bookmark.js", "JqueryBookmark");
if (Scripts == null || !Scripts.DisableContextMenu)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery.contextMenu.js", "contextMenu");
if (Scripts == null || !Scripts.DisableSelectMenu)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery-ui.selectmenu.js", "contextMenu");
if (Scripts == null || !Scripts.DisableInputMask)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery.meio.mask.min.js", "meiomask");
if (Scripts == null || !Scripts.DisableClientNotification)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery.jgrowl_minimized.js", "jgrowl");
if (Scripts == null || !Scripts.DisableToolTip)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery.qtip-1.0.0-rc3.min.js", "qtip");
if (Scripts == null || !Scripts.DisablePopupExtender)
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.jquery.colorbox-min.js", "colorbox");
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.WebGrid_ColumnChanged.js", "WebGrid_ColumnChanged");
RegisterClientScriptResourceInHeader(Page, GetType(), "WebGrid.Resources.WebGrid_Misc.js", "WebGrid_Misc");
if (Page != null)
{
if (EnableCallBack && !Page.ClientScript.IsClientScriptBlockRegistered("wgRegisterAnthem"))
{
Page.ClientScript.RegisterClientScriptBlock(typeof (Page), "wgRegisterAnthem", string.Empty);
Page.ClientScript.RegisterHiddenField("WebGrid_EnabledAnthem", "true");
if (EnableCallBackErrorAlert)
{
if (!Debug)
Page.ClientScript.RegisterClientScriptBlock(typeof (Page), "wgAnthemError",
"function Anthem_Error(result) { alert('Ajax Error:\\r'+ result.error);}",
true);
else
Page.ClientScript.RegisterClientScriptBlock(typeof (Page), "wgAnthemDebugError",
"function Anthem_DebugError(text) { alert('Ajax Debug Error:\\r'+ text);}",
true);
}
if (Scripts == null || !Scripts.DisableAjaxLoader)
{
Page.ClientScript.RegisterClientScriptBlock(typeof (Page), "wgAnthemPreCallBack",
@"function Anthem_PreCallBack() {
$(""#"" + wg_gridupdate).find("".wgAjaxLoader:first"").css(""z-index"", ""10000"");
$(""#"" + wg_gridupdate).find("".wgAjaxLoader:first"").css(""visibility"", ""visible"");}",
true);
}
}
if (!Page.ClientScript.IsClientScriptBlockRegistered("wgRegisterStyle"))
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "wgRegisterStyle", string.Empty);
StringBuilder csslink = new StringBuilder();
switch (SkinType)
{
case SkinType.WebGridJQuery:
csslink.Append("<link href=\"");
csslink.Append(Page.ClientScript.GetWebResourceUrl(GetType(),"WebGrid.Resources.WebGrid_JQueryUI.css").Replace("&", "&"));
csslink.Append("\" rel=\"stylesheet\" type=\"text/css\" />");
break;
case SkinType.Disabled:
break;
default:
csslink.AppendFormat("<link href=\"{0}\" rel=\"stylesheet\" type=\"text/css\" />",
Page.ClientScript.GetWebResourceUrl(GetType(),
string.Format("WebGrid.Resources.jquery-ui-{0}.css", SkinType.ToString().ToLower()))).
Replace("&", "&");
csslink.Append("<link href=\"");
csslink.Append(Page.ClientScript.GetWebResourceUrl(GetType(),"WebGrid.Resources.WebGrid_JQueryUI.css").Replace("&", "&"));
csslink.Append("\" rel=\"stylesheet\" type=\"text/css\" />");
break;
}
//Add
if (!Page.IsPostBack)
Page.ClientScript.RegisterClientScriptBlock(typeof (Page), "wgHoverButtons", HoverButtonScript,
true);
if (Page.Header != null && DesignMode == false)
Page.Header.Controls.Add(new LiteralControl(csslink.ToString()));
else
Tag["wgcsslink"] = csslink.ToString();
if (Page.Header != null)
Page.Header.Controls.Add(new LiteralControl(m_Scriptandstyles.ToString()));
else
Tag["wgscriptstyles"] = m_Scriptandstyles.ToString();
}
}
}
#endregion Methods
}
}