import { ActivatedRoute, Router } from '@angular/router'; import { MdDialog, MdDialogConfig } from '@angular/material'; import { TdDialogService, TdDataTableService, TdPagingBarComponent, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core'; import { IPageChangeEvent } from '@covalent/core'; import { Component, AfterViewInit, ViewChild } from '@angular/core'; import { TdMediaService } from '@covalent/core'; import { Location } from '@angular/common'; import { ToastsManager } from 'ng2-toastr/ng2-toastr'; import { LocaleService } from '../common/i18n'; import { LicensesService, LICENSE_ACTIONS } from '../resources/licenses'; import { PacksService } from '../resources/packs'; import { LicenseFormComponent } from '../forms/license.form.component'; import { ListingBase } from './base'; var lic_example = { activation_code: '19fa8d30-29cb-4b59-81b5-3837af8204b6', code: 'CISA02-494-1', code_suffix: 1, created_by_id: '_client', creation_timestamp: 1447848747000, email: 'ccalvo@curisit.net', expiration_date: 1450440747000, full_name: 'César SA', id: 110, licenseData: '{"appCode":"CISA","appName":"CurisIntegrity SA","licenseCode":"CISA02-494-1","activationCode":"19fa8d30-29cb-4b59-81b5-3837af8204b6","expirationDate":1450440746790,"arch":"x86_64","osName":"Mac OS X","macAddresses":["60-03-08-95-AE-D0","B6-2B-33-E9-64-2D"],"crcLogo":"10f6379e0e1c00ebc403160307e3c5d0aba0727c9cae0bf1ac7cd19d84fdc80f","metadata":{"maxWellLifeLines":"50","simulationModes":"A1,A2,N1,QL"},"signature":"Tejun4bNbknxOyEmPaO/fGfGhv4URhVON/7bESxbODFWMJYKQqOPHrDiSUMlf6RbfWSVg2Dry8bY1WX881QGjTkBaHeDJKCy1EaJBwJ2nv9TYSMOiRj0eqMNYWE9/oLpvufHylAkPUpZwXVkSzTxmN+RvWa2Xt4Fu7xN+4PDHV4t7PSq7QwsFlD9ArgYC6Vx+zuL9WZANBtJ2gU/gKOE0CU0KjsB49RGQSFS/G27+H/YuDkCiQq7PC7VdVwYONQ2HO91fyPvInIrzDC5+sWHcUAqSCop//8klMy03hWl6VvAlaSP7kNM3KadyqXIJ3tx4Jwm1W+gBb3tngHzVCpYmw=="}', modification_timestamp: 1447848747000, pack_code: 'CISA02', pack_id: 18, request_data: '{"appCode":"CISA","activationCode":"19fa8d30-29cb-4b59-81b5-3837af8204b6","arch":"x86_64","osName":"Mac OS X","macAddresses":["60-03-08-95-AE-D0","B6-2B-33-E9-64-2D"],"crcLogo":"10f6379e0e1c00ebc403160307e3c5d0aba0727c9cae0bf1ac7cd19d84fdc80f"}', status: 'AC' } @Component({ selector: 'license-list', templateUrl: 'src/app/listing/license.list.component.html' }) export class LicenseListComponent extends ListingBase implements AfterViewInit { pack: any = null; columns: ITdDataTableColumn[] = [ { name: 'code', label: 'Code', tooltip: 'License code' }, { name: 'metadata_obsolete', label: 'Obsolete', tooltip: 'Metadata obsolete' }, { name: 'full_name', label: 'User name' }, { name: 'email', label: 'User email' }, { name: 'expiration_date', label: 'Expiration date' }, { name: 'status', label: 'Status' }, { name: 'menu', label: '' } ]; license_menu_options = LICENSE_ACTIONS; licenseAction(action: string, license: any) { if (action === 'cancel') { this.dialogs.openPrompt({ message: this.$L.get('Please, specify the reason to cancel the current license'), title: this.$L.get('License cancelation'), //OPTIONAL, hides if not provided value: '', cancelButton: this.$L.get('Close'), acceptButton: this.$L.get('Cancel license'), //OPTIONAL, defaults to 'ACCEPT' }).afterClosed().subscribe((reason: string) => { if (reason) { this.licenses.cancel(license.id, reason).subscribe( (actionResponse : any) => { this.toaster.success(this.$L.get('License cancelled sucessfully')); this.reload(this.pack.id); }, (err : any) => this.toaster.error(err.message, this.$L.get('Action "{}" failed', action) ) ); } }); } else { return this.licenses[action](license.id).subscribe( (actionResponse : any) => { this.toaster.success(this.$L.get('Action "{}" executed successfully', action)); this.reload(this.pack.id); }, (err : any) => this.toaster.error(err.message, this.$L.get('Action "{}" failed', action)) ); } } reload(packId: number) : void { this.licenses.getByPack(packId).subscribe( list => { this.data = list; this.refresh(); }, err => console.error(err) ); } isActionAvailable(action: string, license : any) : boolean { return this.licenses.isActionAvailable(action, license); } constructor( _dataTableService: TdDataTableService, private media: TdMediaService, private dialogs: TdDialogService, private $L: LocaleService, private router: Router, private location: Location, private toaster: ToastsManager, private route: ActivatedRoute, private dialog: MdDialog, private licenseForm: LicenseFormComponent, private licenses: LicensesService, private packs: PacksService) { super(_dataTableService); } ngOnInit(): void { } goBack() : void { this.router.navigate([`packs`]); } editPack(): void { this.router.navigate([`packs/edit/${this.pack.id}`]); } isLicenseExpired(lic: any): boolean { return lic.expiration_date < (new Date().getTime()); } create() : void { this.router.navigate([`packs/${this.pack.id}/licenses/create`]); } edit(licId: number | string) : void { this.router.navigate([`packs/${this.pack.id}/licenses/edit/${licId}`]); } ngAfterViewInit(): void { this.media.broadcast(); this.route.params.subscribe(params => { var packId = +params['packId']; // (+) converts string 'id' to a number this.reload(packId); this.packs.get(packId).subscribe( packData => { this.pack = packData; }, err => console.error(err) ); }); } }