Example #1
0
        /// <summary>
        /// BasicPage.master use only.
        /// </summary>
        public Section(
            DisplaySetup displaySetup, SectionStyle style, ElementClassSet classes, string heading, IReadOnlyCollection <FlowComponent> postHeadingComponents,
            IReadOnlyCollection <FlowComponent> content, bool?expanded, bool disableStatePersistence, IReadOnlyCollection <EtherealComponent> etherealContent)
        {
            children = new DisplayableElement(
                context => {
                var hiddenFieldId = new HiddenFieldId();
                var expandedPmv   = heading.Any() && expanded.HasValue && !disableStatePersistence ? new PageModificationValue <string>() : null;

                FlowComponent getHeadingButton()
                {
                    var headingComponents =
                        new DisplayableElement(
                            headingContext => new DisplayableElementData(
                                null,
                                () => new DisplayableElementLocalData("h1"),
                                classes: headingClass,
                                children: heading.ToComponents())).Concat(postHeadingComponents ?? Enumerable.Empty <FlowComponent>());

                    return(expanded.HasValue
                                                               ?
                           // We cannot use EwfButton because we have flow content.
                           ElementActivationBehavior.GetActivatableElement(
                               "div",
                               ElementClassSet.Empty,
                               Enumerable.Empty <Tuple <string, string> >().Materialize(),
                               ElementActivationBehavior.CreateButton(
                                   buttonBehavior: new CustomButtonBehavior(
                                       () => disableStatePersistence
                                                                                                     ? "$( '#{0}' ).toggleClass( '{1}', 200 );".FormatWith(
                                           context.Id,
                                           StringTools.ConcatenateWithDelimiter(
                                               " ",
                                               style == SectionStyle.Normal
                                                                                                                             ? new[] { normalClosedClass.ClassName, normalExpandedClass.ClassName }
                                                                                                                             : new[] { boxClosedClass.ClassName, boxExpandedClass.ClassName }))
                                                                                                     : hiddenFieldId.GetJsValueModificationStatements(
                                           "document.getElementById( '{0}' ).value === '{2}' ? '{1}' : '{2}'".FormatWith(
                                               hiddenFieldId.ElementId.Id,
                                               bool.FalseString,
                                               bool.TrueString)))),
                               new GenericFlowContainer(
                                   new GenericPhrasingContainer("Click to Expand".ToComponents(), classes: closeClass).ToCollection()
                                   .Append(new GenericPhrasingContainer("Click to Close".ToComponents(), classes: expandClass))
                                   .Concat(headingComponents)
                                   .Materialize(),
                                   classes: headingClass).ToCollection(),
                               Enumerable.Empty <EtherealComponent>().Materialize())
                                                               : new GenericFlowContainer(new GenericFlowContainer(headingComponents.Materialize(), classes: headingClass).ToCollection()));
                }

                content = content ?? Enumerable.Empty <FlowComponent>().Materialize();
                return(new DisplayableElementData(
                           displaySetup,
                           () => new DisplayableElementLocalData(
                               "section",
                               focusDependentData:
                               new DisplayableElementFocusDependentData(includeIdAttribute: heading.Any() && expanded.HasValue && disableStatePersistence)),
                           classes: allStylesBothStatesClass
                           .Add(
                               style == SectionStyle.Normal
                                                                        ? getSectionClasses(expanded, expandedPmv, normalClosedClass, normalExpandedClass)
                                                                        : getSectionClasses(expanded, expandedPmv, boxClosedClass, boxExpandedClass))
                           .Add(classes ?? ElementClassSet.Empty),
                           children: (heading.Any() ? getHeadingButton().ToCollection() : Enumerable.Empty <FlowComponent>()).Concat(
                               content.Any() ? new GenericFlowContainer(content, classes: contentClass).ToCollection() : Enumerable.Empty <FlowComponent>())
                           .Materialize(),
                           etherealChildren: (expandedPmv != null
                                                                            ? new EwfHiddenField(expanded.Value.ToString(), id: hiddenFieldId, pageModificationValue: expandedPmv).PageComponent
                                              .ToCollection()
                                                                            : Enumerable.Empty <EtherealComponent>()).Concat(etherealContent ?? Enumerable.Empty <EtherealComponent>())
                           .Materialize()));
            }).ToCollection();
        }
        /// <summary>
        /// Creates a time control.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="allowEmpty"></param>
        /// <param name="setup">The setup object for the time control.</param>
        /// <param name="minValue">The earliest allowed time.</param>
        /// <param name="maxValue">The latest allowed time. This can be earlier than <paramref name="minValue"/> to create a range spanning midnight.</param>
        /// <param name="minuteInterval">Allows the user to select values only in the given increments. Be aware that other values can still be sent from the
        /// browser via a crafted request.</param>
        /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param>
        public TimeControl(
            LocalTime?value, bool allowEmpty, TimeControlSetup setup = null, LocalTime?minValue = null, LocalTime?maxValue = null, int minuteInterval = 15,
            Action <LocalTime?, Validator> validationMethod          = null)
        {
            setup    = setup ?? TimeControlSetup.Create();
            minValue = minValue ?? LocalTime.Midnight;

            if (minuteInterval < 30)
            {
                var textControl = new TextControl(
                    value.HasValue ? new TimeSpan(value.Value.TickOfDay).ToTimeOfDayHourAndMinuteString() : "",
                    allowEmpty,
                    setup: setup.IsReadOnly
                                                       ? TextControlSetup.CreateReadOnly(validationPredicate: setup.ValidationPredicate, validationErrorNotifier: setup.ValidationErrorNotifier)
                                                       : TextControlSetup.Create(
                        autoFillTokens: setup.AutoFillTokens,
                        action: new SpecifiedValue <FormAction>(setup.Action),
                        valueChangedAction: setup.ValueChangedAction,
                        pageModificationValue: setup.PageModificationValue,
                        validationPredicate: setup.ValidationPredicate,
                        validationErrorNotifier: setup.ValidationErrorNotifier),
                    validationMethod: validationMethod == null
                                                                  ? (Action <string, Validator>)null
                                                                  : (postBackValue, validator) => {
                    var errorHandler   = new ValidationErrorHandler("time");
                    var validatedValue = validator.GetNullableTimeOfDayTimeSpan(
                        errorHandler,
                        postBackValue.ToUpper(),
                        TewlContrib.DateTimeTools.HourAndMinuteFormat.ToCollection().ToArray(),
                        allowEmpty)
                                         .ToNewUnderlyingValue(v => LocalTime.FromTicksSinceMidnight(v.Ticks));
                    if (errorHandler.LastResult != ErrorCondition.NoError)
                    {
                        setup.ValidationErrorNotifier?.Invoke();
                        return;
                    }

                    var wrap = maxValue < minValue.Value;
                    if (!wrap
                                                                                      ? validatedValue < minValue.Value || validatedValue > maxValue
                                                                                      : validatedValue < minValue.Value && validatedValue > maxValue)
                    {
                        validator.NoteErrorAndAddMessage("The time is too early or too late.");
                        setup.ValidationErrorNotifier?.Invoke();
                        return;
                    }

                    validationMethod(validatedValue, validator);
                });

                Labeler = textControl.Labeler;

                PageComponent = new DisplayableElement(
                    context => new DisplayableElementData(
                        setup.DisplaySetup,
                        () => new DisplayableElementLocalData(
                            "div",
                            focusDependentData: new DisplayableElementFocusDependentData(
                                includeIdAttribute: true,
                                jsInitStatements: "{0}.timepicker( {{ {1} }} );".FormatWith(
                                    getTextControlExpression(context.Id),
                                    StringTools.ConcatenateWithDelimiter(
                                        ", ",
                                        "timeFormat: 'h:mmt'",
                                        "stepMinute: {0}".FormatWith(minuteInterval),
                                        "showButtonPanel: false")))),
                        classes: elementClass.Add(setup.Classes ?? ElementClassSet.Empty),
                        children: textControl.PageComponent.ToCollection()
                        .Concat(
                            setup.IsReadOnly
                                                                        ? Enumerable.Empty <PhrasingComponent>()
                                                                        : new EwfButton(
                                new CustomButtonStyle(children: new FontAwesomeIcon("fa-clock-o").ToCollection()),
                                behavior: new CustomButtonBehavior(() => "{0}.timepicker( 'show' );".FormatWith(getTextControlExpression(context.Id))),
                                classes: new ElementClass("icon")).ToCollection())
                        .Materialize()));

                Validation = textControl.Validation;
            }
            else
            {
                var items = from time in getTimes(minValue.Value, maxValue, minuteInterval)
                            let timeSpan = new TimeSpan(time.TickOfDay)
                                           select SelectListItem.Create <LocalTime?>(time, timeSpan.ToTimeOfDayHourAndMinuteString());

                var selectList = SelectList.CreateDropDown(
                    setup.IsReadOnly
                                                ? DropDownSetup.CreateReadOnly(
                        items,
                        placeholderText: "",
                        validationPredicate: setup.ValidationPredicate,
                        validationErrorNotifier: setup.ValidationErrorNotifier)
                                                : DropDownSetup.Create(
                        items,
                        placeholderText: "",
                        autoFillTokens: setup.AutoFillTokens,
                        action: new SpecifiedValue <FormAction>(setup.Action),
                        selectionChangedAction: setup.ValueChangedAction,
                        validationPredicate: setup.ValidationPredicate,
                        validationErrorNotifier: setup.ValidationErrorNotifier),
                    value,
                    placeholderIsValid: allowEmpty,
                    validationMethod: validationMethod);

                Labeler = selectList.Labeler;

                PageComponent = new GenericFlowContainer(
                    selectList.PageComponent.ToCollection(),
                    displaySetup: setup.DisplaySetup,
                    classes: elementClass.Add(setup.Classes ?? ElementClassSet.Empty));

                Validation = selectList.Validation;
            }
        }