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