public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
        {
            var defaultValueType = typeof(string);

            if (propertyType.DataType.Configuration is Dictionary <string, object> configuration &&
                configuration.TryGetValue(DataListConfigurationEditor.DataSource, out var tmp1) &&
                tmp1 is JArray array1 && array1.Count > 0 &&
                configuration.TryGetValue(DataListConfigurationEditor.ListEditor, out var tmp2) &&
                tmp2 is JArray array2 && array2.Count > 0)
            {
                var valueType = default(Type);

                var source = _utility.GetConfigurationEditor <IDataListSourceValueConverter>(array1[0].Value <string>("type"));
                if (source != null)
                {
                    var config = array1[0]["value"].ToObject <Dictionary <string, object> >();
                    valueType = source.GetValueType(config);
                }

                var editor = _utility.GetConfigurationEditor <IDataListEditor>(array2[0].Value <string>("type"));

                return(editor?.HasMultipleValues == true
                    ? typeof(IEnumerable <>).MakeGenericType(valueType ?? defaultValueType)
                    : valueType ?? defaultValueType);
            }

            return(defaultValueType);
        }
        public override IDictionary <string, object> ToValueEditor(object configuration)
        {
            var config = base.ToValueEditor(configuration);

            var toValueEditor = new Dictionary <string, object>();

            if (config.TryGetValueAs(DataSource, out JArray dataSource) && dataSource.Count > 0)
            {
                var item   = dataSource[0];
                var source = _utility.GetConfigurationEditor <IDataListSource>(item.Value <string>("type"));
                if (source != null)
                {
                    var sourceConfig = item["value"].ToObject <Dictionary <string, object> >();
                    var items        = source?.GetItems(sourceConfig) ?? Enumerable.Empty <DataListItem>();

                    toValueEditor.Add(Items, items);
                }
            }

            if (config.TryGetValueAs(ListEditor, out JArray listEditor) && listEditor.Count > 0)
            {
                var item   = listEditor[0];
                var editor = _utility.GetConfigurationEditor <IDataListEditor>(item.Value <string>("type"));
                if (editor != null)
                {
                    var editorConfig = item["value"].ToObject <Dictionary <string, object> >();

                    foreach (var prop in editorConfig)
                    {
                        if (toValueEditor.ContainsKey(prop.Key) == false)
                        {
                            toValueEditor.Add(prop.Key, prop.Value);
                        }
                    }

                    if (editor.DefaultConfig != null)
                    {
                        foreach (var prop in editor.DefaultConfig)
                        {
                            if (toValueEditor.ContainsKey(prop.Key) == false)
                            {
                                toValueEditor.Add(prop.Key, prop.Value);
                            }
                        }
                    }
                }
            }

            return(toValueEditor);
        }
