/// <summary> /// Creates a checkbox. /// </summary> /// <param name="value"></param> /// <param name="label">The checkbox label. Do not pass null. Pass an empty collection for no label.</param> /// <param name="setup">The setup object for the checkbox.</param> /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param> public Checkbox( bool value, IReadOnlyCollection <PhrasingComponent> label, CheckboxSetup setup = null, Action <PostBackValue <bool>, Validator> validationMethod = null) { setup = setup ?? CheckboxSetup.Create(); var id = new ElementId(); var formValue = new FormValue <bool>( () => value, () => setup.IsReadOnly ? "" : id.Id, v => v.ToString(), rawValue => rawValue == null ? PostBackValueValidationResult <bool> .CreateValid(false) : rawValue == "on" ? PostBackValueValidationResult <bool> .CreateValid(true) : PostBackValueValidationResult <bool> .CreateInvalid()); PageComponent = getComponent( formValue, id, null, setup.DisplaySetup, setup.IsReadOnly, setup.Classes, setup.PageModificationValue, label, setup.Action, setup.ValueChangedAction, () => (setup.ValueChangedAction?.GetJsStatements() ?? "").ConcatenateWithSpace( setup.PageModificationValue.GetJsModificationStatements("this.checked"))); formValue.AddPageModificationValue(setup.PageModificationValue, v => v); if (validationMethod != null) { Validation = formValue.CreateValidation(validationMethod); } }
/// <summary> /// Creates a block-level radio button that is part of the group. /// </summary> /// <param name="isSelected"></param> /// <param name="validationMethod">The validation method. Do not pass null.</param> /// <param name="label"></param> /// <param name="action"></param> /// <param name="autoPostBack"></param> /// <param name="pageModificationValue"></param> /// <param name="nestedControlListGetter"></param> /// <returns></returns> public BlockCheckBox CreateBlockRadioButton( bool isSelected, Action <PostBackValue <bool>, Validator> validationMethod, string label = "", FormAction action = null, bool autoPostBack = false, PageModificationValue <bool> pageModificationValue = null, Func <IEnumerable <Control> > nestedControlListGetter = null) { BlockCheckBox checkBox = null; var validation = formValue.CreateValidation( (postBackValue, validator) => validationMethod(new PostBackValue <bool>(postBackValue.Value == checkBox, postBackValue.ChangedOnPostBack), validator)); checkBox = new BlockCheckBox( formValue, label, new BlockCheckBoxSetup(action: action, triggersActionWhenCheckedOrUnchecked: autoPostBack, nestedControlListGetter: nestedControlListGetter), () => checkBoxesAndSelectionStatesAndPageModificationValues.Where(i => i.Item3 != null) .Select(i => i.Item3.GetJsModificationStatements(i.Item1 == checkBox ? "true" : "false")), validation); 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 file-upload control. /// </summary> /// <param name="displaySetup"></param> /// <param name="validationPredicate"></param> /// <param name="validationErrorNotifier"></param> /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param> public FileUpload( DisplaySetup displaySetup = null, Func <bool, bool> validationPredicate = null, Action validationErrorNotifier = null, Action <RsFile, Validator> validationMethod = null) { Labeler = new FormControlLabeler(); var id = new ElementId(); var formValue = new FormValue <HttpPostedFile>( () => null, () => id.Id, v => "", rawValue => rawValue == null ? PostBackValueValidationResult <HttpPostedFile> .CreateInvalid() : PostBackValueValidationResult <HttpPostedFile> .CreateValid(rawValue.ContentLength > 0 ? rawValue : null)); PageComponent = new CustomPhrasingComponent( new DisplayableElement( context => { id.AddId(context.Id); Labeler.AddControlId(context.Id); EwfPage.Instance.Form.Enctype = "multipart/form-data"; return(new DisplayableElementData( displaySetup, () => { var attributes = new List <Tuple <string, string> >(); attributes.Add(Tuple.Create("type", "file")); attributes.Add(Tuple.Create("name", context.Id)); return new DisplayableElementLocalData( "input", new FocusabilityCondition(true), isFocused => { if (isFocused) { attributes.Add(Tuple.Create("autofocus", "autofocus")); } return new DisplayableElementFocusDependentData(attributes: attributes); }); })); }, formValue: formValue).ToCollection()); if (validationMethod != null) { Validation = formValue.CreateValidation( (postBackValue, validator) => { if (validationPredicate != null && !validationPredicate(postBackValue.ChangedOnPostBack)) { return; } validationMethod(getRsFile(postBackValue.Value), validator); }); } }
/// <summary> /// Creates a check box. /// </summary> /// <param name="isChecked"></param> /// <param name="validationMethod">The validation method. Do not pass null.</param> /// <param name="label">Do not pass null.</param> /// <param name="setup">The setup object for the check box.</param> public BlockCheckBox(bool isChecked, Action <PostBackValue <bool>, Validator> validationMethod, string label = "", BlockCheckBoxSetup setup = null) { this.setup = setup ?? new BlockCheckBoxSetup(); checkBoxFormValue = EwfCheckBox.GetFormValue(isChecked, this); this.label = label; action = this.setup.Action ?? FormState.Current.DefaultAction; validation = checkBoxFormValue.CreateValidation(validationMethod); nestedControls = this.setup.NestedControlListGetter != null?this.setup.NestedControlListGetter().ToImmutableArray() : ImmutableArray <Control> .Empty; }
/// <summary> /// Creates a hidden field. /// </summary> /// <param name="value">Do not pass null.</param> /// <param name="id"></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="jsInitStatementGetter">A function that takes the field’s ID and returns the JavaScript statements that should be executed when the DOM is /// loaded. Do not return null.</param> public EwfHiddenField( string value, HiddenFieldId id = null, PageModificationValue <string> pageModificationValue = null, Action <PostBackValue <string>, Validator> validationMethod = null, Func <string, string> jsInitStatementGetter = null) { pageModificationValue = pageModificationValue ?? new PageModificationValue <string>(); var elementId = new ElementId(); var formValue = new FormValue <string>( () => value, () => elementId.Id, v => v, rawValue => rawValue != null ? PostBackValueValidationResult <string> .CreateValid(rawValue) : PostBackValueValidationResult <string> .CreateInvalid()); component = new ElementComponent( context => { elementId.AddId(context.Id); id?.AddId(context.Id); return(new ElementData( () => { var attributes = new List <Tuple <string, string> >(); attributes.Add(Tuple.Create("type", "hidden")); attributes.Add(Tuple.Create("name", context.Id)); attributes.Add(Tuple.Create("value", pageModificationValue.Value)); return new ElementLocalData( "input", focusDependentData: new ElementFocusDependentData( attributes: attributes, includeIdAttribute: id != null || pageModificationValue != null || jsInitStatementGetter != null, jsInitStatements: StringTools.ConcatenateWithDelimiter( " ", pageModificationValue != null ? "$( '#{0}' ).change( function() {{ {1} }} );".FormatWith( context.Id, pageModificationValue.GetJsModificationStatements("$( this ).val()")) : "", jsInitStatementGetter?.Invoke(context.Id) ?? ""))); })); }, formValue: formValue); formValue.AddPageModificationValue(pageModificationValue, v => v); if (validationMethod != null) { validation = formValue.CreateValidation(validationMethod); } }
/// <summary> /// Creates a simple HTML editor. /// </summary> /// <param name="value">Do not pass null.</param> /// <param name="allowEmpty"></param> /// <param name="validationMethod">The validation method. Do not pass null.</param> /// <param name="setup">The setup object for the HTML editor.</param> /// <param name="maxLength"></param> public WysiwygHtmlEditor( string value, bool allowEmpty, Action <string, Validator> validationMethod, WysiwygHtmlEditorSetup setup = null, int?maxLength = null) { setup = setup ?? new WysiwygHtmlEditorSetup(); var id = new ElementId(); FormValue <string> formValue = null; formValue = new FormValue <string>( () => value, () => setup.IsReadOnly ? "" : id.Id, v => v, rawValue => { if (rawValue == null) { return(PostBackValueValidationResult <string> .CreateInvalid()); } // This hack prevents the NewLine that CKEditor seems to always add to the end of the textarea from causing // ValueChangedOnPostBack to always return true. if (rawValue.EndsWith(Environment.NewLine) && rawValue.Remove(rawValue.Length - Environment.NewLine.Length) == formValue.GetDurableValue()) { rawValue = formValue.GetDurableValue(); } return(PostBackValueValidationResult <string> .CreateValid(rawValue)); }); var modificationValue = new PageModificationValue <string>(); component = new ElementComponent( context => { id.AddId(context.Id); var displaySetup = setup.DisplaySetup ?? new DisplaySetup(true); var jsShowStatements = getJsShowStatements(context.Id, setup.CkEditorConfiguration); displaySetup.AddJsShowStatements(jsShowStatements); displaySetup.AddJsHideStatements("CKEDITOR.instances.{0}.destroy(); $( '#{0}' ).css( 'display', 'none' );".FormatWith(context.Id)); return(new ElementData( () => { var attributes = new List <Tuple <string, string> >(); if (setup.IsReadOnly) { attributes.Add(Tuple.Create("disabled", "disabled")); } else { attributes.Add(Tuple.Create("name", context.Id)); } if (!displaySetup.ComponentsDisplayed) { attributes.Add(Tuple.Create("style", "display: none")); } return new ElementLocalData( "textarea", attributes: attributes, includeIdAttribute: true, jsInitStatements: displaySetup.ComponentsDisplayed ? jsShowStatements : ""); }, children: new TextNode(() => EwfTextBox.GetTextareaValue(modificationValue.Value)).ToCollection())); }, formValue: formValue); validation = formValue.CreateValidation( (postBackValue, validator) => { if (setup.ValidationPredicate != null && !setup.ValidationPredicate(postBackValue.ChangedOnPostBack)) { return; } var errorHandler = new ValidationErrorHandler("HTML"); var validatedValue = maxLength.HasValue ? validator.GetString(errorHandler, postBackValue.Value, allowEmpty, maxLength.Value) : validator.GetString(errorHandler, postBackValue.Value, allowEmpty); if (errorHandler.LastResult != ErrorCondition.NoError) { setup.ValidationErrorNotifier(); return; } validationMethod(validatedValue, validator); }); formValue.AddPageModificationValue(modificationValue, v => v); }