/** * 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.Data = JSON.parse($scope.camera.Data); $scope.camera.camera_id = $scope.camera.Id; $scope.permission = "CAMOTION_CAMERA_EDIT"; } else { $scope.camera = { Name: "", Description: "", Data: null, HostId: null, CameraTypeId: null }; $scope.permission = "CAMOTION_CAMERA_ADD"; } $scope.hosts = {}; $scope.cameraTypes = {}; var promiseLoadForeign = { id: "promiseLoadForeign", groups: ["modal"] }; HelperService.getForeignEntities(CamerasService, promiseLoadForeign).then(function(data) { $scope.hosts = data.hosts; $scope.cameraTypes = data.types; 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.cameraTypes); if (keys.length != 0) { $scope.camera.CameraTypeId = $scope.cameraTypes[keys[0]].Id; $scope.setCamerayType($scope.cameraTypes[keys[0]]); } } else if ($scope.camera.Data == null) { $scope.setCameraType($scope.cameraTypes[$scope.camera.CameraTypeId]); } }); $scope.submitForm = function() { if (luticateAuthCache.hasEffectivePermission($scope.permission)) { $scope.camera.Data = JSON.stringify($scope.camera.Data); 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; } }; $scope.setCameraType = function(cameraType) { $scope.camera.CameraTypeId = cameraType.Id; $scope.camera.Data = JSON.parse($scope.cameraTypes[$scope.camera.CameraTypeId].DefaultData); }; }]);