@crnk/angular-ngrx
Version:
Angular helper library for ngrx-json-api and crnk:
203 lines • 8.62 kB
JavaScript
import { Subject } from 'rxjs/Subject';
import { Observable } from 'rxjs/Observable';
import * as _ from 'lodash';
import 'rxjs/add/operator/zip';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinct';
import 'rxjs/add/operator/switch';
import { Direction, FilteringParam, ManyQueryResult, NgrxJsonApiService, Query, Resource, SortingParam, NgrxJsonApiZoneService, NGRX_JSON_API_DEFAULT_ZONE, getNgrxJsonApiZone, selectNgrxJsonApiZone, QueryParams } from 'ngrx-json-api';
import { CrnkBindingUtils, toQueryPath } from './crnk.binding.utils';
// TODO get rid of this? or support multiple ones, only dependency to primeng here...
import { LazyLoadEvent } from 'primeng/primeng';
import { Store } from '@ngrx/store';
import { Subscription } from 'rxjs/Subscription';
var DataTableBinding = /** @class */ (function () {
function DataTableBinding(ngrxJsonApiService, config, utils, store) {
var _this = this;
this.config = config;
this.utils = utils;
this.store = store;
this.baseQuery = null;
this.latestQuery = null;
this.latestImplementationQueryParams = null;
this.latestExternalQueryParams = null;
this.zoneId = config.zoneId || NGRX_JSON_API_DEFAULT_ZONE;
this.zone = ngrxJsonApiService.getZone(this.zoneId);
this.implementationAdapter = this.config.implementationAdapter;
if (!this.implementationAdapter) {
this.implementationAdapter = new DataTablePrimengAdapter();
}
this.implementationAdapter.init({
applyQueryParams: function (implementationParams) { return _this.setImplementationQueryParams(implementationParams); }
});
if (!this.config.queryId) {
throw new Error('no queryId specified in config');
}
if (_.isUndefined(this.config.fromServer)) {
this.config.fromServer = true;
}
this.initBaseQuery();
this.result$ = this.zone
.selectManyResults(this.config.queryId, true)
.do(this.setBaseQueryIfNecessary)
.map(this.guardAgainstEmptyQuery)
.finally(function () { return _this.cancelSubscriptions; })
.share();
}
DataTableBinding.prototype.checkSubscriptions = function () {
var _this = this;
if (!this.customQueryParamsSubscription && this.config.customQueryParams) {
this.customQueryParamsSubscription = this.config.customQueryParams.subscribe(function (customQueryParams) {
_this.latestExternalQueryParams = customQueryParams;
_this.updateQueryParams();
});
}
};
DataTableBinding.prototype.cancelSubscriptions = function () {
if (this.customQueryParamsSubscription !== null) {
this.customQueryParamsSubscription.unsubscribe();
this.customQueryParamsSubscription = null;
}
};
DataTableBinding.prototype.initBaseQuery = function () {
var _this = this;
this.baseQuery = this.config.baseQuery;
if (!this.baseQuery) {
this.store.let(selectNgrxJsonApiZone(this.zoneId)).take(1)
.map(function (it) { return it.queries[_this.config.queryId]; })
.subscribe(function (storeQuery) {
if (storeQuery != null) {
_this.latestQuery = storeQuery.query;
_this.baseQuery = storeQuery.query;
}
});
}
if (!this.baseQuery) {
throw new Error('baseQuery not available');
}
};
DataTableBinding.prototype.setBaseQueryIfNecessary = function (result) {
if (this.baseQuery === null) {
this.baseQuery = result.query;
this.latestQuery = result.query;
}
};
DataTableBinding.prototype.guardAgainstEmptyQuery = function (result) {
// query not available until load event trigger by PrimeNG
// return empty object in that case
if (result) {
return result;
}
else {
var emptyResult = {
query: null,
loading: false,
resultIds: [],
meta: {},
links: {},
data: [],
errors: []
};
return emptyResult;
}
};
DataTableBinding.prototype.refresh = function () {
this.zone.refreshQuery(this.config.queryId);
};
DataTableBinding.prototype.setImplementationQueryParams = function (implementationParams) {
this.checkSubscriptions();
this.latestImplementationQueryParams = implementationParams;
this.updateQueryParams();
};
DataTableBinding.prototype.updateQueryParams = function () {
if (!this.latestImplementationQueryParams) {
// implementation not ready yet, wait
// note that custom parameters are optional and no waiting takes palce
return;
}
if (!this.baseQuery) {
throw new Error('illegal state, base query not available');
}
var query = _.cloneDeep(this.baseQuery);
if (this.latestExternalQueryParams) {
this.utils.applyQueryParams(query, this.latestExternalQueryParams);
}
this.utils.applyQueryParams(query, this.latestImplementationQueryParams);
if (!_.isEqual(query, this.latestQuery)) {
this.zone.putQuery({
query: query,
fromServer: this.config.fromServer
});
this.latestQuery = query;
}
};
DataTableBinding.prototype.onLazyLoad = function (event) {
this.implementationAdapter.onLazyLoad(event);
};
return DataTableBinding;
}());
export { DataTableBinding };
var DefaultDataFilterValueMapper = /** @class */ (function () {
function DefaultDataFilterValueMapper() {
}
DefaultDataFilterValueMapper.prototype.mapFilterValue = function (attributePath, operator, value) {
if (operator === 'like') {
return '%' + value + '%';
}
return value;
};
return DefaultDataFilterValueMapper;
}());
export { DefaultDataFilterValueMapper };
var DataTablePrimengAdapter = /** @class */ (function () {
function DataTablePrimengAdapter() {
this.defaultMatchMode = 'like';
this.filterValueMapper = new DefaultDataFilterValueMapper();
}
DataTablePrimengAdapter.prototype.init = function (context) {
this.context = context;
};
DataTablePrimengAdapter.prototype.onLazyLoad = function (event) {
var offset = event.first;
var limit = event.rows;
var filters = [];
var sorting = [];
var includes = [];
if (event.multiSortMeta) {
for (var _i = 0, _a = event.multiSortMeta; _i < _a.length; _i++) {
var sortMeta = _a[_i];
var direction = sortMeta.order === -1 ? Direction.DESC : Direction.ASC;
sorting.push({ direction: direction, api: toQueryPath(sortMeta.field) });
}
}
else if (event.sortField) {
var direction = event.sortOrder === -1 ? Direction.DESC : Direction.ASC;
sorting.push({ direction: direction, api: toQueryPath(event.sortField) });
}
if (event.filters) {
for (var _b = 0, _c = Object.keys(event.filters); _b < _c.length; _b++) {
var filterKey = _c[_b];
var attributePath = toQueryPath(filterKey);
var filterMeta = event.filters[filterKey];
var matchMode = filterMeta.matchMode;
var value = filterMeta.value;
if (!matchMode) {
matchMode = this.defaultMatchMode;
}
var mappedValue = this.filterValueMapper.mapFilterValue(attributePath, matchMode, value);
filters.push({ value: mappedValue, path: attributePath, operator: matchMode });
}
}
this.context.applyQueryParams({
limit: limit,
offset: offset,
include: includes,
sorting: sorting,
filtering: filters
});
};
return DataTablePrimengAdapter;
}());
export { DataTablePrimengAdapter };
//# sourceMappingURL=crnk.binding.table.js.map