diff --git a/src/addon/mod/resource/components/index/index.ts b/src/addon/mod/resource/components/index/index.ts index 1e8f6a2da..68a388a82 100644 --- a/src/addon/mod/resource/components/index/index.ts +++ b/src/addon/mod/resource/components/index/index.ts @@ -124,7 +124,7 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource } else if (this.resourceHelper.isDisplayedEmbedded(this.module, resource && resource.display)) { this.mode = 'embedded'; - return this.resourceHelper.getEmbeddedHtml(this.module).then((html) => { + return this.resourceHelper.getEmbeddedHtml(this.module, this.courseId).then((html) => { this.contentText = html; }); } else { diff --git a/src/addon/mod/resource/providers/helper.ts b/src/addon/mod/resource/providers/helper.ts index 3eb43e727..d51eba5a2 100644 --- a/src/addon/mod/resource/providers/helper.ts +++ b/src/addon/mod/resource/providers/helper.ts @@ -47,10 +47,11 @@ export class AddonModResourceHelperProvider { * Get the HTML to display an embedded resource. * * @param {any} module The module object. - * @return {Promise} Promise resolved with the iframe src. + * @param {number} courseId The course ID. + * @return {Promise} Promise resolved with the HTML. */ - getEmbeddedHtml(module: any): Promise { - return this.courseHelper.downloadModuleWithMainFileIfNeeded(module, module.course, AddonModResourceProvider.COMPONENT, + getEmbeddedHtml(module: any, courseId: number): Promise { + return this.courseHelper.downloadModuleWithMainFileIfNeeded(module, courseId, AddonModResourceProvider.COMPONENT, module.id, module.contents).then((result) => { const file = module.contents[0], ext = this.mimetypeUtils.getFileExtension(file.filename), diff --git a/src/core/course/components/module/module.ts b/src/core/course/components/module/module.ts index b3140f8fc..7ab0a91b7 100644 --- a/src/core/course/components/module/module.ts +++ b/src/core/course/components/module/module.ts @@ -39,22 +39,13 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy { @Input('downloadEnabled') set enabled(value: boolean) { this.downloadEnabled = value; - if (this.module.handlerData.showDownloadButton && this.downloadEnabled && !this.statusObserver) { + if (this.module.handlerData.showDownloadButton && this.downloadEnabled && !this.statusCalculated) { // First time that the download is enabled. Initialize the data. + this.statusCalculated = true; this.spinner = true; // Show spinner while calculating the status. - this.prefetchHandler = this.prefetchDelegate.getPrefetchHandlerFor(this.module); - // Get current status to decide which icon should be shown. this.prefetchDelegate.getModuleStatus(this.module, this.courseId).then(this.showStatus.bind(this)); - - // Listen for changes on this module status. - this.statusObserver = this.eventsProvider.on(CoreEventsProvider.PACKAGE_STATUS_CHANGED, (data) => { - if (data.componentId === this.module.id && this.prefetchHandler && - data.component === this.prefetchHandler.component) { - this.showStatus(data.status); - } - }, this.sitesProvider.getCurrentSiteId()); } } @Output() completionChanged?: EventEmitter; // Will emit an event when the module completion changes. @@ -66,6 +57,7 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy { protected prefetchHandler: CoreCourseModulePrefetchHandler; protected statusObserver; + protected statusCalculated = false; protected isDestroyed = false; constructor(@Optional() protected navCtrl: NavController, protected prefetchDelegate: CoreCourseModulePrefetchDelegate, @@ -82,6 +74,25 @@ export class CoreCourseModuleComponent implements OnInit, OnDestroy { if (this.module && !this.module.handlerData) { this.module.handlerData = {}; } + + if (this.module.handlerData.showDownloadButton) { + // Listen for changes on this module status, even if download isn't enabled. + this.prefetchHandler = this.prefetchDelegate.getPrefetchHandlerFor(this.module); + + this.statusObserver = this.eventsProvider.on(CoreEventsProvider.PACKAGE_STATUS_CHANGED, (data) => { + if (data.componentId === this.module.id && this.prefetchHandler && + data.component === this.prefetchHandler.component) { + + if (this.downloadEnabled) { + // Download is enabled, show the status. + this.showStatus(data.status); + } else if (this.module.handlerData.updateStatus) { + // Download isn't enabled but the handler defines a updateStatus function, call it anyway. + this.module.handlerData.updateStatus(data.status); + } + } + }, this.sitesProvider.getCurrentSiteId()); + } } /**