Created
February 11, 2021 10:33
-
-
Save leekelleher/5c0abddb3246185ce0ba50a99975e310 to your computer and use it in GitHub Desktop.
Umbraco Image Crop Picker using Contentment Data List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright © 2021 Lee Kelleher. | |
* This Source Code Form is subject to the terms of the Mozilla Public | |
* License, v. 2.0. If a copy of the MPL was not distributed with this | |
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */ | |
using System.Collections.Generic; | |
using System.Linq; | |
using Newtonsoft.Json.Linq; | |
using Umbraco.Community.Contentment.DataEditors; | |
using Umbraco.Core; | |
using Umbraco.Core.IO; | |
using Umbraco.Core.Models; | |
using Umbraco.Core.PropertyEditors; | |
using Umbraco.Core.Services; | |
namespace Our.Umbraco.Community.Contentment.DataSources | |
{ | |
internal sealed class UmbracoImageCropDataListSource : IDataListSource | |
{ | |
private readonly IDataTypeService _dataTypeService; | |
public UmbracoImageCropDataListSource(IDataTypeService dataTypeService) | |
{ | |
_dataTypeService = dataTypeService; | |
} | |
public string Name => "Umbraco Image Crops"; | |
public string Description => "Select an Umbraco Image Cropper to populate the data source."; | |
public string Icon => "icon-crop"; | |
public IEnumerable<ConfigurationField> Fields | |
{ | |
get | |
{ | |
var items = _dataTypeService | |
.GetByEditorAlias(Constants.PropertyEditors.Aliases.ImageCropper) | |
.Select(x => new DataListItem | |
{ | |
Icon = Icon, | |
Description = x.EditorAlias, | |
Name = x.Name, | |
Value = Udi.Create(Constants.UdiEntityType.DataType, x.Key).ToString(), | |
}); | |
return new ConfigurationField[] | |
{ | |
new ConfigurationField | |
{ | |
Key = "imageCropper", | |
Name = "Image Cropper", | |
Description = "Select a Data Type that uses the Image Cropper.", | |
View = "~/App_Plugins/Contentment/editors/item-picker.html", | |
Config = new Dictionary<string, object> | |
{ | |
{ "enableFilter", items.Count() > 5 ? "1" : "0" }, | |
{ "items", items }, | |
{ "listType", "list" }, | |
{ "overlayView", IOHelper.ResolveUrl("~/App_Plugins/Contentment/editors/item-picker.overlay.html") }, | |
{ "maxItems", 1 }, | |
} | |
} | |
}; | |
} | |
} | |
public Dictionary<string, object> DefaultValues => null; | |
public OverlaySize OverlaySize => OverlaySize.Small; | |
public IEnumerable<DataListItem> GetItems(Dictionary<string, object> config) | |
{ | |
if (config.TryGetValue("imageCropper", out var obj) && | |
obj is JArray array && | |
array.Count > 0 && | |
array[0].Value<string>() is string str && | |
string.IsNullOrWhiteSpace(str) == false && | |
GuidUdi.TryParse(str, out var udi)) | |
{ | |
return _dataTypeService | |
.GetDataType(udi.Guid)? | |
.ConfigurationAs<ImageCropperConfiguration>()? | |
.Crops? | |
.Select(x => new DataListItem | |
{ | |
Name = x.Alias, | |
Value = x.Alias, | |
Icon = this.Icon, | |
Description = $"{x.Width}px × {x.Height}px" | |
}); | |
} | |
return Enumerable.Empty<DataListItem>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment