Exemplo n.º 1
0
        /// <summary>
        /// Creates a text box.
        /// </summary>
        /// <param name="value">Do not pass null.</param>
        /// <param name="rows">The number of rows in the text box.</param>
        /// <param name="masksCharacters">Pass true to mask characters entered in the text box. Has no effect when there is more than one row in the text box.
        /// </param>
        /// <param name="maxLength">The maximum number of characters that can be entered in this text box.</param>
        /// <param name="readOnly">Pass true to prevent the contents of the text box from being changed.</param>
        /// <param name="disableBrowserAutoComplete">If true, prevents the browser from displaying values the user previously entered. Keep in mind that there is
        /// currently an "arms race" taking place over forms auto-complete. Banks and other "high-security" organizations keep looking for ways to disable
        /// auto-complete on their login forms while browsers and password managers are always trying to preserve this functionality for their users. Because of
        /// this war, it's possible that your request to disable auto-complete will be ignored. See http://stackoverflow.com/a/23234498/35349 for more information.
        /// </param>
        /// <param name="suggestSpellCheck">By default, Firefox does not spell check single-line text boxes. By default, Firefox does spell check multi-line text
        /// boxes. Setting this parameter to a value will set the spellcheck attribute on the text box to enable/disable spell checking, if the user agent supports
        /// it.</param>
        /// <param name="action">The action that will be performed when the user hits Enter on the text box or selects an auto-complete item.</param>
        /// <param name="autoPostBack">Pass true to cause an action when the text box loses focus.</param>
        public EwfTextBox(
            string value, int rows = 1, bool masksCharacters = false, int?maxLength    = null, bool readOnly = false, bool disableBrowserAutoComplete = false,
            bool?suggestSpellCheck = null, FormAction action = null, bool autoPostBack = false)
        {
            this.rows                       = rows;
            this.masksCharacters            = masksCharacters;
            this.maxLength                  = maxLength;
            this.readOnly                   = readOnly;
            this.disableBrowserAutoComplete = disableBrowserAutoComplete;
            this.suggestSpellCheck          = suggestSpellCheck;

            if (value == null)
            {
                throw new ApplicationException("You cannot create a text box with a null value. Please use the empty string instead.");
            }
            formValue = new FormValue <string>(
                () => value,
                () => this.IsOnPage() ? UniqueID : "",
                v => v,
                rawValue =>
                rawValue != null && (!readOnly || rawValue == formValue.GetDurableValue())
                                        ? PostBackValueValidationResult <string> .CreateValid(rawValue)
                                        : PostBackValueValidationResult <string> .CreateInvalid());

            this.action       = action ?? FormState.Current.DefaultAction;
            this.autoPostBack = autoPostBack;
        }
Exemplo n.º 2
0
        /// <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);
        }