/// <summary> /// Creates a file collection manager. /// </summary> /// <param name="fileCollectionId"></param> /// <param name="displaySetup"></param> /// <param name="postBackIdBase">Do not pass null.</param> /// <param name="sortByName"></param> /// <param name="thumbnailResourceGetter">A function that takes a file ID and returns the corresponding thumbnail resource. Do not return null.</param> /// <param name="openedFileIds">The file IDs that should not be marked with a UI element drawing the user’s attention to the fact that they haven’t read it. /// All other files not in this collection will be marked. The collection can be null, and will result as nothing being shown as new.</param> /// <param name="unopenedFileOpenedNotifier">A method that executes when an unopened file is opened. Use to update the app’s database with an indication /// that the file has been seen by the user.</param> /// <param name="disableModifications">Pass true if there should be no way to upload or delete files.</param> /// <param name="uploadValidationMethod"></param> /// <param name="fileCreatedOrReplacedNotifier">A method that executes after a file is created or replaced.</param> /// <param name="filesDeletedNotifier">A method that executes after one or more files are deleted.</param> public BlobFileCollectionManager( int fileCollectionId, DisplaySetup displaySetup = null, string postBackIdBase = "", bool sortByName = false, Func <int, ResourceInfo> thumbnailResourceGetter = null, IEnumerable <int> openedFileIds = null, MarkFileAsReadMethod unopenedFileOpenedNotifier = null, bool disableModifications = false, Action <RsFile, Validator> uploadValidationMethod = null, NewFileNotificationMethod fileCreatedOrReplacedNotifier = null, Action filesDeletedNotifier = null) { postBackIdBase = PostBack.GetCompositeId("ewfFileCollection", postBackIdBase); var columnSetups = new List <EwfTableField>(); if (thumbnailResourceGetter != null) { columnSetups.Add(new EwfTableField(size: 10.ToPercentage())); } columnSetups.Add(new EwfTableField(classes: new ElementClass("ewfOverflowedCell"))); columnSetups.Add(new EwfTableField(size: 13.ToPercentage())); columnSetups.Add(new EwfTableField(size: 7.ToPercentage())); var table = EwfTable.Create( postBackIdBase: postBackIdBase, caption: "Files", selectedItemActions: disableModifications ? null : SelectedItemAction.CreateWithFullPostBackBehavior <int>( "Delete Selected Files", ids => { foreach (var i in ids) { BlobStorageStatics.SystemProvider.DeleteFile(i); } filesDeletedNotifier?.Invoke(); EwfPage.AddStatusMessage(StatusMessageType.Info, "Selected files deleted successfully."); }) .ToCollection(), fields: columnSetups); IReadOnlyCollection <BlobFile> files = BlobStorageStatics.SystemProvider.GetFilesLinkedToFileCollection(fileCollectionId); files = (sortByName ? files.OrderByName() : files.OrderByUploadedDateDescending()).Materialize(); foreach (var file in files) { addFileRow(postBackIdBase, thumbnailResourceGetter, openedFileIds, unopenedFileOpenedNotifier, table, file); } children = files.Any() || !disableModifications ? table.Concat( !disableModifications ?getUploadComponents( fileCollectionId, files, displaySetup, postBackIdBase, uploadValidationMethod, fileCreatedOrReplacedNotifier ) : Enumerable.Empty <FlowComponent>()) .Materialize() : Enumerable.Empty <FlowComponent>().Materialize(); }
private IReadOnlyCollection <FlowComponent> getUploadComponents( int fileCollectionId, IReadOnlyCollection <BlobFile> files, DisplaySetup displaySetup, string postBackIdBase, Action <RsFile, Validator> uploadValidationMethod, NewFileNotificationMethod fileCreatedOrReplacedNotifier) { RsFile file = null; var dm = PostBack.CreateFull( id: PostBack.GetCompositeId(postBackIdBase, "add"), firstModificationMethod: () => { if (file == null) { return; } var existingFile = files.SingleOrDefault(i => i.FileName == file.FileName); int newFileId; if (existingFile != null) { BlobStorageStatics.SystemProvider.UpdateFile( existingFile.FileId, file.FileName, file.Contents, BlobStorageStatics.GetContentTypeForPostedFile(file)); newFileId = existingFile.FileId; } else { newFileId = BlobStorageStatics.SystemProvider.InsertFile( fileCollectionId, file.FileName, file.Contents, BlobStorageStatics.GetContentTypeForPostedFile(file)); } fileCreatedOrReplacedNotifier?.Invoke(newFileId); EwfPage.AddStatusMessage(StatusMessageType.Info, "File uploaded successfully."); }); return(FormState.ExecuteWithDataModificationsAndDefaultAction( dm.ToCollection(), () => new StackList( new FileUpload( validationMethod: (postBackValue, validator) => { file = postBackValue; uploadValidationMethod?.Invoke(postBackValue, validator); }).ToFormItem() .ToListItem() .Append(new EwfButton(new StandardButtonStyle("Upload new file")).ToCollection().ToComponentListItem())).ToFormItem( setup: new FormItemSetup(displaySetup: displaySetup), label: "Select and upload a new file:".ToComponents()) .ToComponentCollection())); }
private ControlList getUploadControlList() { RsFile file = null; var dm = PostBack.CreateFull( id: PostBack.GetCompositeId(postBackIdBase, "add"), firstModificationMethod: () => { if (file == null) { return; } var existingFile = files.SingleOrDefault(i => i.FileName == file.FileName); int newFileId; if (existingFile != null) { BlobFileOps.SystemProvider.UpdateFile(existingFile.FileId, file.FileName, file.Contents, BlobFileOps.GetContentTypeForPostedFile(file)); newFileId = existingFile.FileId; } else { newFileId = BlobFileOps.SystemProvider.InsertFile(fileCollectionId, file.FileName, file.Contents, BlobFileOps.GetContentTypeForPostedFile(file)); } NewFileNotificationMethod?.Invoke(newFileId); EwfPage.AddStatusMessage(StatusMessageType.Info, "File uploaded successfully."); }); return(FormState.ExecuteWithDataModificationsAndDefaultAction( dm.ToCollection(), () => { var fi = FormItem.Create( "", new EwfFileUpload(), validationGetter: control => new EwfValidation( (pbv, validator) => { BlobFileOps.ValidateUploadedFile(validator, control, acceptableFileExtensions, ValidateImage, AcceptOnlyImages); file = control.GetPostBackValue(pbv); })); return ControlList.CreateWithControls( true, "Select and upload a new file:", fi.ToControl(), new PostBackButton(new ButtonActionControlStyle("Upload new file"), usesSubmitBehavior: false)); })); }