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.

129 lines (111 loc) 4.57 kB
app.directive('entityEdit', function () { return { restrict: 'EA', scope: { entity: '@', item: '=?', lookup: '=?', form: '=', fetch: '@' }, template: '<div ng-include src="contentUrl" include-replace></div>', controller: function ($scope, zapi, $interpolate, $controller, $entity, $rootScope, $routeParams, $timeout, blockUI) { //validate $scope.entity = $scope.entity || $routeParams.entity; var entityObj = zapi.entityMap[$scope.entity]; if (!entityObj) return; if (!entityObj.edit) return; //defaults $scope.useInputLarge = zapi.useInputLarge; $scope.item = $scope.item || undefined; if (typeof $scope.lookup === 'undefined') $scope.lookup = {}; $scope.fetch = $scope.fetch ? true : false; $scope._form = $scope.form; $scope.contentUrl = 'entity/' + $scope.entity + '/' + $scope.entity + '.edit.html'; var idField = entityObj.id || "_id"; var idValue; if ($scope.item) idValue = $scope.item[idField]; else if ($routeParams.entity === $scope.entity && $routeParams.id) idValue = $routeParams.id; if (!idValue && Object.keys($scope.item || {}).length === 0) { if (typeof entityObj.blank === 'function') $scope.item = entityObj.blank(); else $scope.item = {}; } //main if ($scope.fetch) initialize(loadFinished); else loadFinished(); //private functions function initialize(callback) { if (!idValue) get($scope.entity, null, callback); else get($scope.entity, idValue, callback); } function get(entity, id, callback) { blockUI.start($interpolate("{{'api.entity.getData' | translate}}")($scope)); $entity.getMany(entityObj.lookup, function (err, lookups) { if (err) { blockUI.stop(); callback(); return swal("Não foi possível obter os dados", err, 'warning'); } for (var key in lookups) $scope.lookup[key] = lookups[key]; //if user is creating a new record if (!id) { blockUI.stop(); return callback(); } $entity.get(entity, id).then(function (response) { $scope.item = response.data; //todo //$scope.lookup[$scope.entity] = $lookup.set($scope.entity, response.data); }).catch(function (response) { swal({ title: "Não foi possível obter os dados", html: $interpolate("{{'api.entity.errorData' | translate}}")($scope), type: "warning" }); }).finally(function () { blockUI.stop(); callback(); }); }); } function loadFinished() { $timeout(function () { var entityKeys = (entityObj.lookup || []).concat($scope.entity); entityKeys.forEach(function (entityKey) { var entity = zapi.entityMap[entityKey]; var websocket; if (typeof entity.websocket !== 'function') websocket = entity.key; else websocket = entity.websocket($rootScope, $routeParams) + "." + entity.key; $scope.$on('socket:' + websocket + '.remove', onThisEntityRemove); $scope.$on('socket:' + websocket + '.new', onThisEntityEdit); $scope.$on('socket:' + websocket + '.edit', onThisEntityEdit); }); //inject child controller try { if ($scope.fetch) $controller($scope.entity + 'Ctrl', { $scope: $scope }); } catch (err) { console.log(err); } }); } function onThisEntityEdit(ev, data) { var matches = /socket\:(\w+)\./.exec(ev.name) || []; if (matches.length !== 2) return console.log('unexpected socket forwarding', ev, data); var entity = matches[1]; //todo //$scope.lookup[entity] = $lookup.set(entity, data); if (idValue != data[idField]) return; //$scope.item = data; } function onThisEntityRemove(ev, id) { var matches = /socket\:(\w+)\./.exec(ev.name) || []; if (matches.length !== 2) return console.log('unexpected socket forwarding', ev, data); var entity = matches[1]; //todo //$scope.lookup[$scope.entity] = $lookup.unset(entity, id); if (idValue != id) return; swal("Outro utilizador apagou este registo", "Actualize a página para ver reflitadas as alterações", "info"); } } }; });