7767cdbbbbc46518c9b9eb85363c3d0bd353f957
[portal.git] / ecomp-portal-FE-common / client / app / views / dashboard / dashboard-widget.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
22 (function () {
23     class CommonWidgetController {
24         constructor(dashboardService, $scope, message, $q, $http, conf, $filter,confirmBoxService,$log) {
25                 $scope.widgetType = message.type;
26                 $scope.widgetTypeDisplay = message.type;
27                 $scope.modflag = false;
28                 
29                 switch (message.type) {
30             case 'EVENTS':
31                 $scope.widgetTypeDisplay = 'Events'
32                 break;
33             case 'NEWS':
34                 $scope.widgetTypeDisplay = 'News'
35                 break;
36             case 'IMPORTANTRESOURCES':
37                 $scope.widgetTypeDisplay = 'Resources'
38                 break;
39                 }
40  
41                 $scope.widgetData = [];
42                 dashboardService.getCommonWidgetData(message.type)
43                 .then(function (res) {
44                          // console.log('CommonWidgetController: result is ' + res);
45                          $scope.widgetData = res.response.items;
46                  });
47                  
48                  
49                  
50                 $scope.setEdit = function(index) {
51                         
52                         //for(var i=0; i<$scope.widgetData )
53                         
54                         if($scope.modflag === false){
55                                 $scope.widgetData[index].showEdit = true;
56                                 $scope.modflag = true;
57                         }
58                  }               
59                 
60                  $scope.modify = function(index) {
61                         
62                         $scope.widgetObject = {};
63                         $scope.widgetObject.id = $scope.widgetData[parseInt(index)].id;
64                         $scope.widgetObject.category = $scope.widgetData[parseInt(index)].category;
65                         $scope.widgetObject.title = $scope.widgetData[parseInt(index)].title;
66                         $scope.widgetObject.href = $scope.widgetData[parseInt(index)].href;
67                         $scope.widgetObject.eventDate = $scope.widgetData[parseInt(index)].eventDate;
68                         $scope.widgetObject.content = $scope.widgetData[parseInt(index)].content;
69                         $scope.widgetObject.sortOrder = $scope.widgetData[parseInt(index)].sortOrder;
70                         
71                         var validateMsg = $scope.validateWidgetObject($scope.widgetObject);
72                         if (validateMsg) {
73                                 confirmBoxService.showInformation(validateMsg).then(isConfirmed => {return;});
74                                 return;
75                         }
76                         
77                         dashboardService.saveCommonWidgetData($scope.widgetObject)
78                         .then(function(res){
79                                 if (res.status == 'OK') {
80                                 dashboardService.getCommonWidgetData(message.type)
81                                 .then(function(res){
82                                         $scope.widgetData = res.response.items;
83                                 $scope.modflag = false;
84                                         $scope.cancelEdit(index);
85                                         });
86                                 }
87                                 else {
88                                         // Save failed
89                                         confirmBoxService.showInformation("Save failed").then(isConfirmed => {return;});
90                                         return;
91                                 }
92                         });   
93
94                 };      
95                 
96                 $scope.newWidgetObject = {};
97                 
98                 // Answers string error if validation fails; 
99                 // answers null if all is well.
100                 $scope.validateWidgetObject = function(wo) {
101                         if (wo.title == null || wo.title == '')
102                                 return "Please enter a title.";
103                         if (wo.href == null || wo.href == '' || !validateUrl(wo.href))
104                                 return "Please enter a valid URL that starts with http.";
105                         if (wo.sortOrder == null || wo.sortOrder == '' || isNaN(parseInt(wo.sortOrder)))
106                                 return "Please enter a number for the sort order.";
107                         if (wo.category=='EVENTS') {
108                                 if (wo.eventDate == null || wo.eventDate == '')
109                                         return "Please enter a date for the event.";
110                                 // Parses and normalizes the date with rollover.
111                                 var filteredDate = $filter('date')(wo.eventDate, "yyyy-MM-dd");
112                                 // $log.debug('dashboard-widget-controller: date filter yields ' + filteredDate);
113                                 // The date picker shows mm/dd/yy.
114                                 if (filteredDate == null || filteredDate.length != 10)
115                                         return "Please enter a valid date as YYYY-MM-DD";
116                                 if (wo.content==null || wo.content=='')
117                                         return "Please enter content for the event.";
118                         }
119                         return null;
120                 };
121                 
122                 $scope.saveNew = function() {
123                         $scope.newWidgetObject.category = message.type;
124                         // $log.info($scope.newWidgetObject);
125                         var validateMsg = $scope.validateWidgetObject($scope.newWidgetObject);
126                         if (validateMsg) {
127                                 confirmBoxService.showInformation(validateMsg).then(isConfirmed => {return;});
128                                 return;
129                         }
130                         // Transform date into expected storage format
131                         $scope.newWidgetObject.eventDate = $filter('date')($scope.newWidgetObject.eventDate, "yyyy-MM-dd");
132
133                         dashboardService.saveCommonWidgetData($scope.newWidgetObject)
134                         .then(function(res){
135                                 if (res.status == 'OK') {  
136                                         confirmBoxService.showInformation('You have added a new item').then(isConfirmed => {
137                                         });
138                                         dashboardService.getCommonWidgetData(message.type)
139                                         .then(function(res){
140                                                 $scope.widgetData = res.response.items;
141                                                 $scope.newWidgetObject = {};
142                                 });  
143                                 }
144                                 else {
145                                         confirmBoxService.showInformation("Save failed").then(isConfirmed => {return;});
146                                         return;
147                                 }                               
148                         });                     
149                 };
150                 
151                 $scope.remove = function(index) {
152                         var confirmMsg = 'Are you sure you want to delete this item from the list?' + ' Press OK to delete.';
153                         confirmBoxService.confirm(confirmMsg).then(function (confirmed) {
154                     if (confirmed == true) { 
155                         $scope.widgetObject = {};
156                                 $scope.widgetObject.id = $scope.widgetData[parseInt(index)].id;
157                                 $scope.widgetObject.category = $scope.widgetData[parseInt(index)].category;
158                                 $scope.widgetObject.title = $scope.widgetData[parseInt(index)].title;
159                                 $scope.widgetObject.href = $scope.widgetData[parseInt(index)].href;
160                                 $scope.widgetObject.sortOrder = $scope.widgetData[parseInt(index)].sortOrder;
161                                 
162                                 
163                                 dashboardService.removeCommonWidgetData($scope.widgetObject)
164                                 .then(function(res){
165                                         dashboardService.getCommonWidgetData(message.type)
166                                         .then(function(res){
167                                                 $scope.widgetData = res.response.items;
168                                                 $scope.cancelEdit(index);
169                                         });                             
170                                 });                             
171                     }
172                 });
173                                         
174                 };      
175                 $scope.closeDialog = function(){
176                         $scope.closeThisDialog( $scope.widgetData);
177                 }
178                 $scope.deleteSpeedDial = function(widget){
179                         
180                  }
181                  
182                  $scope.manage = function(index) {
183                                 $scope.modflag = true;
184                                 
185                                 var thisdata = $scope.widgetData[index];
186                         $scope.editMode=true;
187                         var category = "#category"+index;
188                         var categoryEdit = "#categoryEdit"+index;
189                         
190                         var title = "#title"+index;
191                         var titleEdit = "#titleEdit"+index;
192
193                         var url = "#href"+index;
194                         var urlEdit = "#hrefEdit"+index;
195                         
196                         var order = "#order"+index;
197                         var orderEdit = "#orderEdit"+index;
198                   
199                         var eventDate = "#eventDate"+index;
200                         var eventDateEdit = "#eventDateEdit"+index;
201                   
202                         
203                         var dsavebutton  ="#savebutton"+index;
204                         var dremovebutton="#removebutton"+index;
205                         var dmanagebutton="#managebutton"+index;
206                         var deditRbutton="#editRbutton"+index;
207                         
208                         $(title).css('display', 'none');
209                         $(titleEdit).css('display', 'inherit'); 
210                         $(url).css('display', 'none');
211                         $(urlEdit).css('display', 'inherit'); 
212                         $(order).css('display', 'none');
213                         $(orderEdit).css('display', 'inherit');
214                         $(eventDate).css('display', 'none');
215                         $(eventDateEdit).css('display', 'inherit');
216                         
217                         $(dsavebutton).css('display', 'inherit');
218                         $(dremovebutton).css('display', 'inherit');
219                         $(dmanagebutton).css('display', 'none');
220                         $(deditRbutton).css('display', 'inherit');
221                         
222                       };
223                       
224                    $scope.cancelEdit = function(index) {
225                         
226                         $scope.editMode=false;
227                         var category = "#category"+index;
228                         var categoryEdit = "#categoryEdit"+index;
229                         
230                         var title = "#title"+index;
231                         var titleEdit = "#titleEdit"+index;
232         
233                         var url = "#href"+index;
234                         var urlEdit = "#hrefEdit"+index;
235                         
236                         var order = "#order"+index;
237                         var orderEdit = "#orderEdit"+index;
238
239                         var eventDate = "#eventDate"+index;
240                         var eventDateEdit = "#eventDateEdit"+index;
241
242                         var dsavebutton  ="#savebutton"+index;
243                         var dremovebutton  ="#removebutton"+index;
244                         var dmanagebutton="#managebutton"+index;
245                         var deditRbutton="#editRbutton"+index;
246                 
247                         $(title).css('display', 'inherit');
248                         $(titleEdit).css('display', 'none'); 
249                         
250                         $(url).css('display', 'inherit');
251                         $(urlEdit).css('display', 'none'); 
252                         
253                         $(order).css('display', 'inherit');
254                         $(orderEdit).css('display', 'none');
255
256                         $(eventDate).css('display', 'inherit');
257                         $(eventDateEdit).css('display', 'none');
258
259                         $(dsavebutton).css('display', 'none');
260                         $(dremovebutton).css('display', 'none');
261                         $(dmanagebutton).css('display', 'inherit');
262                         $(deditRbutton).css('display', 'none');
263                         
264                      };
265                 
266                 $scope.popupConfirmWin = function(title, msgBody, callback){
267                 modalService.popupConfirmWin.apply(null, arguments);
268             };
269         }           
270     }
271     CommonWidgetController.$inject = ['dashboardService', '$scope', 'message', '$q', '$http', 'conf', '$filter','confirmBoxService','$log'];
272     angular.module('ecompApp').controller('CommonWidgetController', CommonWidgetController); 
273 })();
274
275 angular.module('ecompApp').filter('cut', function () {
276     return function (value, wordwise, max, tail) {
277         if (!value) return '';
278
279         max = parseInt(max, 10);
280         if (!max) return value;
281         if (value.length <= max) return value;
282
283         value = value.substr(0, max);
284         if (wordwise) {
285             var lastspace = value.lastIndexOf(' ');
286             if (lastspace != -1) {
287               //Also remove . and , so its gives a cleaner result.
288               if (value.charAt(lastspace-1) == '.' || value.charAt(lastspace-1) == ',') {
289                 lastspace = lastspace - 1;
290               }
291               value = value.substr(0, lastspace);
292             }
293         }
294
295         return value + (tail || ' …');
296     };
297 });
298
299 angular.module('ecompApp').controller('DatePickerController', function ($scope, uibDateParser) {
300                   $scope.today = function() {
301                     $scope.dt = new Date();
302                   };
303                   $scope.today();
304
305                   $scope.clear = function() {
306                     $scope.dt = null;
307                   };
308
309                   $scope.inlineOptions = {
310                     customClass: getDayClass,
311                     minDate: new Date(),
312                     showWeeks: true
313                   };
314
315                   $scope.dateOptions = {
316                     dateDisabled: disabled,
317                     formatYear: 'yy',
318                     minDate: new Date(),
319                     startingDay: 1
320                   };
321
322                   // Disable weekend selection
323                   function disabled(data) {
324                     var date = data.date,
325                       mode = data.mode;
326                     return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
327                   }
328
329                   $scope.toggleMin = function() {
330                     $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
331                     $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
332                   };
333
334                   $scope.toggleMin();
335
336                   $scope.open1 = function() {
337                         // console.log('In open1');
338                     $scope.popup1.opened = true;
339                   };
340
341                   $scope.open2 = function() {
342                         // console.log('In open2');
343                         $scope.popup2.opened = true;
344                   };
345
346                   
347                   $scope.setDate = function(year, month, day) {
348                     $scope.dt = new Date(year, month, day);
349                   };
350
351                   $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
352                   $scope.format = $scope.formats[3];
353                   $scope.altInputFormats = ['M!/d!/yyyy'];
354
355                   $scope.popup1 = {
356                     opened: false
357                   };
358
359                   $scope.popup2 = {
360                     opened: false
361                   };
362
363                   var tomorrow = new Date();
364                   tomorrow.setDate(tomorrow.getDate() + 1);
365                   var afterTomorrow = new Date();
366                   afterTomorrow.setDate(tomorrow.getDate() + 1);
367                   $scope.events = [
368                     {
369                       date: tomorrow,
370                       status: 'full'
371                     },
372                     {
373                       date: afterTomorrow,
374                       status: 'partially'
375                     }
376                   ];
377
378                   function getDayClass(data) {
379                     var date = data.date,
380                       mode = data.mode;
381                     if (mode === 'day') {
382                       var dayToCheck = new Date(date).setHours(0,0,0,0);
383
384                       for (var i = 0; i < $scope.events.length; i++) {
385                         var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);
386
387                         if (dayToCheck === currentDay) {
388                           return $scope.events[i].status;
389                         }
390                       }
391                     }
392
393                     return '';
394                   }
395 });
396
397
398
399 function toggleCollapsible(id, clickedIconId, subtitutingIconId){
400         $("#"+id).toggle();
401         $("#"+clickedIconId).toggle();
402         $("#"+subtitutingIconId).toggle();
403         setTimeout(function(){focusFirstEle(id);}, 1000);
404 }
405
406 function focusFirstEle(id){
407         var focusItems = $("#"+id).find(":focusable");
408         for(var i=0; i<focusItems.length; i++){
409                 if(!isHidden(focusItems[i])){
410                         var targetClassName = focusItems[i].className;
411                         if(targetClassName!='collapsibleArrow'){
412                                 focusItems[i].focus();
413                                 return;
414                         }
415                 }
416         }
417 }     
418
419 function validateUrl(value){
420     return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(value);
421   }