/** * Created by robin on 11/4/15. */ angular.module('camotionAdmin') .controller('CameraEditController', ['$scope', 'CamerasService', 'data', 'dialogs', '$q', 'luticateAuthCache', 'HelperService', function($scope, CamerasService, data, dialogs, $q, luticateAuthCache, HelperService) { if (data != null) { $scope.camera = data; $scope.camera.camera_id = $scope.camera.Id; $scope.permission = "CAMOTION_CAMERA_EDIT"; } else { $scope.camera = { Name: "", Description: "", Token: "", HostId: null, EntityTypeId: null }; $scope.permission = "CAMOTION_CAMERA_ADD"; } $scope.hosts = {}; $scope.entityTypes = {}; var promiseLoadForeign = { id: "promiseLoadForeign", groups: ["modal"] }; HelperService.getForeignEntities(promiseLoadForeign).then(function(data) { $scope.hosts = data.hosts; $scope.entityTypes = data.entityTypes; if ($scope.camera.Id == null) { var keys = Object.keys($scope.hosts); if (keys.length != 0) { $scope.camera.HostId = $scope.hosts[keys[0]].Id; }keys = Object.keys($scope.entityTypes); if (keys.length != 0) { $scope.camera.EntityTypeId = $scope.entityTypes[keys[0]].Id; } } }); $scope.submitForm = function() { if (luticateAuthCache.hasEffectivePermission($scope.permission)) { if ($scope.camera.Id != null) { var promiseEditCamera = { id: "promiseEditCamera", loaderGroups: ["modal"] }; return CamerasService.edit($scope.camera, promiseEditCamera); } else { var promiseAddCamera = { id: "promiseAddCamera", loaderGroups: ["modal"] }; return CamerasService.add($scope.camera, promiseAddCamera); } } else { var defer = $q.defer(); defer.resolve(); return defer.promise; } }; }]);