rsanchez
2017-04-03 347803bd8d8349baa0577156896a1ec924a69e6d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Http } from '@angular/http';
import { LicensesService } 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;
  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) {
    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(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`]);
  }
  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;
          },
          err => console.error(err)
        );
    });
  }
}