BuildCellPlaceholderListsForItems() static private method

static private BuildCellPlaceholderListsForItems ( IReadOnlyCollection items, int fieldCount ) : List>
items IReadOnlyCollection
fieldCount int
return List>
Example #1
0
        void ControlTreeDataLoader.LoadData()
        {
            if (hideIfEmpty && itemGroups.All(itemGroup => !itemGroup.Items.Any()))
            {
                Visible = false;
                return;
            }

            EwfTable.SetUpTableAndCaption(this, style, classes, caption, subCaption);

            var itemSetupLists = new[] { headItems }.Concat(itemGroups.Select(i => i.Items)).Select(i => i.Select(j => j.Setup.FieldOrItemSetup));
            var allItemSetups     = itemSetupLists.SelectMany(i => i).ToImmutableArray();
            var columnWidthFactor = EwfTable.GetColumnWidthFactor(allItemSetups);

            foreach (var itemSetups in itemSetupLists.Where(i => i.Any()))
            {
                Controls.Add(
                    new WebControl(HtmlTextWriterTag.Colgroup).AddControlsReturnThis(itemSetups.Select(i => EwfTable.GetColControl(i, columnWidthFactor))));
            }

            var fields = EwfTable.GetFields(specifiedFields, headItems, itemGroups.SelectMany(i => i.Items));
            var cellPlaceholderListsForItems = TableOps.BuildCellPlaceholderListsForItems(
                headItems.Concat(itemGroups.SelectMany(i => i.Items)).ToList(),
                fields.Count);

            // Pivot the cell placeholders from column primary into row primary format.
            var cellPlaceholderListsForRows =
                Enumerable.Range(0, fields.Count)
                .Select(field => Enumerable.Range(0, allItemSetups.Length).Select(item => cellPlaceholderListsForItems[item][field]).ToList())
                .ToList();

            var headRows = TableOps.BuildRows(
                cellPlaceholderListsForRows.Take(firstDataFieldIndex).ToList(),
                fields.Select(i => i.FieldOrItemSetup).ToImmutableArray(),
                null,
                allItemSetups,
                allItemSetups.Length,
                true);
            var bodyRows = TableOps.BuildRows(
                cellPlaceholderListsForRows.Skip(firstDataFieldIndex).ToList(),
                fields.Select(i => i.FieldOrItemSetup).ToImmutableArray(),
                false,
                allItemSetups,
                headItems.Count,
                true);

            // We can't easily put the head fields in thead because we don't have a way of verifying that cells don't cross between head and data fields.
            Controls.Add(new WebControl(HtmlTextWriterTag.Tbody).AddControlsReturnThis(headRows.Concat(bodyRows)));

            EwfTable.AssertAtLeastOneCellPerField(fields, cellPlaceholderListsForItems);
        }
Example #2
0
        private IEnumerable <Control> buildRows(
            IReadOnlyCollection <EwfTableItem> items, IReadOnlyCollection <EwfTableField> fields, bool?useContrastForFirstRow, bool useHeadCells,
            Func <EwfTableCell> itemActionCheckBoxCellGetter, Func <EwfTableCell> itemReorderingCellGetter, List <EwfTableItem> allVisibleItems)
        {
            // Assert that the cells in the list of items are valid and store a data structure for below.
            var cellPlaceholderListsForRows = TableOps.BuildCellPlaceholderListsForItems(items, fields.Count);

            // NOTE: Be sure to take check box and reordering columns into account.
            var rows = TableOps.BuildRows(
                cellPlaceholderListsForRows,
                items.Select(i => i.Setup.FieldOrItemSetup).ToImmutableArray(),
                useContrastForFirstRow,
                fields.Select(i => i.FieldOrItemSetup).ToImmutableArray(),
                useHeadCells ? fields.Count : 0,
                false);

            allVisibleItems.AddRange(items);
            return(rows);
        }
