UNPKG

rb-data-table

Version:
113 lines 4.86 kB
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 (b.hasOwnProperty(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 __()); }; })(); import { HttpParams } from '@angular/common/http'; import { LocalDataSource } from '../local/local.data-source'; import { ServerSourceConf } from './server-source.conf'; import { getDeepFromObject } from '../../helpers'; import { map } from 'rxjs/operators'; var ServerDataSource = /** @class */ (function (_super) { __extends(ServerDataSource, _super); function ServerDataSource(http, conf) { if (conf === void 0) { conf = {}; } var _this = _super.call(this) || this; _this.http = http; _this.lastRequestCount = 0; _this.conf = new ServerSourceConf(conf); if (!_this.conf.endPoint) { throw new Error('At least endPoint must be specified as a configuration of the server data source.'); } return _this; } ServerDataSource.prototype.count = function () { return this.lastRequestCount; }; ServerDataSource.prototype.getElements = function () { var _this = this; return this.requestElements() .pipe(map(function (res) { _this.lastRequestCount = _this.extractTotalFromResponse(res); _this.data = _this.extractDataFromResponse(res); return _this.data; })).toPromise(); }; /** * Extracts array of data from server response * @param res * @returns {any} */ ServerDataSource.prototype.extractDataFromResponse = function (res) { var rawData = res.body; var data = !!this.conf.dataKey ? getDeepFromObject(rawData, this.conf.dataKey, []) : rawData; if (data instanceof Array) { return data; } throw new Error("Data must be an array.\n Please check that data extracted from the server response by the key '" + this.conf.dataKey + "' exists and is array."); }; /** * Extracts total rows count from the server response * Looks for the count in the heders first, then in the response body * @param res * @returns {any} */ ServerDataSource.prototype.extractTotalFromResponse = function (res) { if (res.headers.has(this.conf.totalKey)) { return +res.headers.get(this.conf.totalKey); } else { var rawData = res.body; return getDeepFromObject(rawData, this.conf.totalKey, 0); } }; ServerDataSource.prototype.requestElements = function () { var httpParams = this.createRequesParams(); return this.http.get(this.conf.endPoint, { params: httpParams, observe: 'response' }); }; ServerDataSource.prototype.createRequesParams = function () { var httpParams = new HttpParams(); httpParams = this.addSortRequestParams(httpParams); httpParams = this.addFilterRequestParams(httpParams); return this.addPagerRequestParams(httpParams); }; ServerDataSource.prototype.addSortRequestParams = function (httpParams) { var _this = this; if (this.sortConf) { this.sortConf.forEach(function (fieldConf) { httpParams = httpParams.set(_this.conf.sortFieldKey, fieldConf.field); httpParams = httpParams.set(_this.conf.sortDirKey, fieldConf.direction.toUpperCase()); }); } return httpParams; }; ServerDataSource.prototype.addFilterRequestParams = function (httpParams) { var _this = this; if (this.filterConf.filters) { this.filterConf.filters.forEach(function (fieldConf) { if (fieldConf['search']) { httpParams = httpParams.set(_this.conf.filterFieldKey.replace('#field#', fieldConf['field']), fieldConf['search']); } }); } return httpParams; }; ServerDataSource.prototype.addPagerRequestParams = function (httpParams) { if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) { httpParams = httpParams.set(this.conf.pagerPageKey, this.pagingConf['page']); httpParams = httpParams.set(this.conf.pagerLimitKey, this.pagingConf['perPage']); } return httpParams; }; return ServerDataSource; }(LocalDataSource)); export { ServerDataSource }; //# sourceMappingURL=server.data-source.js.map