ng2-smart-table-custom
Version:
Angular Smart Table with inline-validations support
112 lines • 4.97 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var 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 function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { URLSearchParams } from '@angular/http';
import { LocalDataSource } from '../local/local.data-source';
import { ServerSourceConf } from './server-source.conf';
import { getDeepFromObject } from '../../helpers';
import 'rxjs/add/operator/toPromise';
var ServerDataSource = (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().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.json();
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.json();
return getDeepFromObject(rawData, this.conf.totalKey, 0);
}
};
ServerDataSource.prototype.requestElements = function () {
return this.http.get(this.conf.endPoint, this.createRequestOptions());
};
ServerDataSource.prototype.createRequestOptions = function () {
var requestOptions = {};
requestOptions.params = new URLSearchParams();
requestOptions = this.addSortRequestOptions(requestOptions);
requestOptions = this.addFilterRequestOptions(requestOptions);
return this.addPagerRequestOptions(requestOptions);
};
ServerDataSource.prototype.addSortRequestOptions = function (requestOptions) {
var _this = this;
var searchParams = requestOptions.params;
if (this.sortConf) {
this.sortConf.forEach(function (fieldConf) {
searchParams.set(_this.conf.sortFieldKey, fieldConf.field);
searchParams.set(_this.conf.sortDirKey, fieldConf.direction.toUpperCase());
});
}
return requestOptions;
};
ServerDataSource.prototype.addFilterRequestOptions = function (requestOptions) {
var _this = this;
var searchParams = requestOptions.params;
if (this.filterConf.filters) {
this.filterConf.filters.forEach(function (fieldConf) {
if (fieldConf['search']) {
searchParams.set(_this.conf.filterFieldKey.replace('#field#', fieldConf['field']), fieldConf['search']);
}
});
}
return requestOptions;
};
ServerDataSource.prototype.addPagerRequestOptions = function (requestOptions) {
var searchParams = requestOptions.params;
if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) {
searchParams.set(this.conf.pagerPageKey, this.pagingConf['page']);
searchParams.set(this.conf.pagerLimitKey, this.pagingConf['perPage']);
}
return requestOptions;
};
return ServerDataSource;
}(LocalDataSource));
export { ServerDataSource };
//# sourceMappingURL=server.data-source.js.map