import { Http } from '@angular/http'; import { LicensesService, LICENSE_ACTIONS } from '../resources/licenses'; import { PacksService } from '../resources/packs'; import { LocaleService } from '../common/i18n'; import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core'; import { IPageChangeEvent } from '@covalent/core'; import { AfterViewInit, Component, ViewChild } from '@angular/core'; import { ActivatedRoute, Router } from '@angular/router'; import { TdMediaService, TdFileInputComponent, TdDialogService } from '@covalent/core'; import { FormBase, IComboOption } from './base'; import { ToastsManager } from "ng2-toastr/ng2-toastr"; declare var window: any; @Component({ selector: 'license-form', templateUrl: 'src/app/forms/license.form.html' }) export class LicenseFormComponent extends FormBase { @ViewChild('requestFileUploader') requestFileUploader : TdFileInputComponent; license_menu_options = LICENSE_ACTIONS; metadata: any[]; pack: any = null; constructor(private http: Http, private licenses: LicensesService, private packs: PacksService, router: Router, toaster: ToastsManager, route: ActivatedRoute, $L: LocaleService, dialogs: TdDialogService) { super($L, router, route, toaster, licenses, $L.get('license'), dialogs); } requestFileSelected(file: File) : void { console.log(file); console.log(this.requestFileUploader); if (!window.FileReader) { // Browser is not // compatible console.log(this.$L.get("Open your .req file with a text editor and copy&paste the content in the form text field")); return; } var reader = new FileReader(); reader.onerror = (err) => console.error(err); reader.onload = (event) => { this.data.request_data = reader.result; } reader.readAsText(file); this.requestFileUploader.clear(); } licenseAction(action: string) { 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(this.data.id, reason).subscribe( (actionResponse : any) => { this.toaster.success(this.$L.get('License cancelled sucessfully')); this.init(); }, (err : any) => this.toaster.error(err.message, this.$L.get('Action "{}" failed', action) ) ); } }); } else { return this.licenses[action](this.data.id).subscribe( (actionResponse : any) => { this.toaster.success(this.$L.get('Action "{}" executed successfully', action)); this.init(); }, (err : any) => this.toaster.error(err.message, this.$L.get('Action "{}" failed', action) ) ); } } canBeDeleted() : boolean { return !this.isNew && this.licenses.isActionAvailable('delete', this.data); } requestFileUploaded(file: File) : void { console.log(file); } createActivationCode() : string { // http://www.ietf.org/rfc/rfc4122.txt var s = new Array(36); var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.random() * 0x10 | 0, 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; } goBack(): void { this.router.navigate([`packs/${this.pack.id}/licenses`]); } editPack(): void { this.router.navigate([`packs/edit/${this.pack.id}`]); } ngAfterViewInit(): void { this.init(); } init(): void { this.route.params.subscribe(params => { var packId = +params['packId']; // (+) converts string 'id' to a number super.prepareInitialData('licenseId', { pack_id: packId, activation_code: this.createActivationCode() }); this.packs.get(packId).subscribe( packData => { this.pack = packData; this.metadata = packData.metadata; }, err => console.error(err) ); if (this.isNew) { this.packs.nextLicCode(packId).subscribe( code => { this.data.code = code; }, err => console.error(err) ); } }); } }