nexus site path corrected
[portal.git] / ecomp-portal-FE / client / app / views / home / widgets-home / widgets-home.controller.js
1 'use strict';
2 (function () {
3
4     class WidgetsHomeCtrl {
5         constructor(widgetsService, $log, $cookies, $scope, userProfileService,dashboardService) {
6             this.userProfileService = userProfileService;
7             this.isLoading = true;
8
9             userProfileService.getUserProfile()
10                 .then(profile=> {
11                     $log.info('WidgetsHomeCtrl::getUserProfile: ', profile);
12                     $scope.attuid = profile.attuid;
13                     $log.info('WidgetsHomeCtrl::getUserProfile: user has the following attuid: ' + profile.attuid);
14                     $scope.widgetsViewData = [];
15                     $scope.widgetsView = [];
16
17                     widgetsService.getUserWidgets()
18                         .then(widgets => {
19                             $log.info('WidgetsHomeCtrl::getUserWidgets', widgets);
20                             this.widgets = widgets;
21                             this.enlargeURL = "";
22                             this.isEnlarged = false;
23                             let rowNo = 0;
24                             for (let i = 0; i < this.widgets.length; i++) {
25                                 $scope.widgetsView[i] = {
26                                     sizeX: 2,
27                                     sizeY: 2,
28                                     headerText: '',
29                                     width: '',
30                                     height: '',
31                                     url: '',
32                                     selected:false
33                                 };
34                                 $scope.widgetsView[i].headerText = this.widgets[i].name;
35                                 $scope.widgetsView[i].url = this.widgets[i].url;
36                                 //$scope.widgetsView[i].width = this.widgets[i].width;
37                                 //$scope.widgetsView[i].height = this.widgets[i].height;
38                                 //$scope.widgetsView[i].sizeX = this.widgets[i].width/180;
39                                 //$scope.widgetsView[i].sizeY = this.widgets[i].height/150;
40                             }
41                             /*News Events Resources*/
42                             $scope.widgetsView[this.widgets.length] = {
43                                     sizeX: 2,
44                                     sizeY: 2,
45                                     headerText: 'News',
46                                     width: '',
47                                     height: '',
48                                     url: '',
49                                     selected:true
50                                 };
51                             $scope.widgetsView[this.widgets.length+1] = {
52                                     sizeX: 2,
53                                     sizeY: 2,
54                                     headerText: 'Events',
55                                     width: '',
56                                     height: '',
57                                     url: '',
58                                     selected:true
59                                 };
60                             $scope.widgetsView[this.widgets.length+2] = {
61                                     sizeX: 2,
62                                     sizeY: 2,
63                                     headerText: 'Resources',
64                                     width: '',
65                                     height: '',
66                                     url: '',
67                                     selected:true
68                                 };
69                             
70                             /*Setting News data*/
71                                 $scope.newsData = [];
72                                 $scope.updateNews = function() {
73                                         $scope.newsData.length=0;
74                                         dashboardService.getCommonWidgetData('NEWS').then(function(res) {
75                                                 // $log.info(res.message);
76                                                 var newsDataJsonArray = res.response.items;
77                                                 for (var i = 0; i < newsDataJsonArray.length; i++) {
78                                                         $scope.newsData.push(newsDataJsonArray[i]);
79                                                 }
80                                         })['catch'](function(err) {
81                                                 $log.error('dashboard controller: failed to get news list', err);
82                                                 _this.isLoading = false;
83                                         });
84                                 }
85                                 $scope.updateNews();
86                                 /*Setting Events data*/
87
88                                 $scope.eventData = [];
89                                 
90                                 $scope.updateEvents = function() {
91                                         $scope.eventData.length=0;
92                                         dashboardService.getCommonWidgetData('EVENTS').then(function(res) {
93                                                 var eventDataJsonArray = res.response.items;    
94                                                 for (var i = 0; i < eventDataJsonArray.length; i++) {
95                                                         if(eventDataJsonArray[i].eventDate !=null) {
96                                                                 // yyyy-mm-dd
97                                                                 eventDataJsonArray[i].year = eventDataJsonArray[i].eventDate.substring(2,4);
98                                                                 eventDataJsonArray[i].mon  = eventDataJsonArray[i].eventDate.substring(5,7);
99                                                                 eventDataJsonArray[i].day  = eventDataJsonArray[i].eventDate.substring(8,10);
100                                                         }
101                                                         $scope.eventData.push(eventDataJsonArray[i]);
102                                                 }
103                                         })['catch'](function(err) {
104                                                 $log.error('dashboard controller: failed to get Events list', err);
105                                                 _this.isLoading = false;
106                                         });
107                                 }
108                                 $scope.updateEvents();
109                                 /*Setting Important Resources data*/
110
111                                 $scope.importResData = [];
112                                 $scope.updateImportRes = function() {
113                                         $scope.importResData.length=0;
114                                         dashboardService.getCommonWidgetData('IMPORTANTRESOURCES').then(
115                                                         function(res) {
116                                                                 //       $log.error('couldnt get important list...', res.response.dataBlob);
117                                                                 $log.info(res);
118                                                                 var importResDataJSONArray = res.response.items;
119                                                                 for (var i = 0; i < importResDataJSONArray.length; i++) {
120                                                                         $scope.importResData.push(importResDataJSONArray[i]);
121                                                                 }
122                                                         })['catch'](function(err) {
123                                                 $log.error('dashboard controller: failed to get resources list...', err);
124                                                 _this.isLoading = false;
125                                         });
126                                 }
127                                 $scope.updateImportRes();
128                            
129                             /********End hardcoded news events and resources****************/
130                             
131                             
132                             $log.info('WidgetsHomeCtrl::getUserWidgets count : ' + $scope.widgetsView.length);
133
134                             if ($cookies.getObject($scope.attuid + '_widget') == undefined || $cookies.getObject($scope.attuid + '_widget') == null || $cookies.getObject($scope.attuid + '_widget').length == 0) {
135                                 if (($scope.widgetsView != undefined) && ($scope.widgetsView != null) && ($scope.widgetsView.length > 0)) {
136                                     $scope.widgetsViewData = $scope.widgetsView;
137                                     //$cookies.putObject($scope.attuid + '_widget', $scope.widgetsView);
138                                 }
139                             }
140                             else {
141                                 this.listChanged = false;
142                                 this.listFromCookie = $cookies.getObject($scope.attuid + '_widget');
143                                 this.finalList = [];
144                                 //
145                                 // If a widget is still valid for this user from backend and
146                                 // it was in the cookie already, put it in the list in the same order
147                                 // it was in within the cookie.
148                                 //
149                                 let finalCount = 0;
150                                 for (let i = 0; i < this.listFromCookie.length; i++) {
151                                     this.foundInListFromBackend = false;
152                                     for (let j = 0; j < $scope.widgetsView.length; j++) {
153                                         if ((this.listFromCookie[i].url == $scope.widgetsView[j].url) &&
154                                             (this.listFromCookie[i].headerText == $scope.widgetsView[j].headerText)) {
155                                             this.finalList[finalCount] = {
156                                                 sizeX: 2,
157                                                 sizeY: 2,
158                                                 headerText: '',
159                                                 width: '',
160                                                 height: '',
161                                                 url: '',
162                                                 selected:false
163                                             };
164                                             this.finalList[finalCount].headerText = this.listFromCookie[i].headerText;
165                                             //this.finalList[finalCount].width = this.listFromCookie[i].width;
166                                             //this.finalList[finalCount].height = this.listFromCookie[i].height;
167                                             this.finalList[finalCount].url = this.listFromCookie[i].url;
168                                             //this.finalList[finalCount].sizeX = this.listFromCookie[i].width/180;
169                                             //this.finalList[finalCount].sizeY = this.listFromCookie[i].height/150;
170                                             finalCount++;
171                                             this.foundInListFromBackend = true;
172                                             break;
173                                         }
174                                     }
175                                     if (this.foundInListFromBackend == false) {
176                                         this.listChanged = true;
177                                     }
178                                 }
179
180                                 //
181                                 // Fill in the rest of the list with the widgets retrieved from the backend that we did
182                                 // not already add.  There could have been
183                                 // new widgets configured for the user that are not in the cookie.
184                                 //
185                                 for (let i = 0; i < $scope.widgetsView.length; i++) {
186                                     this.found = false;
187                                     for (let j = 0; j < this.finalList.length; j++) {
188                                         if (($scope.widgetsView[i].url == this.finalList[j].url) &&
189                                             ($scope.widgetsView[i].headerText == this.finalList[j].headerText)){
190                                             // already present
191                                             this.found = true;
192                                             break;
193                                         }
194                                     }
195                                     if (this.found == false) {
196                                         this.finalList[finalCount] = {
197                                             sizeX: 2,
198                                             sizeY: 2,
199                                             headerText: '',
200                                             width: '',
201                                             height: '',
202                                             url: '',
203                                             selected:false
204                                         };
205                                         this.finalList[finalCount].headerText = $scope.widgetsView[i].headerText;
206                                         //this.finalList[finalCount].width = $scope.widgetsView[i].width;
207                                         //this.finalList[finalCount].height = $scope.widgetsView[i].height;
208                                         this.finalList[finalCount].url = $scope.widgetsView[i].url;
209                                         //this.finalList[finalCount].sizeX = $scope.widgetsView[i].width/180;
210                                         //this.finalList[finalCount].sizeY = $scope.widgetsView[i].height/150;
211                                         finalCount++;
212                                         this.listChanged = true;
213                                     }
214                                 }
215
216                                 if ((this.finalList != undefined) && (this.finalList != null) && (this.finalList.length > 0)) {
217                                     if (this.listChanged) {
218                                         $scope.widgetsViewData = this.finalList;
219                                         $cookies.putObject($scope.attuid + '_widget', this.finalList);
220                                     } else {
221                                         $scope.widgetsViewData = $cookies.getObject($scope.attuid + '_widget');
222                                     }
223                                 }
224                                 this.isLoading = false;
225                             }
226                         }).catch(err => {
227                         $log.error('WidgetsHomeCtrl::getUserWidgets: oh no! couldnt get widgets list...', err);
228                         this.isLoading = false;
229                         });
230                     });
231
232
233             this.gridsterOpts = {
234                 columns: 6,
235                 colWidth: 190,
236                 rowHeight: 190,
237                 margins: [20, 20],
238                 outerMargin: true,
239                 pushing: true,
240                 floating: true,
241                 swapping: true,
242                 draggable: {
243                         handle: 'img',
244                     stop: function () {                         
245                         $cookies.putObject($scope.attuid + '_widget', $scope.widgetsViewData);
246                     }
247                 }
248             };
249         }
250     }
251
252     WidgetsHomeCtrl.$inject = ['widgetsService', '$log', '$cookies', '$scope', 'userProfileService','dashboardService'];
253     angular.module('ecompApp').controller('WidgetsHomeCtrl', WidgetsHomeCtrl);
254
255     angular.module('ecompApp').constant('refreshInterval', '30000');
256
257     angular.module('ecompApp').directive('refreshIframe', ['$interval', 'refreshInterval', function ($interval, refreshInterval) {
258
259         function link(scope, element, attrs) {
260             var timeoutId;
261
262             function updateIframe() {
263                 if(attrs.isEnlarged == "false")
264                 {
265                     element.attr('src', element.attr('src'));
266                 }
267             }
268
269             element.on('$destroy', function () {
270                 $interval.cancel(timeoutId);
271             });
272
273             // start the UI update process; save the timeoutId for cancelling
274             /*timeoutId = $interval(function () {
275                 updateIframe(); // update DOM
276             }, refreshInterval);*/
277         }
278
279         return {
280             link: link
281         };
282     } ]);
283
284 })();