public void ShapeTableCreated(ShapeTable shapeTable) { var typeDefinitions = _contentDefinitionManager.Value.ListTypeDefinitions(); var allPlacements = typeDefinitions.SelectMany(td => td.GetPlacement(PlacementType.Editor).Select(p => new TypePlacement { Placement = p, ContentType = td.Name })); // group all placement settings by shape type var shapePlacements = allPlacements.GroupBy(x => x.Placement.ShapeType).ToDictionary(x => x.Key, y => y.ToList(), StringComparer.OrdinalIgnoreCase); // create a new predicate in a ShapeTableDescriptor has a custom placement foreach (var shapeType in shapeTable.Descriptors.Keys) { List <TypePlacement> customPlacements; if (shapePlacements.TryGetValue(shapeType, out customPlacements)) { var descriptor = shapeTable.Descriptors[shapeType]; // there are some custom placements, build a predicate var placement = descriptor.Placement; if (!customPlacements.Any()) { continue; } descriptor.Placement = ctx => { var workContext = _workContextAccessor.GetContext(); if (ctx.DisplayType == null && AdminFilter.IsApplied(workContext.HttpContext.Request.RequestContext)) // Tests if it's executing in admin in order to override placement.info for editors in back-end only { foreach (var customPlacement in customPlacements) { var type = customPlacement.ContentType; var differentiator = customPlacement.Placement.Differentiator; if (((ctx.Differentiator ?? String.Empty) == (differentiator ?? String.Empty)) && ctx.ContentType == type) { var location = customPlacement.Placement.Zone; if (!String.IsNullOrEmpty(customPlacement.Placement.Position)) { location = String.Concat(location, ":", customPlacement.Placement.Position); } // clone the identified Placement.info into a new one in order to keep original informations like Wrappers and Alternates var originalPlacementInfo = placement(ctx); return(new PlacementInfo { Location = location, Alternates = originalPlacementInfo.Alternates, ShapeType = originalPlacementInfo.ShapeType, Wrappers = originalPlacementInfo.Wrappers }); } } } return(placement(ctx)); }; } } }
private static PlacementInfo FindPlacementImpl(ShapeTable shapeTable, string shapeType, string differentiator, string displayType, IBuildShapeContext context) { var delimiterIndex = shapeType.IndexOf("__"); if (delimiterIndex > 0) { shapeType = shapeType.Substring(0, delimiterIndex); } if (shapeTable.Descriptors.TryGetValue(shapeType, out var descriptor)) { var placementContext = new ShapePlacementContext( shapeType, displayType, differentiator, context.Shape ); var placement = descriptor.Placement(placementContext); if (placement != null) { placement.Source = placementContext.Source; return(placement); } } return(null); }
public UpdateEditorContext(IShape model, IContent content, IUpdateModel updater, string groupInfoId, IShapeFactory shapeFactory, ShapeTable shapeTable, string path) : base(model, content, groupInfoId, shapeFactory) { ShapeTable = shapeTable; Updater = updater; Path = path; }
public void ShapeTableCreated(ShapeTable shapeTable) { foreach (var descriptor in shapeTable.Descriptors.Values) { var existingPlacement = descriptor.Placement; descriptor.Placement = ctx => { var placements = GetPlacements(); if (!placements.ContainsKey(descriptor.ShapeType)) { return(existingPlacement(ctx)); } var declarations = placements[descriptor.ShapeType]; foreach (var declaration in declarations) { if (declaration.Predicate(ctx)) { return(declaration.Placement); } } return(existingPlacement(ctx)); }; } }
public DefaultDisplayManagerTests() { _defaultShapeTable = new ShapeTable ( new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) ); _additionalBindings = new TestShapeBindingsDictionary(); IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <IThemeManager, ThemeManager>(); serviceCollection.AddScoped <IHtmlDisplay, DefaultHtmlDisplay>(); serviceCollection.AddScoped <IShapeTableManager, TestShapeTableManager>(); serviceCollection.AddScoped <IShapeBindingResolver, TestShapeBindingResolver>(); serviceCollection.AddScoped <IShapeDisplayEvents, TestDisplayEvents>(); serviceCollection.AddScoped <IExtensionManager, StubExtensionManager>(); serviceCollection.AddSingleton <IStringLocalizerFactory, NullStringLocalizerFactory>(); serviceCollection.AddTransient(typeof(IStringLocalizer <>), typeof(StringLocalizer <>)); serviceCollection.AddLogging(); serviceCollection.AddSingleton(_defaultShapeTable); serviceCollection.AddSingleton(_additionalBindings); serviceCollection.AddWebEncoders(); _serviceProvider = serviceCollection.BuildServiceProvider(); }
public void ShapeTableCreated(ShapeTable shapeTable) { var typeDefinitions = _contentDefinitionManager.Value .ListTypeDefinitions() .Where(ctd => ctd.Parts.Any(ctpd => ctpd.PartDefinition.Name == "ProfilePart")); var allPlacements = typeDefinitions .SelectMany(td => _frontEndProfileService.GetFrontEndPlacement(td) .Select(p => new TypePlacement { Placement = p, ContentType = td.Name })); // group all placement settings by shape type var shapePlacements = allPlacements .GroupBy(x => x.Placement.ShapeType) .ToDictionary(x => x.Key, y => y.ToList()); foreach (var shapeType in shapeTable.Descriptors.Keys) { List <TypePlacement> customPlacements; if (shapePlacements.TryGetValue(shapeType, out customPlacements)) { if (!customPlacements.Any()) { continue; } // there are some custom placements, build a predicate var descriptor = shapeTable.Descriptors[shapeType]; var placement = descriptor.Placement; descriptor.Placement = ctx => { var WorkContext = _workContextAccessor.GetContext(); //I need the context for the call using the predicates if (ctx.DisplayType == null && !AdminFilter.IsApplied(WorkContext.HttpContext.Request.RequestContext)) { foreach (var customPlacement in customPlacements) { var type = customPlacement.ContentType; var differentiator = customPlacement.Placement.Differentiator; if (((ctx.Differentiator ?? string.Empty) == (differentiator ?? string.Empty)) && ctx.ContentType == type) { var location = customPlacement.Placement.Zone; if (!string.IsNullOrEmpty(customPlacement.Placement.Position)) { location = string.Concat(location, ":", customPlacement.Placement.Position); } return(new PlacementInfo { Location = location }); } } } //fallback return(placement(ctx)); }; } } }
private ShapeTable GetShapeTable() { if (_scopedShapeTable == null) { var theme = _themeManager.GetThemeAsync().GetAwaiter().GetResult(); _scopedShapeTable = _shapeTableManager.GetShapeTable(theme?.Id); } return(_scopedShapeTable); }
private async Task <ShapeTable> GetShapeTableAsync() { if (_scopedShapeTable == null) { var theme = await _themeManager.GetThemeAsync(); _scopedShapeTable = _shapeTableManager.GetShapeTable(theme?.Id); } return(_scopedShapeTable); }
public void ShapeTableCreated(ShapeTable shapeTable) { var typeDefinitions = _contentDefinitionManager.Value.ListTypeDefinitions(); var allPlacements = typeDefinitions.SelectMany(td => td.GetPlacement(PlacementType.Editor).Select(p => new TypePlacement { Placement = p, ContentType = td.Name })); // group all placement settings by shape type var shapePlacements = allPlacements.GroupBy(x => x.Placement.ShapeType).ToDictionary(x => x.Key, y => y.ToList()); // create a new predicate in a ShapeTableDescriptor has a custom placement foreach (var shapeType in shapeTable.Descriptors.Keys) { List <TypePlacement> customPlacements; if (shapePlacements.TryGetValue(shapeType, out customPlacements)) { var descriptor = shapeTable.Descriptors[shapeType]; // there are some custom placements, build a predicate var placement = descriptor.Placement; if (!customPlacements.Any()) { continue; } descriptor.Placement = ctx => { if (ctx.DisplayType == null) { foreach (var customPlacement in customPlacements) { var type = customPlacement.ContentType; var differentiator = customPlacement.Placement.Differentiator; if (((ctx.Differentiator ?? String.Empty) == (differentiator ?? String.Empty)) && ctx.ContentType == type) { var location = customPlacement.Placement.Zone; if (!String.IsNullOrEmpty(customPlacement.Placement.Position)) { location = String.Concat(location, ":", customPlacement.Placement.Position); } return(new PlacementInfo { Location = location }); } } } return(placement(ctx)); }; } } }
private ShapeDescriptor GetShapeDescriptor(string shapeType, ShapeTable shapeTable) { var shapeTypeScan = shapeType; do { if (shapeTable.Descriptors.TryGetValue(shapeTypeScan, out var shapeDescriptor)) { return(shapeDescriptor); } }while (TryGetParentShapeTypeName(ref shapeTypeScan)); return(null); }
static ShapeFactoryBenchmark() { _templateContext = new TemplateContext(); var defaultShapeTable = new ShapeTable ( new Dictionary <string, ShapeDescriptor>(), new Dictionary <string, ShapeBinding>() ); var shapeFactory = new DefaultShapeFactory( events: Enumerable.Empty <IShapeFactoryEvents>(), shapeTableManager: new TestShapeTableManager(defaultShapeTable), themeManager: new MockThemeManager(new ExtensionInfo("path", new ManifestInfo(new ModuleAttribute()), (x, y) => Enumerable.Empty <IFeatureInfo>()))); _templateContext.AmbientValues["DisplayHelper"] = new DisplayHelper(null, shapeFactory, null); }
protected override void Register(ContainerBuilder builder) { var defaultShapeTable = new ShapeTable { Descriptors = new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), Bindings = new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) }; builder.Register(ctx => defaultShapeTable); builder.RegisterType <DefaultDisplayManagerTests.TestWorkContextAccessor>().As <IWorkContextAccessor>(); builder.RegisterType <DefaultShapeFactory>().As <IShapeFactory>(); builder.RegisterType <DefaultShapeTableManager>().As <IShapeTableManager>(); builder.RegisterType <LayoutWorkContext>().As <IWorkContextStateProvider>(); builder.RegisterType <ShapeTableLocator>().As <IShapeTableLocator>(); builder.RegisterType <DefaultDisplayManager>().As <IDisplayManager>(); builder.RegisterType <DefaultDisplayManagerTests.TestShapeTableManager>().As <IShapeTableManager>(); builder.RegisterType <CoreShapes>().As <IShapeTableProvider>(); }
public ShapeFactoryTests() { IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <ILoggerFactory, StubLoggerFactory>(); serviceCollection.AddScoped <IShapeFactory, DefaultShapeFactory>(); serviceCollection.AddScoped <IExtensionManager, StubExtensionManager>(); serviceCollection.AddScoped <IShapeTableManager, TestShapeTableManager>(); var defaultShapeTable = new ShapeTable { Descriptors = new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), Bindings = new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) }; serviceCollection.AddInstance(defaultShapeTable); _serviceProvider = serviceCollection.BuildServiceProvider(); }
private static ShapeDescriptor GetShapeDescriptor(string shapeType, ShapeTable shapeTable) { // Note: The shape type of a descriptor is a fundamental shape type that never contains // any '__' separator. If a shape type contains some '__' separators, its fundamental // shape type is the left part just before the 1st occurrence of the '__' separator. // As a fast path we 1st use the shapeType as is but it may contain some '__'. if (!shapeTable.Descriptors.TryGetValue(shapeType, out var shapeDescriptor)) { // Check if not a fundamental type. var index = shapeType.IndexOf("__", StringComparison.Ordinal); if (index > 0) { // Try again by using the fundamental shape type without any '__' separator. shapeTable.Descriptors.TryGetValue(shapeType.Substring(0, index), out shapeDescriptor); } } return(shapeDescriptor); }
public ShapeHelperTests() { IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddLogging(); serviceCollection.AddScoped <IHtmlDisplay, DefaultHtmlDisplay>(); serviceCollection.AddScoped <IExtensionManager, StubExtensionManager>(); serviceCollection.AddScoped <IThemeManager, ThemeManager>(); serviceCollection.AddScoped <IShapeFactory, DefaultShapeFactory>(); serviceCollection.AddScoped <IShapeTableManager, TestShapeTableManager>(); var defaultShapeTable = new ShapeTable ( new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) ); serviceCollection.AddSingleton(defaultShapeTable); _serviceProvider = serviceCollection.BuildServiceProvider(); }
public DefaultDisplayManagerTests() { _defaultShapeTable = new ShapeTable { Descriptors = new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), Bindings = new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) }; IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <ILoggerFactory, StubLoggerFactory>(); serviceCollection.AddScoped <IHttpContextAccessor, StubHttpContextAccessor>(); serviceCollection.AddScoped <IDisplayManager, DefaultDisplayManager>(); serviceCollection.AddScoped <IShapeTableManager, TestShapeTableManager>(); serviceCollection.AddScoped <IShapeDisplayEvents, TestDisplayEvents>(); serviceCollection.AddScoped <IExtensionManager, StubExtensionManager>(); serviceCollection.AddScoped <IEventBus, StubEventBus>(); serviceCollection.AddInstance(_defaultShapeTable); _serviceProvider = serviceCollection.BuildServiceProvider(); }
public ShapeHelperTests() { IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddScoped <ILoggerFactory, StubLoggerFactory>(); serviceCollection.AddScoped <IHttpContextAccessor, StubHttpContextAccessor>(); serviceCollection.AddScoped <IHtmlDisplay, DefaultIHtmlDisplay>(); serviceCollection.AddScoped <IExtensionManager, StubExtensionManager>(); serviceCollection.AddScoped <IShapeFactory, DefaultShapeFactory>(); serviceCollection.AddScoped <IShapeTableManager, TestShapeTableManager>(); var defaultShapeTable = new ShapeTable { Descriptors = new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), Bindings = new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) }; serviceCollection.AddSingleton(defaultShapeTable); _serviceProvider = serviceCollection.BuildServiceProvider(); }
private static PlacementInfo FindPlacementImpl(ShapeTable shapeTable, string shapeType, string differentiator, string displayType, IBuildShapeContext context) { ShapeDescriptor descriptor; if (shapeTable.Descriptors.TryGetValue(shapeType, out descriptor)) { var placementContext = new ShapePlacementContext( shapeType, displayType, differentiator, context.Shape ); var placement = descriptor.Placement(placementContext); if (placement != null) { placement.Source = placementContext.Source; return(placement); } } return(null); }
protected override void Register(Autofac.ContainerBuilder builder) { _defaultShapeTable = new ShapeTable { Descriptors = new Dictionary <string, ShapeDescriptor>(StringComparer.OrdinalIgnoreCase), Bindings = new Dictionary <string, ShapeBinding>(StringComparer.OrdinalIgnoreCase) }; _workContext = new TestWorkContext { CurrentTheme = new ExtensionDescriptor { Id = "Hello" } }; builder.RegisterType <DefaultDisplayManager>().As <IDisplayManager>(); builder.RegisterType <TestShapeTableManager>().As <IShapeTableManager>(); builder.RegisterType <ShapeTableLocator>().As <IShapeTableLocator>(); builder.RegisterType <TestWorkContextAccessor>().As <IWorkContextAccessor>(); builder.RegisterType <TestDisplayEvents>().As <IShapeDisplayEvents>() .As <TestDisplayEvents>() .InstancePerLifetimeScope(); builder.Register(ctx => _defaultShapeTable); builder.Register(ctx => _workContext); }
private static PlacementInfo FindPlacementImpl(ShapeTable shapeTable, IShape shape, string differentiator, string displayType) { ShapeDescriptor descriptor; var shapeType = shape.Metadata.Type; if (shapeTable.Descriptors.TryGetValue(shapeType, out descriptor)) { var placementContext = new ShapePlacementContext { Shape = shape, DisplayType = displayType, Differentiator = differentiator }; var placement = descriptor.Placement(placementContext); if (placement != null) { placement.Source = placementContext.Source; return(placement); } } return(null); }
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); } }
public TestShapeTableManager(ShapeTable defaultShapeTable) { _defaultShapeTable = defaultShapeTable; }
private async Task <ShapeBinding> GetDescriptorBindingAsync(string shapeType, IEnumerable <string> shapeAlternates, ShapeTable shapeTable) { // 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) { var binding = await shapeBindingResolver.GetDescriptorBindingAsync(shapeAlternate); if (binding != null) { return(binding); } } if (shapeTable.Bindings.TryGetValue(shapeAlternate, out var shapeBinding)) { return(shapeBinding); } } // 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) { var binding = await shapeBindingResolver.GetDescriptorBindingAsync(shapeTypeScan); if (binding != null) { return(binding); } } if (shapeTable.Bindings.TryGetValue(shapeTypeScan, out var shapeBinding)) { return(shapeBinding); } var delimiterIndex = shapeTypeScan.LastIndexOf("__"); if (delimiterIndex < 0) { return(null); } shapeTypeScan = shapeTypeScan.Substring(0, delimiterIndex); } }
private async Task <ShapeBinding> GetShapeBindingAsync(string shapeType, AlternatesCollection shapeAlternates, ShapeTable shapeTable) { // 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 for (var i = shapeAlternates.Count - 1; i >= 0; i--) { var shapeAlternate = shapeAlternates[i]; foreach (var shapeBindingResolver in _shapeBindingResolvers) { var binding = await shapeBindingResolver.GetShapeBindingAsync(shapeAlternate); if (binding != null) { return(binding); } } if (shapeTable.Bindings.TryGetValue(shapeAlternate, out var shapeBinding)) { return(shapeBinding); } } // 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; do { foreach (var shapeBindingResolver in _shapeBindingResolvers) { var binding = await shapeBindingResolver.GetShapeBindingAsync(shapeTypeScan); if (binding != null) { return(binding); } } if (shapeTable.Bindings.TryGetValue(shapeTypeScan, out var shapeBinding)) { return(shapeBinding); } }while (TryGetParentShapeTypeName(ref shapeTypeScan)); return(null); }
public UpdateContentEditorContext(IShape model, IContent content, string displayType, IUpdateModel updater, string groupId, IShapeFactory shapeFactory, ShapeTable shapeTable, ModelShapeContext parentContext = null) : base(model, content, updater, groupId, shapeFactory, shapeTable) { DisplayType = displayType; ParentContext = parentContext; }
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); } }