Exemple #1
0
        /// <summary>
        /// Perform an automatic placement of the block. Also updates the configuration of
        /// the block to match where it was placed.
        /// </summary>
        /// <param name="config">The user configuration information to update.</param>
        /// <param name="block">The block wrapper tha tmus tbe placed.</param>
        /// <param name="columns">The dashboard columns in the UI.</param>
        private void AutoConfigBlockPlacement(DashboardConfig config, DashboardBlockWrapper block, List <DashboardColumn> columns, bool isRequired)
        {
            var shortestColumn = columns.OrderBy(c => c.Placeholder.Controls.Count).First();

            if (isRequired)
            {
                block.Config.Column = columns.IndexOf(shortestColumn);
                block.Config.Order  = 0;
                int i = 1;
                config.Blocks.Where(b => b.Column == block.Config.Column && b.BlockId != block.Config.BlockId)
                .ToList()
                .ForEach(b => b.Order = i++);

                columns[block.Config.Column].Placeholder.Controls.AddAt(0, block);
            }
            else
            {
                block.Config.Column = columns.IndexOf(shortestColumn);
                block.Config.Order  = 999;
                int i = 0;
                config.Blocks.Where(b => b.Column == block.Config.Column)
                .ToList()
                .ForEach(b => b.Order = i++);

                columns[block.Config.Column].Placeholder.Controls.Add(block);
            }
        }
Exemple #2
0
        /// <summary>
        /// Build and layout all the blocks onto the web page.
        /// </summary>
        /// <param name="config">User configuration data that specifies how things should be laid out.</param>
        private void BuildBlocks(DashboardConfig config)
        {
            var sourcePageGuid = GetAttributeValue("SourcePage").AsGuidOrNull();

            if (sourcePageGuid.HasValue)
            {
                var sourcePage = PageCache.Read(sourcePageGuid.Value);

                if (sourcePage != null)
                {
                    var   blocks = GetAvailableBlocks();
                    Panel pnlRow = new Panel {
                        CssClass = "row"
                    };
                    List <DashboardBlockWrapper> dashboardBlocks = new List <DashboardBlockWrapper>();
                    bool needSave = false;

                    phControls.Controls.Add(pnlRow);
                    var columns = BuildColumns(config, pnlRow);

                    foreach (var block in blocks)
                    {
                        DashboardBlockConfig blockConfig = null;

                        blockConfig = config.Blocks.Where(b => b.BlockId == block.BlockId).FirstOrDefault();
                        if (blockConfig == null)
                        {
                            blockConfig         = new DashboardBlockConfig(block.BlockId);
                            blockConfig.Visible = block.DefaultVisible;
                            config.Blocks.Add(blockConfig);
                            needSave = true;
                        }

                        if (block.Required)
                        {
                            blockConfig.Visible = true;
                        }

                        if (blockConfig.Visible)
                        {
                            var control = TemplateControl.LoadControl(block.BlockCache.BlockType.Path);
                            control.ClientIDMode = ClientIDMode.AutoID;

                            var  blockControl    = control as RockBlock;
                            bool canEdit         = block.BlockCache.IsAuthorized(Authorization.EDIT, CurrentPerson);
                            bool canAdministrate = block.BlockCache.IsAuthorized(Authorization.ADMINISTRATE, CurrentPerson);

                            blockControl.SetBlock(PageCache, block.BlockCache, canEdit, canAdministrate);
                            block.BlockCache.BlockType.SetSecurityActions(blockControl);

                            var blockWrapper = new DashboardBlockWrapper(blockControl, block.BlockCache, blockConfig);
                            blockWrapper.ShowDelete = !block.Required;

                            dashboardBlocks.Add(blockWrapper);
                        }
                    }

                    //
                    // Add all the blocks that are properly layed out already.
                    //
                    var existingBlocks = dashboardBlocks.Where(b => b.Config.Column < columns.Count);
                    foreach (var blockGroup in existingBlocks.GroupBy(b => b.Config.Column))
                    {
                        var sortedBlocks = blockGroup.OrderBy(b => b.Config.Order);

                        foreach (var block in sortedBlocks)
                        {
                            columns[block.Config.Column].Placeholder.Controls.Add(block);
                        }
                    }

                    //
                    // Add in blocks that are new or don't currently fit.
                    //
                    var newBlocks = dashboardBlocks.Where(b => b.Config.Column >= columns.Count);
                    foreach (var blockGroup in newBlocks.GroupBy(b => b.Config.Column))
                    {
                        var sortedBlocks = blockGroup.OrderBy(b => b.Config.Order);

                        foreach (var block in sortedBlocks)
                        {
                            bool isRequired = blocks.Where(b => b.BlockId == block.Config.BlockId).First().Required;
                            AutoConfigBlockPlacement(config, block, columns, isRequired);
                            needSave = true;
                        }
                    }

                    //
                    // Cleanup the config for any blocks that don't exist anymore.
                    //
                    var missingKeys = config.Blocks
                                      .Where(b => !blocks.Where(db => b.BlockId == db.BlockId).Any())
                                      .Select(b => b.BlockId)
                                      .ToList();
                    foreach (var missingKey in missingKeys)
                    {
                        config.Blocks.RemoveAll(b => b.BlockId == missingKey);
                        needSave = true;
                    }

                    if (needSave)
                    {
                        SaveConfig(config);
                    }
                }
            }
        }