private SnippetDescriptor DescribeSnippet(dynamic shape)
        {
            // Execute the shape and intercept calls to the Html.SnippetField method.
            var descriptor = new SnippetDescriptor();

            shape.DescriptorRegistrationCallback = (Action <SnippetFieldDescriptor>)(fieldDescriptor => {
                var existingDescriptor = descriptor.Fields.SingleOrDefault(x => x.Name == fieldDescriptor.Name); // Not using Dictionary, as that will break rendering the view for some obscure reason.

                if (existingDescriptor == null)
                {
                    descriptor.Fields.Add(fieldDescriptor);
                }

                if (fieldDescriptor.DisplayName == null)
                {
                    fieldDescriptor.DisplayName = new LocalizedString(fieldDescriptor.Name);
                }
            });

            using (_currentThemeShapeBindingResolver.Value.Enable()) {
                _shapeDisplay.Value.Display(shape);
            }

            shape.SnippetDescriptor = descriptor;
            return(descriptor);
        }
        private SnippetDescriptor ParseSnippetDescriptor(string bindingSource)
        {
            var physicalSourcePath = _wca.GetContext().HttpContext.Server.MapPath(bindingSource);
            var paramsFileName     = Path.Combine(Path.GetDirectoryName(physicalSourcePath) ?? "", Path.GetFileNameWithoutExtension(physicalSourcePath) + ".txt");

            if (!File.Exists(paramsFileName))
            {
                return(null);
            }

            var yaml          = File.ReadAllText(paramsFileName);
            var snippetConfig = Deserialize(yaml);
            var fieldsConfig  = snippetConfig.Fields != null ? snippetConfig.Fields.Children : new dynamic[0];
            var descriptor    = new SnippetDescriptor();

            foreach (var fieldConfig in fieldsConfig)
            {
                descriptor.Fields.Add(new SnippetFieldDescriptor {
                    Name        = (string)fieldConfig.Name,
                    Type        = (string)fieldConfig.Type,
                    DisplayName = new LocalizedString((string)fieldConfig.DisplayName),
                    Description = new LocalizedString((string)fieldConfig.Description)
                });
            }

            return(descriptor);
        }
Exemple #3
0
        private void UpdateEditor(SnippetDescriptor descriptor, ElementEditorContext context)
        {
            var viewModel = new SnippetViewModel {
                Descriptor = descriptor
            };

            if (context.Updater != null)
            {
                foreach (var fieldDescriptor in descriptor.Fields)
                {
                    var name   = fieldDescriptor.Name;
                    var result = context.ValueProvider.GetValue(name);

                    if (result == null)
                    {
                        continue;
                    }

                    context.Element.Data[name] = result.AttemptedValue;
                }
            }

            viewModel.FieldEditors = descriptor.Fields.Select(x => {
                var fieldEditorTemplateName  = $"Elements.Snippet.Field.{x.Type ?? "Text"}";
                var fieldDescriptorViewModel = new SnippetFieldViewModel {
                    Descriptor = x,
                    Value      = context.Element.Data.Get(x.Name)
                };
                var fieldEditor = context.ShapeFactory.EditorTemplate(TemplateName: fieldEditorTemplateName, Model: fieldDescriptorViewModel, Prefix: context.Prefix);

                return(fieldEditor);
            }).ToList();

            var snippetEditorShape = context.ShapeFactory.EditorTemplate(TemplateName: "Elements.Snippet", Model: viewModel, Prefix: context.Prefix);

            snippetEditorShape.Metadata.Position = "Fields:0";

            context.EditorResult.Add(snippetEditorShape);
        }
        private SnippetDescriptor ParseSnippetDescriptor(dynamic snippetManifest)
        {
            if (snippetManifest == null || snippetManifest.Fields.Count == 0)
            {
                return(null);
            }

            var fieldsConfig = snippetManifest.Fields.Children;
            var descriptor   = new SnippetDescriptor();

            foreach (var fieldConfig in fieldsConfig)
            {
                descriptor.Fields.Add(new SnippetFieldDescriptor {
                    Name        = (string)fieldConfig.Name,
                    Type        = (string)fieldConfig.Type,
                    DisplayName = new LocalizedString((string)fieldConfig.DisplayName),
                    Description = new LocalizedString((string)fieldConfig.Description)
                });
            }

            return(descriptor);
        }
Exemple #5
0
 private void Editor(SnippetDescriptor descriptor, ElementEditorContext context)
 {
     UpdateEditor(descriptor, context);
 }
