Ewl » Canonical The canonical version of EWL Read More
Clone URL:  
RadioButtonGroup.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
using System; using System.Collections.Generic; using System.Linq; using System.Web.UI; using EnterpriseWebLibrary.InputValidation; namespace EnterpriseWebLibrary.EnterpriseWebFramework { /// <summary> /// A radio button group. Use this when you want access to the individual selection state of each radio button and do not need the concept of a selected item /// ID for the group. Otherwise use FreeFormRadioList. /// </summary> public class RadioButtonGroup { internal static FormValue<ElementId> GetFormValue( Func<IEnumerable<( ElementId id, bool isReadOnly, bool isSelected )>> buttonGetter, Func<ElementId, string> stringValueSelector, Func<string, IEnumerable<ElementId>> selectedButtonIdInPostBackGetter, bool allowsNoSelection ) { FormValue<ElementId> formValue = null; return formValue = new FormValue<ElementId>( () => buttonGetter().Where( i => i.isSelected ).Select( i => i.id ).FirstOrDefault(), () => buttonGetter().Where( i => i.id.Id.Any() && !i.isReadOnly ).Select( i => i.id ).FirstOrDefault()?.Id ?? "", stringValueSelector, rawValue => { if( rawValue != null ) { var selectedButtonId = selectedButtonIdInPostBackGetter( rawValue ).SingleOrDefault(); return selectedButtonId != null ? PostBackValueValidationResult<ElementId>.CreateValid( selectedButtonId ) : PostBackValueValidationResult<ElementId>.CreateInvalid(); } var durableValue = formValue.GetDurableValue(); if( durableValue != null ) { var button = buttonGetter().Single( i => i.id == durableValue ); if( !button.id.Id.Any() || button.isReadOnly ) return PostBackValueValidationResult<ElementId>.CreateValid( durableValue ); } return allowsNoSelection ? PostBackValueValidationResult<ElementId>.CreateValid( null ) : PostBackValueValidationResult<ElementId>.CreateInvalid(); } ); } internal static void ValidateControls( bool allowsNoSelection, bool inNoSelectionState, IEnumerable<( ElementId id, bool isReadOnly, bool isSelected )> buttons, bool disableSingleButtonDetection ) { if( ( !allowsNoSelection || !inNoSelectionState ) && buttons.Count( i => i.isSelected ) != 1 ) throw new ApplicationException( "If a radio button group is not in the no-selection state, then exactly one radio button must be selected." ); var activeButtons = buttons.Where( i => i.id.Id.Any() && !i.isReadOnly ).Materialize(); if( activeButtons.Any() && !disableSingleButtonDetection && activeButtons.Count < 2 ) { const string link = "http://developers.whatwg.org/states-of-the-type-attribute.html#radio-button-state-%28type=radio%29"; throw new ApplicationException( "A radio button group must contain more than one element; see " + link + "." ); } } private readonly FormValue<ElementId> formValue; private readonly List<( ElementId id, bool isReadOnly, bool value, PageModificationValue<bool> pmv )> buttonIdAndIsReadOnlyAndValueAndPmvQuadruples = new List<( ElementId, bool, bool, PageModificationValue<bool> )>(); private readonly FormAction selectionChangedAction; /// <summary> /// Creates a radio button group. /// </summary> /// <param name="allowNoSelection">Pass true to allow the state in which none of the radio buttons are selected. Note that this is not recommended by the /// Nielsen Norman Group; see http://www.nngroup.com/articles/checkboxes-vs-radio-buttons/ for more information.</param> /// <param name="disableSingleButtonDetection">Pass true to allow just a single radio button to be displayed for this group. Use with caution, as this /// violates the HTML specification.</param> /// <param name="selectionChangedAction">The action that will occur when the selection is changed. Pass null for no action.</param> public RadioButtonGroup( bool allowNoSelection, bool disableSingleButtonDetection = false, FormAction selectionChangedAction = null ) { formValue = GetFormValue( () => from i in buttonIdAndIsReadOnlyAndValueAndPmvQuadruples select ( i.id, i.isReadOnly, i.value ), v => v?.Id ?? "", rawValue => from quadruple in buttonIdAndIsReadOnlyAndValueAndPmvQuadruples let id = quadruple.id where id.Id.Any() && !quadruple.isReadOnly && id.Id == rawValue select id, allowNoSelection ); this.selectionChangedAction = selectionChangedAction; EwfPage.Instance.AddControlTreeValidation( () => ValidateControls( allowNoSelection, buttonIdAndIsReadOnlyAndValueAndPmvQuadruples.All( i => !i.value ), from i in buttonIdAndIsReadOnlyAndValueAndPmvQuadruples select ( i.id, i.isReadOnly, i.value ), disableSingleButtonDetection ) ); } /// <summary> /// Creates a radio button that is part of the group. /// </summary> /// <param name="value"></param> /// <param name="label">The radio button label. Do not pass null. Pass an empty collection for no label.</param> /// <param name="setup">The setup object for the radio button.</param> /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param> public Checkbox CreateRadioButton( bool value, IReadOnlyCollection<PhrasingComponent> label, RadioButtonSetup setup = null, Action<PostBackValue<bool>, Validator> validationMethod = null ) { setup = setup ?? RadioButtonSetup.Create(); var id = new ElementId(); formValue.AddPageModificationValue( setup.PageModificationValue, v => v == id ); buttonIdAndIsReadOnlyAndValueAndPmvQuadruples.Add( ( id, setup.IsReadOnly, value, setup.PageModificationValue ) ); return new Checkbox( formValue, id, setup, label, selectionChangedAction, () => StringTools.ConcatenateWithDelimiter( " ", buttonIdAndIsReadOnlyAndValueAndPmvQuadruples.Select( i => i.pmv.GetJsModificationStatements( i.id == id ? "true" : "false" ) ).ToArray() ), validationMethod != null ? formValue.CreateValidation( ( postBackValue, validator ) => validationMethod( new PostBackValue<bool>( postBackValue.Value == id, postBackValue.ChangedOnPostBack ), validator ) ) : null ); } /// <summary> /// Creates a flow radio button that is part of the group. /// </summary> /// <param name="value"></param> /// <param name="label">The radio button label. Do not pass null. Pass an empty collection for no label.</param> /// <param name="setup">The setup object for the flow radio button.</param> /// <param name="validationMethod">The validation method. Pass null if you’re only using the radio button for page modification.</param> public FlowCheckbox CreateFlowRadioButton( bool value, IReadOnlyCollection<PhrasingComponent> label, FlowRadioButtonSetup setup = null, Action<PostBackValue<bool>, Validator> validationMethod = null ) { setup = setup ?? FlowRadioButtonSetup.Create(); return new FlowCheckbox( setup, CreateRadioButton( value, label, setup: setup.RadioButtonSetup, validationMethod: validationMethod ) ); } } [ Obsolete( "Guaranteed through 28 Feb 2019. Use RadioButtonGroup instead." ) ] public class LegacyRadioButtonGroup { internal static FormValue<CommonCheckBox> GetFormValue( bool allowsNoSelection, Func<IEnumerable<CommonCheckBox>> allCheckBoxesGetter, Func<IEnumerable<CommonCheckBox>> checkedCheckBoxesGetter, Func<CommonCheckBox, string> stringValueSelector, Func<string, IEnumerable<CommonCheckBox>> checkedCheckBoxesInPostBackGetter ) { return new FormValue<CommonCheckBox>( () => checkedCheckBoxesGetter().FirstOrDefault(), () => { var firstCheckBoxOnPage = allCheckBoxesGetter().Select( i => (Control)i ).FirstOrDefault( i => i.IsOnPage() ); return firstCheckBoxOnPage != null ? firstCheckBoxOnPage.UniqueID : ""; }, stringValueSelector, rawValue => { if( rawValue != null ) { var selectedButton = checkedCheckBoxesInPostBackGetter( rawValue ).SingleOrDefault(); return selectedButton != null ? PostBackValueValidationResult<CommonCheckBox>.CreateValid( selectedButton ) : PostBackValueValidationResult<CommonCheckBox>.CreateInvalid(); } return allowsNoSelection ? PostBackValueValidationResult<CommonCheckBox>.CreateValid( null ) : PostBackValueValidationResult<CommonCheckBox>.CreateInvalid(); } ); } internal static void ValidateControls( bool allowsNoSelection, bool inNoSelectionState, IEnumerable<CommonCheckBox> checkBoxes, bool disableSingleButtonDetection ) { Control selectedButton = null; if( !allowsNoSelection || !inNoSelectionState ) { var selectedButtons = checkBoxes.Where( i => i.IsChecked ).ToArray(); if( selectedButtons.Count() != 1 ) throw new ApplicationException( "If a radio button group is not in the no-selection state, then exactly one radio button must be selected." ); selectedButton = selectedButtons.Single() as Control; } var checkBoxesOnPage = checkBoxes.Where( i => ( i as Control ).IsOnPage() ).ToArray(); if( checkBoxesOnPage.Any() ) { if( selectedButton != null && !selectedButton.IsOnPage() ) throw new ApplicationException( "The selected radio button must be on the page." ); if( !disableSingleButtonDetection && checkBoxesOnPage.Count() < 2 ) { const string link = "http://developers.whatwg.org/states-of-the-type-attribute.html#radio-button-state-%28type=radio%29"; throw new ApplicationException( "A radio button group must contain more than one element; see " + link + "." ); } } } private readonly FormValue<CommonCheckBox> formValue; private readonly List<Tuple<CommonCheckBox, bool, PageModificationValue<bool>>> checkBoxesAndSelectionStatesAndPageModificationValues = new List<Tuple<CommonCheckBox, bool, PageModificationValue<bool>>>(); /// <summary> /// Creates a radio button group. /// </summary> /// <param name="allowNoSelection">Pass true to allow the state in which none of the radio buttons are selected. Note that this is not recommended by the /// Nielsen Norman Group; see http://www.nngroup.com/articles/checkboxes-vs-radio-buttons/ for more information.</param> /// <param name="disableSingleButtonDetection">Pass true to allow just a single radio button to be displayed for this group. Use with caution, as this /// violates the HTML specification.</param> public LegacyRadioButtonGroup( bool allowNoSelection, bool disableSingleButtonDetection = false ) { formValue = GetFormValue( allowNoSelection, () => from i in checkBoxesAndSelectionStatesAndPageModificationValues select i.Item1, () => from i in checkBoxesAndSelectionStatesAndPageModificationValues where i.Item2 select i.Item1, v => v != null ? ( (Control)v ).UniqueID : "", rawValue => from checkBoxAndSelectionState in checkBoxesAndSelectionStatesAndPageModificationValues let control = (Control)checkBoxAndSelectionState.Item1 where control.IsOnPage() && control.UniqueID == rawValue select checkBoxAndSelectionState.Item1 ); EwfPage.Instance.AddControlTreeValidation( () => ValidateControls( allowNoSelection, checkBoxesAndSelectionStatesAndPageModificationValues.All( i => !i.Item2 ), checkBoxesAndSelectionStatesAndPageModificationValues.Select( i => i.Item1 ), disableSingleButtonDetection ) ); } /// <summary> /// Creates an in-line radio button that is part of the group. /// </summary> public EwfCheckBox CreateInlineRadioButton( bool isSelected, string label = "", FormAction action = null, bool autoPostBack = false, PageModificationValue<bool> pageModificationValue = null ) { EwfCheckBox checkBox = null; checkBox = new EwfCheckBox( formValue, label, action, () => checkBoxesAndSelectionStatesAndPageModificationValues.Where( i => i.Item3 != null ) .Select( i => i.Item3.GetJsModificationStatements( i.Item1 == checkBox ? "true" : "false" ) ) ) { AutoPostBack = autoPostBack }; checkBoxesAndSelectionStatesAndPageModificationValues.Add( Tuple.Create<CommonCheckBox, bool, PageModificationValue<bool>>( checkBox, isSelected, pageModificationValue ) ); if( pageModificationValue != null ) formValue.AddPageModificationValue( pageModificationValue, value => value == checkBox ); return checkBox; } /// <summary> /// Creates a block-level radio button that is part of the group. /// </summary> /// <param name="isSelected"></param> /// <param name="label"></param> /// <param name="action"></param> /// <param name="autoPostBack"></param> /// <param name="pageModificationValue"></param> /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param> /// <param name="nestedControlListGetter"></param> /// <returns></returns> public BlockCheckBox CreateBlockRadioButton( bool isSelected, string label = "", FormAction action = null, bool autoPostBack = false, PageModificationValue<bool> pageModificationValue = null, Action<PostBackValue<bool>, Validator> validationMethod = null, Func<IEnumerable<Control>> nestedControlListGetter = null ) { BlockCheckBox checkBox = null; checkBox = new BlockCheckBox( formValue, new BlockCheckBoxSetup( action: action, triggersActionWhenCheckedOrUnchecked: autoPostBack, nestedControlListGetter: nestedControlListGetter ), label.ToComponents(), () => checkBoxesAndSelectionStatesAndPageModificationValues.Where( i => i.Item3 != null ) .Select( i => i.Item3.GetJsModificationStatements( i.Item1 == checkBox ? "true" : "false" ) ), validationMethod ); checkBoxesAndSelectionStatesAndPageModificationValues.Add( Tuple.Create<CommonCheckBox, bool, PageModificationValue<bool>>( checkBox, isSelected, pageModificationValue ) ); if( pageModificationValue != null ) formValue.AddPageModificationValue( pageModificationValue, value => value == checkBox ); return checkBox; } } }