Пример #3
0
        public override Type GetPropertyValueType(IPublishedPropertyType propertyType)
        {
            var defaultValueType = typeof(string);

            if (propertyType.DataType.Configuration is Dictionary <string, object> configuration &&
                configuration.TryGetValue(DataListConfigurationEditor.DataSource, out var tmp1) &&
                tmp1 is JArray array1 && array1.Count > 0 && array1[0] is JObject obj1 &&
                configuration.TryGetValue(DataListConfigurationEditor.ListEditor, out var tmp2) &&
                tmp2 is JArray array2 && array2.Count > 0 && array2[0] is JObject obj2)
            {
                var valueType         = default(Type);
                var hasMultipleValues = false;

                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                if (obj1.ContainsKey("key") == false && obj1.ContainsKey("type"))
                {
                    obj1.Add("key", obj1["type"]);
                    obj1.Remove("type");
                }

                var source = _utility.GetConfigurationEditor <IDataListSourceValueConverter>(obj1.Value <string>("key"));
                if (source != null)
                {
                    var config = obj1["value"].ToObject <Dictionary <string, object> >();
                    valueType = source.GetValueType(config);
                }

                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                if (obj2.ContainsKey("key") == false && obj2.ContainsKey("type"))
                {
                    obj2.Add("key", obj2["type"]);
                    obj2.Remove("type");
                }

                var editor = _utility.GetConfigurationEditor <IDataListEditor>(obj2.Value <string>("key"));
                if (editor != null)
                {
                    var config = obj2["value"].ToObject <Dictionary <string, object> >();
                    hasMultipleValues = editor.HasMultipleValues(config);
                }

                return(hasMultipleValues == true
                    ? typeof(IEnumerable <>).MakeGenericType(valueType ?? defaultValueType)
                    : valueType ?? defaultValueType);
            }

            return(defaultValueType);
        }
        public override IDictionary <string, object> ToValueEditor(object configuration)
        {
            var config = base.ToValueEditor(configuration);

            if (config.TryGetValueAs(Constants.Conventions.ConfigurationFieldAliases.Items, out JArray array) && array.Count > 0 && array[0] is JObject item)
            {
                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`.
                if (item.ContainsKey("key") == false && item.ContainsKey("type"))
                {
                    item.Add("key", item["type"]);
                    item.Remove("type");
                }

                var source = _utility.GetConfigurationEditor <IDataListSource>(item.Value <string>("key"));
                if (source != null)
                {
                    var sourceConfig = item["value"].ToObject <Dictionary <string, object> >();
                    var items        = source?.GetItems(sourceConfig) ?? Enumerable.Empty <DataListItem>();

                    config[Constants.Conventions.ConfigurationFieldAliases.Items] = items;
                }
            }

            return(config);
        }
Пример #5
0
        public IDataValueEditor GetValueEditor(object configuration)
        {
            var view = DataEditorViewPath;

            if (configuration is Dictionary <string, object> config)
            {
                if (config.TryGetValue(ContentBlocksConfigurationEditor.DisplayMode, out var tmp1) == true)
                {
                    var displayMode = default(IContentBlocksDisplayMode);

                    if (tmp1 is string str1 && str1?.InvariantStartsWith(Constants.Internals.EditorsPathRoot) == true)
                    {
                        displayMode = _utility.FindConfigurationEditor <IContentBlocksDisplayMode>(x => str1.InvariantEquals(x.View) == true);
                    }
                    else if (tmp1 is JArray array1 && array1.Count > 0 && array1[0] is JObject item1)
                    {
                        displayMode = _utility.GetConfigurationEditor <IContentBlocksDisplayMode>(item1.Value <string>("key"));
                    }

                    if (displayMode != null)
                    {
                        view = displayMode.View;
                    }
                }
            }
Пример #6
0
        private void TryGetPropertyTypeConfiguration(IPublishedPropertyType propertyType, out bool hasMultipleValues, out Type valueType, out Func <Type, string, object> converter)
        {
            hasMultipleValues = false;
            valueType         = typeof(string);
            converter         = default;

            if (propertyType.DataType.Configuration is Dictionary <string, object> configuration &&
                configuration.TryGetValue(DataListConfigurationEditor.DataSource, out var tmp1) &&
                tmp1 is JArray array1 && array1.Count > 0 && array1[0] is JObject obj1 &&
                configuration.TryGetValue(DataListConfigurationEditor.ListEditor, out var tmp2) &&
                tmp2 is JArray array2 && array2.Count > 0 && array2[0] is JObject obj2)
            {
                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                if (obj1.ContainsKey("key") == false && obj1.ContainsKey("type"))
                {
                    obj1.Add("key", obj1["type"]);
                    obj1.Remove("type");
                }

                var source = _utility.GetConfigurationEditor <IDataListSourceValueConverter>(obj1.Value <string>("key"));
                if (source != null)
                {
                    var config = obj1["value"].ToObject <Dictionary <string, object> >();
                    valueType = source.GetValueType(config);
                    converter = source.ConvertValue;
                }

                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                if (obj2.ContainsKey("key") == false && obj2.ContainsKey("type"))
                {
                    obj2.Add("key", obj2["type"]);
                    obj2.Remove("type");
                }

                var editor = _utility.GetConfigurationEditor <IDataListEditor>(obj2.Value <string>("key"));
                if (editor != null)
                {
                    var config = obj2["value"].ToObject <Dictionary <string, object> >();
                    hasMultipleValues = editor.HasMultipleValues(config);
                }
            }
        }
        public override IDictionary <string, object> ToValueEditor(object configuration)
        {
            var config = base.ToValueEditor(configuration);

            var toValueEditor = new Dictionary <string, object>();

            if (config.TryGetValueAs(DataSource, out JArray array1) && array1.Count > 0 && array1[0] is JObject item1)
            {
                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                if (item1.ContainsKey("key") == false && item1.ContainsKey("type"))
                {
                    item1.Add("key", item1["type"]);
                    item1.Remove("type");
                }

                var source = _utility.GetConfigurationEditor <IDataListSource>(item1.Value <string>("key"));
                if (source != null)
                {
                    var sourceConfig = item1["value"].ToObject <Dictionary <string, object> >();
                    var items        = source?.GetItems(sourceConfig) ?? Enumerable.Empty <DataListItem>();

                    toValueEditor.Add(Items, items);
                }
            }

            if (config.TryGetValueAs(ListEditor, out JArray array2) && array2.Count > 0 && array2[0] is JObject item2)
            {
                // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                if (item2.ContainsKey("key") == false && item2.ContainsKey("type"))
                {
                    item2.Add("key", item2["type"]);
                    item2.Remove("type");
                }

                var editor = _utility.GetConfigurationEditor <IDataListEditor>(item2.Value <string>("key"));
                if (editor != null)
                {
                    var editorConfig = item2["value"].ToObject <Dictionary <string, object> >();

                    foreach (var prop in editorConfig)
                    {
                        if (toValueEditor.ContainsKey(prop.Key) == false)
                        {
                            toValueEditor.Add(prop.Key, prop.Value);
                        }
                    }

                    if (editor.DefaultConfig != null)
                    {
                        foreach (var prop in editor.DefaultConfig)
                        {
                            if (toValueEditor.ContainsKey(prop.Key) == false)
                            {
                                toValueEditor.Add(prop.Key, prop.Value);
                            }
                        }
                    }
                }
            }

            return(toValueEditor);
        }
        public override IDictionary <string, object> ToValueEditor(object configuration)
        {
            var config = base.ToValueEditor(configuration);

            if (config.TryGetValueAs(DisplayMode, out JArray array1) == true && array1.Count > 0 && array1[0] is JObject item1)
            {
                var displayMode = _utility.GetConfigurationEditor <IContentBlocksDisplayMode>(item1.Value <string>("key"));
                if (displayMode != null)
                {
                    var editorConfig = item1["value"].ToObject <Dictionary <string, object> >();

                    foreach (var prop in editorConfig)
                    {
                        if (config.ContainsKey(prop.Key) == false)
                        {
                            config.Add(prop.Key, prop.Value);
                        }
                    }

                    if (displayMode.DefaultConfig != null)
                    {
                        foreach (var prop in displayMode.DefaultConfig)
                        {
                            if (config.ContainsKey(prop.Key) == false)
                            {
                                config.Add(prop.Key, prop.Value);
                            }
                        }
                    }
                }
            }

            if (config.TryGetValueAs(ContentBlocksTypesConfigurationField.ContentBlockTypes, out JArray array2) && array2.Count > 0)
            {
                var elementTypes = new List <ContentBlockType>();

                for (var i = 0; i < array2.Count; i++)
                {
                    var item = (JObject)array2[i];

                    // NOTE: Patches a breaking-change. I'd renamed `type` to become `key`. [LK:2020-04-03]
                    if (item.ContainsKey("key") == false && item.ContainsKey("type"))
                    {
                        item.Add("key", item["type"]);
                        item.Remove("type");
                    }

                    if (Guid.TryParse(item.Value <string>("key"), out var guid) && _elementTypes.ContainsKey(guid))
                    {
                        var elementType = _elementTypes[guid];

                        var settings = item["value"].ToObject <Dictionary <string, object> >();

                        var blueprints = _elementBlueprints.Value.Contains(elementType.Id)
                            ? _elementBlueprints.Value[elementType.Id].Select(x => new ContentBlockType.BlueprintItem {
                            Id = x.Id, Name = x.Name
                        })
                            : Enumerable.Empty <ContentBlockType.BlueprintItem>();

                        elementTypes.Add(new ContentBlockType
                        {
                            Alias          = elementType.Alias,
                            Name           = elementType.Name,
                            Description    = elementType.Description,
                            Icon           = elementType.Icon,
                            Key            = elementType.Key,
                            Blueprints     = blueprints,
                            NameTemplate   = settings?.ContainsKey("nameTemplate") == true ? settings["nameTemplate"].ToString() : null,
                            OverlaySize    = settings?.ContainsKey("overlaySize") == true ? settings["overlaySize"].ToString() : null,
                            PreviewEnabled = settings?.ContainsKey("enablePreview") == true && settings["enablePreview"].TryConvertTo <bool>().ResultOr(false),
                        });
                    }
                }

                config[ContentBlocksTypesConfigurationField.ContentBlockTypes] = elementTypes;
            }

            if (config.ContainsKey(Constants.Conventions.ConfigurationFieldAliases.OverlayView) == false)
            {
                config.Add(Constants.Conventions.ConfigurationFieldAliases.OverlayView, IOHelper.ResolveUrl(ContentBlocksDataEditor.DataEditorOverlayViewPath));
            }

            return(config);
        }