@@ -21,7 +21,9 @@ angular.module('mm.addons.mod_wiki')
2121 * @ngdoc service
2222 * @name $mmaModWikiHandlers
2323 */
24- . factory ( '$mmaModWikiHandlers' , function ( $mmCourse , $mmaModWiki , $state , $mmContentLinksHelper , $mmCourseHelper , $mmUtil , $q ) {
24+ . factory ( '$mmaModWikiHandlers' , function ( $mmCourse , $mmaModWiki , $state , $mmContentLinksHelper , $mmCourseHelper , $mmUtil , $q ,
25+ mmaModWikiComponent , $mmaModWikiPrefetchHandler , mmCoreDownloading , mmCoreNotDownloaded , mmCoreEventPackageStatusChanged ,
26+ mmCoreOutdated , $mmCoursePrefetchDelegate , $mmSite , $mmEvents ) {
2527 var self = { } ;
2628
2729 /**
@@ -53,11 +55,94 @@ angular.module('mm.addons.mod_wiki')
5355 */
5456 self . getController = function ( module , courseId ) {
5557 return function ( $scope ) {
58+ var downloadBtn = {
59+ hidden : true ,
60+ icon : 'ion-ios-cloud-download-outline' ,
61+ label : 'mm.core.download' ,
62+ action : function ( e ) {
63+ if ( e ) {
64+ e . preventDefault ( ) ;
65+ e . stopPropagation ( ) ;
66+ }
67+ download ( ) ;
68+ }
69+ } ,
70+ refreshBtn = {
71+ hidden : true ,
72+ icon : 'ion-android-refresh' ,
73+ label : 'mm.core.refresh' ,
74+ action : function ( e ) {
75+ if ( e ) {
76+ e . preventDefault ( ) ;
77+ e . stopPropagation ( ) ;
78+ }
79+ $mmaModWiki . invalidateContent ( module . id , courseId ) . finally ( function ( ) {
80+ download ( ) ;
81+ } ) ;
82+ }
83+ } ;
84+
5685 $scope . title = module . name ;
5786 $scope . icon = $mmCourse . getModuleIconSrc ( 'wiki' ) ;
87+ $scope . buttons = [ downloadBtn , refreshBtn ] ;
88+ $scope . spinner = false ;
89+
5890 $scope . action = function ( e ) {
91+ if ( e ) {
92+ e . preventDefault ( ) ;
93+ e . stopPropagation ( ) ;
94+ }
5995 $state . go ( 'site.mod_wiki' , { module : module , moduleid : module . id , courseid : courseId } ) ;
6096 } ;
97+
98+ function download ( ) {
99+
100+ $scope . spinner = true ; // Show spinner since this operation might take a while.
101+ // We need to call getDownloadSize, the package might have been updated.
102+ $mmaModWikiPrefetchHandler . getDownloadSize ( module , courseId ) . then ( function ( size ) {
103+ $mmUtil . confirmDownloadSize ( size ) . then ( function ( ) {
104+ $mmaModWikiPrefetchHandler . prefetch ( module , courseId ) . catch ( function ( ) {
105+ if ( ! $scope . $$destroyed ) {
106+ $mmUtil . showErrorModal ( 'mm.core.errordownloading' , true ) ;
107+ }
108+ } ) ;
109+ } ) . catch ( function ( ) {
110+ // User hasn't confirmed, stop spinner.
111+ $scope . spinner = false ;
112+ } ) ;
113+ } ) . catch ( function ( error ) {
114+ $scope . spinner = false ;
115+ if ( error ) {
116+ $mmUtil . showErrorModal ( error ) ;
117+ } else {
118+ $mmUtil . showErrorModal ( 'mm.core.errordownloading' , true ) ;
119+ }
120+ } ) ;
121+ }
122+
123+ // Show buttons according to module status.
124+ function showStatus ( status ) {
125+ if ( status ) {
126+ $scope . spinner = status === mmCoreDownloading ;
127+ downloadBtn . hidden = status !== mmCoreNotDownloaded ;
128+ refreshBtn . hidden = status !== mmCoreOutdated ;
129+ }
130+ }
131+
132+ // Listen for changes on this module status.
133+ var statusObserver = $mmEvents . on ( mmCoreEventPackageStatusChanged , function ( data ) {
134+ if ( data . siteid === $mmSite . getId ( ) && data . componentId === module . id &&
135+ data . component === mmaModWikiComponent ) {
136+ showStatus ( data . status ) ;
137+ }
138+ } ) ;
139+
140+ // Get current status to decide which icon should be shown.
141+ $mmCoursePrefetchDelegate . getModuleStatus ( module , courseId ) . then ( showStatus ) ;
142+
143+ $scope . $on ( '$destroy' , function ( ) {
144+ statusObserver && statusObserver . off && statusObserver . off ( ) ;
145+ } ) ;
61146 } ;
62147 } ;
63148
0 commit comments