f8a80bb0e7a8cca8da31f8c6e09b710cf0712a61
[portal.git] / portal-FE-common / src / app / pages / dashboard-application-catalog / dashboard-application-catalog.component.ts
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  *
37  */
38 import { Component, OnInit } from '@angular/core';
39 import { GridsterConfig, GridsterItem } from 'angular-gridster2';
40 import { ApplicationCatalogService } from '../../shared/services/application-catalog/application-catalog.service';
41 import { IApplicationCatalog } from '../../shared/model/application-catalog.model';
42 import { IWidgetCatalog } from '../../shared/model/widget-catalog.model';
43 import { environment } from 'src/environments/environment';
44 import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
45 import { CatalogModalComponent } from '../catalog-modal/catalog-modal.component';
46 import { ExternalRequestAccessService } from 'src/app/shared/services/external-request-access-service/external-request-access.service';
47 import { UsersService } from 'src/app/shared/services/users/users.service';
48 import { AddTabFunctionService } from 'src/app/shared/services/tab/add-tab-function.service';
49
50 @Component({
51   selector: 'app-dashboard-application-catalog',
52   templateUrl: './dashboard-application-catalog.component.html',
53   styleUrls: ['./dashboard-application-catalog.component.scss']
54 })
55 export class DashboardApplicationCatalogComponent implements OnInit {
56
57   widgetCatalogData: IWidgetCatalog[];
58   appCatalogData: IApplicationCatalog[];
59   resultAccessValue: string;
60   orgUserId: string;
61   firstName: string;
62   lastName: string;
63   selectedSortType: any;
64   sortOptions: Array<any>;
65
66   get options(): GridsterConfig {
67     return this.applicationCatalogService.options;
68   } get layout(): GridsterItem[] {
69     return this.applicationCatalogService.layout;
70   } constructor(private applicationCatalogService: ApplicationCatalogService, private externalRequestAccessService: ExternalRequestAccessService, private userService: UsersService,private addTabFuntionService: AddTabFunctionService) {
71     this.sortOptions = [{
72       index: 0,
73       value: 'N',
74       title: 'Name'
75     },
76     {
77       index: 1,
78       value: 'L',
79       title: 'Last used'
80     },
81     {
82       index: 2,
83       value: 'F',
84       title: 'Most used'
85     },
86     {
87       index: 3,
88       value: 'M',
89       title: 'Manual'
90     }
91     ];
92     this.selectedSortType = {};
93    }
94
95   ngOnInit() {
96     this.applicationCatalogService.clearCatalog();
97     //this.selectedSortType = this.sortOptions[0];
98     this.getUserAppsSortTypePreference();
99     //
100     // //this.getUserAppsSortTypePreference();
101     //this.getAppCatalogService('N');
102   }
103
104   getUserAppsSortTypePreference() {
105     this.applicationCatalogService.getUserAppsSortTypePreference().subscribe(data => {
106       //console.log("getUserAppsSortTypePreference data"+data);
107       if (data) {
108         var resJson: any = {};
109         resJson.value = data;
110         if (resJson.value === "N" || resJson.value === "") {
111           resJson.index = 0;
112
113         } else if (resJson.value === "L") {
114           resJson.index = 1;
115
116         } else if (resJson.value === "F") {
117           resJson.index = 2;
118
119         } else {
120           resJson.index = 3;
121
122         } 
123           this.selectedSortType = this.sortOptions[resJson.index];
124         //console.log(this.selectedSortType);
125       this.getAppCatalogService(data);          
126       }
127           else {
128                   this.sortTypeChanged('N');
129           }
130           
131                   
132     }, error => {
133       console.log('getUserAppsSortTypePreference Error Object' + error.message);
134     });
135
136   }
137
138   sortTypeChanged(userAppSortTypePref: string) {
139     //console.log("check whether get into the method");
140     if (!userAppSortTypePref) {
141       this.selectedSortType = this.sortOptions[0];
142     }
143     else {
144       this.sortOptions.forEach(obj => {
145         if (obj.value == userAppSortTypePref) {
146           this.selectedSortType = obj;
147         }
148       })
149
150     }
151
152     this.getAppCatalogService(userAppSortTypePref);
153     this.saveAppsSortTypePreference(this.selectedSortType);
154
155   }
156
157   getAppCatalogService(userAppSortTypePref: string) {
158     //console.log("getAppCatalogServices called");
159         if(!userAppSortTypePref)
160     {
161       userAppSortTypePref = "N";
162           this.selectedSortType = this.sortOptions[0];
163       //console.log("userAppSortTypePref"+userAppSortTypePref);
164     }
165     this.applicationCatalogService.getAppsOrderBySortPref(userAppSortTypePref).subscribe(data => {
166       //console.log("Response data" + data);
167       this.appCatalogData = data;
168       if (data) {
169         this.applicationCatalogService.layout = [];
170         for (let entry of data) {
171           //console.log("Check the URL" + environment.api.appThumbnail);
172                   if(entry.applicationType != '3'){
173           var appCatalog = {
174             x: -1,
175             y: -1,
176             id: entry.id,
177             name: entry.name,
178             subHeaderText: entry.notes,
179             imageLink: environment.api.appThumbnail.replace(':appId', <string><any>entry.id),
180             applicationType: entry.applicationType,
181             select: entry.select,
182             access: entry.access,
183             pending: entry.pending,
184             order: entry.order,
185             url: entry.url,
186             appid: entry.id
187           };
188           this.applicationCatalogService.addItem(appCatalog);
189         }
190                 }
191       }
192     }, error => {
193       console.log('getAppCatalogServices Error Object' + error);
194     });
195   };
196
197   saveAppsSortTypePreference(selectedSortType: any) {
198     this.applicationCatalogService.saveAppsSortTypePreference(selectedSortType).subscribe();
199
200   }
201
202   openAddRoleModal(item: any) {
203     //console.log("OpenModal check" + item.id+" "+item.url);
204     if (item.applicationType =='2') {
205       // Link-based apps open in their own browser tab
206       window.open(item.url, '_blank');
207     } else{
208       var tabContent = {
209         id: new Date(),
210         title: item.name,
211         url: item.url,
212         appId: item.appid
213     };
214       this.addTabFuntionService.filter(tabContent);
215     }
216   }
217 }