import { Component, Input, Output, Inject, forwardRef, EventEmitter } from '@angular/core'; import { RdComponent } from '../../base/rdComponent'; import { RdLib } from '../../base/rdLib'; import { Portlet } from './portlet'; import { ServiceProviderPagingBase } from '../serviceProvider/serviceProviderPagingBase'; @Component({ selector: "rd-tool:[rd-type=download]", template: ` {{currentIteration}} / {{totalIterationCount}} ` }) export class PortletToolDownload extends RdComponent { constructor( @Inject(forwardRef(() => Portlet)) private portlet: Portlet) { super(); } @Input("rd-text") tooltip = RdLib.localization.translateEn("Export as Excel"); @Input("rd-provider") provider: ServiceProviderPagingBase; @Input("rd-items") items: any[]; @Output("rd-click") clickEvent = new EventEmitter(); public currentIteration; public totalIterationCount; public inProgress; click(event) { event.stopPropagation(); this.getAllItems((currentIteration, totalIterationCount) => { if (totalIterationCount > 1) { this.inProgress = true; this.currentIteration = currentIteration; this.totalIterationCount = totalIterationCount; } }) .then(listOfItems => { setTimeout(() => this.inProgress = false, 1000); }) .catch(() => { setTimeout(() => this.inProgress = false, 1000); var title = RdLib.localization.translateEn("Unexpected error occured."); var body = RdLib.localization.translateEn("Error"); RdLib.screenOperations.toastr.error(title, body); }); this.clickEvent.emit(null); } private getAllItems(progressCallback) { return new Promise((resolve, reject) => { if (this.items) resolve(this.items); else if (this.provider) { this.provider.getAllItems(progressCallback) .then(data => resolve(data)) .catch(() => reject()); } else { this.error('rd-items or rd-provider is needed.'); reject(); } }); } }