salix/front/core/components/pagination/pagination.js

104 lines
2.9 KiB
JavaScript
Raw Normal View History

import ngModule from '../../module';
import Component from '../../lib/component';
import './style.scss';
/**
* Pagination component that automatically loads more rows when
* the user scrolls down an element.
*
2018-10-23 10:14:47 +00:00
* @property {Paginable} model The model used for pagination
* @property {String} scrollSelector The the scrollable element selector
* @property {HTMLElement} scrollElement The scrollable element
* @property {Number} scrollOffset The distance, in pixels, until the end that activates the loading of the next rows
2018-09-13 13:19:50 +00:00
* @property {Number} maxLoads The maximum of loads that are automatically performed on scroll, 0 for no limit
*/
class Pagination extends Component {
constructor($element, $scope) {
super($element, $scope);
2018-09-13 13:19:50 +00:00
this.scrollOffset = 500;
this.maxLoads = 5;
this.nLoads = 0;
this.scrollHandler = e => this.onScroll(e);
}
$onInit() {
if (!this._scrollElement)
2019-01-20 17:48:03 +00:00
this.scrollElement = window;
}
set scrollSelector(value) {
this._scrollSelector = value;
this.scrollElement = document.querySelector(value);
}
get scrollSelector() {
return this._scrollSelector;
}
set scrollElement(value) {
if (this._scrollElement)
this._scrollElement.removeEventListener('scroll', this.scrollHandler);
this._scrollElement = value;
if (value)
this._scrollElement.addEventListener('scroll', this.scrollHandler);
}
get scrollElement() {
return this._scrollElement;
}
onScroll() {
2019-01-20 17:48:03 +00:00
let scrollInfo;
let scrollElement = this.scrollElement;
2019-01-20 17:48:03 +00:00
if (scrollElement == window) {
scrollInfo = {
top: window.pageYOffset,
height: window.innerHeight,
position: window.document.body.scrollHeight
};
} else {
scrollInfo = {
top: scrollElement.scrollTop,
height: scrollElement.clientHeight,
position: scrollElement.scrollHeight
};
}
let shouldLoad = scrollInfo.top + scrollInfo.height >= (scrollInfo.position - this.scrollOffset)
2018-09-13 13:19:50 +00:00
&& !this.model.isLoading
&& (this.maxLoads <= 0 || this.nLoads < this.maxLoads);
if (shouldLoad) {
2018-09-13 13:19:50 +00:00
this.nLoads++;
this.model.loadMore(false);
this.$.$apply();
}
}
2018-09-13 13:19:50 +00:00
onLoadClick() {
if (this.maxLoads > 0 && this.nLoads == this.maxLoads)
this.nLoads = 0;
this.model.loadMore(false);
2018-09-13 13:19:50 +00:00
}
$onDestroy() {
this.scrollElement = null;
}
}
2020-07-24 15:46:06 +00:00
ngModule.vnComponent('vnPagination', {
template: require('./pagination.html'),
bindings: {
model: '<',
scrollSelector: '@?',
scrollElement: '<?',
2018-09-13 13:19:50 +00:00
scrollOffset: '<?',
maxLoads: '<?'
},
controller: Pagination
});