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)));
 }
Exemple #2
0
        public IHtmlString ShapeExecute(object shape)
        {
            if (shape == null)
            {
                return(new HtmlString(string.Empty));
            }

            var context = new DisplayContext {
                Display = this, Value = shape, ViewContext = ViewContext, ViewDataContainer = ViewDataContainer
            };

            return(_displayManager.Execute(context));
        }
        public IHtmlString Execute(DisplayContext context)
        {
            var shape = _convertAsShapeCallsite.Target(_convertAsShapeCallsite, context.Value);

            // non-shape arguments are returned as a no-op
            if (shape == null)
            {
                return(CoerceHtmlString(context.Value));
            }

            var shapeMetadata = shape.Metadata;

            // can't really cope with a shape that has no type information
            if (shapeMetadata == null || string.IsNullOrEmpty(shapeMetadata.Type))
            {
                return(CoerceHtmlString(context.Value));
            }

            var workContext = _workContextAccessor.GetContext();
            // CurrentTheme is now available in the background, so here we no longer use IsBackgroundContext().
            // We only do a null check, so we can render in the background a view that only exists in the theme.
            var shapeTable = _httpContextAccessor.Current() != null
                ? _shapeTableLocator.Value.Lookup(workContext.CurrentTheme.Id)
                : _shapeTableLocator.Value.Lookup(null);

            var displayingContext = new ShapeDisplayingContext {
                Shape         = shape,
                ShapeMetadata = shapeMetadata
            };

            _shapeDisplayEvents.Invoke(sde => sde.Displaying(displayingContext), Logger);

            // find base shape association using only the fundamental shape type.
            // alternates that may already be registered do not affect the "displaying" event calls
            ShapeBinding shapeBinding;

            if (TryGetDescriptorBinding(shapeMetadata.Type, Enumerable.Empty <string>(), shapeTable, out shapeBinding))
            {
                shapeBinding.ShapeDescriptor.Displaying.Invoke(action => action(displayingContext), Logger);

                // copy all binding sources (all templates for this shape) in order to use them as Localization scopes
                shapeMetadata.BindingSources = shapeBinding.ShapeDescriptor.BindingSources.Where(x => x != null).ToList();
                if (!shapeMetadata.BindingSources.Any())
                {
                    shapeMetadata.BindingSources.Add(shapeBinding.ShapeDescriptor.BindingSource);
                }
            }

            // invoking ShapeMetadata displaying events
            shapeMetadata.Displaying.Invoke(action => action(displayingContext), Logger);

            // use pre-fectched content if available (e.g. coming from specific cache implmentation)
            if (displayingContext.ChildContent != null)
            {
                shape.Metadata.ChildContent = displayingContext.ChildContent;
            }
            else
            {
                // now find the actual binding to render, taking alternates into account
                ShapeBinding actualBinding;
                if (TryGetDescriptorBinding(shapeMetadata.Type, shapeMetadata.Alternates, shapeTable, out actualBinding))
                {
                    shape.Metadata.ChildContent = Process(actualBinding, shape, context);
                }
                else
                {
                    throw new TomeltException(T("Shape type {0} not found", shapeMetadata.Type));
                }
            }

            foreach (var frameType in shape.Metadata.Wrappers)
            {
                ShapeBinding frameBinding;
                if (TryGetDescriptorBinding(frameType, Enumerable.Empty <string>(), shapeTable, out frameBinding))
                {
                    shape.Metadata.ChildContent = Process(frameBinding, shape, context);
                }
            }

            var displayedContext = new ShapeDisplayedContext {
                Shape         = shape,
                ShapeMetadata = shape.Metadata,
                ChildContent  = shape.Metadata.ChildContent,
            };

            _shapeDisplayEvents.Invoke(sde => {
                var prior = displayedContext.ChildContent = displayedContext.ShapeMetadata.ChildContent;
                sde.Displayed(displayedContext);
                // update the child content if the context variable has been reassigned
                if (prior != displayedContext.ChildContent)
                {
                    displayedContext.ShapeMetadata.ChildContent = displayedContext.ChildContent;
                }
            }, Logger);

            if (shapeBinding != null)
            {
                shapeBinding.ShapeDescriptor.Displayed.Invoke(action => {
                    var prior = displayedContext.ChildContent = displayedContext.ShapeMetadata.ChildContent;
                    action(displayedContext);
                    // update the child content if the context variable has been reassigned
                    if (prior != displayedContext.ChildContent)
                    {
                        displayedContext.ShapeMetadata.ChildContent = displayedContext.ChildContent;
                    }
                }, Logger);
            }

            // invoking ShapeMetadata displayed events
            shapeMetadata.Displayed.Invoke(action => action(displayedContext), Logger);

            return(shape.Metadata.ChildContent);
        }