| .. | .. |
|---|
| 1 | 1 | import { Http } from '@angular/http'; |
|---|
| 2 | 2 | import { LicensesService } from '../resources/licenses'; |
|---|
| 3 | +import { PacksService } from '../resources/packs'; |
|---|
| 3 | 4 | import { LocaleService } from '../common/i18n'; |
|---|
| 4 | 5 | import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core'; |
|---|
| 5 | 6 | import { IPageChangeEvent } from '@covalent/core'; |
|---|
| 6 | | -import { Component, AfterViewInit } from '@angular/core'; |
|---|
| 7 | | -import { TdMediaService } from '@covalent/core'; |
|---|
| 8 | | -import { IComboOption } from './base'; |
|---|
| 7 | +import { AfterViewInit, Component, ViewChild } from '@angular/core'; |
|---|
| 8 | +import { ActivatedRoute, Router } from '@angular/router'; |
|---|
| 9 | + |
|---|
| 10 | +import { TdMediaService, TdFileInputComponent } from '@covalent/core'; |
|---|
| 11 | +import { FormBase, IComboOption } from './base'; |
|---|
| 12 | +import { ToastsManager } from "ng2-toastr/ng2-toastr"; |
|---|
| 13 | + |
|---|
| 14 | +declare var window: any; |
|---|
| 9 | 15 | |
|---|
| 10 | 16 | @Component({ |
|---|
| 11 | 17 | selector: 'license-form', |
|---|
| 12 | 18 | templateUrl: 'src/app/forms/license.form.html' |
|---|
| 13 | 19 | }) |
|---|
| 14 | | -export class LicenseFormComponent implements AfterViewInit { |
|---|
| 20 | +export class LicenseFormComponent extends FormBase { |
|---|
| 15 | 21 | |
|---|
| 16 | | - form_title: string = 'Title'; |
|---|
| 22 | + @ViewChild('requestFileUploader') requestFileUploader : TdFileInputComponent; |
|---|
| 23 | + form_title: string = ''; |
|---|
| 17 | 24 | form_subtitle: string = ''; |
|---|
| 18 | 25 | data: any = {}; |
|---|
| 26 | + pack: any = null; |
|---|
| 19 | 27 | isNew : boolean = true; |
|---|
| 20 | 28 | |
|---|
| 21 | 29 | constructor(private http: Http, |
|---|
| 22 | | - private packs: LicensesService, |
|---|
| 23 | | - private $L: LocaleService) { |
|---|
| 24 | | - |
|---|
| 30 | + private licenses: LicensesService, |
|---|
| 31 | + private router: Router, |
|---|
| 32 | + private packs: PacksService, |
|---|
| 33 | + toaster: ToastsManager, |
|---|
| 34 | + route: ActivatedRoute, |
|---|
| 35 | + $L: LocaleService) { |
|---|
| 36 | + super($L, route, toaster, licenses, $L.get('license')); |
|---|
| 25 | 37 | } |
|---|
| 26 | 38 | |
|---|
| 27 | | - private loadCombos(): void { |
|---|
| 28 | | - /* |
|---|
| 29 | | - this.http.get('organization') |
|---|
| 30 | | - .map(response => response.json().map((org : any) => <IComboOption>{id: org.id, label: `(${org.code}) ${org.name}`})) |
|---|
| 31 | | - .subscribe( |
|---|
| 32 | | - data => this.organizations = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label)), |
|---|
| 33 | | - err => console.error('Error loading orgs') |
|---|
| 34 | | - ); |
|---|
| 35 | | - */ |
|---|
| 36 | | - } |
|---|
| 37 | | - |
|---|
| 38 | | - log(obj: any) { |
|---|
| 39 | | - console.log(obj) |
|---|
| 39 | + requestFileSelected(file: File) : void { |
|---|
| 40 | + console.log(file); |
|---|
| 41 | + console.log(this.requestFileUploader); |
|---|
| 42 | + if (!window.FileReader) { // Browser is not |
|---|
| 43 | + // compatible |
|---|
| 44 | + console.log(this.$L.get("Open your .req file with a text editor and copy&paste the content in the form text field?")); |
|---|
| 45 | + return; |
|---|
| 46 | + } |
|---|
| 47 | + var reader = new FileReader(); |
|---|
| 48 | + reader.onerror = (err) => console.error(err); |
|---|
| 49 | + |
|---|
| 50 | + reader.onload = (event) => { |
|---|
| 51 | + this.data.request_data = reader.result; |
|---|
| 52 | + } |
|---|
| 53 | + reader.readAsText(file); |
|---|
| 54 | + this.requestFileUploader.clear(); |
|---|
| 55 | + } |
|---|
| 56 | + |
|---|
| 57 | + requestFileUploaded(file: File) : void { |
|---|
| 58 | + console.log(file); |
|---|
| 40 | 59 | } |
|---|
| 41 | 60 | |
|---|
| 42 | | - ngOnInit(): void { |
|---|
| 43 | | - this.loadCombos(); |
|---|
| 44 | | - this.data = {}; |
|---|
| 45 | | - this.form_title = this.$L.get('License data'); |
|---|
| 46 | | - this.form_subtitle = this.$L.get(this.isNew ? 'Create a new license': 'Modify the license data') ; |
|---|
| 61 | + createActivationCode() : string { |
|---|
| 62 | + // http://www.ietf.org/rfc/rfc4122.txt |
|---|
| 63 | + var s = new Array(36); |
|---|
| 64 | + var hexDigits = "0123456789abcdef"; |
|---|
| 65 | + for (var i = 0; i < 36; i++) { |
|---|
| 66 | + s[i] = hexDigits.substr(Math.random() * 0x10 | 0, 1); |
|---|
| 67 | + } |
|---|
| 68 | + s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 |
|---|
| 69 | + s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 |
|---|
| 70 | + s[8] = s[13] = s[18] = s[23] = "-"; |
|---|
| 71 | + |
|---|
| 72 | + var uuid = s.join(""); |
|---|
| 73 | + return uuid; |
|---|
| 74 | + } |
|---|
| 75 | + |
|---|
| 76 | + goBack(): void { |
|---|
| 77 | + this.router.navigate([`packs/${this.pack.id}/licenses`]); |
|---|
| 47 | 78 | } |
|---|
| 48 | 79 | |
|---|
| 49 | 80 | |
|---|
| 50 | 81 | ngAfterViewInit(): void { |
|---|
| 82 | + |
|---|
| 83 | + this.route.params.subscribe(params => { |
|---|
| 84 | + var packId = +params['packId']; // (+) converts string 'id' to a number |
|---|
| 85 | + super.prepareData('licenseId', { |
|---|
| 86 | + pack_id: packId, |
|---|
| 87 | + activation_code: this.createActivationCode() |
|---|
| 88 | + }); |
|---|
| 89 | + }); |
|---|
| 51 | 90 | } |
|---|
| 52 | 91 | } |
|---|
| 53 | 92 | |
|---|