angular.module('sba-crud-modal', []) .directive("sbaCrudModal", function() { return { restrict: 'E', templateUrl: "/sbadirective/load/sba-crud-modal.html", scope: { myModal: '=' }, controller: function($scope, $http, $timeout, sbaCrudModalFactory) { $scope.$watch('myModal', function(newValue, oldValue) { init(); }); $scope.dismissModal = function() { $scope.myModal.show = false; } $scope.optionFilter = function(field) { if (!$scope.myModal.fieldDetails[field].options.filter) { return function(item) { return true; }; } return function(item) { var filters = $scope.myModal.fieldDetails[field].options.filter; if (!Array.isArray(filters)) { filters = [filters]; } var show = true; angular.forEach(filters, function(filter) { if (filter.field === undefined || filter.lkey === undefined) { return; } show = show && ($scope.myModal.data[filter.field] == item[filter.lkey]); }); return show; } } $scope.confirm = function() { /* $scope.message = { text: 'Processing action...', status: false, } var type = $scope.myModal.type; var controllerFunctions = { 'c': 'insert', 'u': 'update', 'd': 'delete', } if (!controllerFunctions[type] || type !== 'u') { $scope.myModal.show = false; } var func = controllerFunctions[type]; var req = { method: 'POST', controller: '/' + $scope.myModal.controller + '/' + func + '/' + $scope.myModal.row.id, data: $scope.myModal.data, } */ /* $timeout(function() { $scope.message = { text: 'Not implemented', status: true, } $timeout(function() { $scope.dismissModal(); }, 2000); }, 2000); */ } function init() { if (!$scope.myModal) { return; } /* create (c), update (u), delete (d) */ if (!$scope.myModal.type) { $scope.myModal.type = 'c'; } /* Mandatory fields */ if (!$scope.myModal.controller || ($scope.myModal.type !== 'c' && (!$scope.myModal.row || !$scope.myModal.row.id))) { return; } if (!$scope.myModal.function) { $scope.myModal.function = 'get'; } var req = { method: 'GET', url: '/' + $scope.myModal.controller + '/' + $scope.myModal.function + '/' + $scope.myModal.row.id, } $http(req).then(function(response) { if (response.status && response.data.status) { initiate(response.data.data); } else { $scope.myModal = {}; } }); } function initiate(data) { if (data[0]) { data = data[0]; } $scope.myModal.data = data; $scope.myModal.fieldData = {}; angular.forEach($scope.myModal.fields, function(field) { $scope.myModal.fieldData[field] = { name: field, label: field, type: 'text', }; }); // get options for select getOptions(); $scope.myModal.show = true; } function getOptions() { $scope.myModal.options = {}; angular.forEach($scope.myModal.fieldDetails, function(details, field) { if ($scope.myModal.fields.indexOf(field) < 0) { return; } if (details.label) { $scope.myModal.fieldData[field].label = details.label; } if (details.options && details.options.controller) { if (!details.options.function) { details.options.function = 'get'; } sbaCrudModalFactory.getData(details.options.controller, details.options.function, function(data) { sbaCrudModalFactory.setData(details.options.controller, details.options.function, data); setOptions(field, data); }); } }); } function setOptions(field, data) { $scope.myModal.fieldData[field].options = data; $scope.myModal.fieldData[field].type = 'select'; if ($scope.myModal.fieldDetails[field].options.label) { var label = $scope.myModal.fieldDetails[field].options.label; if (!Array.isArray(label)) { label = [label]; } angular.forEach($scope.myModal.fieldData[field].options, function(item) { if(item.label) { item.labelCopy = item.label; } item.label = ''; angular.forEach(label, function(labelItem) { if (item[labelItem] !== undefined && item[labelItem]) { item.label = item.label + item[labelItem]; } else if (item[labelItem] === undefined) { item.label = item.label + labelItem; } }); }); } } } } }) .factory('sbaCrudModalFactory', function($http) { return { data: {}, setResponseData: function(data, callback) { console.log(data); }, getData: function(controller, func, callback) { if (this.data[controller] !== undefined && this.data[controller][func] !== undefined) { callback(this.data[controller][func]); } else { var req = { method: 'GET', url: '/' + controller + '/' + func, } $http(req).then(function(response) { if (response.status && response.data.status) { callback(response.data.data); } }); } }, setData: function(controller, func, data) { if (this.data[controller] === undefined) { this.data[controller] = {}; } if (this.data[controller][func] === undefined) { this.data[controller][func] = {}; } this.data[controller][func] = data; } } });