1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/gallery/share/share.gallery.component.ts

133 lines
3.9 KiB
TypeScript
Raw Normal View History

2018-05-23 08:58:12 +08:00
import {Component, OnDestroy, OnInit, TemplateRef} from '@angular/core';
2018-03-31 03:30:30 +08:00
import {Utils} from '../../../../common/Utils';
import {ShareService} from '../share.service';
import {GalleryService} from '../gallery.service';
import {ContentWrapper} from '../../../../common/entities/ConentWrapper';
import {SharingDTO} from '../../../../common/entities/SharingDTO';
import {Config} from '../../../../common/config/public/Config';
import {NotificationService} from '../../model/notification.service';
import {DirectoryDTO} from '../../../../common/entities/DirectoryDTO';
import {I18n} from '@ngx-translate/i18n-polyfill';
2018-05-23 08:58:12 +08:00
import {BsModalService} from 'ngx-bootstrap/modal';
import {BsModalRef} from 'ngx-bootstrap/modal/bs-modal-ref.service';
2018-11-29 06:49:33 +08:00
import {Subscription} from 'rxjs';
2017-07-09 18:03:17 +08:00
2017-07-04 01:17:49 +08:00
@Component({
2018-05-04 07:17:08 +08:00
selector: 'app-gallery-share',
2017-07-04 01:17:49 +08:00
templateUrl: './share.gallery.component.html',
styleUrls: ['./share.gallery.component.css'],
})
export class GalleryShareComponent implements OnInit, OnDestroy {
2018-05-04 07:17:08 +08:00
enabled = true;
url = '';
2017-07-04 01:17:49 +08:00
input = {
includeSubfolders: true,
valid: {
amount: 30,
type: ValidityTypes.Days
2017-07-10 04:36:25 +08:00
},
2018-03-31 03:30:30 +08:00
password: ''
2017-07-04 01:17:49 +08:00
};
2018-05-04 07:17:08 +08:00
currentDir = '';
2017-07-28 06:04:19 +08:00
sharing: SharingDTO = null;
2018-11-29 06:49:33 +08:00
contentSubscription: Subscription = null;
2017-07-09 18:03:17 +08:00
passwordProtection = false;
2018-02-04 08:50:42 +08:00
ValidityTypes: any;
2017-07-04 01:17:49 +08:00
2018-05-23 08:58:12 +08:00
modalRef: BsModalRef;
2018-05-14 04:59:57 +08:00
text = {
Yes: 'Yes',
No: 'No'
};
2017-07-09 18:03:17 +08:00
constructor(private _sharingService: ShareService,
public _galleryService: GalleryService,
2018-03-30 08:30:23 +08:00
private _notification: NotificationService,
2018-05-23 08:58:12 +08:00
public i18n: I18n,
private modalService: BsModalService) {
2018-02-04 08:50:42 +08:00
this.ValidityTypes = ValidityTypes;
2018-05-14 04:59:57 +08:00
this.text.Yes = i18n('Yes');
this.text.No = i18n('No');
2017-07-04 01:17:49 +08:00
}
ngOnInit() {
this.contentSubscription = this._galleryService.content.subscribe((content: ContentWrapper) => {
this.enabled = !!content.directory;
if (!this.enabled) {
return;
}
2017-07-20 02:47:09 +08:00
this.currentDir = Utils.concatUrls((<DirectoryDTO>content.directory).path, (<DirectoryDTO>content.directory).name);
2017-07-04 01:17:49 +08:00
});
2017-07-09 18:03:17 +08:00
this.passwordProtection = Config.Client.Sharing.passwordProtected;
2017-07-04 01:17:49 +08:00
}
ngOnDestroy() {
if (this.contentSubscription !== null) {
this.contentSubscription.unsubscribe();
}
}
calcValidity() {
2018-05-04 07:17:08 +08:00
switch (parseInt(this.input.valid.type.toString(), 10)) {
2017-07-04 01:17:49 +08:00
case ValidityTypes.Minutes:
return this.input.valid.amount * 1000 * 60;
case ValidityTypes.Hours:
return this.input.valid.amount * 1000 * 60 * 60;
case ValidityTypes.Days:
return this.input.valid.amount * 1000 * 60 * 60 * 24;
case ValidityTypes.Months:
return this.input.valid.amount * 1000 * 60 * 60 * 24 * 30;
}
2018-03-31 03:30:30 +08:00
throw new Error('unknown type: ' + this.input.valid.type);
2017-07-04 01:17:49 +08:00
}
async update() {
2017-07-28 06:04:19 +08:00
if (this.sharing == null) {
return;
}
2018-05-17 05:47:32 +08:00
this.url = this.i18n('loading..');
2018-05-04 07:17:08 +08:00
this.sharing = await this._sharingService.updateSharing(this.currentDir,
this.sharing.id, this.input.includeSubfolders, this.input.password, this.calcValidity());
2018-03-31 03:30:30 +08:00
this.url = Config.Client.publicUrl + '/share/' + this.sharing.sharingKey;
2017-07-04 01:17:49 +08:00
}
async get() {
2018-05-17 05:47:32 +08:00
this.url = this.i18n('loading..');
2017-07-09 18:03:17 +08:00
this.sharing = await this._sharingService.createSharing(this.currentDir, this.input.includeSubfolders, this.calcValidity());
2018-03-31 03:30:30 +08:00
this.url = Config.Client.publicUrl + '/share/' + this.sharing.sharingKey;
2017-07-09 18:03:17 +08:00
}
2018-05-23 08:58:12 +08:00
async openModal(template: TemplateRef<any>) {
2017-07-09 18:03:17 +08:00
await this.get();
2018-03-31 03:30:30 +08:00
this.input.password = '';
2018-05-23 08:58:12 +08:00
if (this.modalRef) {
this.modalRef.hide();
}
this.modalRef = this.modalService.show(template);
2018-03-31 03:30:30 +08:00
document.body.style.paddingRight = '0px';
2017-07-09 18:03:17 +08:00
}
onCopy() {
2018-03-31 03:30:30 +08:00
this._notification.success(this.i18n('Url has been copied to clipboard'));
2017-07-04 01:17:49 +08:00
}
2017-07-30 15:32:24 +08:00
public hideModal() {
2018-05-23 08:58:12 +08:00
this.modalRef.hide();
this.modalRef = null;
2017-07-30 05:39:06 +08:00
this.sharing = null;
}
2017-07-04 01:17:49 +08:00
}
2017-07-30 05:39:06 +08:00
export enum ValidityTypes {
2018-05-29 02:03:12 +08:00
Minutes = 1, Hours = 2, Days = 3, Months = 4
2017-07-04 01:17:49 +08:00
}