2018-06-07 21:47:19 +00:00
|
|
|
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
|
2018-06-07 21:47:19 +00:00
|
|
|
* @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
|
2018-06-07 21:47:19 +00:00
|
|
|
*/
|
|
|
|
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;
|
2018-06-07 21:47:19 +00:00
|
|
|
this.scrollHandler = e => this.onScroll(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
$onInit() {
|
|
|
|
if (!this._scrollElement)
|
2019-01-20 17:48:03 +00:00
|
|
|
this.scrollElement = window;
|
2018-06-07 21:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2018-06-07 21:47:19 +00:00
|
|
|
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);
|
2018-06-07 21:47:19 +00:00
|
|
|
|
|
|
|
if (shouldLoad) {
|
2018-09-13 13:19:50 +00:00
|
|
|
this.nLoads++;
|
2022-12-20 08:48:41 +00:00
|
|
|
this.model.loadMore(false);
|
2018-06-07 21:47:19 +00:00
|
|
|
this.$.$apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-13 13:19:50 +00:00
|
|
|
onLoadClick() {
|
|
|
|
if (this.maxLoads > 0 && this.nLoads == this.maxLoads)
|
|
|
|
this.nLoads = 0;
|
|
|
|
|
2022-12-20 08:48:41 +00:00
|
|
|
this.model.loadMore(false);
|
2018-09-13 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
2018-06-07 21:47:19 +00:00
|
|
|
$onDestroy() {
|
|
|
|
this.scrollElement = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 15:46:06 +00:00
|
|
|
ngModule.vnComponent('vnPagination', {
|
2018-06-07 21:47:19 +00:00
|
|
|
template: require('./pagination.html'),
|
|
|
|
bindings: {
|
|
|
|
model: '<',
|
|
|
|
scrollSelector: '@?',
|
|
|
|
scrollElement: '<?',
|
2018-09-13 13:19:50 +00:00
|
|
|
scrollOffset: '<?',
|
|
|
|
maxLoads: '<?'
|
2018-06-07 21:47:19 +00:00
|
|
|
},
|
|
|
|
controller: Pagination
|
|
|
|
});
|