yalento
Version:
An awesome integration of Google Firebase for Angular and Node
285 lines (284 loc) • 7.9 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryPaginator = void 0;
const rxjs_1 = require("rxjs");
const operators_1 = require("rxjs/operators");
class QueryPaginator {
/**
*
* @param querySubject
*/
constructor(querySubject) {
this.results = [];
this.results$ = new rxjs_1.BehaviorSubject([]);
this.length = 0;
this.resultsAll = [];
this.pageSize = 0;
this.pageIndex = 0;
this.pageSizeOptions = [];
this.pageSort = { direction: 'ASC', active: '' };
this._hasPageSizeChanges = false;
this._selected = {};
this._isSelectedAll$ = new rxjs_1.BehaviorSubject(false);
this._isSelectedCount$ = new rxjs_1.BehaviorSubject(0);
this._querySubject = querySubject;
querySubject.getQueryCallbackChanges().subscribe((changes) => {
if (changes.count !== undefined) {
this.setLength(changes.count);
}
if (changes.results !== undefined && changes.resultsAll !== undefined) {
const isSelectedAll = this.length === this.getSelectedCount().getValue() && this.getSelectedCount().getValue() > 0;
this._isSelectedAll$.next(isSelectedAll);
this.setResults(changes.results);
this.resultsAll = changes.resultsAll;
this._isSelectedCount$.next(this.countSelected());
}
});
}
/**
*
*/
getSelected() {
const selected = [];
if (this.isSelectedAll().getValue()) {
return this.resultsAll;
}
this.results.forEach((r) => {
// @ts-ignore
if (this._selected[r['__uuid']]) {
selected.push(r);
// @ts-ignore
r['_selected'] = true;
}
else {
// @ts-ignore
r['_selected'] = true;
}
});
return selected;
}
/**
* get selected count
*/
getSelectedCount() {
return this._isSelectedCount$;
}
/**
*
* @param item
*/
toggleSelection(item) {
if (item) {
// @ts-ignore
const uuid = item['__uuid'];
this._selected[uuid] = !this._selected[uuid];
this._isSelectedAll$.next(false);
this._isSelectedCount$.next(this.countSelected());
const isSelectedAll = this.length === this.getSelectedCount().getValue();
this._isSelectedAll$.next(isSelectedAll);
}
else {
this._isSelectedCount$.next(this.countSelected());
if (this._isSelectedCount$.getValue() > 0 && this.isSelectedAll().getValue()) {
Object.keys(this._selected).forEach((key) => {
this._selected[key] = false;
});
this._isSelectedAll$.next(false);
}
else {
this.resultsAll.forEach((result) => {
this._selected[result['__uuid']] = true;
});
this._isSelectedAll$.next(true);
}
}
this._isSelectedCount$.next(this.countSelected());
}
/**
*
*/
isSelected(item) {
return this._selected[item['__uuid']] === true;
}
/**
*
*/
isSelectedAll() {
return this._isSelectedAll$;
}
/**
*
* @param pageEvent
*/
setPage(pageEvent) {
return new Promise((resolve) => {
const changes = {};
if (pageEvent.pageIndex !== undefined && pageEvent.pageIndex !== this.getPageIndex()) {
this.pageIndex = pageEvent.pageIndex;
changes.pageIndex = pageEvent.pageIndex;
}
if (pageEvent.pageSize !== undefined && pageEvent.pageSize !== this.getPageSize()) {
this.pageSize = pageEvent.pageSize;
this._hasPageSizeChanges = true;
changes.pageSize = pageEvent.pageSize;
}
if (Object.keys(changes).length > 0) {
this._querySubject.updateQueryCallbackChanges(changes);
this._querySubject
.getQueryCallbackChanges()
.pipe(operators_1.takeWhile((c) => {
return c.results === undefined;
}))
.toPromise()
.then(() => {
resolve(true);
});
}
else {
resolve(true);
}
});
}
/**
*
* @param pageIndex
*/
setPageIndex(pageIndex) {
this.pageIndex = pageIndex;
this._querySubject.updateQueryCallbackChanges({ pageIndex: pageIndex });
}
/**
*
* @param pageSort
*/
setPageSort(pageSort) {
this.pageSort = pageSort;
this._querySubject.updateQueryCallbackChanges({ pageSort: pageSort });
}
/**
*
*/
getPageSortProperty() {
return this.pageSort.active;
}
/**
*
*/
getPageSortDirection() {
return this.pageSort.direction;
}
/**
*
* @param pageSizeOptions
*/
setPageSizeOptions(pageSizeOptions) {
this.pageSizeOptions = pageSizeOptions;
}
/**
*
* @param size
* @param skipChangeDetection
*/
setPageSize(size, skipChangeDetection) {
this.pageSize = size;
return new Promise((resolve) => {
if (skipChangeDetection !== true) {
this._hasPageSizeChanges = true;
this._querySubject.updateQueryCallbackChanges({ pageSize: size });
this._querySubject
.getQueryCallbackChanges()
.pipe(operators_1.takeWhile((changes) => {
return changes.results === undefined;
}))
.toPromise()
.then(() => {
resolve(true);
});
}
else {
resolve(true);
}
});
}
getResults() {
return this.results;
}
/**
*
*/
getPageSize() {
return this.pageSize;
}
/**
*
*/
getPageSizeOptions() {
return this.pageSizeOptions;
}
/**
*
*/
getPageIndex() {
return this.pageIndex;
}
/**
*
*/
getLength() {
return this.length;
}
/**
*
*/
hasPageSizeChanges() {
return this._hasPageSizeChanges;
}
/**
*
* @param pageSize
*/
addPageSizeOption(pageSize) {
if (this.pageSizeOptions.indexOf(pageSize) >= 0) {
return;
}
this.pageSizeOptions.push(pageSize);
this.pageSizeOptions.sort((a, b) => a - b);
}
/**
*
* @param length
*/
setLength(length) {
this.length = length;
}
/**
*
* @param results
*/
setResults(results) {
const selectedAll = this.isSelectedAll().getValue();
results.forEach((result) => {
if (result && this._selected[result['__uuid']] === undefined) {
this._selected[result['__uuid']] = selectedAll;
}
});
this.results = results;
this.results$.next(this.results);
}
countSelected() {
let count = 0;
if (this.isSelectedAll().getValue()) {
return this.resultsAll.length;
}
else {
this.results.forEach((r) => {
// @ts-ignore
if (r && this._selected[r['__uuid']]) {
count++;
}
});
}
return count;
}
}
exports.QueryPaginator = QueryPaginator;