예제 #1
0
        /// <summary>
        /// Wraps blocks within the region in a specific html tag. Each region
        /// needs to have a single container node in page edit mode and by default a div
        /// element is used if a single container cannot be found in the rendered output.
        /// Use this setting to control this tag. If a tag is specified here the output
        /// will always be wrapped irrespecitive of whether the page is in edit mode to maintain
        /// consistency.
        /// </summary>
        /// <param name="tagName">Name of the element to wrap the output in e.g. div, p, header</param>
        /// <param name="htmlAttributes">Html attributes to apply to the wrapping tag.</param>
        /// <returns>ICustomEntityTemplateRegionTagBuilder for method chaining</returns>
        public ICustomEntityTemplateRegionTagBuilder<TModel> WrapWithTag(string tagName, object htmlAttributes = null)
        {
            if (tagName == null) throw new ArgumentNullException(nameof(tagName));
            if (string.IsNullOrWhiteSpace(tagName)) throw new ArgumentEmptyException(nameof(tagName));

            _wrappingTagName = tagName;
            _additonalHtmlAttributes = TemplateRegionTagBuilderHelper.ParseHtmlAttributesFromAnonymousObject(htmlAttributes);

            return this;
        }
예제 #2
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
                       ));
        }
예제 #3
0
        /// <summary>
        /// Wraps blocks within the region in a specific html tag. Each region
        /// needs to have a single container node in page edit mode and by default a div
        /// element is used if a single container cannot be found in the rendered output.
        /// Use this setting to control this tag. If a tag is specified here the output
        /// will always be wrapped irrespecitive of whether the page is in edit mode to maintain
        /// consistency.
        /// </summary>
        /// <param name="tagName">Name of the element to wrap the output in e.g. div, p, header</param>
        /// <param name="htmlAttributes">Html attributes to apply to the wrapping tag.</param>
        /// <returns>IPageTemplateRegionTagBuilder for method chaining</returns>
        public IPageTemplateRegionTagBuilder WrapWithTag(string tagName, object htmlAttributes = null)
        {
            if (tagName == null)
            {
                throw new ArgumentNullException(nameof(tagName));
            }
            if (string.IsNullOrWhiteSpace(tagName))
            {
                throw new ArgumentEmptyException(nameof(tagName));
            }

            _wrappingTagName         = tagName;
            _additonalHtmlAttributes = TemplateRegionTagBuilderHelper.ParseHtmlAttributesFromAnonymousObject(htmlAttributes);

            return(this);
        }
        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
                       ));
        }