Skip to content

Commit 8db9ad0

Browse files
authored
Merge pull request #2614 from dpalou/MOBILE-3598
MOBILE-3598 h5p: Fix package disappear when opening new pages
2 parents ab73472 + ea81120 commit 8db9ad0

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/core/h5p/components/h5p-iframe/h5p-iframe.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { Component, Input, Output, ElementRef, OnChanges, SimpleChange, EventEmitter } from '@angular/core';
15+
import { Component, Input, Output, ElementRef, OnChanges, SimpleChange, EventEmitter, OnDestroy } from '@angular/core';
16+
import { NavController } from 'ionic-angular';
1617
import { CoreFile } from '@providers/file';
1718
import { CoreFilepool } from '@providers/filepool';
1819
import { CoreLogger } from '@providers/logger';
@@ -26,6 +27,7 @@ import { CoreConstants } from '@core/constants';
2627
import { CoreSite } from '@classes/site';
2728
import { CoreH5PCore, CoreH5PDisplayOptions } from '../../classes/core';
2829
import { CoreH5PHelper } from '../../classes/helper';
30+
import { Subscription } from 'rxjs';
2931

3032
/**
3133
* Component to render an iframe with an H5P package.
@@ -34,7 +36,7 @@ import { CoreH5PHelper } from '../../classes/helper';
3436
selector: 'core-h5p-iframe',
3537
templateUrl: 'core-h5p-iframe.html',
3638
})
37-
export class CoreH5PIframeComponent implements OnChanges {
39+
export class CoreH5PIframeComponent implements OnChanges, OnDestroy {
3840
@Input() fileUrl?: string; // The URL of the H5P file. If not supplied, onlinePlayerUrl is required.
3941
@Input() displayOptions?: CoreH5PDisplayOptions; // Display options.
4042
@Input() onlinePlayerUrl?: string; // The URL of the online player to display the H5P package.
@@ -49,14 +51,30 @@ export class CoreH5PIframeComponent implements OnChanges {
4951
protected siteId: string;
5052
protected siteCanDownload: boolean;
5153
protected logger;
54+
protected currentPageInstance: any;
55+
protected subscription: Subscription;
56+
protected iframeLoadedOnce = false;
5257

53-
constructor(public elementRef: ElementRef,
54-
protected pluginFileDelegate: CorePluginFileDelegate) {
58+
constructor(
59+
public elementRef: ElementRef,
60+
protected pluginFileDelegate: CorePluginFileDelegate,
61+
protected navCtrl: NavController,
62+
) {
5563

5664
this.logger = CoreLogger.instance.getInstance('CoreH5PIframeComponent');
5765
this.site = CoreSites.instance.getCurrentSite();
5866
this.siteId = this.site.getId();
5967
this.siteCanDownload = this.site.canDownloadFiles() && !CoreH5P.instance.isOfflineDisabledInSite();
68+
69+
// Send resize events when the page holding this component is re-entered.
70+
this.currentPageInstance = this.navCtrl.getActive().instance;
71+
this.subscription = this.navCtrl.viewDidEnter.subscribe((viewCtrl) => {
72+
if (!this.iframeLoadedOnce || viewCtrl.instance !== this.currentPageInstance) {
73+
return;
74+
}
75+
76+
window.dispatchEvent(new Event('resize'));
77+
});
6078
}
6179

6280
/**
@@ -169,8 +187,16 @@ export class CoreH5PIframeComponent implements OnChanges {
169187
*/
170188
iframeLoaded(): void {
171189
this.onIframeLoaded.emit();
190+
this.iframeLoadedOnce = true;
172191

173192
// Send a resize event to the window so H5P package recalculates the size.
174193
window.dispatchEvent(new Event('resize'));
175194
}
195+
196+
/**
197+
* Component being destroyed.
198+
*/
199+
ngOnDestroy(): void {
200+
this.subscription.unsubscribe();
201+
}
176202
}

0 commit comments

Comments
 (0)