2013-11-07 17:16:22 +01:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using Umbraco.Core ;
2013-12-04 17:13:26 +11:00
using Umbraco.Core.Models ;
2013-11-07 17:16:22 +01:00
using Umbraco.Core.PropertyEditors ;
namespace Umbraco.Web.PropertyEditors
{
2015-06-24 21:43:14 +02:00
[PropertyEditor(Constants.PropertyEditors.MultiNodeTreePickerAlias, "Multinode Treepicker", "contentpicker", Group="pickers", Icon="icon-page-add")]
2013-11-07 17:16:22 +01:00
public class MultiNodeTreePickerPropertyEditor : PropertyEditor
{
2013-11-18 20:44:12 +01:00
public MultiNodeTreePickerPropertyEditor ( )
{
2013-11-19 10:40:21 +11:00
_internalPreValues = new Dictionary < string , object >
2013-11-18 20:44:12 +01:00
{
2014-10-27 16:11:20 +10:00
{ "multiPicker" , "1" } ,
{ "showEditButton" , "0" }
2013-11-18 20:44:12 +01:00
} ;
}
2013-12-04 17:13:26 +11:00
2013-11-07 17:16:22 +01:00
protected override PreValueEditor CreatePreValueEditor ( )
{
return new MultiNodePickerPreValueEditor ( ) ;
}
2013-11-19 10:40:21 +11:00
private IDictionary < string , object > _internalPreValues ;
2013-11-18 20:44:12 +01:00
public override IDictionary < string , object > DefaultPreValues
{
2013-11-19 10:40:21 +11:00
get { return _internalPreValues ; }
set { _internalPreValues = value ; }
2013-11-18 20:44:12 +01:00
}
2013-11-07 17:16:22 +01:00
internal class MultiNodePickerPreValueEditor : PreValueEditor
{
2013-11-18 16:14:49 +01:00
[PreValueField("startNode", "Node type", "treesource")]
public string StartNode { get ; set ; }
2013-11-07 17:16:22 +01:00
2014-11-20 15:41:06 +00:00
[PreValueField("filter", "Allow items of type", "textstring", Description = "Separate with comma")]
2013-11-07 17:16:22 +01:00
public string Filter { get ; set ; }
2014-05-01 16:35:22 +02:00
[PreValueField("minNumber", "Minimum number of items", "number")]
2013-11-07 17:16:22 +01:00
public string MinNumber { get ; set ; }
[PreValueField("maxNumber", "Maximum number of items", "number")]
public string MaxNumber { get ; set ; }
2014-10-28 08:31:18 +01:00
[PreValueField("showEditButton", "Show edit button (this feature is in preview!)", "boolean")]
public string ShowEditButton { get ; set ; }
2014-10-27 16:11:20 +10:00
2013-12-04 17:13:26 +11:00
/// <summary>
/// This ensures the multiPicker pre-val is set based on the maxNumber of nodes set
/// </summary>
/// <param name="defaultPreVals"></param>
/// <param name="persistedPreVals"></param>
/// <returns></returns>
/// <remarks>
/// Due to compatibility with 7.0.0 the multiPicker pre-val might already exist in the db, but we've removed that setting in 7.0.1 so we need to detect it and if it is
/// there, then we'll set the maxNumber to '1'
/// </remarks>
public override IDictionary < string , object > ConvertDbToEditor ( IDictionary < string , object > defaultPreVals , PreValueCollection persistedPreVals )
{
var result = base . ConvertDbToEditor ( defaultPreVals , persistedPreVals ) ;
//backwards compatibility check
if ( result . ContainsKey ( "multiPicker" ) & & result [ "multiPicker" ] . ToString ( ) = = "0" )
{
result [ "maxNumber" ] = "1" ;
}
//set the multiPicker val correctly depending on the maxNumber
2014-10-02 12:23:43 +10:00
if ( result . ContainsKey ( "maxNumber" ) )
2013-12-04 17:13:26 +11:00
{
2014-10-02 12:23:43 +10:00
var asNumber = result [ "maxNumber" ] . TryConvertTo < int > ( ) ;
if ( asNumber . Success )
2013-12-04 17:13:26 +11:00
{
2014-10-02 12:23:43 +10:00
if ( asNumber . Result < = 1 )
{
result [ "multiPicker" ] = "0" ;
}
else
{
result [ "multiPicker" ] = "1" ;
}
}
2013-12-04 17:13:26 +11:00
}
2014-10-02 12:23:43 +10:00
2013-12-04 17:13:26 +11:00
return result ;
}
2013-11-07 17:16:22 +01:00
}
}
}