UNPKG

bitfront-library

Version:

Angular CLI project with components and classes used by other Angular projects of the BIT foundation.

212 lines 10.6 kB
"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.BaseSublistComponent = void 0; var core_1 = require("@angular/core"); var base_component_1 = require("./base.component"); var item_1 = require("../../shared/data/item"); var message_service_1 = require("../../shared/service/message.service"); var toolbar_service_1 = require("../../shared/service/toolbar.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 i0 = require("@angular/core"); var i1 = require("../../shared/service/general-utils.service"); var i2 = require("../../shared/service/message.service"); var i3 = require("../../shared/service/toolbar.service"); var _c0 = ["tableview"]; /** */ var BaseSublistComponent = /** @class */ (function (_super) { __extends(BaseSublistComponent, _super); function BaseSublistComponent(generalUtils, messageService, toolbarService, componentId) { var _this = _super.call(this, messageService, toolbarService, componentId) || this; _this.generalUtils = generalUtils; _this.messageService = messageService; _this.displayInnerForm = false; // si tenemos que mostrar el form para editar el objeto en linea o no _this.femenino = false; // si tenemos que mostrar los textos en femenino o no (p. ej., Nou vs Nova) _this.reloadFromServer = true; //indica si una vez añadido un elemento, llamaos al servidor para recargar la lista. Por defecto sí lo hace. _this.toolbar = {}; _this.toolbar[bit_toolbar_component_1.BitToolbarComponent.NEW_BUTTON] = { enable: true, visible: true }; _this.toolbar[bit_toolbar_component_1.BitToolbarComponent.DELETE_BUTTON] = { enable: true, visible: false }; _this.updateToolbar(); return _this; } BaseSublistComponent.prototype.ngOnDestroy = function () { _super.prototype.ngOnDestroy.call(this); }; /** Controlamos si la propiedad initLoad cambia a true para cargar la lista de documentos */ BaseSublistComponent.prototype.ngOnChanges = function (changes) { // console.log('BaseSublist OnChanges', changes); for (var propName in changes) { var changedProp = changes[propName]; if (propName === "initLoad" || propName === "parent") { if (this.initLoad && this.parent && this.parent.id && this.loadedParentId !== this.parent.id) { // nos dicen de cargar y no hemos cargado y el padre se ha cargado this.loadItems(); } } if (propName === "itemsPreloaded") { this.lista = changedProp.currentValue; } if (propName === "forceReload") { this.forceReload && this.forceReload.subscribe(function (x) { this.loadedParentId === this.parent.id && this.loadItems(); }.bind(this)); } } }; /** Nos presionan algún botón del toolbar */ BaseSublistComponent.prototype.onToolbarButtonPressed = function (button) { console.log("Button pressed: " + button); if (button === bit_toolbar_component_1.BitToolbarComponent.NEW_BUTTON) { this.gotoForm(); //this.openForm(BitDocument.newObject(), true); } else if (button === bit_toolbar_component_1.BitToolbarComponent.DELETE_BUTTON) { var p = this.messageService.confirm("Està segur que vol eliminar els elements seleccionats? Aquesta acciò no es podrà desfer."); var local_1 = this; p.then(function (confirmacion) { if (confirmacion) { local_1.deleteItems(local_1.selectedItems); } }); //this.deleteItems(this.selectedItems); } else { this.otherAction(button); } }; /** Cualquier otra accion que no sea la típica de guardar o eliminar */ BaseSublistComponent.prototype.otherAction = function (action) { console.warn("otherAction " + action + " pressed"); }; /** Nos checkean algún item del listado */ BaseSublistComponent.prototype.onItemChecked = function (selectedItems) { // console.log('element selected'); this.selectedItems = selectedItems; if (this.toolbar[bit_toolbar_component_1.BitToolbarComponent.DELETE_BUTTON]) { if (selectedItems !== null && selectedItems.length > 0) { this.toolbar[bit_toolbar_component_1.BitToolbarComponent.DELETE_BUTTON].visible = true; } else { this.toolbar[bit_toolbar_component_1.BitToolbarComponent.DELETE_BUTTON].visible = false; } this.updateToolbar(); } }; /** nos piden cerrar el formulario */ BaseSublistComponent.prototype.closeForm = function (doc) { this.displayInnerForm = false; }; /** Cargamos la lista de documentos del usuario */ BaseSublistComponent.prototype.loadItems = function () { var _this = this; this.callLoadItems(this.parent).subscribe(function (result) { _this.lista = result; _this.loadedParentId = _this.parent.id; _this.postLoadItems(result); }); }; /** Elimina los documentos seleccionados */ BaseSublistComponent.prototype.deleteItems = function (items) { var _this = this; console.log("delete items"); this.callDeleteItems(this.parent, items).subscribe(function (result) { _this.lista = _this.lista.filter(function (item) { return items.indexOf("" + item.id) < 0; }); _this.postDelete(result); }); }; /** Inserta o actualiza el documento */ BaseSublistComponent.prototype.saveItem = function (item) { var _this = this; this.preSave(item); //item.idUser = this.parent.id; //mapeamos el identificador del usuario en el objeto BitDocument this.callSaveItem(this.parent, item, this.modeInnerForm).subscribe(function (result) { _this.displayInnerForm = false; if (_this.reloadFromServer) { _this.loadItems(); } else { if (_this.modeInnerForm === global_constants_1.FormType.Insert) { if (!_this.lista) _this.lista = []; //la lista no estará inicializada si el componente se cargó sin elementos _this.lista.push(result); } else { var index = _this.lista.findIndex(function (item2) { return item2.id === item.id; }); //posicion del objeto en el array if (index != -1) { //controlamos que el elemento a insertar esté en la sublista _this.lista.splice(index, 1, item); //sustituimos el objeto de la posición por el nuevo que nos llega del servidor } } } _this.postSave(result); }); }; /** Función para hacer algo antes de guardar un item */ BaseSublistComponent.prototype.preSave = function (item) { return item; }; /** Función para hacer algo después de haber guardado */ BaseSublistComponent.prototype.postSave = function (result) { console.log("postSave llamado"); }; /** Función para hacer algo después de haber eliminado los elementos seleccionados */ BaseSublistComponent.prototype.postDelete = function (result) { console.log("postDelete llamado"); this.toolbar[bit_toolbar_component_1.BitToolbarComponent.DELETE_BUTTON].visible = false; this.updateToolbar(); if (this.tableView != null) this.tableView.selectedItems = []; }; /** Función para hacer algo después de haber recargado la lista */ BaseSublistComponent.prototype.postLoadItems = function (items) { // Nada por defecto // Por ejemplo, una sublista no-store incluida en un formulario con stores, aquí // podría hacer un changeDetectorRef.detectChanges() }; BaseSublistComponent.ɵfac = function BaseSublistComponent_Factory(t) { i0.ɵɵinvalidFactory(); }; BaseSublistComponent.ɵdir = i0.ɵɵdefineDirective({ type: BaseSublistComponent, viewQuery: function BaseSublistComponent_Query(rf, ctx) { if (rf & 1) { i0.ɵɵviewQuery(_c0, 1); } if (rf & 2) { var _t = void 0; i0.ɵɵqueryRefresh(_t = i0.ɵɵloadQuery()) && (ctx.tableView = _t.first); } }, inputs: { parent: "parent", parentField: "parentField", initLoad: "initLoad", itemsPreloaded: "itemsPreloaded", femenino: "femenino", forceReload: "forceReload", reloadFromServer: "reloadFromServer" }, features: [i0.ɵɵInheritDefinitionFeature, i0.ɵɵNgOnChangesFeature] }); return BaseSublistComponent; }(base_component_1.BaseComponent)); exports.BaseSublistComponent = BaseSublistComponent; (function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(BaseSublistComponent, [{ type: core_1.Directive }], function () { return [{ type: i1.GeneralUtils }, { type: i2.MessageService }, { type: i3.ToolbarService }, { type: undefined }]; }, { tableView: [{ type: core_1.ViewChild, args: ["tableview"] }], parent: [{ type: core_1.Input }], parentField: [{ type: core_1.Input }], initLoad: [{ type: core_1.Input }], itemsPreloaded: [{ type: core_1.Input }], femenino: [{ type: core_1.Input }], forceReload: [{ type: core_1.Input }], reloadFromServer: [{ type: core_1.Input }] }); })(); //# sourceMappingURL=base-sublist.component.js.map