Refactor support for the files component
continuous-integration/drone/push Build is passing Details
continuous-integration/drone Build is passing Details

pull/18/head
Garrett Mills 4 years ago
parent e30e8681e4
commit 9a53faf338
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246

@ -4,7 +4,6 @@
<input style="margin-top: 10px;" type="file" id="file" name="uploaded_file"> <input style="margin-top: 10px;" type="file" id="file" name="uploaded_file">
<input type="hidden" name="redirectTo" [value]="getReturnUrl()"> <input type="hidden" name="redirectTo" [value]="getReturnUrl()">
<ion-button (click)="onSubmitClick()" type="submit" fill="outline" class="ion-margin-start">Upload</ion-button> <ion-button (click)="onSubmitClick()" type="submit" fill="outline" class="ion-margin-start">Upload</ion-button>
<ion-button (click)="onDestroyClick()" type="submit" fill="outline" class="ion-margin-start" color="danger">Drop Files</ion-button>
</form> </form>
</div> </div>
<div class="existing-uploads"> <div class="existing-uploads">

@ -4,116 +4,122 @@ import {ApiService} from '../../../service/api.service';
import {AlertController} from '@ionic/angular'; import {AlertController} from '@ionic/angular';
import {Observable} from 'rxjs'; import {Observable} from 'rxjs';
import { APP_BASE_HREF } from '@angular/common'; import { APP_BASE_HREF } from '@angular/common';
import {EditorService} from '../../../service/editor.service';
import {EditorNodeContract} from '../../nodes/EditorNode.contract';
@Component({ @Component({
selector: 'editor-files', selector: 'editor-files',
templateUrl: './files.component.html', templateUrl: './files.component.html',
styleUrls: ['./files.component.scss'], styleUrls: ['./files.component.scss'],
}) })
export class FilesComponent implements OnInit { export class FilesComponent extends EditorNodeContract implements OnInit {
@Input() readonly = false; @Input() nodeId: string;
@Input() hostRecord: HostRecord;
@Output() hostRecordChange = new EventEmitter<HostRecord>();
@Output() requestParentSave = new EventEmitter<FilesComponent>();
@Output() requestParentDelete = new EventEmitter<FilesComponent>();
@ViewChild('uploadForm') uploadForm: ElementRef; @ViewChild('uploadForm') uploadForm: ElementRef;
// @Input() readonly = false;
// @Input() hostRecord: HostRecord;
// @Output() hostRecordChange = new EventEmitter<HostRecord>();
// @Output() requestParentSave = new EventEmitter<FilesComponent>();
// @Output() requestParentDelete = new EventEmitter<FilesComponent>();
public fileRecords: Array<any> = []; public fileRecords: Array<any> = [];
public pendingSetup = true; public pendingSetup = true;
public dbRecord: any = {}; public dbRecord: any = {};
public dirty = false;
public get readonly() {
return !this.node || !this.editorService.canEdit();
}
constructor( constructor(
protected api: ApiService, protected api: ApiService,
protected alerts: AlertController, protected alerts: AlertController,
public readonly editorService: EditorService,
@Inject(APP_BASE_HREF) private baseHref: string @Inject(APP_BASE_HREF) private baseHref: string
) { } ) { super(); }
ngOnInit() {
this.getInitObservable().subscribe(() => {
}); public isDirty(): boolean | Promise<boolean> {
return this.dirty;
} }
getApiSubmit() { public writeChangesToNode(): void | Promise<void> {
return this.api._build_url(`file/upload/${this.hostRecord.PageId}/${this.hostRecord.UUID}/${this.hostRecord.Value.Value}`); this.node.Value.Mode = 'files';
} }
onSubmitClick() { public needsLoad(): boolean | Promise<boolean> {
if ( this.readonly ) { return this.node && this.pendingSetup;
return;
}
this.uploadForm.nativeElement.submit();
} }
getReturnUrl() { public async performLoad(): Promise<void> {
return `${this.baseHref}editor;id=${this.hostRecord.PageId}`; if ( !this.node.Value ) {
this.node.Value = {};
} }
downloadFile(fileRecord) { if ( !this.node.Value.Value && !this.readonly ) {
// tslint:disable-next-line:max-line-length await new Promise((res, rej) => {
window.open(this.api._build_url(`files/${this.hostRecord.PageId}/${this.hostRecord.UUID}/get/${this.hostRecord.Value.Value}/${fileRecord._id}`), '_blank'); this.api.post(`/files/${this.page.UUID}/${this.node.UUID}/create`).subscribe({
next: result => {
this.dbRecord = result.data;
this.fileRecords = result.data.files;
this.node.Value.Mode = 'files';
this.node.Value.Value = result.data.UUID;
this.node.value = result.data.UUID;
this.dirty = true;
res();
},
error: rej,
});
});
} else {
await new Promise((res, rej) => {
this.api.get(`/files/${this.page.UUID}/${this.node.UUID}/get/${this.node.Value.Value}`).subscribe({
next: result => {
this.dbRecord = result.data;
this.fileRecords = result.data.files;
res();
},
error: rej,
});
});
} }
async onDestroyClick() { this.pendingSetup = false;
if ( this.readonly ) {
return;
} }
const alert = await this.alerts.create({ public async performDelete(): Promise<void> {
header: 'Are you sure?', await new Promise((res, rej) => {
message: 'You are about to delete these files. This action cannot be undone.', this.api.post(`/files/${this.page.UUID}/${this.node.UUID}/delete/${this.node.Value.Value}`).subscribe({
buttons: [ next: result => {
{ res();
text: 'Keep Them',
role: 'cancel',
}, },
{ error: rej,
text: 'Delete Them',
handler: async () => {
this.api.post(`/files/${this.hostRecord.PageId}/${this.hostRecord.UUID}/delete/${this.hostRecord.Value.Value}`)
.subscribe(res => {
this.requestParentDelete.emit(this);
}); });
},
},
],
}); });
}
await alert.present(); ngOnInit() {
this.editorService.registerNodeEditor(this.nodeId, this).then(() => {
});
} }
getInitObservable(): Observable<any> { getApiSubmit() {
return new Observable<any>(sub => { return this.api._build_url(`file/upload/${this.page.UUID}/${this.node.UUID}/${this.node.Value.Value}`);
if ( this.hostRecord && this.pendingSetup ) {
if ( !this.hostRecord.Value ) {
this.hostRecord.Value = {};
} }
if ( !this.hostRecord.Value.Value && !this.readonly ) { onSubmitClick() {
this.api.post(`/files/${this.hostRecord.PageId}/${this.hostRecord.UUID}/create`).subscribe(res => { if ( this.readonly ) {
this.dbRecord = res.data; return;
this.fileRecords = res.data.files;
this.hostRecord.Value.Mode = 'files';
this.hostRecord.Value.Value = res.data.UUID;
this.hostRecord.value = res.data.UUID;
this.hostRecordChange.emit(this.hostRecord);
this.pendingSetup = false;
sub.next(true);
sub.complete();
});
} else {
this.api.get(`/files/${this.hostRecord.PageId}/${this.hostRecord.UUID}/get/${this.hostRecord.Value.Value}`).subscribe(res => {
this.fileRecords = res.data.files;
this.pendingSetup = false;
sub.next(true);
sub.complete();
});
} }
} else {
this.pendingSetup = true; this.uploadForm.nativeElement.submit();
} }
});
getReturnUrl() {
return `${this.baseHref}editor;id=${this.page.UUID}`;
} }
downloadFile(fileRecord) {
// tslint:disable-next-line:max-line-length
window.open(this.api._build_url(`files/${this.page.UUID}/${this.node.UUID}/get/${this.node.Value.Value}/${fileRecord._id}`), '_blank');
}
} }

@ -37,6 +37,9 @@
<ng-container *ngIf="node.type === 'code_ref'"> <ng-container *ngIf="node.type === 'code_ref'">
<editor-code style="flex: 1;" [nodeId]="node.UUID"></editor-code> <editor-code style="flex: 1;" [nodeId]="node.UUID"></editor-code>
</ng-container> </ng-container>
<ng-container *ngIf="node.type === 'file_ref'">
<editor-files style="flex: 1;" [nodeId]="node.UUID"></editor-files>
</ng-container>
</div> </div>
<button class="host-add-button" (click)="onAddClick($event)"> <button class="host-add-button" (click)="onAddClick($event)">
<i class="fa fa-plus"></i> Add Node <i class="fa fa-plus"></i> Add Node

Loading…
Cancel
Save