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.
149 lines (129 loc) • 4.77 kB
JavaScript
app.directive('zlReport', function () {
return {
restrict: 'E',
scope: {
reportKey: '@',
namespace: '@',
db: '@'
},
replace: false,
templateUrl: 'directives/report/report.html',
controller: function ($scope, NgTableParams, $entity, $util, moment, $report, $httpParamSerializer, blockUI) {
$scope.savedItems = [];
$scope.fields = [];
$scope.fieldTabs = [];
$scope.activeTab = 0;
reset();
initialize();
$scope.reset = reset;
$scope.appendQuery = function () {
$scope.item.query.push({});
};
$scope.removeQuery = function (index) {
$scope.item.query.splice(index, 1);
};
$scope.getReport = function (item) {
var map = {};
$scope.cols = [];
item.select.forEach(function (fieldId) {
var field = $scope.fields.find(function (field) {
return field.id == fieldId;
});
var key = typeof field.name === 'undefined' ? field.invoke : field.name;
if (["Date", "Boolean"].indexOf(field.type) !== -1) map[key] = field.type;
$scope.cols.push({
field: key,
title: field.caption,
sortable: "'" + key + "'",
show: true
});
});
$report.getReport($scope.namespace, $scope.db, $scope.reportKey, item, function (err, data) {
if (err) return swal("Ocorreu um problema", err, "error");
var items = $util.flattenArray(data, true);
items.forEach(function (item) {
for (var key in item) {
if (map[key] === "Date") item[key] = item[key] ? moment(item[key]).format('YYYY-MM-DD') : '';
else if (map[key] === "Boolean") item[key] = item[key] ? "Sim" : "Não";
}
});
$scope.items = items;
$scope.tableParams = new NgTableParams({}, { dataset: items });
});
};
$scope.loadReport = function (item, index) {
$scope.item = item;
$scope.activeTab = index;
};
$scope.saveReport = function (item) {
item.key = $scope.reportKey;
blockUI.start("A guardar listagem...");
$entity.add('report', item, $scope).then(function (response) {
swal("Guardado", "A listagem foi guardada com sucesso. Poderá reutiliza-la no futuro a partir do separador 'Restaurar'", "success");
//todo $route.reload();
}).catch(function (response) {
swal("Atenção", response.data, "warning");
}).finally(function () {
blockUI.stop();
});
};
$scope.removeReport = function (item) {
blockUI.start("A apagar listagem...");
$entity.remove('report', item, $scope).then(function (response) {
swal("Apagado", "A listagem foi apagada com sucesso", "success");
//todo $route.reload();
}).catch(function (response) {
swal("Atenção", response.data, "warning");
}).finally(function () {
blockUI.stop();
});
};
$scope.getExcelUrl = function (item) {
return '/api/report/get/' + $scope.namespace + '/' + $scope.db + '/' + $scope.reportKey + '?xls=1&' + $httpParamSerializer(item);
};
$scope.applySearch = function (search) {
var criteria = angular.copy(search.text);
if (search.inverted) {
criteria = "!" + criteria;
}
$scope.tableParams.filter({ $: criteria });
};
function reset() {
$scope.tableParams = null;
$scope.cols = [];
$scope.item = {
select: [],
query: [],
sort: {}
};
$scope.activeTab = 1;
}
function initialize() {
$report.getMetadata($scope.reportKey, function (err, metadata) {
if (err) return swal("Atenção", err, "error");
$scope.fieldTabs = [];
metadata.fields.forEach(function (field) {
field.tab = field.tab || "Outros campos";
var index = $scope.fieldTabs.findIndex(function (fieldTab) {
return fieldTab.name === field.tab;
});
if (index !== -1) return;
$scope.fieldTabs.push({
name: field.tab,
isOpen: false
});
});
$scope.fields = metadata.fields;
});
blockUI.start("A obter listagens guardadas...");
$entity.getByKey('report', { key: $scope.reportKey }).then(function (response) {
$scope.savedItems = response.data;
}).catch(function (response) {
swal("Atenção", response.data, "error");
}).finally(function () {
blockUI.stop();
});
}
}
};
});