rsanchez
2017-03-19 280daa7f3f858ecfef9c91ffd5dea1007f021048
securis/src/main/webapp/src/app/forms/license.form.component.ts
....@@ -1,53 +1,92 @@
11 import { Http } from '@angular/http';
22 import { LicensesService } from '../resources/licenses';
3
+import { PacksService } from '../resources/packs';
34 import { LocaleService } from '../common/i18n';
45 import { TdDataTableService, TdDataTableSortingOrder, ITdDataTableSortChangeEvent, ITdDataTableColumn } from '@covalent/core';
56 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;
915
1016 @Component({
1117 selector: 'license-form',
1218 templateUrl: 'src/app/forms/license.form.html'
1319 })
14
-export class LicenseFormComponent implements AfterViewInit {
20
+export class LicenseFormComponent extends FormBase {
1521
16
- form_title: string = 'Title';
22
+ @ViewChild('requestFileUploader') requestFileUploader : TdFileInputComponent;
23
+ form_title: string = '';
1724 form_subtitle: string = '';
1825 data: any = {};
26
+ pack: any = null;
1927 isNew : boolean = true;
2028
2129 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'));
2537 }
2638
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);
4059 }
4160
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`]);
4778 }
4879
4980
5081 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
+ });
5190 }
5291 }
5392