Exemple #6
0
        private SnippetDescriptor CreateSnippetDescriptor(ShapeDescriptor shapeDescriptor, Snippet snippetElement)
        {
            // Initializing and checking access to the Snippet manifest file.
            var physicalSourcePath = _wca.GetContext().HttpContext.Server.MapPath(shapeDescriptor.BindingSource);
            var fullPath           = Path.Combine(
                Path.GetDirectoryName(physicalSourcePath) ?? "",
                Path.GetFileNameWithoutExtension(physicalSourcePath) + ".txt");

            SnippetDescriptor descriptor;

            // Reading and parsing the manifest if it exists.
            if (File.Exists(fullPath))
            {
                var deserializer = new DeserializerBuilder()
                                   .IgnoreUnmatchedProperties()
                                   .WithTypeConverter(new LocalizedStringYamlConverter())
                                   .Build();

                descriptor = deserializer.Deserialize <SnippetDescriptor>(File.OpenText(fullPath));
            }
            // Otherwise extract the Fields from the Snippet shape template.
            else
            {
                var shape = (dynamic)_shapeFactory.Value.Create(shapeDescriptor.ShapeType);
                shape.Element = snippetElement;

                descriptor = new SnippetDescriptor();

                shape.DescriptorRegistrationCallback = (Action <SnippetFieldDescriptor>)(fieldDescriptor => {
                    // Not using Dictionary, as that will break rendering the view for some obscure reason.
                    var existingFieldDescriptor = descriptor.Fields.SingleOrDefault(field => field.Name == fieldDescriptor.Name);

                    if (existingFieldDescriptor == null)
                    {
                        descriptor.Fields.Add(fieldDescriptor);
                    }

                    if (fieldDescriptor.DisplayName == null)
                    {
                        fieldDescriptor.DisplayName = new LocalizedString(fieldDescriptor.Name);
                    }
                });

                using (_currentThemeShapeBindingResolver.Value.Enable()) {
                    _shapeDisplay.Value.Display(shape);
                }

                shape.SnippetDescriptor = descriptor;
            }

            // Checking the validity of the parsed values, include those of the Snippet's Fields.
            if (string.IsNullOrEmpty(descriptor.Category))
            {
                descriptor.Category = snippetElement.Category;
            }

            if (string.IsNullOrEmpty(descriptor.Description?.Text))
            {
                descriptor.Description = T("An element that renders the {0} shape.", shapeDescriptor.ShapeType);
            }

            if (string.IsNullOrEmpty(descriptor.DisplayName?.Text))
            {
                var fileName  = Path.GetFileNameWithoutExtension(shapeDescriptor.BindingSource) ?? "";
                var lastIndex = fileName.IndexOf(SnippetShapeSuffix, StringComparison.OrdinalIgnoreCase);
                descriptor.DisplayName = T(fileName.Substring(0, lastIndex).CamelFriendly());
            }

            if (string.IsNullOrEmpty(descriptor.ToolboxIcon))
            {
                descriptor.ToolboxIcon = snippetElement.ToolboxIcon;
            }

            descriptor.Fields = descriptor.Fields.Where(field => field.IsValid).ToList();

            return(descriptor);
        }
Exemple #7
0
        private void Displaying(ElementDisplayingContext context, ShapeDescriptor shapeDescriptor, SnippetDescriptor snippetDescriptor)
        {
            var shapeType = shapeDescriptor.ShapeType;
            var shape     = (dynamic)_shapeFactory.Value.Create(shapeType);

            shape.Element           = context.Element;
            shape.SnippetDescriptor = snippetDescriptor;

            if (snippetDescriptor != null)
            {
                foreach (var fieldDescriptor in snippetDescriptor.Fields)
                {
                    var value = context.Element.Data.Get(fieldDescriptor.Name);
                    shape.Properties[fieldDescriptor.Name] = value;
                }
            }

            ElementShapes.AddTokenizers(shape, _tokenizer.Value);
            context.ElementShape.Snippet = shape;
        }
        private void Displaying(ElementDisplayingContext context, ShapeDescriptor shapeDescriptor, SnippetDescriptor snippetDescriptor)
        {
            var shapeType = shapeDescriptor.ShapeType;
            var shape     = (dynamic)_shapeFactory.Value.Create(shapeType);

            shape.Element           = context.Element;
            shape.SnippetDescriptor = snippetDescriptor;

            ElementShapes.AddTokenizers(shape, _tokenizer.Value);
            context.ElementShape.Snippet           = shape;
            context.ElementShape.SnippetDescriptor = snippetDescriptor;
        }