(function() { 'use strict'; var app = angular.module('securis'); var HTTP_ERRORS = { 403: "Forbidden action", 500: "Server error", 404: "Element not found" } app.directive( 'catalogField', function() { return { restrict : 'A', // only activate on element // attribute require : '?ngModel', // get a hold of // NgModelController link : function(scope, element, attrs, ngModel) { if (!ngModel) return; // do nothing if no ng-model // TODO: Replace the hard-coded form ID by the // appropiate dynamic field scope.catalogForm[attrs.name] = scope.catalogForm['{{field.name}}']; scope.catalogForm[attrs.name].$name = attrs.name; } }; }); app.controller('AdminCtrl', [ '$scope', '$http', 'toaster', 'Catalogs', function($scope, $http, toaster, Catalogs) { $scope.showForm = false; $scope.isNew = false; $scope.formu = {}; $scope.catalogIndex = 0; $scope.catalogMetadata = {}; $scope.catalogsList = null; $scope.list = null; var _changeCatalog = function(index) { $scope.showForm = false; $scope.formu = {}; if (!$scope.catalogsList) $scope.catalogsList = Catalogs.getList(); // catalog list is also in index.data if (typeof index === 'number') $scope.catalogIndex = index; Catalogs.setCurrent($scope.catalogIndex); $scope.catalogMetadata = Catalogs.getMetadata(); $scope.list = Catalogs.query(); $scope.refs = {} Catalogs.loadRefs($scope.refs) } Catalogs.init().then(_changeCatalog); $scope.selectCatalog = _changeCatalog; $scope.edit = function(data) { $scope.showForm = true; $scope.isNew = false; // Next line is a wirkaround due to some issues with values with ID == 0 $('select').val(null); $scope.formu = {} var fields = Catalogs.getMetadata().fields; console.log($scope); fields.forEach(function(field) { if (!field.listingOnly) $scope.formu[field.name] = data[field.name] || null; }) setTimeout(function() { $('#'+Catalogs.getFFF()).focus(); }, 0); } $scope.delete = function(data) { BootstrapDialog.confirm('The record will be deleted, are you sure?', function(result){ if(result) { var promise = Catalogs.remove(data).$promise; promise.then(function(data) { $scope.list = Catalogs.query(); Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.list); toaster.pop('success', Catalogs.getName(), "Element deleted successfully"); },function(error) { console.log(error); toaster.pop('error', Catalogs.getName(), "Error deleting element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 10000); }); } }); $scope.showForm = false; $scope.isNew = false; } } ]); app.controller('CatalogFormCtrl', [ '$scope', '$http', 'toaster', 'Catalogs', function($scope, $http, toaster, Catalogs) { $scope.scope = $scope; console.log('Form: currentCatalog:' + $scope.cataLogIndex); $scope.inputType = function(field) { if (field.readOnly && field.type === 'date') return 'readonly_date'; if (field.readOnly && (!field.pk || !$scope.isNew )) return 'readonly'; if (field.type === 'select') return 'select'; if (field.type === 'multiselect') return 'multiselect'; if (!field.multiline) return 'normal'; if (field.multiline) return 'textarea'; } $scope.editNew = function() { $scope.$parent.isNew = true; $scope.$parent.showForm = true; $('select').val(null); $scope.$parent.formu = {}; setTimeout(function() { $('#'+Catalogs.getFFF()).focus(); }, 0); } $scope.cancel = function() { $scope.$parent.showForm = false; $scope.catalogForm.$setPristine(); } $scope.saveCatalog = function() { if ($scope.catalogForm.$invalid) { toaster.pop('error', Catalogs.getName(), "There are wrong data in current form, please fix it before to save"); } else { var promise = Catalogs.save($scope.formu).$promise; promise.then(function(data, otro) { if ($scope.isNew) { $scope.catalogForm.$setPristine(); $scope.$parent.formu = {} $('#'+ Catalogs.getFFF()).focus(); } else { $scope.cancel(); } $scope.$parent.list = Catalogs.query(); Catalogs.refreshRef($scope.refs, Catalogs.getMetadata().resource, $scope.$parent.list); toaster.pop('success', Catalogs.getName(), "Element saved successfully"); }, function(error) { console.log(error); toaster.pop('error', Catalogs.getName(), "Error saving element, reason: " + HTTP_ERRORS[error.status] + ". Details: " + error.headers('X-SECURIS-ERROR'), 10000); }); } } } ]); app.controller('CatalogListCtrl', [ '$scope', '$http', '$filter', 'Catalogs', function($scope, $http, $filter, Catalogs) { $scope.print = function(name, row) { var value = row[name]; var type = Catalogs.getField(name).type; var printedValue = type === 'date' ? $filter('date')(value, 'yyyy-MM-dd') : value; if (printedValue !== value) // this line is a work around to allow search in formatted fields row['_display_'+name] = printedValue; return printedValue; } $scope.display = function(name) { return Catalogs.getField(name).display; } } ]); })();