Replace ecomp references
[portal.git] / ecomp-portal-FE-common / client / app / views / widget-catalog / widget-catalog.controller.js
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 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 'use strict';
39 (function () {
40
41     class WidgetCatalogCtrl {
42         constructor(widgetsService, $log, $cookies, $scope, conf, beReaderService, widgetsCatalogService, userProfileService,dashboardService,$compile, ngDialog) {
43             
44                 $scope.editWidgetModalPopup = function(availableData, resourceType) {
45                         $scope.editData = JSON.stringify(availableData);
46                         $scope.availableDataTemp = $scope.availableData;
47                         ngDialog.open({
48                                         templateUrl : 'app/views/dashboard/dashboard-widget-manage.html',
49                                         controller : 'CommonWidgetController',
50                                         resolve : {
51                                                 message : function message() {
52                                                         var message = {
53                                                                 type : resourceType,
54                                                                 availableData : $scope.editData
55                                                         };
56                                                         return message;
57                                                 }
58                                         }
59                                 }).closePromise.then(needUpdate => {    
60                                         if(resourceType=='NEWS'){
61                                                 $scope.updateNews();
62                                         }else if(resourceType=='EVENTS'){
63                                                 $scope.updateEvents();
64                                         }else if(resourceType=='IMPORTANTRESOURCES'){
65                                                 $scope.updateImportRes();
66                                         }
67                   });           
68                 };
69                 
70                 $scope.WidgetCatView = [];  
71                 $scope.applyPresentationDetailsToWidgetsCatalog = function(widgetsReturned){
72                         var rowNo = 0;
73                         for (var i = 0; i < widgetsReturned.length; i++) {
74                         $scope.WidgetCatView[i] = {
75                         sizeX : 2,
76                         sizeY : 2,
77                         headerText:'',
78                         widgetIdentifier : '',
79                         url : '',
80                         widgetid: '',
81                         attrb:'',
82                         select: false,
83                         };
84                         $scope.WidgetCatView[i].widgetid = widgetsReturned[i].id;
85                         $scope.WidgetCatView[i].headerText = widgetsReturned[i].headerName;
86
87                         if(widgetsReturned[i].headerName === 'widget-news'){
88                         $scope.WidgetCatView[i].widgetIdentifier = 'NEWS';
89                         }
90                         else
91                         if(widgetsReturned[i].headerName === 'widget-resources'){
92                         $scope.WidgetCatView[i].widgetIdentifier = 'IMPORTANTRESOURCES';
93                         }
94                         else
95                         if(widgetsReturned[i].headerName === 'widget-events'){
96                         $scope.WidgetCatView[i].widgetIdentifier = 'EVENTS';
97                         }
98
99                         $scope.WidgetCatView[i].url = widgetsReturned[i].url;
100                         $scope.WidgetCatView[i].attrb = widgetsReturned[i].attrs;
101                         $scope.WidgetCatView[i].select = widgetsReturned[i].select;                     
102                         }
103
104                         $scope.widgetViewData = $scope.WidgetCatView;
105                         
106                         }
107                 
108                 /** Widget code starts */
109             let getUserWidgets = (loginName) => {
110                  
111                  this.isCommError = false;
112                  var conf = this.conf;
113                          widgetsCatalogService.getUserWidgets(loginName).then(res => {
114                                  
115                                  if(!(res instanceof Array)){
116                                         this.isCommError = true;
117                                         return;
118                                  }
119                          for(var i = 0; i < res.length; i++){   
120                                  var widget_id = res[i][0];
121                                  var widget_name = res[i][1];
122                                  let url = this.conf.api.widgetCommon + "/" + widget_id + "/framework.js";
123                                  $scope.widgetsList.push({
124                                                    id: widget_id,
125                                            name: widget_name,
126                                            headerName: widget_name,
127                                            url: url,
128                                            attrs: [{attr: 'data-' + widget_id, value: ''}],
129                                            status: res[i][4],
130                                            select: (res[i][4] == 'S' || res[i][4] === null) ? true : false
131                                          }); 
132                                  var script = document
133                                          .createElement('script');
134                                          script.src = url;
135                                          script.async = true;
136                                          var entry = document
137                                                 .getElementsByTagName('script')[0];
138                                          entry.parentNode
139                                                 .insertBefore(script, entry);
140                          }
141                          $scope.applyPresentationDetailsToWidgetsCatalog($scope.widgetsList);
142                  }).catch(err => {
143                      $log.error('WidgetCatalogCtrl::getUserWidgets caught error', err);
144                  }).finally(()=> {
145                          
146                  });
147             };
148             
149             let init = () => {
150                 userProfileService.getUserProfile()
151                 .then(profile=> {
152                     // $log.info('WidgetCatalogCtrl::getUserProfile: ',
153                                         // profile);
154                     $scope.orgUserId = profile.orgUserId;
155                     $scope.widgetsViewData = [];
156                     $scope.widgetsView = [];    
157                         getUserWidgets($scope.orgUserId);  
158                 });
159                 this.conf = conf;
160                 $scope.widgetsList = [];
161             };
162             
163             /** Widget code ends */
164           
165             $scope.activateThis = function(ele){
166                                 $compile(ele.contents())($scope);
167                                 $scope.$apply();
168             };
169             $scope.setCommonWidget = function() {
170                 /* News Events Resources */
171                 var widgetLength = ($scope.widgetsViewData==null || $scope.widgetsViewData.length==0) ? 0:$scope.widgetsViewData.length;
172                 $scope.widgetsViewData[widgetLength] = {
173                         sizeX: 2,
174                         sizeY: 2,
175                         headerText: 'News',
176                         width: '',
177                         height: '',
178                         url: '',
179                         selected:true
180                     };
181                 $scope.widgetsViewData[widgetLength+1] = {
182                         sizeX: 2,
183                         sizeY: 2,
184                         headerText: 'Calendar Events',
185                         width: '',
186                         height: '',
187                         url: '',
188                         selected:true
189                     };
190                 $scope.widgetsViewData[widgetLength+2] = {
191                         sizeX: 2,
192                         sizeY: 2,
193                         headerText: 'Resources',
194                         width: '',
195                         height: '',
196                         url: '',
197                         selected:true
198                     };
199                 
200                 /* Setting News data */
201                         $scope.newsData = [];
202                         $scope.updateNews = function() {
203                                 $scope.newsData.length=0;
204                                 dashboardService.getCommonWidgetData('NEWS').then(function(res) {
205                                         // $log.info(res.message);
206                                         var newsDataJsonArray = res.response.items;
207                                         for (var i = 0; i < newsDataJsonArray.length; i++) {
208                                                 $scope.newsData.push(newsDataJsonArray[i]);
209                                         }
210                                 })['catch'](function(err) {
211                                         $log.error('dashboard controller: failed to get news list', err);                                       
212                                 });
213                         }
214                         $scope.updateNews();
215                         /* Setting Events data */
216
217                         $scope.eventData = [];
218                         
219                         $scope.updateEvents = function() {
220                                 $scope.eventData.length=0;
221                                 dashboardService.getCommonWidgetData('EVENTS').then(function(res) {
222                                         var eventDataJsonArray = res.response.items;    
223                                         for (var i = 0; i < eventDataJsonArray.length; i++) {
224                                                 if(eventDataJsonArray[i].eventDate !=null) {
225                                                         // yyyy-mm-dd
226                                                         eventDataJsonArray[i].year = eventDataJsonArray[i].eventDate.substring(2,4);
227                                                         eventDataJsonArray[i].mon  = eventDataJsonArray[i].eventDate.substring(5,7);
228                                                         eventDataJsonArray[i].day  = eventDataJsonArray[i].eventDate.substring(8,10);
229                                                 }
230                                                 $scope.eventData.push(eventDataJsonArray[i]);
231                                         }
232                                 })['catch'](function(err) {
233                                         $log.error('dashboard controller: failed to get Events list', err);     
234                                 });
235                         }
236                         $scope.updateEvents();
237                         /* Setting Important Resources data */
238
239                         $scope.importResData = [];
240                         $scope.updateImportRes = function() {
241                                 $scope.importResData.length=0;
242                                 dashboardService.getCommonWidgetData('IMPORTANTRESOURCES').then(
243                                                 function(res) {
244                                                         // $log.info(res);
245                                                         var importResDataJSONArray = res.response.items;
246                                                         for (var i = 0; i < importResDataJSONArray.length; i++) {
247                                                                 $scope.importResData.push(importResDataJSONArray[i]);
248                                                         }
249                                                 })['catch'](function(err) {
250                                         $log.error('dashboard controller: failed to get resources list...', err);
251                                 });
252                         }
253                         $scope.updateImportRes();
254                 
255                 /** ******End hardcoded news events and resources*************** */
256                         
257             }
258             
259                 $scope.newsGridsterItem = {
260                                 headerText : 'Test',
261                                 subHeaderText : ''
262                 };
263                 
264                 $scope.newsGridsterItem = {
265                         headerText : 'News',
266                         subHeaderText : ''
267                 };
268             
269             $scope.eventsGridsterItem = {
270                 headerText : 'Events',
271                         subHeaderText : ''
272                 };
273
274                 $scope.impoResGridsterItem = {
275                         headerText : 'Resources',
276                         subHeaderText : ''
277                 };
278                 
279             this.gridsterOpts = {
280                 columns: 6,
281                 colWidth: 190,
282                 rowHeight: 190,
283                 margins: [20, 20],
284                 outerMargin: true,
285                 pushing: true,
286                 floating: true,
287                 swapping: true,
288                 draggable: {
289                         handle: '.icon-content-gridguide'
290                 }
291             };
292             
293             // Run this function when user clicks on checkbox.
294                 $scope.storeSelection = function(widget) {
295                         
296                         // not needed as only 'SHOW' and 'HIDE' status_cd is expected from the micro service now
297                         /*var pendingFlag = false;                      
298                         if(widget.access) 
299                                 pendingFlag = false;
300                         else
301                                 pendingFlag =  widget.pending;  */      
302                         
303                         var appData = { 
304                                         widgetId: widget.widgetid,
305                                         select  : widget.select,
306                                 //      pending : pendingFlag   
307                         };
308
309                         widgetsCatalogService.updateWidgetCatalog(appData).then(
310                                 function(result) {
311                                         // $log.debug('CatalogCtrl:storeSelection result is ', result);
312                                 })['catch'](function(err) {
313                                         $log.error('CatalogCtrl:storeSelection: exception: ', err);
314                                 });
315                 };
316
317             init();
318         }
319     }
320     
321     
322     
323     WidgetCatalogCtrl.$inject = ['widgetsService', '$log', '$cookies', '$scope', 'conf', 'beReaderService', 'widgetsCatalogService', 'userProfileService','dashboardService','$compile','ngDialog'];
324     angular.module('ecompApp').controller('WidgetCatalogCtrl', WidgetCatalogCtrl);
325
326     angular.module('ecompApp').constant('refreshInterval', '30000');
327
328     angular.module('ecompApp').directive('refreshIframe', ['$interval', 'refreshInterval', function ($interval, refreshInterval) {
329
330         function link(scope, element, attrs) {
331             var timeoutId;
332
333             function updateIframe() {
334                 if(attrs.isEnlarged == "false")
335                 {
336                     element.attr('src', element.attr('src'));
337                 }
338             }
339
340             element.on('$destroy', function () {
341                 $interval.cancel(timeoutId);
342             });
343
344             // start the UI update process; save the timeoutId for cancelling
345             /*
346                          * timeoutId = $interval(function () { updateIframe(); // update DOM },
347                          * refreshInterval);
348                          */
349         }
350
351         return {
352             link: link
353         };
354     } ]);
355
356 })();
357
358
359 app.directive('dynAttr', function() {
360     return {
361         scope: { list: '=dynAttr' },
362         link: function(scope, elem, attrs){
363             for(var attr in scope.list){
364                 elem.attr(scope.list[attr].attr, scope.list[attr].value);   
365             }
366         }
367     };
368 });