public object ToEditorModel(Element element, DescribeElementsContext describeContext)
        {
            var map       = GetMapFor(element);
            var node      = new JObject();
            var container = element as Container;

            // We want to convert any null value being added to this node to an empty string,
            // so that we can perform a JSON string comparison on the client side editor to detect if the user changed anything.
            // If the initial state would contain null values, these would become empty strings after the user made a change
            // (e.g. setting some HtmlID property from empty to "my-id" and then clearing out that field).
            node.PropertyChanged += (sender, args) => {
                var value = node[args.PropertyName] as JValue;

                if (value != null && value.Value == null)
                {
                    node[args.PropertyName] = "";
                }
            };

            map.FromElement(element, describeContext, node);
            node["type"] = map.LayoutElementType;

            if (container != null)
            {
                node["children"] = new JArray(container.Elements.Select(x => ToEditorModel(x, describeContext)).ToList());
            }

            // Would be nicer if we could turn JObject into an anonymous object directly, but this seems to be the only way.
            return(JsonConvert.DeserializeObject(node.ToString()));
        }
Beispiel #2
0
        public IEnumerable <CategoryDescriptor> GetCategories(DescribeElementsContext context)
        {
            var contentType = context.Content != null ? context.Content.ContentItem.ContentType : default(string);

            return(_cacheManager.Get(String.Format("ElementCategories-{0}-{1}", contentType ?? "AnyType", context.CacheVaryParam), acquireContext => {
                var elementDescriptors = DescribeElements(context);
                var categoryDictionary = GetCategories();
                var categoryDescriptorDictionary = new Dictionary <string, CategoryDescriptor>();

                foreach (var elementDescriptor in elementDescriptors)
                {
                    var category = categoryDictionary.ContainsKey(elementDescriptor.Category)
                        ? categoryDictionary[elementDescriptor.Category]
                        : new Category(elementDescriptor.Category, T(elementDescriptor.Category), position: int.MaxValue);

                    var descriptor = categoryDescriptorDictionary.ContainsKey(elementDescriptor.Category)
                        ? categoryDescriptorDictionary[elementDescriptor.Category]
                        : categoryDescriptorDictionary[elementDescriptor.Category] = new CategoryDescriptor(category.Name, category.DisplayName, category.Description, category.Position);

                    descriptor.Elements.Add(elementDescriptor);
                }

                acquireContext.Monitor(_signals.When(Signals.ElementDescriptors));
                return categoryDescriptorDictionary.Values.OrderBy(x => x.Position);
            }));
        }
        public object ToEditorModel(string layoutData, DescribeElementsContext describeContext)
        {
            var elements = _serializer.Deserialize(layoutData, describeContext);
            var canvas   = elements.FirstOrDefault(x => x is Canvas) ?? _elementManager.ActivateElement <Canvas>();

            return(ToEditorModel(canvas, describeContext));
        }
Beispiel #4
0
        public ElementDescriptor GetElementDescriptorByTypeName(DescribeElementsContext context, string typeName)
        {
            var elements = DescribeElements(context);
            var element  = elements.SingleOrDefault(x => String.Equals(x.TypeName, typeName, StringComparison.OrdinalIgnoreCase));

            return(element);
        }
Beispiel #5
0
 public override void FromElement(Column element, DescribeElementsContext describeContext, JToken node)
 {
     base.FromElement(element, describeContext, node);
     node["width"]       = element.Width;
     node["offset"]      = element.Offset;
     node["collapsible"] = element.Collapsible;
 }
Beispiel #6
0
        public IEnumerable <Element> LoadElements(ILayoutAspect layout)
        {
            var describeContext = new DescribeElementsContext {
                Content = layout
            };

            return(_serializer.Deserialize(layout.LayoutData, describeContext));
        }
Beispiel #7
0
        public Element ToElement(IElementManager elementManager, DescribeElementsContext describeContext, JToken node)
        {
            var descriptor = elementManager.GetElementDescriptorByType <T>(describeContext);
            var element    = elementManager.ActivateElement <T>(descriptor);

            ToElement(element, node);
            return(element);
        }
