(function() { 'use strict'; angular .module('hyperion') .controller('system.GroupsController.index', ['$http', '$scope', '$mdDialog', '$timeout', '$location', '$sessionStorage', '$filter', '$stateParams', 'timerange', 'modalDialog', 'authService', 'CONFIG', function($http, $scope, $mdDialog, $timeout, $location, $sessionStorage, $filter, $stateParams, timerange, modalDialog, authService, CONFIG) { // set #content div margin if($sessionStorage.currentUser !== undefined) { if($sessionStorage.currentUser.smallMenu) { $("#content").css('margin-left', 50); } } var scopeIsActive = true; $scope.$on('$destroy', function() { scopeIsActive = false; }); $scope.ismanager = $sessionStorage.currentUser.ismanager; $scope.isLoading = true; // show ajax loader authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipmentgroup { ' + 'id ' + 'name ' + 'isactive ' + 'created ' + 'equipment_count ' + 'type { ' + 'id ' + 'name ' + '} ' + 'location { ' + 'id ' + 'name ' + '} ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.groups = response.data.data.equipmentgroup; $scope.isLoading = false; // hide ajax loader console.log($scope.groups); },function(error) { // failure console.error(error); } ); }); $scope.viewGroup = (group_id) => { $location.path('/system/groups/view/' + group_id); } $scope.editGroup = (group_id) => { $location.path('system/groups/edit/' + group_id); } $scope.removeGroup = (group_id) => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { ' + 'equipmentgroupdelete(equipment_group_id: "' + group_id + '")' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.groups.splice($scope.groups.indexOf($filter('filter')($scope.groups, {id: group_id})[0]), 1); },function(error) { // failure console.error(error); } ); }); } $scope.addGroup = () => { $location.path('/system/groups/new'); } }]) .controller('system.GroupsController.view', ['$http', '$scope', '$mdDialog', '$timeout', '$location', '$sessionStorage', '$filter', '$stateParams', 'timerange', 'modalDialog', 'authService', 'CONFIG', function($http, $scope, $mdDialog, $timeout, $location, $sessionStorage, $filter, $stateParams, timerange, modalDialog, authService, CONFIG) { // set #content div margin if($sessionStorage.currentUser !== undefined) { if($sessionStorage.currentUser.smallMenu) { $("#content").css('margin-left', 50); } } var scopeIsActive = true; $scope.$on('$destroy', function() { scopeIsActive = false; }); $scope.ismanager = $sessionStorage.currentUser.ismanager; $scope.isLoading = true; // show ajax loader authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipmentgroup(id:"' + $stateParams.group_id + '") { ' + 'id ' + 'name ' + 'isactive ' + 'created ' + 'equipment_count ' + 'type { ' + 'id ' + 'name ' + '} ' + 'location { ' + 'id ' + 'name ' + '} ' + 'equipment { ' + 'id ' + 'name ' + 'status ' + 'model { ' + 'id ' + 'name ' + '} ' + 'floor { ' + 'id ' + 'name ' + '} ' + 'room { ' + 'id ' + 'name ' + '} ' + '} ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.group = response.data.data.equipmentgroup[0]; $scope.group.isvisible = true; $scope.equipment = $scope.group.equipment $scope.isLoading = false; // hide ajax loader },function(error) { // failure console.error(error); } ); }); $scope.editGroup = (group_id) => { $location.path('system/groups/edit/' + group_id); } $scope.removeGroup = (group_id) => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { ' + 'equipmentgroupdelete(equipment_group_id: "' + group_id + '")' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $location.path('/system/groups'); },function(error) { // failure console.error(error); } ); }); } $scope.runUPSTest = () => { console.log('runUPSTest'); modalDialog.showDialog({ 'template':'modal.codeconfirmation.html', 'controller':'modalCodeConfirmationController', 'subtitle': "UPS DISCHARGE TEST", 'message': ["Frequent UPS discharge tests can degrade the battery rapidly!","Limit tests to extend battery life and ensure optimal performance"] }).then( function(confirmationcode){ console.log('confirmationcode', confirmationcode); if(confirmationcode!==null) { } } ) }; $scope.viewDevice = (device_id) => { $location.path('system/equipment/' + device_id); } $scope.back = () => { $location.path('/system/groups'); } }]) .controller('system.GroupsController.new', ['$http', '$scope', '$element', '$mdDialog', '$timeout', '$location', '$sessionStorage', '$filter', '$stateParams', 'timerange', 'modalDialog', 'authService', 'CONFIG', function($http, $scope, $element, $mdDialog, $timeout, $location, $sessionStorage, $filter, $stateParams, timerange, modalDialog, authService, CONFIG) { // set #content div margin if($sessionStorage.currentUser !== undefined) { if($sessionStorage.currentUser.smallMenu) { $("#content").css('margin-left', 50); } } var scopeIsActive = true; $scope.$on('$destroy', function() { scopeIsActive = false; }); ////////////// $scope.searchDevice = ''; $scope.clearSearchDevice = () => { $scope.searchDevice = ''; }; $element.find('input').on('keydown', function(event) { event.stopPropagation(); }); ////////////// var time = timerange.calc($sessionStorage.currentUser.timerange); let time_start = moment(time.start).format('YYYY-MM-DDTHH:mm:ss'); let time_end = moment(time.today).format('YYYY-MM-DDTHH:mm:ss'); console.log('time_start: ' + time_start); console.log('time_end: ' + time_end); authService.getJWTAuth().then(authHeader => { $scope.isLoading = true; // show ajax loader var request = {'query': 'query { ' + 'locations(time_start:"' + time_start + '", time_end:"' + time_end + '") { ' + 'id ' + 'name ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.locations = response.data.data.locations; $scope.isLoading = false; // hide ajax loader },function(error) { // failure console.error(error); } ); }); $scope.selectLocation = () =>{ $scope.newgroup.devicetype = null; // reset selected types $scope.newgroup.devices = null; // reset selected devices $scope.isLoading = true; // show ajax loader authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipment_type { ' + 'id ' + 'name ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.types = response.data.data.equipment_type; $scope.isLoading = false; // hide ajax loader },function(error) { // failure console.error(error); } ); }); }; $scope.displaySelected = () => { if($scope.newgroup && $scope.newgroup.devices) { let count = $scope.newgroup.devices.length; if(count > 0) { return 'selected ' + count + ' device' + (count!=1?'s':''); } } return 'Equipment'; }; $scope.selectDeviceType = () => { $scope.newgroup.devices = null; // reset selected devices authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipment_index(location_id:"' + $scope.newgroup.location + '", type_id:' + $scope.newgroup.devicetype + ') { ' + 'id ' + 'name ' + 'status ' + 'type {' + 'id ' + 'name ' + '}' + 'model {' + 'id ' + 'name ' + '}' + 'location {' + 'id ' + 'name ' + '}' + 'floor {' + 'id ' + 'name ' + '}' + 'room {' + 'id ' + 'name ' + '}' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.devices = response.data.data.equipment_index; $scope.isLoading = false; // hide ajax loader },function(error) { // failure console.error(error); } ); }); } $scope.removeMember = (device_id) => { $scope.newgroup.devices.splice($scope.newgroup.devices.indexOf($filter('filter')($scope.newgroup.devices, {id: device_id})[0]), 1); if($scope.newgroup.devices.length == 0) { $scope.newgroup.devices = null; } }; $scope.save = () => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { ' + 'equipmentgroupadd ( ' + 'name: "' + $scope.newgroup.name + '", ' + 'location_id: "' + $scope.newgroup.location + '", ' + 'type_id: ' + $scope.newgroup.devicetype + ', ' + 'devices: "' + $scope.newgroup.devices.map(device => { return device.id; }) + '"' + '){ id }' + '}' }; console.log(request); $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve console.log(response); $location.path('/system/groups/view/' + response.data.data.equipmentgroupadd.id); },function(error) { // failure console.error(error); } ); }); } $scope.back = () => { $location.path('/system/groups'); } }]) .controller('system.GroupsController.edit', ['$http', '$scope', '$element', '$mdDialog', '$timeout', '$location', '$sessionStorage', '$filter', '$stateParams', 'timerange', 'modalDialog', 'authService', 'CONFIG', function($http, $scope, $element, $mdDialog, $timeout, $location, $sessionStorage, $filter, $stateParams, timerange, modalDialog, authService, CONFIG) { // set #content div margin if($sessionStorage.currentUser !== undefined) { if($sessionStorage.currentUser.smallMenu) { $("#content").css('margin-left', 50); } } var scopeIsActive = true; $scope.$on('$destroy', function() { scopeIsActive = false; }); ////////////// $scope.searchDevice = ''; $scope.clearSearchDevice = () => { $scope.searchDevice = ''; }; $element.find('input').on('keydown', function(event) { event.stopPropagation(); }); ////////////// var time = timerange.calc($sessionStorage.currentUser.timerange); let date_start = moment(time.start).format('YYYYMMDD'); let date_end = moment(time.today).format('YYYYMMDD'); $scope.newgroup = {}; authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipmentgroup(id:"' + $stateParams.group_id + '") { ' + 'id ' + 'name ' + 'type { ' + 'id ' + 'name ' + '} ' + 'location { ' + 'id ' + 'name ' + '} ' + 'equipment { ' + 'id ' + '} ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.group = response.data.data.equipmentgroup[0]; $scope.group.isvisible = true; $scope.newgroup.name = $scope.group.name; $scope.newgroup.location = $scope.group.location; $scope.newgroup.devicetype = $scope.group.type; $scope.newgroup.devices = [null] authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipment_type { ' + 'id ' + 'name ' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.types = response.data.data.equipment_type; $scope.types.forEach(type => { type.selected = type.id === $scope.newgroup.devicetype.id?true:false; }); },function(error) { // failure console.error(error); } ); }); },function(error) { // failure console.error(error); } ); }); $scope.displaySelected = () => { if($scope.newgroup && $scope.newgroup.devices) { let count = $scope.newgroup.devices.length; if(count > 0) { return 'selected ' + count + ' device' + (count!=1?'s':''); } } return 'Equipment'; }; $scope.selectDeviceType = () => { $scope.isLoading = true; // hide ajax loader if($scope.newgroup.devicetype != $scope.group.type.id) { $scope.group.equipment = []; } authService.getJWTAuth().then(authHeader => { var request = {'query': 'query { ' + 'equipment_index(location_id:"' + $scope.newgroup.location.id + '", type_id:' + $scope.newgroup.devicetype + ') { ' + 'id ' + 'name ' + 'status ' + 'type {' + 'id ' + 'name ' + '}' + 'model {' + 'id ' + 'name ' + '}' + 'location {' + 'id ' + 'name ' + '}' + 'floor {' + 'id ' + 'name ' + '}' + 'room {' + 'id ' + 'name ' + '}' + '} ' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $scope.devices = response.data.data.equipment_index; $scope.newgroup.devices = []; $scope.group.equipment.forEach(device => { let found = $filter('filter')($scope.devices, {id: device.id})[0]; if(found) { $scope.newgroup.devices.push(found); } }); $scope.isLoading = false; // hide ajax loader },function(error) { // failure console.error(error); } ); }); } $scope.removeMember = (device_id) => { $scope.newgroup.devices.splice($scope.newgroup.devices.indexOf($filter('filter')($scope.newgroup.devices, {id: device_id})[0]), 1); if($scope.newgroup.devices.length == 0) { $scope.newgroup.devices = null; } }; $scope.save = () => { authService.getJWTAuth().then(authHeader => { var request = {'query': 'mutation { ' + 'equipmentgroupupdate ( ' + 'equipment_group_id: "' + $stateParams.group_id + '", ' + 'name: "' + $scope.newgroup.name + '", ' + 'type_id: ' + $scope.newgroup.devicetype + ', ' + 'devices: "' + $scope.newgroup.devices.map(device => { return device.id; }) + '"' + '){ id }' + '}' }; $http({ method: 'POST', url: CONFIG.APP_API, data: request, headers: authHeader }).then( function(response){ // resolve $location.path('/system/groups/view/' + response.data.data.equipmentgroupupdate.id); },function(error) { // failure console.error(error); } ); }); } $scope.back = () => { //TODO: change to window.back $location.path('/system/groups'); } }]); })();