-
-
Notifications
You must be signed in to change notification settings - Fork 345
/
DatePicker.cs
472 lines (416 loc) · 15.8 KB
/
DatePicker.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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// <copyright>
// Copyright by the Spark Development Network
//
// Licensed under the Rock Community License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.rockrms.com/license
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
//
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Rock.Web.UI.Controls
{
/// <summary>
/// Control that can be used to select a date
/// </summary>
public class DatePicker : DataTextBox, IPostBackEventHandler, IRockChangeHandlerControl
{
private CheckBox _cbCurrent;
private RockTextBox _nbDayOffset;
/// <summary>
/// Gets or sets a value indicating whether [display current option].
/// </summary>
/// <value>
/// <c>true</c> if [display current option]; otherwise, <c>false</c>.
/// </value>
public bool DisplayCurrentOption
{
get
{
return ViewState["DisplayCurrentOption"] as bool? ?? false;
}
set
{
ViewState["DisplayCurrentOption"] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether the selected date is the CurrentDate +- offset
/// </summary>
/// <value>
/// <c>true</c> if [current date]; otherwise, <c>false</c>.
/// </value>
public bool IsCurrentDateOffset
{
get
{
if ( DisplayCurrentOption )
{
EnsureChildControls();
return _cbCurrent.Checked;
}
return false;
}
set
{
EnsureChildControls();
_cbCurrent.Checked = value;
}
}
/// <summary>
/// Gets or sets the current date offset.
/// </summary>
/// <value>
/// The current date offset.
/// </value>
public int CurrentDateOffsetDays
{
get
{
if ( DisplayCurrentOption )
{
EnsureChildControls();
return _nbDayOffset.Text.AsIntegerOrNull() ?? 0;
}
return 0;
}
set
{
EnsureChildControls();
_nbDayOffset.Text = value.ToString();
}
}
/// <summary>
/// Controls whether or not the DatePicker allows for future dates to be selected (default true). If set to false, all future dates will be disabled.
/// </summary>
/// <value>
/// <c>true</c> (default); otherwise, <c>false</c>.
/// </value>
public bool AllowFutureDateSelection
{
get
{
return ViewState["AllowFutureDateSelection"] as bool? ?? true;
}
set
{
ViewState["AllowFutureDateSelection"] = value;
}
}
/// <summary>
/// Controls whether or not the DatePicker allows for past dates to be selected (default true). If set to false, all past dates will be disabled.
/// </summary>
/// <value>
/// <c>true</c> (default); otherwise, <c>false</c>.
/// </value>
public bool AllowPastDateSelection
{
get
{
return ViewState["AllowPastDateSelection"] as bool? ?? true;
}
set
{
ViewState["AllowPastDateSelection"] = value;
}
}
/// <summary>
/// Controls whether or not the DatePicker enables javascript (default true). If set to false, date picker will be disabled.
/// </summary>
/// <value>
/// <c>true</c> (default); otherwise, <c>false</c>.
/// </value>
public bool EnableJavascript
{
get
{
return ViewState["EnableJavascript"] as bool? ?? true;
}
set
{
ViewState["EnableJavascript"] = value;
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Init" /> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnInit( EventArgs e )
{
base.OnInit( e );
this.AddCssClass( "input-width-md js-date-picker date" );
this.AppendText = "<i class='fa fa-calendar'></i>";
if ( string.IsNullOrWhiteSpace( this.SourceTypeName ) )
{
this.LabelTextFromPropertyName = false;
this.SourceTypeName = "Rock.Web.UI.Controls.DatePicker, Rock";
this.PropertyName = "SelectedDate";
}
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.Load" /> event.
/// </summary>
/// <param name="e">The <see cref="T:System.EventArgs" /> object that contains the event data.</param>
protected override void OnLoad( EventArgs e )
{
base.OnLoad( e );
// make sure the input's value matches the text on PostBack (DatePicker relies on "value" of input and text)
this.Attributes["value"] = this.Text;
}
/// <summary>
/// Outputs server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter" /> object and stores tracing information about the control if tracing is enabled.
/// </summary>
/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter" /> object that receives the control content.</param>
public override void RenderControl( HtmlTextWriter writer )
{
if ( EnableJavascript )
{
RegisterJavascript();
}
base.RenderControl( writer );
}
/// <summary>
/// Registers the javascript.
/// </summary>
private void RegisterJavascript()
{
var postBackScript = ( this.SelectDate != null || this.ValueChanged != null ) ? this.Page.ClientScript.GetPostBackEventReference( new PostBackOptions( this, "SelectDate" ), true ) : "";
postBackScript = postBackScript.Replace( '\'', '"' );
// Get current date format and make sure it has double-lower-case month and day designators for the js date picker to use
var dateFormat = System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern;
var jsDateFormat = dateFormat.Replace( "M", "m" ).Replace( "m", "mm" ).Replace( "mmmm", "mm" );
jsDateFormat = jsDateFormat.Replace( "d", "dd" ).Replace( "dddd", "dd" );
// StartDate and EndDate are optional, and must match the format of the dateFormat (which is the format of the input)
var endDateParam = ( this.AllowFutureDateSelection ) ? "" : "endDate: '" + RockDateTime.Today.ToString( dateFormat ) + "',";
var startDateParam = ( this.AllowPastDateSelection ) ? "" : "startDate: '" + RockDateTime.Today.ToString( dateFormat ) + "',";
var script = $@"Rock.controls.datePicker.initialize(
{{
id: '{this.ClientID}',
startView: {this.StartView.ConvertToInt()},
showOnFocus: {this.ShowOnFocus.ToString().ToLower()},
format: '{jsDateFormat}',
todayHighlight: {this.HighlightToday.ToString().ToLower()},
forceParse: {this.ForceParse.ToString().ToLower()},
postbackScript: '{postBackScript}',
{endDateParam}
{startDateParam}
}});";
ScriptManager.RegisterStartupScript( this, this.GetType(), "date_picker-" + this.ClientID, script, true );
}
/// <summary>
/// Gets or sets a value indicating if picker selections (popup) should be shown as soon as control gets focus
/// </summary>
/// <value>
/// <c>true</c> if [show on focus]; otherwise, <c>false</c>.
/// </value>
public bool ShowOnFocus
{
get
{
return ViewState["ShowOnFocus"] as bool? ?? true;
}
set
{
ViewState["ShowOnFocus"] = value;
}
}
/// <summary>
/// Gets or sets the start view.
/// </summary>
/// <value>
/// The start view.
/// </value>
public StartViewOption StartView
{
get
{
return ViewState["StartView"] as StartViewOption? ?? StartViewOption.month;
}
set
{
ViewState["StartView"] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether [highlight today].
/// </summary>
/// <value>
/// <c>true</c> if [highlight today]; otherwise, <c>false</c>.
/// </value>
public bool HighlightToday
{
get
{
return ViewState["HighlightToday"] as bool? ?? true;
}
set
{
ViewState["HighlightToday"] = value;
}
}
/// <summary>
/// Gets or sets a value indicating whether [force parse].
/// </summary>
/// <value>
/// <c>true</c> if [force parse]; otherwise, <c>false</c>.
/// </value>
public bool ForceParse
{
get
{
return ViewState["ForceParse"] as bool? ?? true;
}
set
{
ViewState["ForceParse"] = value;
}
}
/// <summary>
/// The mode to start in when first displaying selection window
/// </summary>
public enum StartViewOption
{
/// <summary>
/// Month
/// </summary>
month = 0,
/// <summary>
/// Year
/// </summary>
year = 1,
/// <summary>
/// Decade
/// </summary>
decade = 2
}
/// <summary>
/// Gets or sets the selected date.
/// </summary>
/// <value>
/// The selected date.
/// </value>
public DateTime? SelectedDate
{
get
{
if ( !string.IsNullOrWhiteSpace( this.Text ) )
{
DateTime result;
if ( DateTime.TryParse( this.Text, out result ) )
{
return result.Date;
}
else
{
if ( !( this.DisplayCurrentOption && this.IsCurrentDateOffset ) )
{
ShowErrorMessage( Rock.Constants.WarningMessage.DateTimeFormatInvalid( this.PropertyName ) );
}
}
}
return null;
}
set
{
if ( ( value ?? DateTime.MinValue ) != DateTime.MinValue )
{
this.Text = value.Value.ToShortDateString();
}
else
{
this.Text = string.Empty;
}
// set value to equal text when date is set (DatePicker relies on "value" of input and text)
this.Attributes["value"] = this.Text;
}
}
/// <summary>
/// Occurs when [select date].
/// </summary>
public event EventHandler SelectDate;
/// <summary>
/// Occurs when the selected value has changed
/// </summary>
public event EventHandler ValueChanged;
/// <summary>
/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
/// </summary>
protected override void CreateChildControls()
{
base.CreateChildControls();
_cbCurrent = new CheckBox();
_cbCurrent.ID = this.ID + "_cbCurrent";
_cbCurrent.AddCssClass( "js-current-date-checkbox" );
_cbCurrent.Text = "Current Date";
this.Controls.Add( _cbCurrent );
_nbDayOffset = new RockTextBox();
_nbDayOffset.ID = this.ID + "_nbDayOffset";
_nbDayOffset.Help = "Enter the number of days after the current date to use as the date. Use a negative number to specify days before.";
_nbDayOffset.AddCssClass( "input-width-md js-current-date-offset" );
_nbDayOffset.Label = "+- Days";
this.Controls.Add( _nbDayOffset );
}
/// <summary>
/// Renders the base control.
/// </summary>
/// <param name="writer">The writer.</param>
public override void RenderBaseControl( HtmlTextWriter writer )
{
if ( DisplayCurrentOption )
{
writer.AddAttribute( HtmlTextWriterAttribute.Class, "form-control-group js-date-picker-container" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );
writer.AddAttribute( HtmlTextWriterAttribute.Class, "form-row" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );
if ( IsCurrentDateOffset )
{
// set this.Attributes["disabled"] instead of this.Enabled so that our child controls don't get disabled
this.Attributes["disabled"] = "true";
// set textbox val to something instead of empty string so that validation doesn't complain
this.Text = "Current";
_nbDayOffset.Style[HtmlTextWriterStyle.Display] = "";
}
else
{
_nbDayOffset.Style[HtmlTextWriterStyle.Display] = "none";
}
}
base.RenderBaseControl( writer );
if ( DisplayCurrentOption )
{
writer.AddAttribute( HtmlTextWriterAttribute.Class, "input-group" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );
_cbCurrent.RenderControl( writer );
writer.RenderEndTag();
writer.RenderEndTag(); // form-row
writer.AddAttribute( HtmlTextWriterAttribute.Class, "form-row" );
writer.RenderBeginTag( HtmlTextWriterTag.Div );
_nbDayOffset.RenderControl( writer );
writer.RenderEndTag();
writer.RenderEndTag();
}
}
/// <summary>
/// When implemented by a class, enables a server control to process an event raised when a form is posted to the server.
/// </summary>
/// <param name="eventArgument">A <see cref="T:System.String" /> that represents an optional event argument to be passed to the event handler.</param>
public void RaisePostBackEvent( string eventArgument )
{
if ( eventArgument == "SelectDate" )
{
EnsureChildControls();
SelectDate?.Invoke( this, new EventArgs() );
ValueChanged?.Invoke( this, new EventArgs() );
}
}
}
}