diff --git a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs index 809716af62..0f49b50070 100644 --- a/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/FileUploadPropertyEditor.cs @@ -32,11 +32,11 @@ namespace Umbraco.Web.PropertyEditors if (mediaFileSystem == null) throw new ArgumentNullException("mediaFileSystem"); if (contentSettings == null) throw new ArgumentNullException("contentSettings"); if (textService == null) throw new ArgumentNullException("textService"); + _applicationStartup = new FileUploadPropertyEditorApplicationStartup(this); _mediaFileSystem = mediaFileSystem; _contentSettings = contentSettings; _textService = textService; - MemberService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); + } /// @@ -59,7 +59,7 @@ namespace Umbraco.Web.PropertyEditors /// Ensures any files associated are removed /// /// - static IEnumerable ServiceEmptiedRecycleBin(Dictionary> allPropertyData) + IEnumerable ServiceEmptiedRecycleBin(Dictionary> allPropertyData) { var list = new List(); //Get all values for any image croppers found @@ -81,7 +81,7 @@ namespace Umbraco.Web.PropertyEditors /// Ensures any files associated are removed /// /// - static IEnumerable ServiceDeleted(IEnumerable deletedEntities) + IEnumerable ServiceDeleted(IEnumerable deletedEntities) { var list = new List(); foreach (var property in deletedEntities.SelectMany(deletedEntity => deletedEntity @@ -267,36 +267,60 @@ namespace Umbraco.Web.PropertyEditors } #region Application event handler, used to bind to events on startup - public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - } - public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - } + private readonly FileUploadPropertyEditorApplicationStartup _applicationStartup; /// - /// 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. + /// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured /// - /// - /// 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. - /// + private class FileUploadPropertyEditorApplicationStartup : ApplicationEventHandler + { + private FileUploadPropertyEditor _fileUploadPropertyEditor; + + public FileUploadPropertyEditorApplicationStartup(FileUploadPropertyEditor fileUploadPropertyEditor) + { + this._fileUploadPropertyEditor = fileUploadPropertyEditor; + } + + /// + /// 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 += _fileUploadPropertyEditor.MediaServiceSaving; + MediaService.Created += _fileUploadPropertyEditor.MediaServiceCreating; + ContentService.Copied += _fileUploadPropertyEditor.ContentServiceCopied; + + MediaService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(_fileUploadPropertyEditor.ServiceDeleted(args.DeletedEntities.Cast())); + MediaService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(_fileUploadPropertyEditor.ServiceEmptiedRecycleBin(args.AllPropertyData)); + ContentService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(_fileUploadPropertyEditor.ServiceDeleted(args.DeletedEntities.Cast())); + ContentService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(_fileUploadPropertyEditor.ServiceEmptiedRecycleBin(args.AllPropertyData)); + MemberService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(_fileUploadPropertyEditor.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) { - 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)); - } + //wrap + _applicationStartup.OnApplicationStarted(umbracoApplication, applicationContext); + } #endregion + } } diff --git a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs index 5eb3769e4d..522978fef1 100644 --- a/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/GridPropertyEditor.cs @@ -14,15 +14,13 @@ 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, IApplicationEventHandler - { - private readonly IExamineIndexCollectionAccessor _indexCollection; - + { /// /// Constructor /// public GridPropertyEditor(ILogger logger, IExamineIndexCollectionAccessor indexCollection) : base(logger) - { - _indexCollection = indexCollection; + { + _applicationStartup = new GridPropertyEditorApplicationStartup(indexCollection); } private static void DocumentWriting(object sender, Examine.LuceneEngine.DocumentWritingEventArgs e) @@ -132,23 +130,47 @@ namespace Umbraco.Web.PropertyEditors } #region Application event handler, used to bind to events on startup - public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - } - public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - } + private readonly GridPropertyEditorApplicationStartup _applicationStartup; /// - /// We're going to bind to the Examine events so we can ensure grid data is index nicely. - /// + /// 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 + { + private readonly IExamineIndexCollectionAccessor _indexCollection; + + public GridPropertyEditorApplicationStartup(IExamineIndexCollectionAccessor indexCollection) + { + this._indexCollection = indexCollection; + } + + /// + /// 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 _indexCollection.Indexes.Values.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) { - foreach (var i in _indexCollection.Indexes.Values.OfType()) - { - i.DocumentWriting += DocumentWriting; - } + //wrap + _applicationStartup.OnApplicationStarted(umbracoApplication, applicationContext); } #endregion } diff --git a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs index 229835125f..a9c040b022 100644 --- a/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs +++ b/src/Umbraco.Web/PropertyEditors/ImageCropperPropertyEditor.cs @@ -27,6 +27,9 @@ namespace Umbraco.Web.PropertyEditors { if (mediaFileSystem == null) throw new ArgumentNullException("mediaFileSystem"); if (contentSettings == null) throw new ArgumentNullException("contentSettings"); + + _applicationStartup = new FileUploadPropertyEditorApplicationStartup(this); + _mediaFileSystem = mediaFileSystem; _contentSettings = contentSettings; @@ -34,9 +37,7 @@ namespace Umbraco.Web.PropertyEditors { {"focalPoint", "{left: 0.5, top: 0.5}"}, {"src", ""} - }; - MemberService.Deleted += (sender, args) => - args.MediaFilesToDelete.AddRange(ServiceDeleted(args.DeletedEntities.Cast())); + }; } /// @@ -253,35 +254,58 @@ namespace Umbraco.Web.PropertyEditors } #region Application event handler, used to bind to events on startup - public void OnApplicationInitialized(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - } - public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext) - { - } + private readonly FileUploadPropertyEditorApplicationStartup _applicationStartup; /// - /// 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. + /// we're using a sub -class because this has the logic to prevent it from executing if the application is not configured /// - /// - /// 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. - /// + private class FileUploadPropertyEditorApplicationStartup : ApplicationEventHandler + { + private readonly ImageCropperPropertyEditor _imageCropperPropertyEditor; + + public FileUploadPropertyEditorApplicationStartup(ImageCropperPropertyEditor imageCropperPropertyEditor) + { + _imageCropperPropertyEditor = imageCropperPropertyEditor; + } + + /// + /// 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 += _imageCropperPropertyEditor.MediaServiceSaving; + MediaService.Created += _imageCropperPropertyEditor.MediaServiceCreated; + ContentService.Copied += _imageCropperPropertyEditor.ContentServiceCopied; + + MediaService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(_imageCropperPropertyEditor.ServiceDeleted(args.DeletedEntities.Cast())); + MediaService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(_imageCropperPropertyEditor.ServiceEmptiedRecycleBin(args.AllPropertyData)); + ContentService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(_imageCropperPropertyEditor.ServiceDeleted(args.DeletedEntities.Cast())); + ContentService.EmptiedRecycleBin += (sender, args) => + args.Files.AddRange(_imageCropperPropertyEditor.ServiceEmptiedRecycleBin(args.AllPropertyData)); + MemberService.Deleted += (sender, args) => + args.MediaFilesToDelete.AddRange(_imageCropperPropertyEditor.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) { - 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)); + //wrap + _applicationStartup.OnApplicationStarted(umbracoApplication, applicationContext); } #endregion }