/// <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);
            }
        }
 private EwfHiddenField(string value)
 {
     formValue = new FormValue <string>(
         () => value,
         () => UniqueID,
         v => v,
         rawValue =>
         rawValue != null ? PostBackValueValidationResult <string> .CreateValidWithValue(rawValue) : PostBackValueValidationResult <string> .CreateInvalid());
 }
Example #3
0
        /// <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);
                });
            }
        }
Example #4
0
 internal static FormValue <bool> GetFormValue(bool isChecked, Control checkBox)
 {
     return(new FormValue <bool>(() => isChecked,
                                 () => checkBox.IsOnPage() ? checkBox.UniqueID : "",
                                 v => v.ToString(),
                                 rawValue =>
                                 rawValue == null
                                 ? PostBackValueValidationResult <bool> .CreateValidWithValue(false)
                                 : rawValue == "on" ? PostBackValueValidationResult <bool> .CreateValidWithValue(true) : PostBackValueValidationResult <bool> .CreateInvalid()));
 }
 public EwfFileUpload()
 {
     formValue = new FormValue <HttpPostedFile>(
         () => null,
         () => this.IsOnPage() ? UniqueID : "",
         v => "",
         rawValue =>
         rawValue != null
                                 ? PostBackValueValidationResult <HttpPostedFile> .CreateValid(rawValue.ContentLength > 0 ? rawValue : null)
                                 : PostBackValueValidationResult <HttpPostedFile> .CreateInvalid());
 }
        /// <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);
            }
        }
Example #7
0
 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();
     }));
 }
        /// <summary>
        /// Creates a simple HTML editor.
        /// </summary>
        /// <param name="value">Do not pass null.</param>
        /// <param name="ckEditorConfiguration">A comma-separated list of CKEditor configuration options ("toolbar: [ [ 'Bold', 'Italic' ] ]", etc.). Use this to
        /// customize the underlying CKEditor. Do not pass null.</param>
        public WysiwygHtmlEditor(string value, string ckEditorConfiguration = "")
        {
            this.ckEditorConfiguration = ckEditorConfiguration;

            formValue = new FormValue <string>(
                () => value,
                () => this.IsOnPage() ? UniqueID : "",
                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> .CreateValidWithValue(rawValue));
            });
        }
        /// <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);
        }