rsanchez
2017-06-07 89a0646d18da6f3290a883121e38f4086a6fb37e
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Router, ActivatedRoute } from '@angular/router';
import { Location } from '@angular/common';
import { ToastsManager } from 'ng2-toastr/ng2-toastr';
import { MdDialog, MdDialogConfig } from '@angular/material';
import {
    ITdDataTableColumn,
    ITdDataTableSortChangeEvent,
    TdDataTableService,
    TdDataTableSortingOrder,
    TdPagingBarComponent
} from '@covalent/core';
import { IPageChangeEvent } from '@covalent/core';
import { Component, ViewChild, AfterViewInit } from '@angular/core';
import { TdMediaService } from '@covalent/core';
import { PacksService, PACK_ACTIONS, PacksFilter } from '../resources/packs';
import { PackFormComponent } from '../forms/pack.form.component';
import { LocaleService } from '../common/i18n';
import { ListingBase } from './base';
/**
var pack_example = { 
  id: 7,
  code: 'DX250000',
  status: 'AC',
  application_name: 'Doxr',
  created_by_id: 'admin',
  created_by_name: 'Administrator  (admin)',
  creation_timestamp: 1440597540000,
  end_valid_date: 2051222400000,
  init_valid_date: 1440547200000,
  license_preactivation: true,
  license_type_id: 5,
  licensetype_code: 'DXL3',
  metadata: 
   [ { key: 'max_docs',
       value: '250000',
       readonly: true,
       mandatory: true,
       pack_id: 7 } ],
  num_activations: 7,
  num_available: -2,
  num_creations: 7,
  num_licenses: 5,
  organization_id: 2,
  organization_name: 'CurisTec',
  preactivation_valid_period: 70,
  renew_valid_period: 0,
}
*/
@Component({
  selector: 'pack-list',
  templateUrl: 'src/app/listing/pack.list.component.html'
})
export class PackListComponent extends ListingBase implements AfterViewInit {
  columns: ITdDataTableColumn[] = [
    { name: 'code', label: 'Code', tooltip: 'License pack code' },
    { name: 'application_name', label: 'App name' },
    { name: 'licensetype_code', label: 'License type' },
    { name: 'organization_name', label: 'Organization' },
    { name: 'used_licenses', label: 'Licenses', tooltip: 'Initial/Available pack licenses' },
    { name: 'status', label: 'Status', tooltip: 'Pack status' },
    { name: 'menu', label: '' }
  ];
  pack_menu_options = PACK_ACTIONS;
  filter_description : string;
  prevUrl : string = null;
  constructor(_dataTableService: TdDataTableService,
    private media: TdMediaService,
    private router: Router,
    private route: ActivatedRoute,
    private dialog: MdDialog,
    private $L: LocaleService,
    private toaster: ToastsManager,
    private packForm: PackFormComponent,
    private packs: PacksService) {
      super(_dataTableService);
  }
  reload(filter?: PacksFilter) : void  {
    this.prepareFilteredBehavior(filter);
    this.packs.get(filter).subscribe(
      (list : any[]) => {
        this.data = list;
        this.refresh();
      },
      (err: any) => console.error(err)
    );
  }
  goBack() : void {
    if (this.prevUrl) {
      this.router.navigate([this.prevUrl]);
    }
  }
  prepareFilteredBehavior(filter: PacksFilter) {
    if (!filter) {
      this.filter_description = '';
      this.prevUrl = null;
    } else {
      if (!!filter.applicationId) {
        this.filter_description = `Application: ${filter.name}`;
        this.prevUrl = 'applications';
      } else if (!!filter.organizationId) {
        this.filter_description = `Organization: ${filter.name}`;
        this.prevUrl = 'organizations';
      } else if (!!filter.licenseTypeId) {
        this.filter_description = `License type: ${filter.name}`;
        this.prevUrl = 'licensetypes';
      } else {
        this.filter_description = '';
        this.prevUrl = null;
      }
    }
  }
  packAction(action: string, pack: any) {
    return this.packs[action](pack.id).subscribe(
      (actionResponse : any) => {
        this.toaster.success(this.$L.get('Action "{}" executed successfully', action));
        this.reload();
      },
      (err : any) => this.toaster.error(err.message, this.$L.get('Action "{}" failed', action))
    );
  }
  showLicenses(pack: any) : void {
    this.router.navigate([`${pack.id}/licenses`], {relativeTo: this.route});
  }
  create() : void {
    this.router.navigate([`create`], {relativeTo: this.route});
  }
  edit(packId: number) : void {
    this.router.navigate([`edit/${packId}`], {relativeTo: this.route});
  }
  sort(sortEvent: ITdDataTableSortChangeEvent): void {
    this.sortBy = sortEvent.name === 'used_licenses' ? 'num_available' : sortEvent.name;
    this.sortOrder = sortEvent.order;
    this.refresh();
  }
 
  ngAfterViewInit(): void {
    this.media.broadcast();
    this.route.queryParams.subscribe(params => {
        let filter = params as PacksFilter;
        this.reload(filter);
      });
  }
}