示例#1
0
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            if (AdminAttribute.IsApplied(_httpContextAccessor.HttpContext))
            {
                shapeBinding = null;
                return(false);
            }

            var localTemplates = _previewTemplatesProvider.GetTemplates();

            if (localTemplates != null)
            {
                if (localTemplates.Templates.TryGetValue(shapeType, out var localTemplate))
                {
                    shapeBinding = BuildShapeBinding(shapeType, localTemplate);
                    return(true);
                }
            }

            if (_templatesDocument.Templates.TryGetValue(shapeType, out var template))
            {
                shapeBinding = BuildShapeBinding(shapeType, template);

                return(true);
            }
            else
            {
                shapeBinding = null;
                return(false);
            }
        }
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            var processors = BuildShapeProcessors();

            TemplateResult templateResult = null;

            if (processors.TryGetValue(shapeType, out templateResult))
            {
                shapeBinding = new ShapeBinding {
                    BindingName = "Templates",
                    Binding     = ctx => CoerceHtmlString(_templateService.Execute(
                                                              templateResult.Template,
                                                              templateResult.Name,
                                                              templateResult.Processor, ctx.Value)),
                    ShapeDescriptor = new ShapeDescriptor {
                        ShapeType = shapeType
                    }
                };

                return(true);
            }

            shapeBinding = null;
            return(false);
        }
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            var localTemplates = _previewTemplatesProvider.GetTemplates();

            if (localTemplates != null)
            {
                if (localTemplates.Templates.TryGetValue(shapeType, out var localTemplate))
                {
                    shapeBinding = BuildShapeBinding(shapeType, localTemplate);
                    return(true);
                }
            }

            if (_templatesDocument.Templates.TryGetValue(shapeType, out var template))
            {
                shapeBinding = BuildShapeBinding(shapeType, template);

                return(true);
            }
            else
            {
                shapeBinding = null;
                return(false);
            }
        }
        public ShapeAlterationBuilder BoundAs(string bindingSource, Func <ShapeDescriptor, Func <DisplayContext, Task <IHtmlContent> > > binder)
        {
            // schedule the configuration
            return(Configure(descriptor =>
            {
                Func <DisplayContext, Task <IHtmlContent> > target = null;

                var binding = new ShapeBinding
                {
                    ShapeDescriptor = descriptor,
                    BindingName = _bindingName,
                    BindingSource = bindingSource,
                };

                binding.BindingAsync = displayContext =>
                {
                    // when used, first realize the actual target once
                    if (target == null)
                    {
                        target = binder(binding.ShapeDescriptor);
                    }

                    // and execute the re
                    return target(displayContext);
                };

                // ShapeDescriptor.Bindings is a case insensitive dictionary
                descriptor.Bindings[_bindingName] = binding;
                descriptor.BindingSources.Add(bindingSource);
            }));
        }
 private static IHtmlString Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context)
 {
     if (shapeBinding == null || shapeBinding.Binding == null)
     {
         return(shape.Metadata.ChildContent ?? new HtmlString(string.Empty));
     }
     return(CoerceHtmlString(shapeBinding.Binding(context)));
 }
示例#6
0
 static IHtmlString Process(ShapeBinding shapeBinding, IShape shape, DisplayContext context)
 {
     if (shapeBinding == null || shapeBinding.Binding == null)
     {
         // todo: create result from all child shapes
         return(shape.Metadata.ChildContent ?? new HtmlString(""));
     }
     return(CoerceHtmlString(shapeBinding.Binding(context)));
 }
示例#7
0
 static async Task <IHtmlContent> ProcessAsync(ShapeBinding shapeBinding, IShape shape, DisplayContext context)
 {
     if (shapeBinding == null || shapeBinding.BindingAsync == null)
     {
         // todo: create result from all child shapes
         return(shape.Metadata.ChildContent ?? HtmlString.Empty);
     }
     return(CoerceHtmlString(await shapeBinding.BindingAsync(context)));
 }
