Moves static event binding for prop editors to startup handlers

This commit is contained in:
Shannon
2016-06-01 11:27:08 +02:00
parent cff7dbc81a
commit aff03997d9
3 changed files with 140 additions and 67 deletions

View File

@@ -21,32 +21,8 @@ using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Constants.PropertyEditors.UploadFieldAlias, "File upload", "fileupload", Icon = "icon-download-alt", Group = "media")]
public class FileUploadPropertyEditor : PropertyEditor
public class FileUploadPropertyEditor : PropertyEditor, IApplicationEventHandler
{
/// <summary>
/// We're going to bind to the MediaService Saving event so that we can populate the umbracoFile size, type, etc... label fields
/// if we find any attached to the current media item.
/// </summary>
/// <remarks>
/// I think this kind of logic belongs on this property editor, I guess it could exist elsewhere but it all has to do with the upload field.
/// </remarks>
static FileUploadPropertyEditor()
{
MediaService.Saving += MediaServiceSaving;
MediaService.Created += MediaServiceCreating;
ContentService.Copied += ContentServiceCopied;
MediaService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
MediaService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
ContentService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
ContentService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
MemberService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
}
/// <summary>
/// Creates our custom value editor
@@ -275,5 +251,54 @@ namespace Umbraco.Web.PropertyEditors
}
}
#region Application event handler, used to bind to events on startup
private readonly FileUploadPropertyEditorApplicationStartup _applicationStartup = new FileUploadPropertyEditorApplicationStartup();
/// <summary>
/// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured
/// </summary>
private class FileUploadPropertyEditorApplicationStartup : ApplicationEventHandler
{
/// <summary>
/// We're going to bind to the MediaService Saving event so that we can populate the umbracoFile size, type, etc... label fields
/// if we find any attached to the current media item.
/// </summary>
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saving += MediaServiceSaving;
MediaService.Created += MediaServiceCreating;
ContentService.Copied += ContentServiceCopied;
MediaService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
MediaService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
ContentService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
ContentService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
MemberService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
}
}
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationInitialized(umbracoApplication, applicationContext);
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationStarting(umbracoApplication, applicationContext);
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationStarted(umbracoApplication, applicationContext);
}
#endregion
}
}

View File

@@ -17,21 +17,8 @@ using UmbracoExamine;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Core.Constants.PropertyEditors.GridAlias, "Grid layout", "grid", HideLabel = true, IsParameterEditor = false, ValueType = PropertyEditorValueTypes.Json, Group="rich content", Icon="icon-layout")]
public class GridPropertyEditor : PropertyEditor
{
/// <summary>
/// We're going to bind to the Examine events so we can ensure grid data is index nicely
/// </summary>
/// <remarks>
/// I think this kind of logic belongs on this property editor, putting this inside of the indexer certainly doesn't seem right
/// </remarks>
static GridPropertyEditor()
{
foreach (var i in ExamineManager.Instance.IndexProviderCollection.OfType<BaseUmbracoIndexer>())
{
i.DocumentWriting += DocumentWriting;
}
}
public class GridPropertyEditor : PropertyEditor, IApplicationEventHandler
{
private static void DocumentWriting(object sender, Examine.LuceneEngine.DocumentWritingEventArgs e)
{
@@ -138,6 +125,44 @@ namespace Umbraco.Web.PropertyEditors
[PreValueField("rte", "Rich text editor", "views/propertyeditors/rte/rte.prevalues.html", Description = "Rich text editor configuration")]
public string Rte { get; set; }
}
#region Application event handler, used to bind to events on startup
private readonly GridPropertyEditorApplicationStartup _applicationStartup = new GridPropertyEditorApplicationStartup();
/// <summary>
/// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured
/// </summary>
private class GridPropertyEditorApplicationStartup : ApplicationEventHandler
{
/// <summary>
/// We're going to bind to the Examine events so we can ensure grid data is index nicely.
/// </summary>
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
foreach (var i in ExamineManager.Instance.IndexProviderCollection.OfType<BaseUmbracoIndexer>())
{
i.DocumentWriting += DocumentWriting;
}
}
}
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationInitialized(umbracoApplication, applicationContext);
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationStarting(umbracoApplication, applicationContext);
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationStarted(umbracoApplication, applicationContext);
}
#endregion
}

View File

@@ -16,34 +16,8 @@ using Umbraco.Core.Services;
namespace Umbraco.Web.PropertyEditors
{
[PropertyEditor(Constants.PropertyEditors.ImageCropperAlias, "Image Cropper", "imagecropper", ValueType = PropertyEditorValueTypes.Json, HideLabel = false, Group="media", Icon="icon-crop")]
public class ImageCropperPropertyEditor : PropertyEditor
public class ImageCropperPropertyEditor : PropertyEditor, IApplicationEventHandler
{
/// <summary>
/// We're going to bind to the MediaService Saving event so that we can populate the umbracoFile size, type, etc... label fields
/// if we find any attached to the current media item.
/// </summary>
/// <remarks>
/// I think this kind of logic belongs on this property editor, I guess it could exist elsewhere but it all has to do with the cropper.
/// </remarks>
static ImageCropperPropertyEditor()
{
MediaService.Saving += MediaServiceSaving;
MediaService.Created += MediaServiceCreated;
ContentService.Copied += ContentServiceCopied;
MediaService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
MediaService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
ContentService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
ContentService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
MemberService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
}
/// <summary>
/// Creates our custom value editor
/// </summary>
@@ -265,5 +239,54 @@ namespace Umbraco.Web.PropertyEditors
[PreValueField("crops", "Crop sizes", "views/propertyeditors/imagecropper/imagecropper.prevalues.html")]
public string Crops { get; set; }
}
#region Application event handler, used to bind to events on startup
private readonly FileUploadPropertyEditorApplicationStartup _applicationStartup = new FileUploadPropertyEditorApplicationStartup();
/// <summary>
/// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured
/// </summary>
private class FileUploadPropertyEditorApplicationStartup : ApplicationEventHandler
{
/// <summary>
/// We're going to bind to the MediaService Saving event so that we can populate the umbracoFile size, type, etc... label fields
/// if we find any attached to the current media item.
/// </summary>
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
MediaService.Saving += MediaServiceSaving;
MediaService.Created += MediaServiceCreated;
ContentService.Copied += ContentServiceCopied;
MediaService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
MediaService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
ContentService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
ContentService.EmptiedRecycleBin += (sender, args) =>
args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData));
MemberService.Deleted += (sender, args) =>
args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast<ContentBase>()));
}
}
public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationInitialized(umbracoApplication, applicationContext);
}
public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationStarting(umbracoApplication, applicationContext);
}
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
//wrap
_applicationStartup.OnApplicationStarted(umbracoApplication, applicationContext);
}
#endregion
}
}