From aff03997d974bfa62638c2006041b071748fc96a Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 1 Jun 2016 11:27:08 +0200 Subject: [PATCH] Moves static event binding for prop editors to startup handlers --- .../FileUploadPropertyEditor.cs | 75 ++++++++++++------ .../PropertyEditors/GridPropertyEditor.cs | 55 +++++++++---- .../ImageCropperPropertyEditor.cs | 77 ++++++++++++------- 3 files changed, 140 insertions(+), 67 deletions(-) diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index 72beb09e69..3c30586383 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -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 { - /// - /// 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. - /// - /// - /// 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. - /// - static FileUploadPropertyEditor() - { - MediaService.Saving += MediaServiceSaving; - MediaService.Created += MediaServiceCreating; - ContentService.Copied += ContentServiceCopied; - - MediaService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); - MediaService.EmptiedRecycleBin += (sender, args) => - args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); - ContentService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); - ContentService.EmptiedRecycleBin += (sender, args) => - args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); - MemberService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); - } /// /// 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(); + + /// + /// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured + /// + private class FileUploadPropertyEditorApplicationStartup : ApplicationEventHandler + { + /// + /// 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. + /// + 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())); + MediaService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); + ContentService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); + ContentService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); + MemberService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); + } + } + + 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 + } } diff --git a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs index 85177540d2..5bb05cd6f6 100644 --- a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs @@ -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 - { - /// - /// We're going to bind to the Examine events so we can ensure grid data is index nicely - /// - /// - /// I think this kind of logic belongs on this property editor, putting this inside of the indexer certainly doesn't seem right - /// - static GridPropertyEditor() - { - foreach (var i in ExamineManager.Instance.IndexProviderCollection.OfType()) - { - 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(); + + /// + /// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured + /// + private class GridPropertyEditorApplicationStartup : ApplicationEventHandler + { + /// + /// We're going to bind to the Examine events so we can ensure grid data is index nicely. + /// + protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) + { + foreach (var i in ExamineManager.Instance.IndexProviderCollection.OfType()) + { + 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 } diff --git a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs index 1072448628..14c267cf7d 100644 --- a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs @@ -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 { - - /// - /// 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. - /// - /// - /// 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. - /// - static ImageCropperPropertyEditor() - { - MediaService.Saving += MediaServiceSaving; - MediaService.Created += MediaServiceCreated; - ContentService.Copied += ContentServiceCopied; - - MediaService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); - MediaService.EmptiedRecycleBin += (sender, args) => - args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); - ContentService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); - ContentService.EmptiedRecycleBin += (sender, args) => - args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); - MemberService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); - } - /// /// Creates our custom value editor /// @@ -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(); + + /// + /// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured + /// + private class FileUploadPropertyEditorApplicationStartup : ApplicationEventHandler + { + /// + /// 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. + /// + 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())); + MediaService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); + ContentService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); + ContentService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(ServiceEmptiedRecycleBin(args.AllPropertyData)); + MemberService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); + } + } + + 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 } }