UNPKG

zettapi_client

Version:

Admin panel and client-side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project.

112 lines (97 loc) 4.21 kB
app.directive('entityList', function () { return { restrict: 'E', scope: { entity: '@', items: '=?', lookup: '=?', noTable: '@' }, templateUrl: 'directives/entity/entity.list.html', controller: function ($scope, $interpolate, $controller, $entity, $routeParams, $rootScope, $timeout, zapi, blockUI, NgTableParams) { //validate $scope.entity = $scope.entity || $routeParams.entity; $scope.entityObj = zapi.entityMap[$scope.entity]; if (!$scope.entityObj) return; if (!$scope.entityObj.list) return; //defaults var idField = $scope.entityObj.id || "_id"; $scope.items = $scope.items || []; if (typeof $scope.lookup === 'undefined') $scope.lookup = {}; $scope.noTable = $scope.noTable || false; $scope.useInputLarge = zapi.useInputLarge; if (!$scope.noTable) $scope.table = { params: null }; $scope.contentUrl = 'entity/' + $scope.entity + '/' + $scope.entity + '.list.html'; //main initialize(loadFinished); //private functions function initialize(callback) { blockUI.start($interpolate("{{'api.entity.getData' | translate}}")($scope)); var entities = ($scope.entityObj.lookup || []).concat($scope.entity); $entity.getMany(entities, function (err, lookups) { blockUI.stop(); callback(); if (err) return swal("Não foi possível obter os dados", err, 'warning'); if (typeof $scope.lookup === 'undefined') $scope.lookup = {}; for (var key in lookups) $scope.lookup[key] = lookups[key]; $scope.items = angular.copy(lookups[$scope.entity]); if ($scope.noTable) return; $scope.table.params = new NgTableParams({}, { dataset: $scope.items }); }); } function loadFinished() { $timeout(function () { var websocket; if (typeof $scope.entityObj.websocket !== 'function') websocket = $scope.entity; else websocket = $scope.entityObj.websocket($rootScope, $routeParams) + "." + $scope.entity; $scope.$on('socket:' + websocket + '.remove', onThisEntityRemove); $scope.$on('socket:' + websocket + '.new', onThisEntityNew); $scope.$on('socket:' + websocket + '.edit', onThisEntityEdit); //inject child controller try { $controller($scope.entity + 'Ctrl', { $scope: $scope }); } catch (err) { console.log(err); } }); } function onThisEntityNew(ev, data) { //todo //$scope.lookup[$scope.entity] = $lookup.set($scope.entity, data); //$scope.items = angular.copy($lookup.get($scope.entity)); $scope.items.unshift(data); if ($scope.noTable) return; $scope.table.params.total($scope.items.length); $scope.table.params.reload(); //$scope.table.params = new NgTableParams({}, { dataset: $scope.items }); } function onThisEntityEdit(ev, data) { //todo //$scope.lookup[$scope.entity] = $lookup.set($scope.entity, data); //$scope.items = angular.copy($lookup.get($scope.entity)); var index = $scope.items.findIndex(function (item) { return data[idField] == item[idField]; }); if (index === -1) return; $scope.items[index] = data; if ($scope.noTable) return; $scope.table.params.total($scope.items.length); $scope.table.params.reload(); //$scope.table.params = new NgTableParams({}, { dataset: $scope.items }); } function onThisEntityRemove(ev, id) { //todo //$scope.lookup[$scope.entity] = $lookup.unset($scope.entity, id); //$scope.items = angular.copy($lookup.get($scope.entity)); var index = $scope.items.findIndex(function (item) { return item[idField] == id; }); if (index !== -1) $scope.items.splice(index, 1); if ($scope.noTable) return; $scope.table.params.total($scope.items.length); $scope.table.params.reload(); //$scope.table.params = new NgTableParams({}, { dataset: $scope.items }); } } }; });