UNPKG

ng2-bs-table

Version:
251 lines 8.97 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); var core_1 = require("@angular/core"); var common_1 = require("@angular/common"); var order_by_pipe_1 = require("./pipes/order-by.pipe"); var format_pipe_1 = require("./pipes/format.pipe"); var table_view_sorting_1 = require("./table-view-sorting"); var table_shared_module_1 = require("./table-shared.module"); var transform_by_pipe_1 = require("./pipes/transform-by.pipe"); var table_view_pagination_1 = require("./table-view-pagination"); var TableViewComponent = (function () { function TableViewComponent() { this.filters = []; this.actions = []; this.imports = []; this.autoPipe = true; this.transformers = []; this.pagination = new table_view_pagination_1.TableViewPagination; // @todo Implement interface //Outputs this.order = new core_1.EventEmitter(); this.filter = new core_1.EventEmitter(); this.cellChange = new core_1.EventEmitter(); this.pageChange = new core_1.EventEmitter(); this.actionClick = new core_1.EventEmitter(); } /** * ng init */ TableViewComponent.prototype.ngOnInit = function () { var _this = this; this.imports = Object.assign(this.imports, [table_shared_module_1.TableSharedModule]); this.sorting.onEventSort().subscribe(function (event) { _this.onOrder(event); }); this.pagination.onEventPage().subscribe(function (event) { _this.onPageChange(event); }); this.setTransformer(this.pagination.doPaging(1, this.getTotalItems())); this.setTransformer(this.sorting.orderBy()); }; /** * On changes input * @param changes */ TableViewComponent.prototype.ngOnChanges = function (changes) { this.setTransformer(this.pagination.doPaging(1, this.getTotalItems())); }; // /** // * Order by column // * @return {string} // */ // orderBy() { // return this.sorting.orderBy(); // } /** * Transform by all transformers * @return {{pipe: TableFilterInterfacePipe, args: any}[]} */ TableViewComponent.prototype.transformBy = function () { return this.transformers.map(function (data) { return data.params; }); }; /** * Set collection * @param collection * @param totalItems */ TableViewComponent.prototype.setCollection = function (collection, totalItems) { this.collection = collection; this.setTotalItems(totalItems); }; /** * Set total items. You can set total items. If you set total then * getTotalItems will always return <total>. * @param totalItems */ TableViewComponent.prototype.setTotalItems = function (totalItems) { this.totalItems = totalItems; }; /** * Get total items. * @return {number} */ TableViewComponent.prototype.getTotalItems = function () { var value = this.totalItems || 0; if (this.collection !== null || this.totalItems === null) { var filters = this.transformers.filter(function (transformer) { return transformer.priority == 0; }); var collection = (new transform_by_pipe_1.TransformByPipe()).transform(this.collection, filters.map(function (transformer) { return transformer.params; })); value = collection.length; } return value; }; /** * Set transformer to transformers list. If transformer exists then it will be rewritten. * * @param transformer */ TableViewComponent.prototype.setTransformer = function (transformer) { if (null === transformer || this.autoPipe === false) { return; } var transformers = this.transformers.filter(function (e) { return e.id != transformer.id; }); transformers.push(transformer); transformers = transformers.sort(function (a, b) { return a.priority - b.priority; }); this.transformers = transformers; }; /** * Get transformer by id * @param id * @return {undefined|TableTransformerInterface} */ TableViewComponent.prototype.getTransformer = function (id) { return this.transformers.find(function (transformer) { return transformer.id == id; }); }; /** * Event page Change * @param event */ TableViewComponent.prototype.onPageChange = function (event) { this.setTransformer(this.pagination.doPaging(event.currentPage, this.getTotalItems())); this.pageChange.emit(event); }; /** * Event cell change * @param event */ TableViewComponent.prototype.onCellChange = function (event) { this.cellChange.emit(event); }; /** * Event collection order * @param event */ TableViewComponent.prototype.onOrder = function (event) { this.setTransformer(this.sorting.orderBy()); this.order.emit(event); }; /** * Event collection filter * @param event */ TableViewComponent.prototype.onFilter = function (event) { this.setTransformer({ id: event.columnName, priority: 0, params: { pipe: event.pipe, args: { columnName: event.columnName, filter: event.filter } } }); this.setTransformer(this.pagination.doPaging(1, this.getTotalItems())); this.filter.emit(event); }; /** * Event action click * @param event */ TableViewComponent.prototype.onActionClick = function (event) { this.actionClick.emit(event); }; return TableViewComponent; }()); __decorate([ core_1.Input(), __metadata("design:type", Array) ], TableViewComponent.prototype, "collection", void 0); __decorate([ core_1.Input(), __metadata("design:type", Array) ], TableViewComponent.prototype, "columns", void 0); __decorate([ core_1.Input(), __metadata("design:type", Array) ], TableViewComponent.prototype, "filters", void 0); __decorate([ core_1.Input(), __metadata("design:type", Array) ], TableViewComponent.prototype, "actions", void 0); __decorate([ core_1.Input(), __metadata("design:type", Array) ], TableViewComponent.prototype, "imports", void 0); __decorate([ core_1.Input(), __metadata("design:type", Boolean) ], TableViewComponent.prototype, "autoPipe", void 0); __decorate([ core_1.Input(), __metadata("design:type", Array) ], TableViewComponent.prototype, "transformers", void 0); __decorate([ core_1.Input(), __metadata("design:type", table_view_sorting_1.TableViewSorting) ], TableViewComponent.prototype, "sorting", void 0); __decorate([ core_1.Input(), __metadata("design:type", table_view_pagination_1.TableViewPagination) ], TableViewComponent.prototype, "pagination", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], TableViewComponent.prototype, "order", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], TableViewComponent.prototype, "filter", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], TableViewComponent.prototype, "cellChange", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], TableViewComponent.prototype, "pageChange", void 0); __decorate([ core_1.Output(), __metadata("design:type", core_1.EventEmitter) ], TableViewComponent.prototype, "actionClick", void 0); TableViewComponent = __decorate([ core_1.Component({ moduleId: module.id, selector: 'table-view', templateUrl: 'table-view.component.html', providers: [order_by_pipe_1.OrderByPipe, common_1.JsonPipe, format_pipe_1.FormatPipe, transform_by_pipe_1.TransformByPipe] }) ], TableViewComponent); exports.TableViewComponent = TableViewComponent; //# sourceMappingURL=table-view.component.js.map