2018-01-02 08:47:21 +01:00
|
|
|
// (C) Copyright 2015 Martin Dougiamas
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-03-02 15:25:00 +01:00
|
|
|
import { Component, Input, Output, EventEmitter, OnInit, OnDestroy, Optional } from '@angular/core';
|
2018-01-02 15:30:50 +01:00
|
|
|
import { NavController } from 'ionic-angular';
|
2018-03-01 16:55:49 +01:00
|
|
|
import { CoreEventsProvider } from '@providers/events';
|
|
|
|
import { CoreSitesProvider } from '@providers/sites';
|
|
|
|
import { CoreDomUtilsProvider } from '@providers/utils/dom';
|
2018-02-06 08:43:46 +01:00
|
|
|
import { CoreCourseHelperProvider } from '../../providers/helper';
|
2019-02-26 15:06:33 +01:00
|
|
|
import { CoreCourseProvider } from '../../providers/course';
|
2018-01-02 15:30:50 +01:00
|
|
|
import { CoreCourseModuleHandlerButton } from '../../providers/module-delegate';
|
2018-02-06 08:43:46 +01:00
|
|
|
import { CoreCourseModulePrefetchDelegate, CoreCourseModulePrefetchHandler } from '../../providers/module-prefetch-delegate';
|
|
|
|
import { CoreConstants } from '../../../constants';
|
2018-01-02 08:47:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to display a module entry in a list of modules.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
*
|
|
|
|
* <core-course-module [module]="module" [courseId]="courseId" (completionChanged)="onCompletionChange()"></core-course-module>
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'core-course-module',
|
2018-06-12 08:41:17 +02:00
|
|
|
templateUrl: 'core-course-module.html'
|
2018-01-02 08:47:21 +01:00
|
|
|
})
|
2018-02-06 08:43:46 +01:00
|
|
|
export class CoreCourseModuleComponent implements OnInit, OnDestroy {
|
2018-01-02 08:47:21 +01:00
|
|
|
@Input() module: any; // The module to render.
|
|
|
|
@Input() courseId: number; // The course the module belongs to.
|
2018-10-08 13:18:40 +02:00
|
|
|
@Input() section: any; // The section the module belongs to.
|
2018-02-06 08:43:46 +01:00
|
|
|
@Input('downloadEnabled') set enabled(value: boolean) {
|
|
|
|
this.downloadEnabled = value;
|
|
|
|
|
2018-05-11 09:21:08 +02:00
|
|
|
if (this.module.handlerData.showDownloadButton && this.downloadEnabled && !this.statusCalculated) {
|
2018-02-06 08:43:46 +01:00
|
|
|
// First time that the download is enabled. Initialize the data.
|
2018-05-11 09:21:08 +02:00
|
|
|
this.statusCalculated = true;
|
2018-02-06 08:43:46 +01:00
|
|
|
this.spinner = true; // Show spinner while calculating the status.
|
|
|
|
|
|
|
|
// Get current status to decide which icon should be shown.
|
|
|
|
this.prefetchDelegate.getModuleStatus(this.module, this.courseId).then(this.showStatus.bind(this));
|
|
|
|
}
|
|
|
|
}
|
2019-02-06 11:04:21 +00:00
|
|
|
@Output() completionChanged?: EventEmitter<any>; // Will emit an event when the module completion changes.
|
2018-01-02 08:47:21 +01:00
|
|
|
|
2018-02-06 08:43:46 +01:00
|
|
|
showDownload: boolean; // Whether to display the download button.
|
|
|
|
showRefresh: boolean; // Whether to display the refresh button.
|
|
|
|
spinner: boolean; // Whether to display a spinner.
|
|
|
|
downloadEnabled: boolean; // Whether the download of sections and modules is enabled.
|
|
|
|
|
|
|
|
protected prefetchHandler: CoreCourseModulePrefetchHandler;
|
|
|
|
protected statusObserver;
|
2018-05-11 09:21:08 +02:00
|
|
|
protected statusCalculated = false;
|
2018-04-05 16:17:03 +02:00
|
|
|
protected isDestroyed = false;
|
2018-02-06 08:43:46 +01:00
|
|
|
|
2018-03-02 15:25:00 +01:00
|
|
|
constructor(@Optional() protected navCtrl: NavController, protected prefetchDelegate: CoreCourseModulePrefetchDelegate,
|
2018-02-06 08:43:46 +01:00
|
|
|
protected domUtils: CoreDomUtilsProvider, protected courseHelper: CoreCourseHelperProvider,
|
2019-02-26 15:06:33 +01:00
|
|
|
protected eventsProvider: CoreEventsProvider, protected sitesProvider: CoreSitesProvider,
|
|
|
|
protected courseProvider: CoreCourseProvider) {
|
2018-01-02 08:47:21 +01:00
|
|
|
this.completionChanged = new EventEmitter();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component being initialized.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
ngOnInit(): void {
|
2018-01-02 08:47:21 +01:00
|
|
|
// Handler data must be defined. If it isn't, set it to prevent errors.
|
|
|
|
if (this.module && !this.module.handlerData) {
|
|
|
|
this.module.handlerData = {};
|
|
|
|
}
|
2018-05-11 09:21:08 +02:00
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
2019-01-02 09:38:47 +01:00
|
|
|
|
|
|
|
this.module.handlerData.a11yTitle = typeof this.module.handlerData.a11yTitle != 'undefined' ?
|
|
|
|
this.module.handlerData.a11yTitle : this.module.handlerData.title;
|
2019-02-26 15:06:33 +01:00
|
|
|
|
|
|
|
this.module.modnametranslated = this.courseProvider.translateModuleName(this.module.modname) || '';
|
2018-01-02 08:47:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function called when the module is clicked.
|
|
|
|
*
|
|
|
|
* @param {Event} event Click event.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
moduleClicked(event: Event): void {
|
2018-01-02 08:47:21 +01:00
|
|
|
if (this.module.uservisible !== false && this.module.handlerData.action) {
|
2018-01-02 15:30:50 +01:00
|
|
|
this.module.handlerData.action(event, this.navCtrl, this.module, this.courseId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function called when a button is clicked.
|
|
|
|
*
|
|
|
|
* @param {Event} event Click event.
|
|
|
|
* @param {CoreCourseModuleHandlerButton} button The clicked button.
|
|
|
|
*/
|
2018-01-29 10:05:20 +01:00
|
|
|
buttonClicked(event: Event, button: CoreCourseModuleHandlerButton): void {
|
2018-01-02 15:30:50 +01:00
|
|
|
if (button && button.action) {
|
2018-03-05 15:19:39 +01:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
2018-01-02 15:30:50 +01:00
|
|
|
button.action(event, this.navCtrl, this.module, this.courseId);
|
2018-01-02 08:47:21 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-06 08:43:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Download the module.
|
|
|
|
*
|
|
|
|
* @param {Event} event Click event.
|
|
|
|
* @param {boolean} refresh Whether it's refreshing.
|
|
|
|
*/
|
|
|
|
download(event: Event, refresh: boolean): void {
|
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
|
|
|
|
if (!this.prefetchHandler) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show spinner since this operation might take a while.
|
|
|
|
this.spinner = true;
|
|
|
|
|
|
|
|
// Get download size to ask for confirm if it's high.
|
2018-05-07 08:34:54 +02:00
|
|
|
this.prefetchHandler.getDownloadSize(this.module, this.courseId, true).then((size) => {
|
2018-04-05 16:17:03 +02:00
|
|
|
return this.courseHelper.prefetchModule(this.prefetchHandler, this.module, size, this.courseId, refresh);
|
2018-02-06 08:43:46 +01:00
|
|
|
}).catch((error) => {
|
2018-04-05 16:17:03 +02:00
|
|
|
// Error, hide spinner.
|
2018-02-06 08:43:46 +01:00
|
|
|
this.spinner = false;
|
2018-04-19 13:28:22 +02:00
|
|
|
if (!this.isDestroyed) {
|
2018-04-05 16:17:03 +02:00
|
|
|
this.domUtils.showErrorModalDefault(error, 'core.errordownloading', true);
|
|
|
|
}
|
2018-02-06 08:43:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show download buttons according to module status.
|
|
|
|
*
|
|
|
|
* @param {string} status Module status.
|
|
|
|
*/
|
|
|
|
protected showStatus(status: string): void {
|
|
|
|
if (status) {
|
|
|
|
this.spinner = status === CoreConstants.DOWNLOADING;
|
|
|
|
this.showDownload = status === CoreConstants.NOT_DOWNLOADED;
|
|
|
|
this.showRefresh = status === CoreConstants.OUTDATED ||
|
|
|
|
(!this.prefetchDelegate.canCheckUpdates() && status === CoreConstants.DOWNLOADED);
|
2018-04-09 13:14:55 +02:00
|
|
|
|
|
|
|
if (this.module.handlerData.updateStatus) {
|
|
|
|
this.module.handlerData.updateStatus(status);
|
|
|
|
}
|
2018-02-06 08:43:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component destroyed.
|
|
|
|
*/
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
this.statusObserver && this.statusObserver.off();
|
2018-08-16 12:20:23 +02:00
|
|
|
this.module && this.module.handlerData && this.module.handlerData.onDestroy && this.module.handlerData.onDestroy();
|
2018-04-05 16:17:03 +02:00
|
|
|
this.isDestroyed = true;
|
2018-02-06 08:43:46 +01:00
|
|
|
}
|
2018-01-02 08:47:21 +01:00
|
|
|
}
|