(function() { 'use strict'; var app = angular.module('app', [ 'ngRoute', 'ngAnimate', 'ngResource', 'toaster', 'catalogs' ]); 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('CatalogsCtrl', [ '$scope', '$http', 'Catalogs', function($scope, $http, Catalogs) { $scope.showForm = false; $scope.isNew = false; $scope.formu = {}; $scope.catalogIndex = 0; $scope.catalogMetadata = {}; $scope.catalogsList = null; $scope.list = null; var _changeCatalog = function(index) { 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) console.log($scope.refs) } Catalogs.init().then(_changeCatalog); $scope.selectCatalog = _changeCatalog; $scope.edit = function(data) { $scope.showForm = true; $scope.isNew = false; $scope.formu = {} for (var k in data) { if (k.indexOf('$') !== 0) $scope.formu[k] = data[k] } console.log('$scope.edit') console.log($scope.formu) $('#'+ Catalogs.getFFF()).focus(); } $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(); }); } }); $scope.showForm = false; $scope.isNew = false; // TODO: Load in formu values for Form // $scope.formu = {}; } } ]); 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) return 'readonly'; if (field.type === 'select') return 'select'; if (!field.multiline) return 'normal'; if (field.multiline) return 'textarea'; } $scope.editNew = function() { $('#'+ Catalogs.getFFF()).focus(); $scope.$parent.showForm = true; $scope.$parent.isNew = true; $scope.$parent.formu = {}; } $scope.cancel = function() { $scope.$parent.showForm = false; } $scope.saveCatalog = function() { if ($scope.catalogForm.$invalid) { alert(JSON.stringify($scope.catalogForm)) } else { var promise = Catalogs.save($scope.formu).$promise; promise.then(function(data, otro) { if ($scope.isNew) { $scope.$parent.formu = {} $('#'+ Catalogs.getFFF()).focus(); } else { $scope.cancel(); } $scope.$parent.list = Catalogs.query(); }, function(error) { console.log(error); }); } } } ]); 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; } } ]); })();