/// <summary>
        /// Creates a change-based checkbox list, which is a checkbox list that is based on changes to the selections rather than the absolute set of selected
        /// items.
        /// </summary>
        /// <param name="items">The items in the list.</param>
        /// <param name="selectedItemIds">The selected-item IDs.</param>
        /// <param name="modificationMethod">A method that executes the change handlers of the items that were selected or deselected on this post back.</param>
        /// <param name="displaySetup"></param>
        /// <param name="includeSelectAndDeselectAllButtons"></param>
        /// <param name="minColumnWidth">The minimum width of each column in the list. Pass null to force a single column.</param>
        /// <param name="uiSelectedItemIds"></param>
        /// <param name="action">The action that will occur when the user hits Enter on any of the checkboxes. Pass null to use the current default action.</param>
        /// <param name="selectionChangedAction">The action that will occur when the selection is changed. Pass null for no action.</param>
        /// <param name="validationPredicate"></param>
        /// <param name="validationErrorNotifier"></param>
        public static CheckboxList <ItemIdType> Create <ItemIdType>(
            IEnumerable <ChangeBasedListItem <ItemIdType> > items, IEnumerable <ItemIdType> selectedItemIds, out Action modificationMethod,
            DisplaySetup displaySetup = null, bool includeSelectAndDeselectAllButtons             = false, ContentBasedLength minColumnWidth = null,
            IEnumerable <ItemIdType> uiSelectedItemIds = null, SpecifiedValue <FormAction> action = null, FormAction selectionChangedAction  = null,
            Func <bool, bool> validationPredicate      = null, Action validationErrorNotifier     = null)
        {
            items = items.Materialize();
            var selectedItemIdSet = selectedItemIds.ToImmutableHashSet();

            ImmutableHashSet <ItemIdType> selectedItemIdsInPostBack = null;

            modificationMethod = () => {
                if (selectedItemIdsInPostBack == null)
                {
                    return;
                }
                var changedItemIds = selectedItemIdsInPostBack.Except(selectedItemIdSet).Union(selectedItemIdSet.Except(selectedItemIdsInPostBack)).ToArray();
                foreach (var i in items.Where(i => changedItemIds.Contains(i.Item.Id)))
                {
                    i.ChangeHandler(selectedItemIdsInPostBack.Contains(i.Item.Id));
                }
            };

            return(new CheckboxList <ItemIdType>(
                       CheckboxListSetup.Create(
                           from i in items select i.Item,
                           displaySetup: displaySetup,
                           includeSelectAndDeselectAllButtons: includeSelectAndDeselectAllButtons,
                           minColumnWidth: minColumnWidth,
                           action: action,
                           selectionChangedAction: selectionChangedAction,
                           validationPredicate: validationPredicate,
                           validationErrorNotifier: validationErrorNotifier),
                       uiSelectedItemIds ?? selectedItemIdSet,
                       validationMethod: (postBackValue, validator) => selectedItemIdsInPostBack = postBackValue.ToImmutableHashSet()));
        }
Esempio n. 2
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);
                });
            }
        }