Esempio n. 1
0
        private void ApplyImplementation(BuildDisplayContext context, string displayType)
        {
            var placement = context.FindPlacement(_shapeType, _differentiator, _defaultLocation);

            if (string.IsNullOrEmpty(placement.Location) || placement.Location == "-")
            {
                return;
            }

            // parse group placement
            var group = placement.GetGroup();

            if (!String.IsNullOrEmpty(group))
            {
                _groupId = group;
            }

            if (!string.Equals(context.GroupId ?? "", _groupId ?? "", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            dynamic parentShape = context.Shape;

            // the zone name is in reference of Layout, e.g. /AsideSecond
            if (placement.IsLayoutZone())
            {
                parentShape = context.Layout;
            }

            var position = placement.GetPosition();
            var zone     = placement.GetZone();

            var cachedModel = _cacheService.Get <OutputCachedPartsModel>(_cachedPartMetadata.CacheKey);

            if (cachedModel != null)
            {
                if (String.IsNullOrEmpty(position))
                {
                    parentShape.Zones[zone].Add(context.New.CachedHtml(cachedModel: cachedModel, cacheKey: _cachedPartMetadata.CacheKey));
                }
                else
                {
                    parentShape.Zones[zone].Add(context.New.CachedHtml(cachedModel: cachedModel, cacheKey: _cachedPartMetadata.CacheKey), position);
                }

                return;
            }

            //if we have got this far, then the output does not exist in the cache
            _outputCachedPartsContext.PutCachedPartMetadata(ContentPart, _cachedPartMetadata);
            _driverResultFactory().Apply(context);
        }
        public async Task ApplyAsync(BuildDisplayContext context)
        {
            var displayType = context.DisplayType;

            // If no location is set from the driver, use the one from the context.
            if (String.IsNullOrEmpty(_defaultLocation))
            {
                _defaultLocation = context.DefaultZone;
            }

            // Look into specific implementations of placements (like placement.json files and IShapePlacementProviders)
            var placement = context.FindPlacement(_shapeType, _differentiator, displayType, context);

            // Look for mapped display type locations.
            if (_otherLocations != null)
            {
                if (_otherLocations.TryGetValue(displayType, out var displayTypePlacement))
                {
                    _defaultLocation = displayTypePlacement;
                }
            }

            // If no placement is found, use the default location
            if (placement == null)
            {
                placement = new PlacementInfo
                {
                    Location        = _defaultLocation !,
                    DefaultPosition = _defaultLocation // Temporary until decided whether or not to support zones.
                }
            }
            ;

            if (placement.DefaultPosition == null)
            {
                placement.DefaultPosition = context.DefaultPosition;
            }

            // If there are no placement or it's explicitly noop then stop rendering execution.
            if (string.IsNullOrEmpty(placement.Location) || placement.Location == "-")
            {
                return;
            }

            var newShape = Shape = await _shapeBuilder(context);

            // Ignore if the driver returned a null shape.
            if (newShape == null)
            {
                return;
            }

            var newShapeMetadata = newShape.Metadata;

            newShapeMetadata.Name            = _name ?? _differentiator ?? _shapeType;
            newShapeMetadata.Differentiator  = _differentiator ?? _shapeType;
            newShapeMetadata.DisplayType     = displayType;
            newShapeMetadata.PlacementSource = placement.Source;
            newShapeMetadata.Type            = _shapeType;

            var position = placement.GetPosition();

            position = !string.IsNullOrEmpty(position) ? position : default;

            dynamic parentShape = context.Shape;

            if (parentShape is Shape shape)
            {
                shape.Add(newShape, position);
            }
        }