public async Task DisplayingAsync(ShapeDisplayContext context) { // TODO: replace with configurable UI var debugMode = false; // The shape has cache settings and no content yet if (context.ShapeMetadata.IsCached && context.ChildContent == null) { var cacheContext = context.ShapeMetadata.Cache(); _cacheScopeManager.EnterScope(cacheContext); _openScopes[cacheContext.CacheId] = cacheContext; var cachedContent = await _dynamicCacheService.GetCachedValueAsync(cacheContext); if (cachedContent != null) { // The contents of this shape was found in the cache. // Add the cacheContext to _cached so that we don't try to cache the content again in the DisplayedAsync method. _cached[cacheContext.CacheId] = cacheContext; context.ChildContent = new HtmlString(cachedContent); } else if (debugMode) { context.ShapeMetadata.Wrappers.Add("CachedShapeWrapper"); } } }
public Task DisplayingAsync(ShapeDisplayContext context) { var timing = StackExchange.Profiling.MiniProfiler.Current.Step($"Shape: {context.ShapeMetadata.Type}"); _timings.Add(context, timing); return(Task.CompletedTask); }
public async Task DisplayedAsync(ShapeDisplayContext context) { var cacheContext = context.ShapeMetadata.Cache(); // If the shape is not configured to be cached, continue as usual if (cacheContext == null) { if (context.ChildContent == null) { context.ChildContent = HtmlString.Empty; } return; } // If we have got this far, then this shape is configured to be cached. // We need to determine whether or not the ChildContent of this shape was retrieved from the cache by the DisplayingAsync method above, as opposed to generated by the View Engine. // ChildContent will be generated by the View Engine if it was not available in the cache when we rendered the shape. // In this instance, we need insert the ChildContent into the cache so that subsequent attempt to render this shape can take advantage of the cached content. // If the ChildContent was retrieved form the cache, then the Cache Context will be present in the _cached collection (see the DisplayingAsync method in this class). // So, if the cache context is not present in the _cached collection, we need to insert the ChildContent value into the cache: if (!_cached.ContainsKey(cacheContext.CacheId) && context.ChildContent != null) { // The content is pre-encoded in the cache so we don't have to do it every time it's rendered using (var sw = new StringWriter()) { context.ChildContent.WriteTo(sw, HtmlEncoder.Default); await _dynamicCacheService.SetCachedValueAsync(cacheContext, sw.ToString()); } } }
private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as LiquidPartViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); model.Html = await liquidTemplateManager.RenderAsync(model.LiquidPart.Liquid, _htmlEncoder, shapeDisplayContext.DisplayContext.Value, scope => scope.SetValue("ContentItem", model.ContentItem)); }
private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as FacebookPluginPartViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); model.Html = await liquidTemplateManager.RenderStringAsync(model.FacebookPluginPart.Liquid, _htmlEncoder, shapeDisplayContext.DisplayContext.Value, new Dictionary <string, FluidValue>() { ["ContentItem"] = new ObjectValue(model.ContentItem) }); }
public void Displayed(ShapeDisplayContext context) { // TODO: Configure duration of sliding expiration var cacheContext = context.ShapeMetadata.Cache(); // If the shape is not cached, evaluate the ESIs if (cacheContext == null) { string content; using (var sw = new StringWriter()) { context.ChildContent.WriteTo(sw, HtmlEncoder.Default); content = sw.ToString(); } ProcessESIs(ref content, GetDistributedCache); context.ChildContent = new HtmlString(content); } else if (!_cached.Contains(cacheContext) && context.ChildContent != null) { var cacheEntries = GetCacheEntries(cacheContext).ToList(); string cacheKey = GetCacheKey(cacheContext.CacheId, cacheEntries); using (var sw = new StringWriter()) { context.ChildContent.WriteTo(sw, HtmlEncoder.Default); var content = sw.ToString(); _cached.Add(cacheContext); _cache[cacheKey] = content; var contexts = String.Join(ContextSeparator.ToString(), cacheContext.Contexts.ToArray()); context.ChildContent = new HtmlString($"[[cache id='{cacheContext.CacheId}' contexts='{contexts}']]"); var bytes = Encoding.UTF8.GetBytes(content); // Default duration is sliding expiration (permanent as long as it's used) DistributedCacheEntryOptions options = new DistributedCacheEntryOptions { SlidingExpiration = new TimeSpan(0, 1, 0) }; // If a custom duration is specified, replace the default options if (cacheContext.Duration.HasValue) { options = new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = cacheContext.Duration }; } _dynamicCache.SetAsync(cacheKey, bytes, options).Wait(); _tagCache.Tag(cacheKey, cacheContext.Tags.ToArray()); } } }
private static async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as WorkDocumentPartViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); model.Html = await liquidTemplateManager.RenderAsync(model.Body, HtmlEncoder.Default, shapeDisplayContext.DisplayContext.Value, scope => scope.SetValue("ContentItem", model.ContentItem)); }
public Task DisplayingFinalizedAsync(ShapeDisplayContext context) { if (_timings.TryGetValue(context, out IDisposable timing)) { _timings.Remove(context); timing.Dispose(); } return(Task.CompletedTask); }
public Task DisplayingFinalizedAsync(ShapeDisplayContext context) { var cacheContext = context.ShapeMetadata.Cache(); if (cacheContext != null && _openScopes.ContainsKey(cacheContext.CacheId)) { _cacheScopeManager.ExitScope(); _openScopes.Remove(cacheContext.CacheId); } return(Task.CompletedTask); }
private async Task <Type> GetComponentTypeAsync(IShape shape) { var shapeTable = _shapeTableManager.GetShapeTable(); var shapeMetadata = shape.Metadata; var displayContext = new ShapeDisplayContext { Shape = shape, ServiceProvider = _serviceProvider }; if (shapeTable.Descriptors.TryGetValue(shapeMetadata.Type, out var shapeDescriptor)) { await shapeDescriptor.DisplayingAsync.InvokeAsync( (action, displayContext) => action(displayContext), displayContext, _logger); } // Invoking ShapeMetadata displaying events. shapeMetadata.Displaying.Invoke(action => action(displayContext), _logger); var shapeTypes = shapeMetadata.Alternates.Reverse().Concat(new[] { shape.Metadata.Type }).ToList(); var query = from shapeType in shapeTypes from b in shapeTable.ShapeBindings where b.ShapeType == shapeType select b; var binding = query.FirstOrDefault(); if (binding == null) { throw new Exception($"No component binding found for shape {shape.Metadata.Type}."); } if (shapeDescriptor != null) { await shapeDescriptor.DisplayedAsync.InvokeAsync( (action, displayContext) => action(displayContext), displayContext, _logger); } // Invoking ShapeMetadata displayed events. shapeMetadata.Displayed.Invoke((action, displayContext) => action(displayContext), displayContext, _logger); return(binding.ComponentType); }
private async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as LiquidPartViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); var liquidPart = model.LiquidPart; var templateContext = new TemplateContext(); templateContext.SetValue("ContentItem", liquidPart.ContentItem); templateContext.MemberAccessStrategy.Register <LiquidPartViewModel>(); await templateContext.ContextualizeAsync(shapeDisplayContext.DisplayContext); model.Html = await liquidTemplateManager.RenderAsync(liquidPart.Liquid, _htmlEncoder, templateContext); }
private static async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as FacebookPluginPartViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); var part = model.FacebookPluginPart; var templateContext = new TemplateContext(); templateContext.SetValue("ContentItem", part.ContentItem); templateContext.MemberAccessStrategy.Register <FacebookPluginPartViewModel>(); await templateContext.ContextualizeAsync(shapeDisplayContext.DisplayContext); model.Html = await liquidTemplateManager.RenderAsync(part.Liquid, templateContext); model.Liquid = part.Liquid; model.FacebookPluginPart = part; model.ContentItem = part.ContentItem; }
public void Displaying(ShapeDisplayContext context) { if (context.ShapeMetadata.IsCached && context.ChildContent == null) { var cacheContext = context.ShapeMetadata.Cache(); var cacheEntries = GetCacheEntries(cacheContext).ToList(); string cacheKey = GetCacheKey(cacheContext.CacheId, cacheEntries); var content = GetDistributedCache(cacheKey); if (content != null) { if (ProcessESIs(ref content, GetDistributedCache)) { _cached.Add(cacheContext); var contexts = String.Join(ContextSeparator.ToString(), cacheContext.Contexts.ToArray()); context.ChildContent = new HtmlString($"[[cache id='{cacheContext.CacheId}' contexts='{contexts}']]"); } } } }
private static async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as LiquidPartViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); var liquidPart = model.LiquidPart; var templateContext = new TemplateContext(); templateContext.SetValue("ContentItem", liquidPart.ContentItem); templateContext.MemberAccessStrategy.Register <LiquidPartViewModel>(); await templateContext.ContextualizeAsync(shapeDisplayContext.DisplayContext); using (var writer = new StringWriter()) { await liquidTemplateManager.RenderAsync(liquidPart.Liquid, writer, HtmlEncoder.Default, templateContext); model.Html = writer.ToString(); } model.Liquid = liquidPart.Liquid; model.LiquidPart = liquidPart; model.ContentItem = liquidPart.ContentItem; }
private static async Task BuildViewModelAsync(ShapeDisplayContext shapeDisplayContext) { var model = shapeDisplayContext.Shape as FreeFormLiquidProfileViewModel; var liquidTemplateManager = shapeDisplayContext.ServiceProvider.GetRequiredService <ILiquidTemplateManager>(); // var liquidPart = model.LiquidPart; // var templateContext = new TemplateContext(); // templateContext.SetValue("ContentItem", liquidPart.ContentItem); var templateContext = liquidTemplateManager.Context; templateContext.SetValue("Profile", model.FreeFormLiquidProfile); templateContext.SetValue("QueryResult", model.FreeFormLiquidProfile.QueryResult); templateContext.MemberAccessStrategy.Register <FreeFormLiquidProfileViewModel>(); //await templateContext.ContextualizeAsync(shapeDisplayContext.DisplayContext); if (model != null) { model.Html = await liquidTemplateManager.RenderAsync(model.FreeFormLiquidProfile.Liquid, HtmlEncoder.Default, templateContext); } // model.Liquid = liquidPart.Liquid; // model.LiquidPart = liquidPart; // model.ContentItem = liquidPart.ContentItem; }
public virtual void Displayed(ShapeDisplayContext context) { }
void IShapeDisplayEvents.Displayed(ShapeDisplayContext context) { Displayed(context); }
void IShapeDisplayEvents.Displaying(ShapeDisplayContext context) { Displaying(context); }
Task IShapeDisplayEvents.DisplayingAsync(ShapeDisplayContext context) { Displaying(context); return(Task.CompletedTask); }
Task IShapeDisplayEvents.DisplayingFinalizedAsync(ShapeDisplayContext context) { Finalized(context); return(Task.CompletedTask); }
private async Task PopulateListPartSearchOptions(ListPartViewModel model, ShapeDisplayContext context) { //We populate the SelectLists model.ListPartOptions.ContentStatuses = new List <SelectListItem>() { new SelectListItem() { Text = S["Latest"], Value = nameof(ContentsStatus.Latest) }, new SelectListItem() { Text = S["Owned by me"], Value = nameof(ContentsStatus.Owner) }, new SelectListItem() { Text = S["Published"], Value = nameof(ContentsStatus.Published) }, new SelectListItem() { Text = S["Unpublished"], Value = nameof(ContentsStatus.Draft) }, new SelectListItem() { Text = S["All versions"], Value = nameof(ContentsStatus.AllVersions) } }; // model.ListPartOptions.ContentSorts = new List<SelectListItem>() { // new SelectListItem() { Text = S["Recently created"], Value = nameof(ContentsOrder.Created) }, // new SelectListItem() { Text = S["Recently modified"], Value = nameof(ContentsOrder.Modified) }, // new SelectListItem() { Text = S["Recently published"], Value = nameof(ContentsOrder.Published) }, // new SelectListItem() { Text = S["Title"], Value = nameof(ContentsOrder.Title) } // }; if (model.DataFillMode == DataFillMode.ContentTypes) { model.ListPartOptions.ContentTypeOptions = new List <SelectListItem>(); model.ListPartOptions.ContentTypeOptions.Add(new SelectListItem() { Text = S["All content types"], Value = "" }); if (model.ContainedContentTypeDefinitions != null) { foreach (var option in model.ContainedContentTypeDefinitions) { model.ListPartOptions.ContentTypeOptions.Add(new SelectListItem() { Text = option.DisplayName, Value = option.Name }); } } } else if (model.DataFillMode == DataFillMode.WorkMaps) { model.ListPartOptions.ContentTypeOptions = new List <SelectListItem>(); model.ListPartOptions.ContentTypeOptions.Add(new SelectListItem() { Text = S["All content types"], Value = "" }); /*for classroom content types * * 1. get assigned workmaps (eg μαθηματικά γυμνασιου, επικοινωνίες) ListPartViewModel.SelectedWorkMapContentItemIds * 2. for each workmap get contained content types WorkMapPart.WorkMapTerms.each get contenttype * 2.1 if context type is shortcut get the content type assigned to shortcut content item instance * 3. end result will be a list of content types * 4. store this list at the content item (class room ) level to show as options in search picklist see PartyPartHandler to save at part level when saving the contentitem */ var contentManager = context.ServiceProvider.GetService <IContentManager>(); var selectedworkMapContentItems = model.SelectedWorkMapContentItems; var containedContentTypes = new List <string>(); foreach (var selectedworkMapContentItem in selectedworkMapContentItems) { var workMapPart = selectedworkMapContentItem.Content.WorkMapPart; // .As<WorkMapPart>(); if (workMapPart != null) { var workMapTerms = workMapPart?.WorkMapTerms as JArray; if (workMapTerms != null) { foreach (var workmapterm in workMapTerms) { await WalkTreeNode(workmapterm, containedContentTypes, contentManager); /*var workMapTerm = (dynamic) workmapterm; * var termContentType = workMapTerm?.ContentType?.ToString(); * if (termContentType == "ShortCut") * { * //get shortcut contentitem ids and from there its content type and add to array * //FindTerm(workmap.Content.WorkMapPart.WorkMapTerms as JArray, workmaptermContentItemId); * string[] shortcutsContentItemIds = workMapTerm?.ShortCut?.ContentItem?.ContentItemIds?.ToObject<string[]>(); * var shortCutContentItems = (await contentManager.GetAsync(shortcutsContentItemIds))?.ToArray(); * if (shortCutContentItems != null) * { * if (shortCutContentItems.Any()) * { * foreach (var contentItem in shortCutContentItems) * { * containedContentTypes.Add(contentItem.ContentType); * } * } * } * * * } * else * { * * containedContentTypes.Add(termContentType); * }*/ } } } } //get final contained types for workmaps var contentDefinitionManager = context.ServiceProvider.GetService <IContentDefinitionManager>(); containedContentTypes.Add("Post"); //also add post by default var distinctContainedContentTypes = containedContentTypes.Except(new [] { "PlaceHolder" }).Distinct().ToArray(); //dont show the folder content type in picklist var workMapContentTypes = GetContainedContentTypes(distinctContainedContentTypes, contentDefinitionManager); if (workMapContentTypes != null) { foreach (var option in workMapContentTypes) { model.ListPartOptions.ContentTypeOptions.Add( new SelectListItem() { Text = option.DisplayName, Value = option.Name }); } } } }