Example #3
0
        void ControlTreeDataLoader.LoadData()
        {
            using (MiniProfiler.Current.Step("EWF - Load table data")) {
                FormState.ExecuteWithDataModificationsAndDefaultAction(
                    dataModifications,
                    () => {
                    if (hideIfEmpty && itemGroups.All(itemGroup => !itemGroup.Items.Any()))
                    {
                        Visible = false;
                        return;
                    }

                    SetUpTableAndCaption(this, style, classes, caption, subCaption);

                    var visibleItemGroupsAndItems = new List <Tuple <EwfTableItemGroup, IReadOnlyCollection <EwfTableItem> > >();
                    foreach (var itemGroup in itemGroups)
                    {
                        var visibleItems = itemGroup.Items.Take(CurrentItemLimit - visibleItemGroupsAndItems.Sum(i => i.Item2.Count)).Select(i => i());
                        visibleItemGroupsAndItems.Add(Tuple.Create <EwfTableItemGroup, IReadOnlyCollection <EwfTableItem> >(itemGroup, visibleItems.ToImmutableArray()));
                        if (visibleItemGroupsAndItems.Sum(i => i.Item2.Count) == CurrentItemLimit)
                        {
                            break;
                        }
                    }

                    var fields = GetFields(specifiedFields, headItems.AsReadOnly(), visibleItemGroupsAndItems.SelectMany(i => i.Item2));
                    if (!fields.Any())
                    {
                        fields = new EwfTableField().ToCollection();
                    }

                    addColumnSpecifications(fields);

                    var allVisibleItems = new List <EwfTableItem>();

                    var itemLimitingUpdateRegionSet = new UpdateRegionSet();
                    var headRows =
                        buildRows(
                            getItemLimitingAndGeneralActionsItem(fields.Count, itemLimitingUpdateRegionSet).Concat(getItemActionsItem(fields.Count)).ToList(),
                            Enumerable.Repeat(new EwfTableField(), fields.Count).ToArray(),
                            null,
                            false,
                            null,
                            null,
                            allVisibleItems).Concat(buildRows(headItems, fields, null, true, null, null, allVisibleItems)).ToArray();
                    if (headRows.Any())
                    {
                        Controls.Add(new WebControl(HtmlTextWriterTag.Thead).AddControlsReturnThis(headRows));
                    }

                    var bodyRowGroupsAndRows = new List <Tuple <WebControl, ImmutableArray <Control> > >();
                    var updateRegionSetListsAndStaticRowGroupCounts = new List <Tuple <IReadOnlyCollection <UpdateRegionSet>, int> >();
                    for (var visibleGroupIndex = 0; visibleGroupIndex < visibleItemGroupsAndItems.Count; visibleGroupIndex += 1)
                    {
                        var groupAndItems          = visibleItemGroupsAndItems[visibleGroupIndex];
                        var useContrastForFirstRow = visibleItemGroupsAndItems.Where((group, i) => i < visibleGroupIndex).Sum(i => i.Item2.Count) % 2 == 1;
                        var groupBodyRows          = buildRows(groupAndItems.Item2, fields, useContrastForFirstRow, false, null, null, allVisibleItems).ToImmutableArray();
                        var rowGroup =
                            new WebControl(HtmlTextWriterTag.Tbody).AddControlsReturnThis(
                                buildRows(
                                    groupAndItems.Item1.GetHeadItems(fields.Count),
                                    Enumerable.Repeat(new EwfTableField(), fields.Count).ToArray(),
                                    null,
                                    true,
                                    null,
                                    null,
                                    allVisibleItems).Concat(new NamingPlaceholder(groupBodyRows).ToCollection()));
                        bodyRowGroupsAndRows.Add(Tuple.Create(rowGroup, groupBodyRows));

                        var cachedVisibleGroupIndex = visibleGroupIndex;
                        EwfPage.Instance.AddUpdateRegionLinker(
                            new LegacyUpdateRegionLinker(
                                rowGroup,
                                "tail",
                                from region in groupAndItems.Item1.RemainingData.Value.TailUpdateRegions
                                let staticRowCount = itemGroups[cachedVisibleGroupIndex].Items.Count - region.UpdatingItemCount
                                                     select new LegacyPreModificationUpdateRegion(region.Sets, () => groupBodyRows.Skip(staticRowCount), staticRowCount.ToString),
                                arg => groupBodyRows.Skip(int.Parse(arg))));

                        // If item limiting is enabled, include all subsequent item groups in tail update regions since any number of items could be appended.
                        if (defaultItemLimit != DataRowLimit.Unlimited)
                        {
                            updateRegionSetListsAndStaticRowGroupCounts.Add(
                                Tuple.Create <IReadOnlyCollection <UpdateRegionSet>, int>(
                                    groupAndItems.Item1.RemainingData.Value.TailUpdateRegions.SelectMany(i => i.Sets).ToImmutableArray(),
                                    visibleGroupIndex + 1));
                        }
                    }
                    Controls.Add(new NamingPlaceholder(bodyRowGroupsAndRows.Select(i => i.Item1)));

                    if (defaultItemLimit != DataRowLimit.Unlimited)
                    {
                        var oldItemLimit   = CurrentItemLimit;
                        var lowerItemLimit = new Lazy <int>(() => Math.Min(oldItemLimit, CurrentItemLimit));

                        var itemLimitingTailUpdateRegionControlGetter = new Func <int, IEnumerable <Control> >(
                            staticItemCount => {
                            var rowCount = 0;
                            for (var groupIndex = 0; groupIndex < bodyRowGroupsAndRows.Count; groupIndex += 1)
                            {
                                var rows  = bodyRowGroupsAndRows[groupIndex].Item2;
                                rowCount += rows.Length;
                                if (rowCount < staticItemCount)
                                {
                                    continue;
                                }
                                return(rows.Skip(rows.Length - (rowCount - staticItemCount)).Concat(bodyRowGroupsAndRows.Skip(groupIndex + 1).Select(i => i.Item1)));
                            }
                            return(ImmutableArray <Control> .Empty);
                        });

                        EwfPage.Instance.AddUpdateRegionLinker(
                            new LegacyUpdateRegionLinker(
                                this,
                                "itemLimitingTail",
                                new LegacyPreModificationUpdateRegion(
                                    itemLimitingUpdateRegionSet.ToCollection(),
                                    () => itemLimitingTailUpdateRegionControlGetter(lowerItemLimit.Value),
                                    () => lowerItemLimit.Value.ToString()).ToCollection(),
                                arg => itemLimitingTailUpdateRegionControlGetter(int.Parse(arg))));
                    }

                    EwfPage.Instance.AddUpdateRegionLinker(
                        new LegacyUpdateRegionLinker(
                            this,
                            "tail",
                            from region in
                            tailUpdateRegions.Select(i => new { sets = i.Sets, staticRowGroupCount = itemGroups.Count - i.UpdatingItemCount })
                            .Concat(updateRegionSetListsAndStaticRowGroupCounts.Select(i => new { sets = i.Item1, staticRowGroupCount = i.Item2 }))
                            select
                            new LegacyPreModificationUpdateRegion(
                                region.sets,
                                () => bodyRowGroupsAndRows.Skip(region.staticRowGroupCount).Select(i => i.Item1),
                                region.staticRowGroupCount.ToString),
                            arg => bodyRowGroupsAndRows.Skip(int.Parse(arg)).Select(i => i.Item1)));

                    var itemCount            = itemGroups.Sum(i => i.Items.Count);
                    var itemLimitingRowGroup = new List <Control>();
                    if (CurrentItemLimit < itemCount)
                    {
                        var nextLimit          = EnumTools.GetValues <DataRowLimit>().First(i => i > (DataRowLimit)CurrentItemLimit);
                        var itemIncrementCount = Math.Min((int)nextLimit, itemCount) - CurrentItemLimit;
                        var button             = new PostBackButton(
                            new TextActionControlStyle("Show " + itemIncrementCount + " more item" + (itemIncrementCount != 1 ? "s" : "")),
                            usesSubmitBehavior: false,
                            postBack:
                            PostBack.CreateIntermediate(
                                itemLimitingUpdateRegionSet.ToCollection(),
                                id: PostBack.GetCompositeId(postBackIdBase, "showMore"),
                                firstModificationMethod: () => EwfPage.Instance.PageState.SetValue(this, itemLimitPageStateKey, (int)nextLimit)));
                        var item        = new EwfTableItem(button.ToCell(new TableCellSetup(fieldSpan: fields.Count)));
                        var useContrast = visibleItemGroupsAndItems.Sum(i => i.Item2.Count) % 2 == 1;
                        itemLimitingRowGroup.Add(
                            new WebControl(HtmlTextWriterTag.Tbody).AddControlsReturnThis(
                                buildRows(
                                    item.ToCollection().ToList(),
                                    Enumerable.Repeat(new EwfTableField(), fields.Count).ToArray(),
                                    useContrast,
                                    false,
                                    null,
                                    null,
                                    allVisibleItems)));
                    }
                    Controls.Add(
                        new NamingPlaceholder(
                            itemLimitingRowGroup,
                            updateRegionSets:
                            itemLimitingUpdateRegionSet.ToCollection()
                            .Concat(
                                itemGroups.SelectMany(i => i.RemainingData.Value.TailUpdateRegions).ToImmutableArray().Concat(tailUpdateRegions).SelectMany(i => i.Sets))));

                    // Assert that every visible item in the table has the same number of cells and store a data structure for below.
                    var cellPlaceholderListsForItems = TableOps.BuildCellPlaceholderListsForItems(allVisibleItems, fields.Count);

                    if (!disableEmptyFieldDetection)
                    {
                        AssertAtLeastOneCellPerField(fields, cellPlaceholderListsForItems);
                    }
                });
            }
        }