From e71e18148949985ce2327c9488b64da79a76b679 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Fri, 25 Jan 2019 09:40:28 +0100 Subject: [PATCH] MOBILE-2832 core: Make infinite scroll load more automatically --- .../infinite-loading/infinite-loading.ts | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/components/infinite-loading/infinite-loading.ts b/src/components/infinite-loading/infinite-loading.ts index 6b72837f0..a3f1f89df 100644 --- a/src/components/infinite-loading/infinite-loading.ts +++ b/src/components/infinite-loading/infinite-loading.ts @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Component, Input, Output, EventEmitter } from '@angular/core'; -import { InfiniteScroll } from 'ionic-angular'; +import { Component, Input, Output, EventEmitter, OnChanges, SimpleChange, Optional } from '@angular/core'; +import { InfiniteScroll, Content } from 'ionic-angular'; /** * Component to show a infinite loading trigger and spinner while more data is being loaded. @@ -25,7 +25,7 @@ import { InfiniteScroll } from 'ionic-angular'; selector: 'core-infinite-loading', templateUrl: 'core-infinite-loading.html', }) -export class CoreInfiniteLoadingComponent { +export class CoreInfiniteLoadingComponent implements OnChanges { @Input() enabled: boolean; @Input() error = false; @Input() position = 'bottom'; @@ -35,10 +35,26 @@ export class CoreInfiniteLoadingComponent { protected infiniteScroll: InfiniteScroll; - constructor() { + constructor(@Optional() private content: Content) { this.action = new EventEmitter(); } + /** + * Detect changes on input properties. + * + * @param {SimpleChange}} changes Changes. + */ + ngOnChanges(changes: {[name: string]: SimpleChange}): void { + if (changes.enabled && this.enabled) { + // Infinite scroll enabled. If the list doesn't fill the full height, infinite scroll isn't triggered automatically. + // Send a fake scroll event to make infinite scroll check if it should load more items. + setTimeout(() => { + const event: any = new Event('scroll'); + this.content.ionScroll.emit(event); + }); + } + } + /** * Load More items calling the action provided. * @@ -64,6 +80,13 @@ export class CoreInfiniteLoadingComponent { this.loadingMore = false; this.infiniteScroll && this.infiniteScroll.complete(); this.infiniteScroll = undefined; + + // More items loaded. If the list doesn't fill the full height, infinite scroll isn't triggered automatically. + // Send a fake scroll event to make infinite scroll check if it should load more items. + setTimeout(() => { + const event: any = new Event('scroll'); + this.content.ionScroll.emit(event); + }); } }