Beispiel #8
0
        private IEnumerable <CategoryDescriptor> GetCategories(IContent content)
        {
            var describeContext = new DescribeElementsContext {
                Content = content
            };
            var elementCategories = _elementManager.GetCategories(describeContext).ToArray();

            return(elementCategories.Where(category => category.Elements.Any(x => !x.IsSystemElement)));
        }
        private Element LoadElement(JToken node, Container parent, int index, DescribeElementsContext describeContext)
        {
            var type    = (string)node["type"];
            var map     = SelectMap(x => x.LayoutElementType == type);
            var element = map.ToElement(_elementManager, describeContext, node);

            element.Container = parent;
            element.Index     = index;

            return(element);
        }
        public IEnumerable <Element> ToLayoutModel(string editorData, DescribeElementsContext describeContext)
        {
            if (String.IsNullOrWhiteSpace(editorData))
            {
                yield break;
            }

            var canvas = JToken.Parse(editorData);

            yield return(ParseEditorNode(node: canvas, parent: null, index: 0, describeContext: describeContext));
        }
Beispiel #11
0
        public Element Deserialize(string data, DescribeElementsContext describeContext)
        {
            if (String.IsNullOrWhiteSpace(data))
            {
                return(null);
            }

            var token   = JToken.Parse(data);
            var element = ParseNode(node: token, parent: null, index: 0, describeContext: describeContext);

            return(element);
        }
Beispiel #12
0
 public virtual void FromElement(T element, DescribeElementsContext describeContext, JToken node)
 {
     node["data"]                   = element.Data.Serialize();
     node["htmlId"]                 = element.HtmlId;
     node["htmlClass"]              = element.HtmlClass;
     node["htmlStyle"]              = element.HtmlStyle;
     node["rule"]                   = element.Rule;
     node["isTemplated"]            = element.IsTemplated;
     node["hasEditor"]              = element.Descriptor.EnableEditorDialog;
     node["contentType"]            = element.Descriptor.TypeName;
     node["contentTypeLabel"]       = element.Descriptor.DisplayText.Text;
     node["contentTypeClass"]       = element.DisplayText.Text.HtmlClassify();
     node["contentTypeDescription"] = element.Descriptor.Description.Text;
 }
Beispiel #13
0
 public void FromElement(Element element, DescribeElementsContext describeContext, JToken node)
 {
     node["data"]                   = element.Data.Serialize();
     node["htmlId"]                 = element.HtmlId;
     node["htmlClass"]              = element.HtmlClass;
     node["htmlStyle"]              = element.HtmlStyle;
     node["rule"]                   = element.Rule;
     node["isTemplated"]            = element.IsTemplated;
     node["hasEditor"]              = element.Descriptor.EnableEditorDialog;
     node["contentType"]            = element.Descriptor.TypeName;
     node["contentTypeLabel"]       = element.Descriptor.DisplayText.Text;
     node["contentTypeClass"]       = element.DisplayText.Text.HtmlClassify();
     node["contentTypeDescription"] = element.Descriptor.Description.Text;
     node["html"]                   = _shapeDisplay.Display(_elementDisplay.DisplayElement(element, content: describeContext.Content, displayType: "Design"));
 }
Beispiel #14
0
        public virtual Element ToElement(IElementManager elementManager, DescribeElementsContext describeContext, JToken node)
        {
            var elementTypeName = (string)node["contentType"];
            var descriptor      = elementManager.GetElementDescriptorByTypeName(describeContext, elementTypeName);
            var element         = elementManager.ActivateElement(descriptor);

            element.Data        = ElementDataHelper.Deserialize((string)node["data"]);
            element.HtmlId      = (string)node["htmlId"];
            element.HtmlClass   = (string)node["htmlClass"];
            element.HtmlStyle   = (string)node["htmlStyle"];
            element.IsTemplated = (bool)(node["isTemplated"] ?? false);
            element.Rule        = (string)node["rule"];

            return(element);
        }
        private Element ParseEditorNode(JToken node, Container parent, int index, DescribeElementsContext describeContext)
        {
            var element    = LoadElement(node, parent, index, describeContext);
            var childNodes = (JArray)node["children"];
            var container  = element as Container;

            if (container != null)
            {
                container.Elements = childNodes != null
                    ? childNodes.Select((x, i) => ParseEditorNode(x, container, i, describeContext)).Where(x => x != null).ToList()
                    : new List <Element>();
            }

            return(element);
        }
