nexus site path corrected
[portal.git] / ecomp-portal-FE / 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                                         $scope.widgetForm.$setPristine();  
137                                         confirmBoxService.showInformation('You have added a new item').then(isConfirmed => {
138                                         });
139                                         dashboardService.getCommonWidgetData(message.type)
140                                         .then(function(res){
141                                                 $scope.widgetData = res.response.items;
142                                                 $scope.newWidgetObject = {};
143                                 });  
144                                 }
145                                 else {
146                                         confirmBoxService.showInformation("Save failed").then(isConfirmed => {return;});
147                                         return;
148                                 }                               
149                         });                     
150                 };
151                 
152                 $scope.remove = function(index) {
153                         var confirmMsg = 'Are you sure you want to delete this item from the list?' + ' Press OK to delete.';
154                         confirmBoxService.confirm(confirmMsg).then(function (confirmed) {
155                     if (confirmed == true) { 
156                         $scope.widgetObject = {};
157                                 $scope.widgetObject.id = $scope.widgetData[parseInt(index)].id;
158                                 $scope.widgetObject.category = $scope.widgetData[parseInt(index)].category;
159                                 $scope.widgetObject.title = $scope.widgetData[parseInt(index)].title;
160                                 $scope.widgetObject.href = $scope.widgetData[parseInt(index)].href;
161                                 $scope.widgetObject.sortOrder = $scope.widgetData[parseInt(index)].sortOrder;
162                                 
163                                 
164                                 dashboardService.removeCommonWidgetData($scope.widgetObject)
165                                 .then(function(res){
166                                         dashboardService.getCommonWidgetData(message.type)
167                                         .then(function(res){
168                                                 $scope.widgetData = res.response.items;
169                                                 $scope.cancelEdit(index);
170                                         });                             
171                                 });                             
172                     }
173                 });
174                                         
175                 };      
176                 $scope.closeDialog = function(){
177                         $scope.closeThisDialog( $scope.widgetData);
178                 }
179                 $scope.deleteSpeedDial = function(widget){
180                         
181                  }
182                  
183                  $scope.manage = function(index) {
184                                 $scope.modflag = true;
185                                 
186                                 var thisdata = $scope.widgetData[index];
187                         $scope.editMode=true;
188                         var category = "#category"+index;
189                         var categoryEdit = "#categoryEdit"+index;
190                         
191                         var title = "#title"+index;
192                         var titleEdit = "#titleEdit"+index;
193
194                         var url = "#href"+index;
195                         var urlEdit = "#hrefEdit"+index;
196                         
197                         var order = "#order"+index;
198                         var orderEdit = "#orderEdit"+index;
199                   
200                         var eventDate = "#eventDate"+index;
201                         var eventDateEdit = "#eventDateEdit"+index;
202                   
203                         
204                         var dsavebutton  ="#savebutton"+index;
205                         var dremovebutton="#removebutton"+index;
206                         var dmanagebutton="#managebutton"+index;
207                         var deditRbutton="#editRbutton"+index;
208                         
209                         $(title).css('display', 'none');
210                         $(titleEdit).css('display', 'inherit'); 
211                         $(url).css('display', 'none');
212                         $(urlEdit).css('display', 'inherit'); 
213                         $(order).css('display', 'none');
214                         $(orderEdit).css('display', 'inherit');
215                         $(eventDate).css('display', 'none');
216                         $(eventDateEdit).css('display', 'inherit');
217                         
218                         $(dsavebutton).css('display', 'inherit');
219                         $(dremovebutton).css('display', 'inherit');
220                         $(dmanagebutton).css('display', 'none');
221                         $(deditRbutton).css('display', 'inherit');
222                         
223                       };
224                       
225                    $scope.cancelEdit = function(index) {
226                         
227                         $scope.editMode=false;
228                         var category = "#category"+index;
229                         var categoryEdit = "#categoryEdit"+index;
230                         
231                         var title = "#title"+index;
232                         var titleEdit = "#titleEdit"+index;
233         
234                         var url = "#href"+index;
235                         var urlEdit = "#hrefEdit"+index;
236                         
237                         var order = "#order"+index;
238                         var orderEdit = "#orderEdit"+index;
239
240                         var eventDate = "#eventDate"+index;
241                         var eventDateEdit = "#eventDateEdit"+index;
242
243                         var dsavebutton  ="#savebutton"+index;
244                         var dremovebutton  ="#removebutton"+index;
245                         var dmanagebutton="#managebutton"+index;
246                         var deditRbutton="#editRbutton"+index;
247                 
248                         $(title).css('display', 'inherit');
249                         $(titleEdit).css('display', 'none'); 
250                         
251                         $(url).css('display', 'inherit');
252                         $(urlEdit).css('display', 'none'); 
253                         
254                         $(order).css('display', 'inherit');
255                         $(orderEdit).css('display', 'none');
256
257                         $(eventDate).css('display', 'inherit');
258                         $(eventDateEdit).css('display', 'none');
259
260                         $(dsavebutton).css('display', 'none');
261                         $(dremovebutton).css('display', 'none');
262                         $(dmanagebutton).css('display', 'inherit');
263                         $(deditRbutton).css('display', 'none');
264                         
265                      };
266                 
267                 $scope.popupConfirmWin = function(title, msgBody, callback){
268                 modalService.popupConfirmWin.apply(null, arguments);
269             };
270         }           
271     }
272     CommonWidgetController.$inject = ['dashboardService', '$scope', 'message', '$q', '$http', 'conf', '$filter','confirmBoxService','$log'];
273     angular.module('ecompApp').controller('CommonWidgetController', CommonWidgetController); 
274 })();
275
276 angular.module('ecompApp').filter('cut', function () {
277     return function (value, wordwise, max, tail) {
278         if (!value) return '';
279
280         max = parseInt(max, 10);
281         if (!max) return value;
282         if (value.length <= max) return value;
283
284         value = value.substr(0, max);
285         if (wordwise) {
286             var lastspace = value.lastIndexOf(' ');
287             if (lastspace != -1) {
288               //Also remove . and , so its gives a cleaner result.
289               if (value.charAt(lastspace-1) == '.' || value.charAt(lastspace-1) == ',') {
290                 lastspace = lastspace - 1;
291               }
292               value = value.substr(0, lastspace);
293             }
294         }
295
296         return value + (tail || ' …');
297     };
298 });
299
300 angular.module('ecompApp').controller('DatePickerController', function ($scope, uibDateParser) {
301                   $scope.today = function() {
302                     $scope.dt = new Date();
303                   };
304                   $scope.today();
305
306                   $scope.clear = function() {
307                     $scope.dt = null;
308                   };
309
310                   $scope.inlineOptions = {
311                     customClass: getDayClass,
312                     minDate: new Date(),
313                     showWeeks: true
314                   };
315
316                   $scope.dateOptions = {
317                     dateDisabled: disabled,
318                     formatYear: 'yy',
319                     minDate: new Date(),
320                     startingDay: 1
321                   };
322
323                   // Disable weekend selection
324                   function disabled(data) {
325                     var date = data.date,
326                       mode = data.mode;
327                     return mode === 'day' && (date.getDay() === 0 || date.getDay() === 6);
328                   }
329
330                   $scope.toggleMin = function() {
331                     $scope.inlineOptions.minDate = $scope.inlineOptions.minDate ? null : new Date();
332                     $scope.dateOptions.minDate = $scope.inlineOptions.minDate;
333                   };
334
335                   $scope.toggleMin();
336
337                   $scope.open1 = function() {
338                         // console.log('In open1');
339                     $scope.popup1.opened = true;
340                   };
341
342                   $scope.open2 = function() {
343                         // console.log('In open2');
344                         $scope.popup2.opened = true;
345                   };
346
347                   
348                   $scope.setDate = function(year, month, day) {
349                     $scope.dt = new Date(year, month, day);
350                   };
351
352                   $scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
353                   $scope.format = $scope.formats[3];
354                   $scope.altInputFormats = ['M!/d!/yyyy'];
355
356                   $scope.popup1 = {
357                     opened: false
358                   };
359
360                   $scope.popup2 = {
361                     opened: false
362                   };
363
364                   var tomorrow = new Date();
365                   tomorrow.setDate(tomorrow.getDate() + 1);
366                   var afterTomorrow = new Date();
367                   afterTomorrow.setDate(tomorrow.getDate() + 1);
368                   $scope.events = [
369                     {
370                       date: tomorrow,
371                       status: 'full'
372                     },
373                     {
374                       date: afterTomorrow,
375                       status: 'partially'
376                     }
377                   ];
378
379                   function getDayClass(data) {
380                     var date = data.date,
381                       mode = data.mode;
382                     if (mode === 'day') {
383                       var dayToCheck = new Date(date).setHours(0,0,0,0);
384
385                       for (var i = 0; i < $scope.events.length; i++) {
386                         var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);
387
388                         if (dayToCheck === currentDay) {
389                           return $scope.events[i].status;
390                         }
391                       }
392                     }
393
394                     return '';
395                   }
396 });
397
398
399
400 function toggleCollapsible(id, clickedIconId, subtitutingIconId){
401         $("#"+id).toggle();
402         $("#"+clickedIconId).toggle();
403         $("#"+subtitutingIconId).toggle();
404         setTimeout(function(){focusFirstEle(id);}, 1000);
405 }
406
407 function focusFirstEle(id){
408         var focusItems = $("#"+id).find(":focusable");
409         for(var i=0; i<focusItems.length; i++){
410                 if(!isHidden(focusItems[i])){
411                         var targetClassName = focusItems[i].className;
412                         if(targetClassName!='collapsibleArrow'){
413                                 focusItems[i].focus();
414                                 return;
415                         }
416                 }
417         }
418 }     
419
420 function validateUrl(value){
421     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);
422   }