2017-07-19 13:42:47 +02:00
using System.Collections.Generic ;
using System.Linq ;
using Umbraco.Core ;
using Umbraco.Core.Logging ;
2019-03-25 09:03:46 +01:00
using Umbraco.Core.Mapping ;
2017-07-19 13:42:47 +02:00
using Umbraco.Core.Models ;
2019-03-25 09:03:46 +01:00
using Umbraco.Core.PropertyEditors ;
2017-07-19 13:42:47 +02:00
using Umbraco.Web.Models.ContentEditing ;
namespace Umbraco.Web.Models.Mapping
{
2019-04-03 10:39:49 +02:00
internal class MacroMapDefinition : IMapDefinition
2017-07-19 13:42:47 +02:00
{
2019-03-25 09:03:46 +01:00
private readonly ParameterEditorCollection _parameterEditors ;
private readonly ILogger _logger ;
2019-04-03 10:39:49 +02:00
public MacroMapDefinition ( ParameterEditorCollection parameterEditors , ILogger logger )
2019-03-25 09:03:46 +01:00
{
_parameterEditors = parameterEditors ;
_logger = logger ;
}
2019-04-03 10:39:49 +02:00
public void DefineMaps ( UmbracoMapper mapper )
2019-03-25 09:03:46 +01:00
{
2019-03-26 10:39:50 +01:00
mapper . Define < IMacro , EntityBasic > ( ( source , context ) = > new EntityBasic ( ) , Map ) ;
2019-04-08 10:05:21 +02:00
mapper . Define < IMacro , IEnumerable < MacroParameter > > ( ( source , context ) = > context . MapEnumerable < IMacroProperty , MacroParameter > ( source . Properties . Values ) ) ;
2019-03-26 10:39:50 +01:00
mapper . Define < IMacroProperty , MacroParameter > ( ( source , context ) = > new MacroParameter ( ) , Map ) ;
2019-03-25 09:03:46 +01:00
}
// Umbraco.Code.MapAll -Trashed -AdditionalData
2019-03-26 10:39:50 +01:00
private static void Map ( IMacro source , EntityBasic target , MapperContext context )
2017-07-19 13:42:47 +02:00
{
2019-03-25 09:03:46 +01:00
target . Alias = source . Alias ;
target . Icon = "icon-settings-alt" ;
target . Id = source . Id ;
target . Key = source . Key ;
target . Name = source . Name ;
target . ParentId = - 1 ;
target . Path = "-1," + source . Id ;
target . Udi = Udi . Create ( Constants . UdiEntityType . Macro , source . Key ) ;
}
// Umbraco.Code.MapAll -Value
2019-03-26 10:39:50 +01:00
private void Map ( IMacroProperty source , MacroParameter target , MapperContext context )
2019-03-25 09:03:46 +01:00
{
target . Alias = source . Alias ;
target . Name = source . Name ;
target . SortOrder = source . SortOrder ;
//map the view and the config
// we need to show the deprecated ones for backwards compatibility
var paramEditor = _parameterEditors [ source . EditorAlias ] ; // TODO: include/filter deprecated?!
if ( paramEditor = = null )
{
//we'll just map this to a text box
paramEditor = _parameterEditors [ Constants . PropertyEditors . Aliases . TextBox ] ;
2019-04-03 10:39:49 +02:00
_logger . Warn < MacroMapDefinition > ( "Could not resolve a parameter editor with alias {PropertyEditorAlias}, a textbox will be rendered in it's place" , source . EditorAlias ) ;
2019-03-25 09:03:46 +01:00
}
target . View = paramEditor . GetValueEditor ( ) . View ;
// sets the parameter configuration to be the default configuration editor's configuration,
// ie configurationEditor.DefaultConfigurationObject, prepared for the value editor, ie
// after ToValueEditor - important to use DefaultConfigurationObject here, because depending
// on editors, ToValueEditor expects the actual strongly typed configuration - not the
// dictionary thing returned by DefaultConfiguration
var configurationEditor = paramEditor . GetConfigurationEditor ( ) ;
target . Configuration = configurationEditor . ToValueEditor ( configurationEditor . DefaultConfigurationObject ) ;
2017-07-19 13:42:47 +02:00
}
}
2017-07-20 11:21:28 +02:00
}