示例#8
0
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            var processors = BuildShapeProcessors();

            var acceptableRenderingModes = new List <RenderingMode>()
            {
                RenderingMode.FrontEndAndAdmin
            };

            if (AdminFilter.IsApplied(_requestContext))
            {
                acceptableRenderingModes.Add(RenderingMode.Admin);
            }
            else
            {
                acceptableRenderingModes.Add(RenderingMode.FrontEnd);
            }

            var            templateResults      = processors[shapeType].Where(template => acceptableRenderingModes.Contains(template.RenderingMode));
            TemplateResult templateResult       = null;
            var            templateResultsCount = templateResults.Count();

            if (templateResultsCount == 1)
            {
                templateResult = templateResults.FirstOrDefault();
            }
            else if (templateResultsCount > 1)
            {
                // Templates with the same name but specified rendering mode are prioritized.
                templateResult = templateResults.FirstOrDefault(template => template.RenderingMode != RenderingMode.FrontEndAndAdmin);
            }

            if (templateResult != null)
            {
                shapeBinding = new ShapeBinding {
                    BindingName = "Templates",
                    Binding     = ctx => CoerceHtmlString(_templateService.Execute(
                                                              templateResult.Template,
                                                              templateResult.Name,
                                                              templateResult.Processor, ctx.Value)),
                    ShapeDescriptor = new ShapeDescriptor {
                        ShapeType = shapeType
                    }
                };

                return(true);
            }

            shapeBinding = null;
            return(false);
        }
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            shapeBinding = null;

            if (!Enabled)
            {
                return(false);
            }

            var currentThemeName = _siteThemeService.GetCurrentThemeName();
            var shapeTable       = _shapeTableLocator.Lookup(currentThemeName);

            return(shapeTable.Bindings.TryGetValue(shapeType, out shapeBinding));
        }
        public async Task IShapeBindingResolverProvidedShapesDoesNotOverrideShapeDescriptor()
        {
            var displayManager = _serviceProvider.GetService <IHtmlDisplay>();

            var shape = new Shape();

            shape.Metadata.Type = "Foo";

            var descriptor = new ShapeDescriptor
            {
                ShapeType       = "Foo",
                ProcessingAsync = new Func <ShapeDisplayContext, Task>[] {
                    context =>
                    {
                        dynamic dynamicShape = context.Shape;
                        dynamicShape.Data = "some data";
                        return(Task.CompletedTask);
                    }
                }
            };

            descriptor.Bindings["Foo"] = new ShapeBinding
            {
                BindingName  = "Foo",
                BindingAsync = ctx => Task.FromResult <IHtmlContent>(new HtmlString("Is there any data ?"))
            };
            AddShapeDescriptor(descriptor);

            _additionalBindings["Foo"] = new ShapeBinding
            {
                BindingName  = "Foo",
                BindingAsync = ctx => Task.FromResult <IHtmlContent>(new HtmlString($"Yes there is { ((dynamic)ctx.Value).Data }."))
            };

            var result = await displayManager.ExecuteAsync(CreateDisplayContext(shape));

            // Cleanup
            _additionalBindings.Clear();

            Assert.Equal("Yes there is some data.", result.ToString());
        }
        public async Task RenderIShapeBindingResolverProvidedShapes()
        {
            var displayManager = _serviceProvider.GetService <IHtmlDisplay>();

            var shape = new Shape();

            shape.Metadata.Type = "Baz";

            _additionalBindings["Baz"] = new ShapeBinding
            {
                BindingName  = "Baz",
                BindingAsync = ctx => Task.FromResult <IHtmlContent>(new HtmlString("Hi from IShapeBindingResolver."))
            };

            var result = await displayManager.ExecuteAsync(CreateDisplayContext(shape));

            // Cleanup
            _additionalBindings.Clear();

            Assert.Equal("Hi from IShapeBindingResolver.", result.ToString());
        }
示例#12
0
        private static ValueTask <IHtmlContent> ProcessAsync(ShapeBinding shapeBinding, IShape shape, DisplayContext context)
        {
            async ValueTask <IHtmlContent> Awaited(Task <IHtmlContent> task)
            {
                return(CoerceHtmlString(await task));
            }

            if (shapeBinding?.BindingAsync == null)
            {
                // todo: create result from all child shapes
                return(new ValueTask <IHtmlContent>(shape.Metadata.ChildContent ?? HtmlString.Empty));
            }

            var task = shapeBinding.BindingAsync(context);

            if (!task.IsCompletedSuccessfully)
            {
                return(Awaited(task));
            }

            return(new ValueTask <IHtmlContent>(CoerceHtmlString(task.Result)));
        }
示例#13
0
        public bool TryGetDescriptorBinding(string shapeType, out ShapeBinding shapeBinding)
        {
            if (_templatesDocument.Templates.TryGetValue(shapeType, out var template))
            {
                shapeBinding = new ShapeBinding()
                {
                    ShapeDescriptor = new ShapeDescriptor()
                    {
                        ShapeType = shapeType
                    },
                    BindingName   = shapeType,
                    BindingSource = shapeType,
                    BindingAsync  = async displayContext =>
                    {
                        var context = new TemplateContext();

                        var actionContext = new ActionContext(displayContext.ViewContext.HttpContext, displayContext.ViewContext.RouteData, displayContext.ViewContext.ActionDescriptor);
                        var urlHelper     = _urlHelperFactory.GetUrlHelper(actionContext);

                        context.LocalScope.SetValue("Context", displayContext.ViewContext);
                        context.AmbientValues.Add("UrlHelper", urlHelper);

                        context.LocalScope.SetValue("Model", displayContext.Value);
                        context.MemberAccessStrategy.Register(displayContext.Value.GetType());

                        var htmlContent = await _liquidTemplateManager.RenderAsync(template.Content, context);

                        return(new HtmlString(htmlContent));
                    }
                };

                return(true);
            }
            else
            {
                shapeBinding = null;
                return(false);
            }
        }
示例#14
0
        private bool TryGetDescriptorBinding(string shapeType, IEnumerable <string> shapeAlternates, ShapeTable shapeTable, string bindingType, out ShapeBinding shapeBinding)
        {
            var prefix = bindingType == "Display" ? string.Empty : string.Concat(bindingType, "@");

            // shape alternates are optional, fully qualified binding names
            // the earliest added alternates have the lowest priority
            // the descriptor returned is based on the binding that is matched, so it may be an entirely
            // different descriptor if the alternate has a different base name
            foreach (var shapeAlternate in shapeAlternates.Reverse())
            {
                foreach (var shapeBindingResolver in _shapeBindingResolvers)
                {
                    if (shapeBindingResolver.TryGetDescriptorBinding(prefix + shapeAlternate, out shapeBinding))
                    {
                        return(true);
                    }
                }

                if (shapeTable.Bindings.TryGetValue(prefix + shapeAlternate, out shapeBinding))
                {
                    return(true);
                }
            }

            // when no alternates match, the shapeType is used to find the longest matching binding
            // the shapetype name can break itself into shorter fallbacks at double-underscore marks
            // so the shapetype itself may contain a longer alternate forms that falls back to a shorter one
            var shapeTypeScan = shapeType;

            for (; ;)
            {
                foreach (var shapeBindingResolver in _shapeBindingResolvers)
                {
                    if (shapeBindingResolver.TryGetDescriptorBinding(prefix + shapeTypeScan, out shapeBinding))
                    {
                        return(true);
                    }
                }

                if (shapeTable.Bindings.TryGetValue(prefix + shapeTypeScan, out shapeBinding))
                {
                    return(true);
                }

                var delimiterIndex = shapeTypeScan.LastIndexOf("__");
                if (delimiterIndex < 0)
                {
                    shapeBinding = null;
                    return(false);
                }

                shapeTypeScan = shapeTypeScan.Substring(0, delimiterIndex);
            }
        }
示例#15
0
 private static ValueTask <IHtmlContent> ProcessAsync(ShapeBinding shapeBinding, IShape shape, DisplayContext context)
 {
        private static bool TryGetDescriptorBinding(string shapeType, IEnumerable <string> shapeAlternates, ShapeTable shapeTable, out ShapeBinding shapeBinding)
        {
            foreach (var shapeAlternate in shapeAlternates.Reverse())
            {
                if (shapeTable.Bindings.TryGetValue(shapeAlternate, out shapeBinding))
                {
                    return(true);
                }
            }

            var shapeTypeScan = shapeType;

            for (; ;)
            {
                if (shapeTable.Bindings.TryGetValue(shapeTypeScan, out shapeBinding))
                {
                    return(true);
                }

                var delimiterIndex = shapeTypeScan.LastIndexOf("__", StringComparison.Ordinal);
                if (delimiterIndex < 0)
                {
                    return(false);
                }

                shapeTypeScan = shapeTypeScan.Substring(0, delimiterIndex);
            }
        }