Ejemplo n.º 1
0
        public override ValueTask <bool> EvaluateAsync(JavascriptCondition condition)
        {
            _engine ??= _scriptingManager.GetScriptingEngine("js");
            _scope ??= _engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), _serviceProvider, null, null);

            return(Convert.ToBoolean(_engine.Evaluate(_scope, condition.Script)) ? True : False);
        }
Ejemplo n.º 2
0
        public async Task OnResultExecutionAsync(ResultExecutingContext filterContext, ResultExecutionDelegate next)
        {
            // Should only run on the front-end for a full view
            if ((filterContext.Result is ViewResult || filterContext.Result is PageResult) && !AdminAttribute.IsApplied(filterContext.HttpContext))
            {
                //evaluate all rules provided
                var dynamicResources = await _dynamicResourcesService.GetDynamicResourcesAsync();

                //  string rule = "url('/my-user-home-page')";

                if (dynamicResources.DynamicResources.Any())
                {
                    var engine = _scriptingManager.GetScriptingEngine("js");
                    var scope  = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()),
                                                    ShellScope.Services, null, null);


                    foreach (var rule in dynamicResources.DynamicResources)
                    {
                        if (!string.IsNullOrEmpty(rule.Rule) && rule.Enabled)
                        {
                            var display = Convert.ToBoolean(engine.Evaluate(scope, rule.Rule));
                            if (display)
                            {
                                IEnumerable <string> rawTypes = rule.Resources
                                                                .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();

                                if (rawTypes.Any())
                                {
                                    foreach (string resource in rawTypes)
                                    {
                                        dynamic layout = await _layoutAccessor.GetLayoutAsync();

                                        // Render a mock widget so that its resources are included in the page
                                        var contentItem = await _contentManager.NewAsync(resource);      // ("MessageRoom");

                                        var shape = await _contentItemDisplayManager.BuildEditorAsync(contentItem,
                                                                                                      _modelUpdaterAccessor.ModelUpdater, true, "", Guid.NewGuid().ToString("n"));

                                        //This is a trick.
                                        //AdditionalResourcesDummyZone Zone does not need to be registered in layout
                                        //but this hack makes the zone shape render before the layout is executed , thus registering scripts and styles to thne style and script registries, to be executed later by layout
                                        //only if there was a    @await RenderSectionAsync("AdditionalResourcesDummyZone", required: false) in layout would it be shown in view
                                        layout.Zones["AdditionalResourcesDummyZone"].Add(shape);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            await next.Invoke();
        }
Ejemplo n.º 3
0
        public bool IsVisible(string rule)
        {
            //check LayerFilter to apply caching
            if (String.IsNullOrEmpty(rule))
            {
                return(true);
            }

            var  engine  = _scriptingManager.GetScriptingEngine("js");
            var  scope   = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), _serviceProvider, null, null);
            bool display = Convert.ToBoolean(engine.Evaluate(scope, rule));

            return(display);
        }
Ejemplo n.º 4
0
        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            // Should only run on the front-end for a full view
            if ((context.Result is ViewResult || context.Result is PageResult) &&
                !AdminAttribute.IsApplied(context.HttpContext))
            {
                // Even if the Admin attribute is not applied we might be using the admin theme, for instance in Login views.
                // In this case don't render Layers.
                var selectedTheme = (await _themeManager.GetThemeAsync())?.Id;
                var adminTheme    = await _adminThemeService.GetAdminThemeNameAsync();

                if (selectedTheme == adminTheme)
                {
                    await next.Invoke();

                    return;
                }

                var widgets = await _memoryCache.GetOrCreateAsync("OrchardCore.Layers.LayerFilter:AllWidgets", entry =>
                {
                    entry.AddExpirationToken(_signal.GetToken(LayerMetadataHandler.LayerChangeToken));
                    return(_layerService.GetLayerWidgetsAsync(x => x.Published));
                });

                var layers = (await _layerService.GetLayersAsync()).Layers.ToDictionary(x => x.Name);

                dynamic layout = await _layoutAccessor.GetLayoutAsync();

                var updater = _modelUpdaterAccessor.ModelUpdater;

                var engine = _scriptingManager.GetScriptingEngine("js");
                var scope  = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), _serviceProvider, null, null);

                var layersCache = new Dictionary <string, bool>();

                foreach (var widget in widgets)
                {
                    var layer = layers[widget.Layer];

                    if (layer == null)
                    {
                        continue;
                    }

                    bool display;
                    if (!layersCache.TryGetValue(layer.Name, out display))
                    {
                        if (String.IsNullOrEmpty(layer.Rule))
                        {
                            display = false;
                        }
                        else
                        {
                            display = Convert.ToBoolean(engine.Evaluate(scope, layer.Rule));
                        }

                        layersCache[layer.Rule] = display;
                    }

                    if (!display)
                    {
                        continue;
                    }

                    var widgetContent = await _contentItemDisplayManager.BuildDisplayAsync(widget.ContentItem, updater);

                    widgetContent.Classes.Add("widget");
                    widgetContent.Classes.Add("widget-" + widget.ContentItem.ContentType.HtmlClassify());

                    var wrapper = new WidgetWrapper
                    {
                        Widget  = widget.ContentItem,
                        Content = widgetContent
                    };

                    wrapper.Metadata.Alternates.Add("Widget_Wrapper__" + widget.ContentItem.ContentType);
                    wrapper.Metadata.Alternates.Add("Widget_Wrapper__Zone__" + widget.Zone);

                    var contentZone = layout.Zones[widget.Zone];
                    contentZone.Add(wrapper);
                }
            }

            await next.Invoke();
        }
Ejemplo n.º 5
0
        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            // Should only run on the front-end for a full view
            if (context.Result is ViewResult && !AdminAttribute.IsApplied(context.HttpContext))
            {
                var widgets = await _layerService.GetLayerWidgetsAsync(x => x.Published);

                var layers = (await _layerService.GetLayersAsync()).Layers.ToDictionary(x => x.Name);

                var layout  = _layoutAccessor.GetLayout();
                var updater = _modelUpdaterAccessor.ModelUpdater;

                var engine = _scriptingManager.GetScriptingEngine("js");
                var scope  = engine.CreateScope(_scriptingManager.GlobalMethodProviders.SelectMany(x => x.GetMethods()), _serviceProvider);

                var layersCache = new Dictionary <string, bool>();

                foreach (var widget in widgets)
                {
                    var layer = layers[widget.Layer];

                    if (layer == null)
                    {
                        continue;
                    }

                    bool display;
                    if (!layersCache.TryGetValue(layer.Name, out display))
                    {
                        if (String.IsNullOrEmpty(layer.Rule))
                        {
                            display = false;
                        }
                        else
                        {
                            display = Convert.ToBoolean(engine.Evaluate(scope, layer.Rule));
                        }

                        layersCache[layer.Rule] = display;
                    }

                    if (!display)
                    {
                        continue;
                    }

                    IShape widgetContent = await _contentItemDisplayManager.BuildDisplayAsync(widget.ContentItem, updater);

                    widgetContent.Classes.Add("widget");
                    widgetContent.Classes.Add("widget-" + widget.ContentItem.ContentType.HtmlClassify());

                    var wrapper = _shapeFactory.Create("Widget_Wrapper", new { Widget = widget.ContentItem, Content = widgetContent });
                    wrapper.Metadata.Alternates.Add("Widget_Wrapper__" + widget.ContentItem.ContentType);

                    var contentZone = layout.Zones[widget.Zone];
                    contentZone.Add(wrapper);
                }
            }

            await next.Invoke();
        }