@rdkmaster/jigsaw-labs
Version:
Jigsaw, the next generation component set for RDK
227 lines (197 loc) • 8.07 kB
text/typescript
import {
Component,
Directive,
ElementRef,
EventEmitter,
HostListener,
Input,
OnDestroy,
Optional,
Output,
Renderer2
} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {
ButtonInfo, IPopupable, PopupEffect, PopupInfo, PopupOptions, PopupPositionType, PopupPositionValue, PopupService
} from "../../service/popup.service";
import {JigsawUploadBase, UploadFileInfo} from "./upload.base";
import {AbstractJigsawComponent} from "../common";
import {TranslateService} from "@ngx-translate/core";
export class JigsawUploadDirective extends JigsawUploadBase implements OnDestroy {
constructor( protected _http: HttpClient,
protected _renderer: Renderer2,
protected _elementRef: ElementRef,
private _popupService: PopupService,
protected _translateService: TranslateService) {
super(_http, _renderer, _elementRef, _translateService);
}
private _removeMouseOverHandler: Function;
private _removeMouseOutHandler: Function;
private _rollOutDenouncesTimer: any = null;
private _rollInDenouncesTimer: any = null;
public targetUrl: string = '/rdk/service/common/upload';
public fileType: string;
public multiple: boolean = true;
public progress = new EventEmitter<UploadFileInfo>();
public remove = new EventEmitter<UploadFileInfo>();
public complete = new EventEmitter<UploadFileInfo[]>();
public start = new EventEmitter<void>();
public update = new EventEmitter<UploadFileInfo[]>();
public uploadOptionCount: number;
onClick($event) {
this._$selectFile($event);
}
onMouseEnter() {
if (!this._$fileInfoList.length) return;
this.clearCallLater(this._rollOutDenouncesTimer);
this._addRollInDenouncesTimer();
}
onMouseLeave() {
this.clearCallLater(this._rollInDenouncesTimer);
this._addRollOutDenouncesTimer();
}
private _addRollInDenouncesTimer() {
this._rollInDenouncesTimer = this.callLater(() => {
if (this._popupInfo) return;
this._popupInfo = this._popupService.popup(JigsawUploadFileInfoList, this._getUnModalOptions(), this._$fileInfoList);
if (!this._popupInfo || !this._popupInfo.element || !this._popupInfo.instance) {
console.error('unable to popup drop down, unknown error!');
return;
}
if (this._popupInfo.instance instanceof JigsawUploadFileInfoList) {
this._popupInfo.instance.uploader = this;
this._popupInfo.instance.optionCount = this.uploadOptionCount;
this._popupInfo.instance.removable = false;
}
this._closeAllListener();
this._removeMouseOverHandler = this._renderer.listen(
this._popupInfo.element, 'mouseenter',
() => this.clearCallLater(this._rollOutDenouncesTimer));
this._removeMouseOutHandler = this._renderer.listen(
this._popupInfo.element, 'mouseleave', () => {
this._addRollOutDenouncesTimer();
});
}, 100);
}
private _addRollOutDenouncesTimer() {
this._rollOutDenouncesTimer = this.callLater(() => {
this._closePopup();
}, 400);
}
private _popupInfo: PopupInfo;
private _getUnModalOptions(): PopupOptions {
return {
modal: false,
showEffect: PopupEffect.fadeIn,
hideEffect: PopupEffect.fadeOut,
pos: this._elementRef,
posOffset: {
top: this._elementRef.nativeElement.offsetHeight
},
posReviser: (pos: PopupPositionValue, popupElement: HTMLElement): PopupPositionValue => {
return this._popupService.positionReviser(pos, popupElement, {
offsetWidth: this._elementRef.nativeElement.offsetWidth,
offsetHeight: this._elementRef.nativeElement.offsetHeight
});
},
size: {width: 300},
posType: PopupPositionType.absolute
};
}
private _closePopup() {
if (this._popupInfo) {
this._popupInfo.dispose();
this._popupInfo = null;
}
this._closeAllListener();
}
private _closeAllListener() {
if (this._removeMouseOverHandler) {
this._removeMouseOverHandler();
this._removeMouseOverHandler = null;
}
if (this._removeMouseOutHandler) {
this._removeMouseOutHandler();
this._removeMouseOutHandler = null;
}
}
protected _upload() {
super._upload();
this._reCalculatePopupPosition();
}
public _$removeFile(file) {
super._$removeFile(file);
this._reCalculatePopupPosition();
}
private _reCalculatePopupPosition() {
setTimeout(() => {
if (this._popupInfo) {
this._popupService.setPosition(this._getUnModalOptions(), this._popupInfo.element);
}
});
}
ngOnDestroy() {
super.ngOnDestroy();
this._closePopup();
}
}
export class JigsawUploadFileInfoList extends AbstractJigsawComponent implements IPopupable {
public answer: EventEmitter<ButtonInfo>;
public initData: UploadFileInfo[];
public uploader: JigsawUploadDirective;
public optionCount: number = 5;
public removable: boolean = true;
}