Exemple #1
0
        private FlowComponent getAppLogoAndUserInfoContainer()
        {
            var appLogo = new GenericFlowContainer(
                (!ConfigurationStatics.IsIntermediateInstallation || AppRequestState.Instance.IntermediateUserExists
                                          ? EwfUiStatics.AppProvider.GetLogoComponent()
                                          : null) ?? (BasePageStatics.AppProvider.AppDisplayName.Length > 0
                                                                ? BasePageStatics.AppProvider.AppDisplayName
                                                                : ConfigurationStatics.SystemDisplayName).ToComponents(),
                classes: appLogoClass);

            var userInfo = new List <FlowComponent>();

            if (AppRequestState.Instance.UserAccessible)
            {
                var changePasswordPage = new UserManagement.Pages.ChangePassword(PageBase.Current.GetUrl());
                if (changePasswordPage.UserCanAccessResource && AppTools.User != null)
                {
                    userInfo.Add(new GenericFlowContainer(getUserInfo(changePasswordPage), classes: userInfoClass));
                }
            }

            return(new GenericFlowContainer(appLogo.Concat(userInfo).Materialize(), classes: appLogoAndUserInfoClass));
        }
Exemple #2
0
        /// <summary>
        /// Creates a chart displaying a supported <see cref="ChartType"/> with the given data. Includes a chart and a table, and allows exporting the data to
        /// Excel. Assuming <paramref name="dataSets"/> has multiple elements, draws multiple sets of Y values on the same chart.
        /// </summary>
        /// <param name="setup">The setup object for the chart.</param>
        /// <param name="dataSets">The data sets.</param>
        /// <param name="colors">The colors to use for the data sets. Pass null for default colors. If you specify your own colors, the number of colors does not
        /// need to match the number of data sets. If you pass fewer colors than data sets, the chart will use random colors for the remaining data sets.</param>
        public Chart(ChartSetup setup, [NotNull] IReadOnlyCollection <ChartDataSet> dataSets, IEnumerable <Color> colors = null)
        {
            var rand = new Random();

            colors = (colors ?? getDefaultColors()).Take(dataSets.Count)
                     .Pad(dataSets.Count, () => Color.FromArgb(rand.Next(256), rand.Next(256), rand.Next(256)));

            string chartType;

            string toRgbaString(Color color, string opacity) => "rgba( {0}, {1}, {2}, {3} )".FormatWith(color.R, color.G, color.B, opacity);

            Func <ChartDataSet, Color, JObject> datasetSelector;
            var yAxisTicksCallbackProperty = setup.YAxisLabelFormatOptions != null
                                                                 ? new JProperty(
                "callback",
                new JRaw(
                    "function( value, index, values ) {{ return new Intl.NumberFormat( '{0}', {1} ).format( value ); }}".FormatWith(
                        Cultures.EnglishUnitedStates.Name,
                        setup.YAxisLabelFormatOptions.ToString(Formatting.None)))).ToCollection()
                                                                 : Enumerable.Empty <JProperty>();
            JObject options;

            switch (setup.ChartType)
            {
            case ChartType.Line:
                chartType       = "line";
                datasetSelector = (set, color) => new JObject(
                    new JProperty("label", set.Label),
                    new JProperty("data", new JArray(set.Values.TakeLast(setup.MaxXValues))),
                    new JProperty("pointBackgroundColor", toRgbaString(color, "1")),
                    new JProperty("backgroundColor", toRgbaString(color, "0.25")),
                    new JProperty("borderColor", toRgbaString(color, "1")));
                options = new JObject(
                    new JProperty("aspectRatio", setup.AspectRatio),
                    new JProperty("legend", new JObject(new JProperty("display", dataSets.Count > 1))),
                    new JProperty(
                        "scales",
                        new JObject(
                            new JProperty(
                                "xAxes",
                                new JArray(
                                    new JObject(
                                        new JProperty(
                                            "scaleLabel",
                                            new JObject(new JProperty("display", setup.XAxisTitle.Any()), new JProperty("labelString", setup.XAxisTitle)))))),
                            new JProperty(
                                "yAxes",
                                new JArray(
                                    new JObject(
                                        new JProperty(
                                            "scaleLabel",
                                            new JObject(new JProperty("display", setup.YAxisTitle.Any()), new JProperty("labelString", setup.YAxisTitle))),
                                        new JProperty(
                                            "ticks",
                                            new JObject(new JProperty("beginAtZero", true).ToCollection().Concat(yAxisTicksCallbackProperty)))))))));
                break;

            case ChartType.Bar:
            case ChartType.StackedBar:
            case ChartType.HorizontalBar:
            case ChartType.HorizontalStackedBar:
                var horizontal = setup.ChartType == ChartType.HorizontalBar || setup.ChartType == ChartType.HorizontalStackedBar;
                chartType = horizontal ? "horizontalBar" : "bar";

                var stacked = setup.ChartType == ChartType.StackedBar || setup.ChartType == ChartType.HorizontalStackedBar;
                datasetSelector = (set, color) => new JObject(
                    new JProperty("label", set.Label).ToCollection()
                    .Append(new JProperty("data", new JArray(set.Values.TakeLast(setup.MaxXValues))))
                    .Append(new JProperty("backgroundColor", toRgbaString(color, "1")))
                    .Concat(stacked ? new JProperty("stack", set.StackedGroupName).ToCollection() : Enumerable.Empty <JProperty>()));

                var xAxis = new JObject(
                    new JProperty("stacked", stacked),
                    new JProperty(
                        "scaleLabel",
                        new JObject(new JProperty("display", setup.XAxisTitle.Any()), new JProperty("labelString", setup.XAxisTitle))));
                var yAxis = new JObject(
                    new JProperty("stacked", stacked),
                    new JProperty("scaleLabel", new JObject(new JProperty("display", setup.YAxisTitle.Any()), new JProperty("labelString", setup.YAxisTitle))),
                    new JProperty("ticks", new JObject(new JProperty("beginAtZero", true).ToCollection().Concat(yAxisTicksCallbackProperty))));
                options = new JObject(
                    new JProperty("aspectRatio", setup.AspectRatio),
                    new JProperty("legend", new JObject(new JProperty("display", dataSets.Count > 1))),
                    new JProperty(
                        "scales",
                        new JObject(
                            new JProperty("xAxes", new JArray(horizontal ? yAxis : xAxis)),
                            new JProperty("yAxes", new JArray(horizontal ? xAxis : yAxis)))));

                break;

            default:
                throw new UnexpectedValueException(setup.ChartType);
            }

            var canvas = new GenericFlowContainer(
                new ElementComponent(
                    context => new ElementData(
                        () => {
                var jsInitStatement = "new Chart( '{0}', {{ type: '{1}', data: {2}, options: {3} }} );".FormatWith(
                    context.Id,
                    chartType,
                    new JObject(
                        new JProperty("labels", new JArray(setup.Labels.TakeLast(setup.MaxXValues))),
                        new JProperty("datasets", new JArray(dataSets.Zip(colors, (set, color) => datasetSelector(set, color))))).ToString(
                        Formatting.None),
                    options.ToString(Formatting.None));

                return(new ElementLocalData(
                           "canvas",
                           focusDependentData: new ElementFocusDependentData(includeIdAttribute: true, jsInitStatements: jsInitStatement)));
            })).ToCollection());

            var table = setup.OmitTable
                                            ? Enumerable.Empty <FlowComponent>()
                                            : new FlowCheckbox(
                false,
                "Show underlying data".ToComponents(),
                setup: FlowCheckboxSetup.Create(
                    nestedContentGetter: () =>
                    ColumnPrimaryTable.Create(postBackIdBase: setup.PostBackIdBase, allowExportToExcel: true, firstDataFieldIndex: 1)
                    .AddItems(
                        (setup.XAxisTitle.Any() || setup.Labels.Any(i => i.Any())
                                                                                                      ? EwfTableItem.Create(setup.XAxisTitle.ToCollection().Concat(setup.Labels).Select(i => i.ToCell()).Materialize())
                         .ToCollection()
                                                                                                      : Enumerable.Empty <EwfTableItem>()).Concat(
                            dataSets.Select(
                                dataSet => EwfTableItem.Create(
                                    dataSet.Label.ToCell().Concat(from i in dataSet.Values select i.ToString().ToCell()).Materialize())))
                        .Materialize())
                    .ToCollection())).ToFormItem()
                        .ToComponentCollection();

            children = new GenericFlowContainer(canvas.Concat(table).Materialize(), classes: elementClass).ToCollection();
        }
