[PORTAL-16 PORTAL-18] Widget ms; staging
[portal.git] / ecomp-portal-FE-common / client / app / views / widgets / widgets.controller.js
index e46f1ce..1998d6d 100644 (file)
-/*-\r
- * ================================================================================\r
- * ECOMP Portal\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ================================================================================\r
- */\r
-'use strict';\r
-(function () {\r
-    class WidgetsCtrl {\r
-        constructor($log, applicationsService, widgetsService, ngDialog, confirmBoxService,\r
-                    userProfileService, $cookies, $scope, $rootScope) {\r
-            //$log.info('WidgetsCtrl::init: Starting Up');\r
-            $scope.infoMessage = true;\r
-            $rootScope.noWidgets = false;\r
-\r
-            let populateAvailableApps = widgets => {\r
-                let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};\r
-                this.availableApps = [allPortalsFilterObject];\r
-                this.filterByApp = this.availableApps[0];\r
-                applicationsService.getAppsForSuperAdminAndAccountAdmin().then(myApps => {\r
-                    var reSortedApp = myApps.sort(getSortOrder("name"));\r
-                    var realAppIndex = 1;\r
-                    for (let i = 1; i <= reSortedApp.length; i++) {\r
-                        if (!reSortedApp[i-1].restrictedApp) {\r
-                            $log.debug('WidgetsCtrl::populateAvailableApps: pushing {index: ', realAppIndex, 'title: ', reSortedApp[i - 1].name,\r
-                                'value: ', reSortedApp[i - 1].name, '}');\r
-                            this.availableApps.push({\r
-                                index: realAppIndex,\r
-                                title: reSortedApp[i - 1].name,\r
-                                value: reSortedApp[i - 1].name\r
-                            })\r
-                            realAppIndex = realAppIndex + 1;\r
-                        }\r
-                    }\r
-                }).catch(err => {\r
-                    $log.error(err);\r
-                });\r
-            };\r
-\r
-            let getOnboardingWidgets = () => {\r
-                this.isLoadingTable = true;\r
-                widgetsService.getManagedWidgets().then(res => {\r
-                    $log.debug('WidgetsCtrl.getOnboardingWidgets:: ' + JSON.stringify(res));\r
-                    // if (JSON.stringify(res) === '[]') {\r
-                    //     confirmBoxService.showInformation('There are currently no Widgets. ').then(isConfirmed => {});\r
-                    // }\r
-                    var reSortedWidget = res.sort(getSortOrder("name"));\r
-                    this.widgetsList = reSortedWidget;\r
-                    populateAvailableApps(reSortedWidget);\r
-                    // $log.info('WidgetsHomeCtrl::getUserWidgets count : ' + $scope.widgetsList.length);\r
-                    if (Object.keys(res).length === 0 ) {\r
-                        $rootScope.noWidgets = true;\r
-                        $scope.isLoadingTable = false;\r
-                        $log.info('WidgetsHomeCtrl::getUserWidgets: There are no available Widgets');\r
-                    }\r
-                }).catch(err => {\r
-                    confirmBoxService.showInformation('There was a problem retrieving the Widgets. ' +\r
-                        'Please try again later.').then(isConfirmed => {});\r
-                    $log.error('WidgetsCtrl::getOnboardingWidgets error: ' + err);\r
-                }).finally(()=> {\r
-                    this.isLoadingTable = false;\r
-                });\r
-            };\r
-\r
-            // Refactor this into a directive\r
-            let getSortOrder = (prop) => {\r
-                return function(a, b) {\r
-                    // $log.debug('a = ' + JSON.stringify(a) + "| b = " + JSON.stringify(b));\r
-                    if (a[prop].toLowerCase() > b[prop].toLowerCase()) {\r
-                        return 1;\r
-                    } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {\r
-                        return -1;\r
-                    }\r
-                    return 0;\r
-                }\r
-            }\r
-\r
-            $scope.hideMe = function () {\r
-                $scope.infoMessage = false;\r
-            }\r
-\r
-            let init = () => {\r
-                this.isLoadingTable = false;\r
-                getOnboardingWidgets();\r
-\r
-                /*Table general configuration params*/\r
-                this.searchString = '';\r
-                /*Table data*/\r
-                this.widgetsTableHeaders = [\r
-                    {name: 'Widget Name', value: 'name', isSortable: false},\r
-                    {name: 'Application', value: 'appName', isSortable: true},\r
-                    {name: 'Width', value: 'width', isSortable: false},\r
-                    {name: 'Height', value: 'height', isSortable: false}\r
-                ];\r
-                this.widgetsList = [];\r
-            };\r
-\r
-            this.filterByDropdownValue = item => {\r
-                if(this.filterByApp.value === ''){\r
-                    return true;\r
-                }\r
-                return item.appName === this.filterByApp.value;\r
-            };\r
-\r
-            this.openWidgetDetailsModal = (selectedWidget) => {\r
-                let data = null;\r
-                if(selectedWidget){\r
-                    if(!selectedWidget.id){\r
-                        $log.error('Widget id not found');\r
-                        return;\r
-                    }\r
-                    data = {\r
-                        widget: selectedWidget\r
-                    }\r
-                }\r
-                ngDialog.open({\r
-                    templateUrl: 'app/views/widgets/widget-details-dialog/widget-details.modal.html',\r
-                    controller: 'WidgetDetailsModalCtrl',\r
-                    controllerAs: 'widgetDetails',\r
-                    data: data\r
-                }).closePromise.then(needUpdate => {\r
-                    if(needUpdate.value === true){\r
-                        $log.debug('WidgetsCtrl::openWidgetDetailsModal: updating table data...');\r
-                        getOnboardingWidgets();\r
-                    }\r
-                });\r
-            };\r
-\r
-            this.deleteWidget = widget => {\r
-                confirmBoxService.deleteItem(widget.name).then(isConfirmed => {\r
-                    if(isConfirmed){\r
-                        if(!widget || !widget.id){\r
-                            $log.error('WidgetsCtrl::deleteWidget: No widget or ID... cannot delete');\r
-                            return;\r
-                        }\r
-                        widgetsService.deleteWidget(widget.id).then(() => {\r
-                            this.widgetsList.splice(this.widgetsList.indexOf(widget), 1);\r
-                        }).catch(err => {\r
-                            $log.error('WidgetsCtrl::deleteWidget error:',err);\r
-                            confirmBoxService.showInformation('There was a problem deleting the Widget. ' +\r
-                                'Please try again later.').then(isConfirmed => {});\r
-                        });\r
-                    }\r
-                }).catch(err => {\r
-                    $log.error('WidgetsCtrl::deleteWidget error:',err);\r
-                });\r
-            };\r
-\r
-            init();\r
-        }\r
-    }\r
-    WidgetsCtrl.$inject = ['$log', 'applicationsService', 'widgetsService', 'ngDialog', 'confirmBoxService',\r
-        'userProfileService','$cookies', '$scope', '$rootScope'];\r
-    angular.module('ecompApp').controller('WidgetsCtrl', WidgetsCtrl);\r
-})();\r
+/*-
+ * ================================================================================
+ * ECOMP Portal
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ================================================================================
+ */
+'use strict';
+(function () {
+    class WidgetsCtrl {
+        constructor($log, applicationsService, widgetsService, ngDialog, confirmBoxService,
+                    userProfileService, $cookies, $scope, $rootScope) {
+            //$log.info('WidgetsCtrl::init: Starting Up');
+            $scope.infoMessage = true;
+            $rootScope.noWidgets = false;
+
+            let populateAvailableApps = widgets => {
+                let allPortalsFilterObject = {index: 0, title: 'All applications', value: ''};
+                this.availableApps = [allPortalsFilterObject];
+                this.filterByApp = this.availableApps[0];
+                applicationsService.getAppsForSuperAdminAndAccountAdmin().then(myApps => {
+                    var reSortedApp = myApps.sort(getSortOrder("name"));
+                    var realAppIndex = 1;
+                    for (let i = 1; i <= reSortedApp.length; i++) {
+                        if (!reSortedApp[i-1].restrictedApp) {
+                            $log.debug('WidgetsCtrl::populateAvailableApps: pushing {index: ', realAppIndex, 'title: ', reSortedApp[i - 1].name,
+                                'value: ', reSortedApp[i - 1].name, '}');
+                            this.availableApps.push({
+                                index: realAppIndex,
+                                title: reSortedApp[i - 1].name,
+                                value: reSortedApp[i - 1].name
+                            })
+                            realAppIndex = realAppIndex + 1;
+                        }
+                    }
+                }).catch(err => {
+                    $log.error(err);
+                });
+            };
+
+            let getOnboardingWidgets = () => {
+                this.isLoadingTable = true;
+                widgetsService.getManagedWidgets().then(res => {
+                    $log.debug('WidgetsCtrl.getOnboardingWidgets:: ' + JSON.stringify(res));
+                    // if (JSON.stringify(res) === '[]') {
+                    //     confirmBoxService.showInformation('There are currently no Widgets. ').then(isConfirmed => {});
+                    // }
+                    var reSortedWidget = res.sort(getSortOrder("name"));
+                    this.widgetsList = reSortedWidget;
+                    populateAvailableApps(reSortedWidget);
+                    // $log.info('WidgetsHomeCtrl::getUserWidgets count : ' + $scope.widgetsList.length);
+                    if (Object.keys(res).length === 0 ) {
+                        $rootScope.noWidgets = true;
+                        $scope.isLoadingTable = false;
+                        $log.info('WidgetsHomeCtrl::getUserWidgets: There are no available Widgets');
+                    }
+                }).catch(err => {
+                    confirmBoxService.showInformation('There was a problem retrieving the Widgets. ' +
+                        'Please try again later.').then(isConfirmed => {});
+                    $log.error('WidgetsCtrl::getOnboardingWidgets error: ' + err);
+                }).finally(()=> {
+                    this.isLoadingTable = false;
+                });
+            };
+
+            // Refactor this into a directive
+            let getSortOrder = (prop) => {
+                return function(a, b) {
+                    // $log.debug('a = ' + JSON.stringify(a) + "| b = " + JSON.stringify(b));
+                    if (a[prop].toLowerCase() > b[prop].toLowerCase()) {
+                        return 1;
+                    } else if (a[prop].toLowerCase() < b[prop].toLowerCase()) {
+                        return -1;
+                    }
+                    return 0;
+                }
+            }
+
+            $scope.hideMe = function () {
+                $scope.infoMessage = false;
+            }
+
+            let init = () => {
+                this.isLoadingTable = false;
+                getOnboardingWidgets();
+
+                /*Table general configuration params*/
+                this.searchString = '';
+                /*Table data*/
+                this.widgetsTableHeaders = [
+                    {name: 'Widget Name', value: 'name', isSortable: false},
+                    {name: 'Application', value: 'appName', isSortable: true},
+                    {name: 'Width', value: 'width', isSortable: false},
+                    {name: 'Height', value: 'height', isSortable: false}
+                ];
+                this.widgetsList = [];
+            };
+
+            this.filterByDropdownValue = item => {
+                if(this.filterByApp.value === ''){
+                    return true;
+                }
+                return item.appName === this.filterByApp.value;
+            };
+
+            this.openWidgetDetailsModal = (selectedWidget) => {
+                let data = null;
+                if(selectedWidget){
+                    if(!selectedWidget.id){
+                        $log.error('Widget id not found');
+                        return;
+                    }
+                    data = {
+                        widget: selectedWidget
+                    }
+                }
+                ngDialog.open({
+                    templateUrl: 'app/views/widgets/widget-details-dialog/widget-details.modal.html',
+                    controller: 'WidgetDetailsModalCtrl',
+                    controllerAs: 'widgetDetails',
+                    data: data
+                }).closePromise.then(needUpdate => {
+                    if(needUpdate.value === true){
+                        $log.debug('WidgetsCtrl::openWidgetDetailsModal: updating table data...');
+                        getOnboardingWidgets();
+                    }
+                });
+            };
+
+            this.deleteWidget = widget => {
+                confirmBoxService.deleteItem(widget.name).then(isConfirmed => {
+                    if(isConfirmed){
+                        if(!widget || !widget.id){
+                            $log.error('WidgetsCtrl::deleteWidget: No widget or ID... cannot delete');
+                            return;
+                        }
+                        widgetsService.deleteWidget(widget.id).then(() => {
+                            this.widgetsList.splice(this.widgetsList.indexOf(widget), 1);
+                        }).catch(err => {
+                            $log.error('WidgetsCtrl::deleteWidget error:',err);
+                            confirmBoxService.showInformation('There was a problem deleting the Widget. ' +
+                                'Please try again later.').then(isConfirmed => {});
+                        });
+                    }
+                }).catch(err => {
+                    $log.error('WidgetsCtrl::deleteWidget error:',err);
+                });
+            };
+
+            init();
+        }
+    }
+    WidgetsCtrl.$inject = ['$log', 'applicationsService', 'widgetsService', 'ngDialog', 'confirmBoxService',
+        'userProfileService','$cookies', '$scope', '$rootScope'];
+    angular.module('ecompApp').controller('WidgetsCtrl', WidgetsCtrl);
+})();