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