Beispiel #16
0
        public Element ParseNode(JToken node, Container parent, int index, DescribeElementsContext describeContext)
        {
            var elementTypeName = (string)node["typeName"];

            if (String.IsNullOrWhiteSpace(elementTypeName))
            {
                return(null);
            }

            var data              = (string)node["data"];
            var htmlId            = (string)node["htmlId"];
            var htmlClass         = (string)node["htmlClass"];
            var htmlStyle         = (string)node["htmlStyle"];
            var rule              = (string)node["rule"];
            var elementData       = ElementDataHelper.Deserialize(data);
            var exportableData    = ElementDataHelper.Deserialize((string)node["exportableData"]);
            var childNodes        = node["elements"];
            var elementDescriptor = _elementManager.GetElementDescriptorByTypeName(describeContext, elementTypeName);

            if (elementDescriptor == null)
            {
                return(null); // This happens if an element exists in a layout, but its type is no longer available due to its feature being disabled.
            }
            var element = _elementFactory.Activate(elementDescriptor, e => {
                e.Container      = parent;
                e.Index          = index;
                e.Data           = elementData;
                e.ExportableData = exportableData;
                e.HtmlId         = htmlId;
                e.HtmlClass      = htmlClass;
                e.HtmlStyle      = htmlStyle;
                e.Rule           = rule;
            });

            var container = element as Container;

            if (container != null)
            {
                container.Elements = childNodes != null
                    ? childNodes.Select((x, i) => ParseNode(x, container, i, describeContext)).Where(x => x != null).ToList()
                    : new List <Element>();
            }

            element.IsTemplated = node.Value <bool>("isTemplated");

            return(element);
        }
Beispiel #17
0
        public IEnumerable <Element> Deserialize(string data, DescribeElementsContext describeContext)
        {
            var emptyList = Enumerable.Empty <Element>();

            if (String.IsNullOrWhiteSpace(data))
            {
                return(emptyList);
            }

            var token    = JToken.Parse(data);
            var nodes    = (JArray)token["elements"];
            var elements = nodes != null
                ? nodes.Select((x, i) => _elementSerializer.ParseNode(node: x, parent: null, index: i, describeContext: describeContext)).Where(x => x != null).ToArray()
                : emptyList;

            return(elements);
        }
Beispiel #18
0
        public IEnumerable <ElementDescriptor> DescribeElements(DescribeElementsContext context)
        {
            var contentType = context.Content != null ? context.Content.ContentItem.ContentType : default(string);
            var cacheKey    = String.Format("LayoutElementTypes-{0}-{1}", contentType ?? "AnyType", context.CacheVaryParam);

            return(_cacheManager.Get(cacheKey, true, acquireContext => {
                var harvesterContext = new HarvestElementsContext {
                    Content = context.Content,
                    IsHarvesting = context.IsHarvesting
                };
                var query =
                    from harvester in _elementHarvesters.Value
                    from elementDescriptor in harvester.HarvestElements(harvesterContext)
                    orderby elementDescriptor.DisplayText.Text
                    select elementDescriptor;

                acquireContext.Monitor(_signals.When(Signals.ElementDescriptors));
                return query.ToArray();
            }));
        }
Beispiel #19
0
 void ILayoutModelMap.FromElement(Element element, DescribeElementsContext describeContext, JToken node)
 {
     FromElement((T)element, describeContext, node);
 }
Beispiel #20
0
 public void FromElement(Element element, DescribeElementsContext describeContext, JToken node)
 {
 }
Beispiel #21
0
 public Element ToElement(IElementManager elementManager, DescribeElementsContext describeContext, JToken node)
 {
     return(new RecycleBin());
 }
Beispiel #22
0
 public ElementDescriptor GetElementDescriptorByType <T>(DescribeElementsContext context) where T : Element
 {
     return(GetElementDescriptorByTypeName(context, typeof(T).FullName));
 }
Beispiel #23
0
 private string RenderElement(Element element, DescribeElementsContext describeContext, string displayType = "Design")
 {
     return(_shapeDisplay.Display(_elementDisplay.DisplayElement(element, describeContext.Content, displayType)));
 }