Beispiel #1
0
        private async Task <string> RenderRegion(PageRegionRenderDetails pageRegion)
        {
            var regionAttributes  = new Dictionary <string, string>();
            var visualEditorState = await _visualEditorStateService.GetCurrentAsync();

            var blocksHtml = await RenderBlocksToHtml(pageRegion, regionAttributes, visualEditorState);

            // If we're not in edit mode just return the blocks.
            if (visualEditorState.VisualEditorMode != VisualEditorMode.Edit)
            {
                if (_wrappingTagName != null)
                {
                    return(TemplateRegionTagBuilderHelper.WrapInTag(
                               blocksHtml,
                               _wrappingTagName,
                               _allowMultipleBlocks,
                               _additonalHtmlAttributes
                               ));
                }

                return(blocksHtml);
            }

            regionAttributes.Add("data-cms-page-template-region-id", pageRegion.PageTemplateRegionId.ToString());
            regionAttributes.Add("data-cms-page-region-name", pageRegion.Name);
            regionAttributes.Add("data-cms-page-region", string.Empty);
            regionAttributes.Add("class", "cofoundry__sv-region");

            if (_permittedBlocks.Any())
            {
                regionAttributes.Add("data-cms-page-region-permitted-block-types", string.Join(",", _permittedBlocks));
            }

            if (_allowMultipleBlocks)
            {
                regionAttributes.Add("data-cms-multi-block", "true");

                if (_emptyContentMinHeight.HasValue)
                {
                    regionAttributes.Add("style", "min-height:" + _emptyContentMinHeight + "px");
                }
            }

            return(TemplateRegionTagBuilderHelper.WrapInTag(
                       blocksHtml,
                       _wrappingTagName,
                       _allowMultipleBlocks,
                       _additonalHtmlAttributes,
                       regionAttributes
                       ));
        }
        private async Task <string> RenderRegion(PageRegionRenderDetails pageRegion)
        {
            string blocksHtml = string.Empty;

            // No _permittedBlocks means any is allowed.
            var renderingTasks = pageRegion
                                 .Blocks
                                 .Where(m => _permittedBlocks.Count == 0 || _permittedBlocks.ContainsKey(m.BlockType.FileName))
                                 .Select(m => _blockRenderer.RenderBlockAsync(_viewContext, _pageViewModel, m));

            var blockHtmlParts = await Task.WhenAll(renderingTasks);

            if (blockHtmlParts.Any())
            {
                if (!_allowMultipleBlocks)
                {
                    // If for some reason another block has been added in error, make sure we only display one.
                    blocksHtml = blockHtmlParts.Last();
                }
                else
                {
                    blocksHtml = string.Join(string.Empty, blockHtmlParts);
                }
            }
            else if (!_allowMultipleBlocks && _pageViewModel.IsPageEditMode)
            {
                // If there are no blocks and this is a single block region
                // add a placeholder element so we always have a menu
                blocksHtml = _blockRenderer.RenderPlaceholderBlock(_emptyContentMinHeight);
            }

            // If we're not in edit mode just return the blocks.
            if (!_pageViewModel.IsPageEditMode)
            {
                if (_wrappingTagName != null)
                {
                    return(TemplateRegionTagBuilderHelper.WrapInTag(
                               blocksHtml,
                               _wrappingTagName,
                               _allowMultipleBlocks,
                               _additonalHtmlAttributes
                               ));
                }

                return(blocksHtml);
            }

            var attrs = new Dictionary <string, string>();

            attrs.Add("data-cms-page-template-region-id", pageRegion.PageTemplateRegionId.ToString());
            attrs.Add("data-cms-page-region-name", pageRegion.Name);
            attrs.Add("data-cms-page-region", string.Empty);
            attrs.Add("class", "cofoundry__sv-region");

            if (_permittedBlocks.Any())
            {
                var permittedBlockTypes = _permittedBlocks.Select(m => m.Key);
                attrs.Add("data-cms-page-region-permitted-block-types", string.Join(",", permittedBlockTypes));
            }

            if (_allowMultipleBlocks)
            {
                attrs.Add("data-cms-multi-block", "true");

                if (_emptyContentMinHeight.HasValue)
                {
                    attrs.Add("style", "min-height:" + _emptyContentMinHeight + "px");
                }
            }

            return(TemplateRegionTagBuilderHelper.WrapInTag(
                       blocksHtml,
                       _wrappingTagName,
                       _allowMultipleBlocks,
                       _additonalHtmlAttributes,
                       attrs
                       ));
        }