Exemple #3
0
        /// <summary>
        /// Creates a checkbox list.
        /// </summary>
        /// <param name="setup">The setup object for the checkbox list. Do not pass null.</param>
        /// <param name="value">The selected-item IDs.</param>
        /// <param name="validationMethod">The validation method. Pass null if you’re only using this control for page modification.</param>
        public CheckboxList(
            CheckboxListSetup <ItemIdType> setup, IEnumerable <ItemIdType> value, Action <IReadOnlyCollection <ItemIdType>, Validator> validationMethod = null)
        {
            var valueSet = value.ToImmutableHashSet();

            var selectedItemIdsInPostBack  = new List <ItemIdType>();
            var selectionChangedOnPostBack = false;
            var checkboxes = setup.Items.Select(
                i => new FlowCheckbox(
                    valueSet.Contains(i.Id),
                    i.Label.ToComponents(),
                    setup: FlowCheckboxSetup.Create(
                        highlightedWhenChecked: true,
                        action: new SpecifiedValue <FormAction>(setup.Action),
                        valueChangedAction: setup.SelectionChangedAction),
                    validationMethod: (postBackValue, validator) => {
                if (postBackValue.Value)
                {
                    selectedItemIdsInPostBack.Add(i.Id);
                }
                selectionChangedOnPostBack = selectionChangedOnPostBack || postBackValue.ChangedOnPostBack;
            }))
                             .Materialize();

            var contentContainerId = new ElementId();

            PageComponent = new GenericFlowContainer(
                (setup.IncludeSelectAndDeselectAllButtons
                                          ? new GenericFlowContainer(
                     new InlineList(
                         new EwfButton(
                             new StandardButtonStyle(
                                 "Select All",
                                 buttonSize: ButtonSize.ShrinkWrap,
                                 icon: new ActionComponentIcon(new FontAwesomeIcon("fa-check-square-o"))),
                             behavior: new CustomButtonBehavior(
                                 () => "$( '#{0}' ).find( 'input[type=checkbox]:not(:checked)' ).click();".FormatWith(contentContainerId.Id))).ToCollection()
                         .ToComponentListItem()
                         .ToCollection()
                         .Append(
                             new EwfButton(
                                 new StandardButtonStyle(
                                     "Deselect All",
                                     buttonSize: ButtonSize.ShrinkWrap,
                                     icon: new ActionComponentIcon(new FontAwesomeIcon("fa-square-o"))),
                                 behavior: new CustomButtonBehavior(
                                     () => "$( '#{0}' ).find( 'input[type=checkbox]:checked' ).click();".FormatWith(contentContainerId.Id))).ToCollection()
                             .ToComponentListItem())).ToCollection(),
                     classes: CheckboxListCssElementCreator.ActionContainerClass).ToCollection()
                                          : Enumerable.Empty <FlowComponent>()).Append(
                    new DisplayableElement(
                        context => {
                contentContainerId.AddId(context.Id);
                return(new DisplayableElementData(
                           null,
                           () => new DisplayableElementLocalData(
                               "div",
                               focusDependentData: new DisplayableElementFocusDependentData(
                                   attributes: setup.MinColumnWidth != null
                                                                                                    ? Tuple.Create("style", "column-width: {0}".FormatWith(((CssLength)setup.MinColumnWidth).Value)).ToCollection()
                                                                                                    : null,
                                   includeIdAttribute: true)),
                           classes: CheckboxListCssElementCreator.ContentContainerClass,
                           children: new RawList(from i in checkboxes select i.PageComponent.ToCollection().ToComponentListItem()).ToCollection()));
            }))
                .Materialize(),
                displaySetup: setup.DisplaySetup,
                classes: CheckboxListCssElementCreator.ListClass);

            if (validationMethod != null)
            {
                Validation = new EwfValidation(
                    validator => {
                    if (setup.ValidationPredicate != null && !setup.ValidationPredicate(selectionChangedOnPostBack))
                    {
                        return;
                    }
                    validationMethod(selectedItemIdsInPostBack, validator);
                });
            }
        }
        /// <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;
            }
        }