bitfront-library
Version:
Angular CLI project with components and classes used by other Angular projects of the BIT foundation.
243 lines • 12.3 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseListComponent = void 0;
var router_1 = require("@angular/router");
var rxjs_1 = require("rxjs");
var base_component_1 = require("./base.component");
var base_filter_1 = require("../shared/base.filter");
var message_service_1 = require("../../shared/service/message.service");
var toolbar_service_1 = require("../../shared/service/toolbar.service");
var tableview_service_1 = require("../../shared/service/tableview.service");
var general_utils_service_1 = require("../../shared/service/general-utils.service");
var global_constants_1 = require("../../shared/global.constants");
var bit_toolbar_component_1 = require("../../shared/component/bit-toolbar.component");
var core_1 = require("@angular/core");
var i0 = require("@angular/core");
var i1 = require("../../shared/service/message.service");
var i2 = require("@angular/router");
var i3 = require("../../shared/service/toolbar.service");
var i4 = require("../../shared/service/general-utils.service");
var i5 = require("../../shared/service/tableview.service");
/**
* Clase base para implementar listados de elementos con paginación. Provee el 95% de la funcionalidad necesaria.
*/
var BaseListComponent = /** @class */ (function (_super) {
__extends(BaseListComponent, _super);
/** Constructor de la clase. */
function BaseListComponent(dataService, messageService, activatedRoute, router, toolbarService, componentId, generalUtils, tableViewService) {
var _this = _super.call(this, messageService, toolbarService, componentId) || this;
_this.displayFilter = true; // indicará si se debe o no mostrar el componente hijo que representa el filtro
_this.breadcrumb = []; //contiene la miga de pan de nuestro componente
//heredado! status: PageStatus; // determina en qué estado está la página: PageStatus.Init, PageStatus.SEARCHING, PageStatus.FINISH_SEARCH, PageStatus.ERROR
_this.autoQuerySubject = new rxjs_1.ReplaySubject(1); //es necesario que sea publica para que con AOT se pueda acceder a ella
_this.customToolbarActions = {};
_this.dataService = dataService;
_this.toolbarService = toolbarService;
_this.activatedRoute = activatedRoute;
_this.router = router;
_this.generalUtils = generalUtils;
_this.tableViewService = tableViewService;
//inicializamos los datos
_this.resetData();
//mensajes para el footer
_this.messages = new Map();
_this.messages.set(global_constants_1.PageStatus.Init, "Introdueixi els criteris de cerca i premi 'Cerca'");
_this.messages.set(global_constants_1.PageStatus.Searching, "Cercant...");
_this.messages.set(global_constants_1.PageStatus.FinishSearch, "No s'ha trobat cap dada amb les dades introduïdes");
return _this;
}
BaseListComponent.prototype.ngOnDestroy = function () {
_super.prototype.ngOnDestroy.call(this);
};
/** Determina si se debe autoejecutar la búsqueda en función de si nos envían el parametro query=true en la URL */
BaseListComponent.prototype.init = function (filter) {
//¿ejecutamos el filtro?
this.filter = filter;
var query = this.activatedRoute.snapshot.params["query"];
if (query == "true") {
console.log("autoquery en marcha");
if (this.filter.pagina != base_filter_1.BaseFilter.PAGE || this.filter.resultados != base_filter_1.BaseFilter.RESULTADOS_PAGE) {
this.autoQuerySubject.next(this.filter);
}
this.search();
}
};
/** Resetea la página como si entráramos de nuevo en ella. */
BaseListComponent.prototype.resetData = function () {
_super.prototype.resetData.call(this);
this.items = null;
this.totalItems = 0;
this.status = global_constants_1.PageStatus.Init;
this.displayFilter = true;
this.toolbar = {
//opciones comunies del toolbar para los listados
filter: { enable: false, visible: true },
print: { enable: false, visible: true },
export: { enable: false, visible: true },
new: { enable: false, visible: false }
};
this.updateToolbar();
};
/** Se invocará cuando el componente paginator hijo nos comunique que el usuario ha cambiado el número de elementos a mostrar por página */
BaseListComponent.prototype.onNewPageSize = function (pageSize) {
this.filter.pagina = 1; // reseteamos a la página inicial para evitar comportamienos extraños
this.filter.resultados = pageSize;
this.search(false);
};
/** Se invocará cuando el componente paginator hijo nos comunique que el usuario ha cambiado el número de página a mostrar */
BaseListComponent.prototype.onPageChange = function (page) {
this.filter.pagina = page;
this.search(false);
};
/** Cuando algún componente hijo como el filtro nos comunique que se debe realizar la búsqueda */
BaseListComponent.prototype.onSearch = function (filter, count) {
if (count === void 0) { count = true; }
//no sabemos cuantos elementos tendremos aun. Ponerlo a undefined nos permite saber si lanzamos un count o nos quedamos con el numero de elemntos retornados
this.totalItems = undefined;
this.filter = filter;
this.prepareDataForSubmit();
this.autoQuerySubject.next(this.filter); // comunicamos el nuevo filtro para que el footer se actualice
this.search(count);
};
/** Cuando resetean el filtro de busqueda */
BaseListComponent.prototype.onReset = function (filter) {
this.filter = filter;
};
/** Cuando se pulsa un botón del toolbar */
BaseListComponent.prototype.onToolbarButtonPressed = function (button) {
console.log("Button pressed:" + button);
if (this.customToolbarActions[button]) {
this.customToolbarActions[button]();
}
else if (button === bit_toolbar_component_1.BitToolbarComponent.FILTER_BUTTON) {
this.showFilter();
}
else if (button === bit_toolbar_component_1.BitToolbarComponent.PRINT_BUTTON) {
this.handlePrint();
}
else if (button === bit_toolbar_component_1.BitToolbarComponent.EXPORT_BUTTON) {
this.handleExport();
}
else if (button === bit_toolbar_component_1.BitToolbarComponent.NEW_BUTTON) {
this.handleNew();
}
else {
this.otherAction(button);
}
};
BaseListComponent.prototype.handlePrint = function () {
window.print();
};
BaseListComponent.prototype.handleExport = function () {
if (this.tableViewService) {
this.tableViewService.sendExportMessage();
}
else {
alert("Exportació no implementada per aquest llistat");
}
};
BaseListComponent.prototype.handleNew = function () {
this.router.navigate(["form"], { relativeTo: this.activatedRoute });
};
/**cualquier otra accion que no sea la típica de guardar o eliminar */
BaseListComponent.prototype.otherAction = function (button) {
console.warn("otherAction " + button + " pressed");
};
BaseListComponent.prototype.initializeSearch = function (count) {
// Inicializamos los datos básicos (solo si es necesario)
if (count) {
this.totalItems = 0;
}
this.status = global_constants_1.PageStatus.Searching;
};
/** Marca el estado como "búsqueda finalizada" (PageStatus.FinishSearch). */
BaseListComponent.prototype.finishSearch = function () {
this.status = global_constants_1.PageStatus.FinishSearch;
//controlamos si tenemos que activar o desactivar los botones de toolbar print y export
if (this.totalItems > 0) {
//hay resulstados
this.toolbar[bit_toolbar_component_1.BitToolbarComponent.PRINT_BUTTON].enable = true;
this.toolbar[bit_toolbar_component_1.BitToolbarComponent.EXPORT_BUTTON].enable = true;
this.hideFilter(); //en este método se envía el toolbar
}
else {
this.toolbar[bit_toolbar_component_1.BitToolbarComponent.PRINT_BUTTON].enable = false;
this.toolbar[bit_toolbar_component_1.BitToolbarComponent.EXPORT_BUTTON].enable = false;
this.updateToolbar();
}
};
/** Realiza la búsqueda filtrada de elementos. */
BaseListComponent.prototype.search = function (count) {
var _this = this;
if (count === void 0) { count = true; }
// Evitamos iniciar una nueva búsqueda cuando la anterior aún está en curso (p. ej., double click)
if (this.status === global_constants_1.PageStatus.Searching) {
return;
}
//inicializamos la búsqueda
this.initializeSearch(count);
// Consultamos los datos
this.dataService.getAllByFilter(this.filter, this.paramsDataService).subscribe(function (result) {
_this.items = result;
if (count) {
_this.count();
}
else {
// si no teníamos antes un total, es un listado no paginado y el total es la pagina actual
_this.totalItems = _this.totalItems || _this.items.length;
_this.finishSearch();
}
if (!_this.items || _this.items.length == 0) {
_this.showFilter();
}
}, //finishSearch si no estamos pendiente de la cuenta
function (error) {
_this.status = global_constants_1.PageStatus.Error;
_this.items = null;
});
};
/** Realiza la búsqueda filtrada de elementos. */
BaseListComponent.prototype.count = function () {
var _this = this;
this.dataService.countAllByFilter(this.filter, this.paramsDataService).subscribe(function (result) {
_this.totalItems = result || 0; //por alguna razón que desconozco el servidor envía un 0 pero el observable interpreta un null
_this.finishSearch();
}, function (error) {
_this.status = global_constants_1.PageStatus.Error;
_this.items = null;
});
};
/** Muestra el filtro de búsqueda. */
BaseListComponent.prototype.showFilter = function () {
this.displayFilter = true;
this.toolbar[bit_toolbar_component_1.BitToolbarComponent.FILTER_BUTTON].enable = false;
this.updateToolbar();
};
/** Oculta el filtro de búsqueda. */
BaseListComponent.prototype.hideFilter = function () {
this.displayFilter = false;
this.toolbar[bit_toolbar_component_1.BitToolbarComponent.FILTER_BUTTON].enable = true;
this.updateToolbar();
};
BaseListComponent.ɵfac = function BaseListComponent_Factory(t) { i0.ɵɵinvalidFactory(); };
BaseListComponent.ɵdir = i0.ɵɵdefineDirective({ type: BaseListComponent, features: [i0.ɵɵInheritDefinitionFeature] });
return BaseListComponent;
}(base_component_1.BaseComponent));
exports.BaseListComponent = BaseListComponent;
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BaseListComponent, [{
type: core_1.Directive
}], function () { return [{ type: undefined }, { type: i1.MessageService }, { type: i2.ActivatedRoute }, { type: i2.Router }, { type: i3.ToolbarService }, { type: undefined }, { type: i4.GeneralUtils }, { type: i5.TableViewService }]; }, null); })();
//# sourceMappingURL=base-list.component.js.map