Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 6 additions & 15 deletions www/addons/mod_book/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ angular.module('mm.addons.mod_book')
* @ngdoc service
* @name $mmaModBookHandlers
*/
.factory('$mmaModBookHandlers', function($mmCourse, $mmaModBook, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
.factory('$mmaModBookHandlers', function($mmCourse, $mmaModBook, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreDownloaded,
mmCoreEventPackageStatusChanged, mmaModBookComponent, $mmContentLinksHelper, $q) {
mmCoreEventPackageStatusChanged, mmaModBookComponent, $mmContentLinksHelper, $q, $mmaModBookPrefetchHandler) {

var self = {};

Expand Down Expand Up @@ -67,11 +67,8 @@ angular.module('mm.addons.mod_book')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModBook.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
var size = $mmaModBookPrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModBook, module, size, false);
}
};

Expand All @@ -82,14 +79,8 @@ angular.module('mm.addons.mod_book')
action: function(e) {
e.preventDefault();
e.stopPropagation();

$mmaModBook.invalidateContent(module.id).finally(function() {
$mmaModBook.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
});
var size = $mmaModBookPrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModBook, module, size, true);
}
};

Expand Down
13 changes: 6 additions & 7 deletions www/addons/mod_folder/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ angular.module('mm.addons.mod_folder')
* @ngdoc service
* @name $mmaModFolderHandlers
*/
.factory('$mmaModFolderHandlers', function($mmCourse, $mmaModFolder, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
.factory('$mmaModFolderHandlers', function($mmCourse, $mmaModFolder, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
mmaModFolderComponent, $mmContentLinksHelper, $q) {
mmaModFolderComponent, $mmContentLinksHelper, $q, $mmaModFolderPrefetchHandler) {
var self = {};

/**
Expand Down Expand Up @@ -70,11 +70,10 @@ angular.module('mm.addons.mod_folder')
function prefetchFolder(e) {
e.preventDefault();
e.stopPropagation();
$mmaModFolder.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});

// Check size and show confirmation if needed.
var size = $mmaModFolderPrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModFolder, module, size, false);
}

downloadBtn = {
Expand Down
20 changes: 6 additions & 14 deletions www/addons/mod_imscp/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ angular.module('mm.addons.mod_imscp')
* @ngdoc service
* @name $mmaModImscpHandlers
*/
.factory('$mmaModImscpHandlers', function($mmCourse, $mmaModImscp, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
.factory('$mmaModImscpHandlers', function($mmCourse, $mmaModImscp, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
mmaModImscpComponent, $mmContentLinksHelper, $q) {
mmaModImscpComponent, $mmContentLinksHelper, $q, $mmaModImscpPrefetchHandler) {
var self = {};

/**
Expand Down Expand Up @@ -73,11 +73,8 @@ angular.module('mm.addons.mod_imscp')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModImscp.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
var size = $mmaModImscpPrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModImscp, module, size, false);
}
};

Expand All @@ -88,13 +85,8 @@ angular.module('mm.addons.mod_imscp')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModImscp.invalidateContent(module.id).then(function() {
$mmaModImscp.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
});
var size = $mmaModImscpPrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModImscp, module, size, true);
}
};

Expand Down
20 changes: 6 additions & 14 deletions www/addons/mod_page/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ angular.module('mm.addons.mod_page')
* @ngdoc service
* @name $mmaModPageHandlers
*/
.factory('$mmaModPageHandlers', function($mmCourse, $mmaModPage, $mmEvents, $state, $mmSite, $mmUtil, $mmFilepool,
.factory('$mmaModPageHandlers', function($mmCourse, $mmaModPage, $mmEvents, $state, $mmSite, $mmCourseHelper, $mmFilepool,
$mmCoursePrefetchDelegate, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated, mmCoreEventPackageStatusChanged,
mmaModPageComponent, $mmContentLinksHelper, $q) {
mmaModPageComponent, $mmContentLinksHelper, $q, $mmaModPagePrefetchHandler) {
var self = {};

/**
Expand Down Expand Up @@ -67,11 +67,8 @@ angular.module('mm.addons.mod_page')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModPage.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
var size = $mmaModPagePrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModPage, module, size, false);
}
};

Expand All @@ -82,13 +79,8 @@ angular.module('mm.addons.mod_page')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModPage.invalidateContent(module.id).finally(function() {
$mmaModPage.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
});
var size = $mmaModPagePrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModPage, module, size, true);
}
};

Expand Down
20 changes: 6 additions & 14 deletions www/addons/mod_resource/services/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ angular.module('mm.addons.mod_resource')
* @ngdoc service
* @name $mmaModResourceHandlers
*/
.factory('$mmaModResourceHandlers', function($mmCourse, $mmaModResource, $mmEvents, $state, $mmSite, $mmUtil,
.factory('$mmaModResourceHandlers', function($mmCourse, $mmaModResource, $mmEvents, $state, $mmSite, $mmCourseHelper,
$mmCoursePrefetchDelegate, $mmFilepool, $mmFS, mmCoreDownloading, mmCoreNotDownloaded, mmCoreOutdated,
mmCoreEventPackageStatusChanged, mmaModResourceComponent, $q, $mmContentLinksHelper) {
mmCoreEventPackageStatusChanged, mmaModResourceComponent, $q, $mmContentLinksHelper, $mmaModResourcePrefetchHandler) {
var self = {};

/**
Expand Down Expand Up @@ -67,11 +67,8 @@ angular.module('mm.addons.mod_resource')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModResource.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
var size = $mmaModResourcePrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModResource, module, size, false);
}
};

Expand All @@ -82,13 +79,8 @@ angular.module('mm.addons.mod_resource')
action: function(e) {
e.preventDefault();
e.stopPropagation();
$mmaModResource.invalidateContent(module.id).finally(function() {
$mmaModResource.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
});
var size = $mmaModResourcePrefetchHandler.getDownloadSize(module);
$mmCourseHelper.prefetchModule($mmaModResource, module, size, true);
}
};

Expand Down
30 changes: 30 additions & 0 deletions www/core/components/course/services/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,36 @@ angular.module('mm.core.course')
}
};

/**
* Helper function to prefetch a module, showing a confirmation modal if the size is big
* and invalidating contents if refreshing.
*
* @module mm.core.course
* @ngdoc method
* @name $mmCourseHelper#prefetchModule
* @param {Object} service Service implementing 'invalidateContent' and 'prefetchContent'.
* @param {Object} module Module to download.
* @param {Number} size Size of the module.
* @param {Boolean} refresh True if refreshing, false otherwise.
* @return {Promise} Promise resolved when downloaded.
*/
self.prefetchModule = function(service, module, size, refresh) {
// Show confirmation if needed.
return $mmUtil.confirmDownloadSize(size).then(function() {
// Invalidate content if refreshing and download the data.
var promise = refresh ? service.invalidateContent(module.id) : $q.when();
return promise.catch(function() {
// Ignore errors.
}).then(function() {
return service.prefetchContent(module).catch(function() {
if (!$scope.$$destroyed) {
$mmUtil.showErrorModal('mm.core.errordownloading', true);
}
});
});
});
};

/**
* Prefetch or restore the prefetch of a certain section if it needs to be prefetched.
* If the section is "All sections" it will be ignored.
Expand Down
17 changes: 12 additions & 5 deletions www/core/directives/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ angular.module('mm.core')
* @param {Object} file Required. Object with the following attributes:
* 'filename': Name of the file.
* 'fileurl' or 'url': File URL.
* 'filesize': Optional. Size of the file.
* @param {String} [component] Component the file belongs to.
* @param {Number} [componentId] Component ID.
* @param {Boolean} [timemodified] If set, the value will be used to check if the file is outdated.
Expand Down Expand Up @@ -94,6 +95,7 @@ angular.module('mm.core')
link: function(scope, element, attrs) {
var fileurl = scope.file.fileurl || scope.file.url,
filename = scope.file.filename,
filesize = scope.file.filesize,
timemodified = attrs.timemodified || 0,
siteid = $mmSite.getId(),
component = attrs.component,
Expand All @@ -116,6 +118,7 @@ angular.module('mm.core')
scope.download = function(e, openAfterDownload) {
e.preventDefault();
e.stopPropagation();
var promise;

if (scope.isDownloading) {
return;
Expand All @@ -134,13 +137,17 @@ angular.module('mm.core')
});
});
} else {
// File doesn't need to be opened, add it to queue.
$mmFilepool.invalidateFileByUrl(siteid, fileurl).finally(function() {
scope.isDownloading = true;
$mmFilepool.addToQueueByUrl(siteid, fileurl, component, componentid, timemodified);
// File doesn't need to be opened (it's a prefetch). Show confirm modal if file size is defined and it's big.
promise = filesize ? $mmUtil.confirmDownloadSize(filesize) : $q.when();
promise.then(function() {
// User confirmed, add the file to queue.
$mmFilepool.invalidateFileByUrl(siteid, fileurl).finally(function() {
scope.isDownloading = true;
$mmFilepool.addToQueueByUrl(siteid, fileurl, component, componentid, timemodified);
});
});
}
}
};

scope.$on('$destroy', function() {
if (observer && observer.off) {
Expand Down