@@ -23,29 +23,34 @@ angular.module('mm.addons.mod_forum')
2323 */
2424. controller ( 'mmaModForumDiscussionCtrl' , function ( $q , $scope , $stateParams , $mmaModForum , $mmSite , $mmUtil , $translate , $mmEvents ,
2525 $ionicScrollDelegate , mmaModForumComponent , mmaModForumReplyDiscussionEvent , $mmaModForumOffline , $mmaModForumSync ,
26- mmaModForumAutomSyncedEvent , mmaModForumManualSyncedEvent , $mmApp , $ionicPlatform , mmCoreEventOnlineStatusChanged ) {
26+ mmaModForumAutomSyncedEvent , mmaModForumManualSyncedEvent , $mmApp , $ionicPlatform , mmCoreEventOnlineStatusChanged ,
27+ $mmaModForumHelper ) {
2728
2829 var discussionId = $stateParams . discussionid ,
29- courseid = $stateParams . cid ,
30+ courseId = $stateParams . cid ,
3031 forumId = $stateParams . forumid ,
3132 cmid = $stateParams . cmid ,
3233 scrollView ,
3334 syncObserver , syncManualObserver , onlineObserver ;
3435
36+ // Block leaving the view, we want to show a confirm to the user if there's unsaved data.
37+ $mmUtil . blockLeaveView ( $scope , leaveView ) ;
38+
3539 $scope . discussionId = discussionId ;
3640 $scope . trackPosts = $stateParams . trackposts ;
3741 $scope . component = mmaModForumComponent ;
3842 $scope . discussionStr = $translate . instant ( 'discussion' ) ;
3943 $scope . componentId = cmid ;
40- $scope . courseid = courseid ;
44+ $scope . courseId = courseId ;
4145 $scope . refreshPostsIcon = 'spinner' ;
4246 $scope . syncIcon = 'spinner' ;
43- $scope . newpost = {
47+ $scope . newPost = {
4448 replyingto : undefined ,
4549 editing : undefined ,
4650 subject : '' ,
4751 text : '' ,
48- isEditing : false
52+ isEditing : false ,
53+ files : [ ]
4954 } ;
5055 $scope . sort = {
5156 icon : 'ion-arrow-up-c' ,
@@ -60,12 +65,14 @@ angular.module('mm.addons.mod_forum')
6065 // Receive locked as param since it's returned by getDiscussions. This means that PullToRefresh won't update this value.
6166 $scope . locked = ! ! $stateParams . locked ;
6267
68+ $scope . originalData = { } ;
69+
6370 // Convenience function to get the forum.
6471 function fetchForum ( ) {
65- if ( courseid && cmid ) {
66- return $mmaModForum . getForum ( courseid , cmid ) ;
67- } else if ( courseid && forumId ) {
68- return $mmaModForum . getForumById ( courseid , forumId ) ;
72+ if ( courseId && cmid ) {
73+ return $mmaModForum . getForum ( courseId , cmid ) ;
74+ } else if ( courseId && forumId ) {
75+ return $mmaModForum . getForumById ( courseId , forumId ) ;
6976 } else {
7077 // Cannot get the forum.
7178 return $q . reject ( ) ;
@@ -95,44 +102,39 @@ angular.module('mm.addons.mod_forum')
95102
96103 } ) . then ( function ( ) {
97104 // Check if there are responses stored in offline.
98- return $mmaModForumOffline . hasDiscussionReplies ( discussionId ) . then ( function ( hasOffline ) {
99- $scope . postHasOffline = hasOffline ;
100-
101- if ( hasOffline ) {
102- return $mmaModForumOffline . getDiscussionReplies ( discussionId ) . then ( function ( replies ) {
103- var convertPromises = [ ] ;
104-
105- // Index posts to allow quick access.
106- var posts = { } ;
107- angular . forEach ( onlinePosts , function ( post ) {
108- posts [ post . id ] = post ;
109- } ) ;
110-
111- angular . forEach ( replies , function ( offlineReply ) {
112- // If we don't have forumId and courseId, get it from the post.
113- if ( ! forumId ) {
114- forumId = offlineReply . forumid ;
115- }
116- if ( ! courseid ) {
117- courseid = offlineReply . courseid ;
118- $scope . courseid = courseid ;
119- }
120-
121- convertPromises . push ( $mmaModForumOffline . convertOfflineReplyToOnline ( offlineReply )
122- . then ( function ( reply ) {
123- offlineReplies . push ( reply ) ;
124-
125- // Disable reply of the parent. Reply in offline to the same post is not allowed, edit instead.
126- posts [ reply . parent ] . canreply = false ;
127- } ) ) ;
128- } ) ;
129-
130- return $q . all ( convertPromises ) . then ( function ( ) {
131- // Convert back to array.
132- onlinePosts = Object . keys ( posts ) . map ( function ( key ) { return posts [ key ] ; } ) ;
133- } ) ;
134- } ) ;
135- }
105+ return $mmaModForumOffline . getDiscussionReplies ( discussionId ) . then ( function ( replies ) {
106+ $scope . postHasOffline = ! ! replies . length ;
107+
108+ var convertPromises = [ ] ;
109+
110+ // Index posts to allow quick access.
111+ var posts = { } ;
112+ angular . forEach ( onlinePosts , function ( post ) {
113+ posts [ post . id ] = post ;
114+ } ) ;
115+
116+ angular . forEach ( replies , function ( offlineReply ) {
117+ // If we don't have forumId and courseId, get it from the post.
118+ if ( ! forumId ) {
119+ forumId = offlineReply . forumid ;
120+ }
121+ if ( ! courseId ) {
122+ courseId = offlineReply . courseid ;
123+ $scope . courseId = courseId ;
124+ }
125+
126+ convertPromises . push ( $mmaModForumHelper . convertOfflineReplyToOnline ( offlineReply ) . then ( function ( reply ) {
127+ offlineReplies . push ( reply ) ;
128+
129+ // Disable reply of the parent. Reply in offline to the same post is not allowed, edit instead.
130+ posts [ reply . parent ] . canreply = false ;
131+ } ) ) ;
132+ } ) ;
133+
134+ return $q . all ( convertPromises ) . then ( function ( ) {
135+ // Convert back to array.
136+ onlinePosts = Object . keys ( posts ) . map ( function ( key ) { return posts [ key ] ; } ) ;
137+ } ) ;
136138 } ) ;
137139 } ) ;
138140 } ) . then ( function ( ) {
@@ -149,7 +151,7 @@ angular.module('mm.addons.mod_forum')
149151 $scope . posts = $mmaModForum . sortDiscussionPosts ( posts , $scope . sort . direction ) ;
150152 }
151153 $scope . defaultSubject = $translate . instant ( 'mma.mod_forum.re' ) + ' ' + $scope . discussion . subject ;
152- $scope . newpost . subject = $scope . defaultSubject ;
154+ $scope . newPost . subject = $scope . defaultSubject ;
153155
154156 // Now try to get the forum.
155157 return fetchForum ( ) . then ( function ( forum ) {
@@ -161,6 +163,7 @@ angular.module('mm.addons.mod_forum')
161163 forumId = forum . id ;
162164 cmid = forum . cmid ;
163165 $scope . componentId = cmid ;
166+ $scope . forum = forum ;
164167 } ) . catch ( function ( ) {
165168 // Ignore errors.
166169 } ) ;
@@ -303,6 +306,23 @@ angular.module('mm.addons.mod_forum')
303306 }
304307 } ) ;
305308
309+ // Ask to confirm if there are changes.
310+ function leaveView ( ) {
311+ var promise ;
312+
313+ if ( ! $mmaModForumHelper . hasPostDataChanged ( $scope . newPost , $scope . originalData ) ) {
314+ promise = $q . when ( ) ;
315+ } else {
316+ // Show confirmation if some data has been modified.
317+ promise = $mmUtil . showConfirm ( $translate ( 'mm.core.confirmcanceledit' ) ) ;
318+ }
319+
320+ return promise . then ( function ( ) {
321+ // Delete the local files from the tmp folder.
322+ $mmaModForumHelper . clearTmpFiles ( $scope . newPost . files ) ;
323+ } ) ;
324+ }
325+
306326 function scrollTop ( ) {
307327 if ( ! scrollView ) {
308328 scrollView = $ionicScrollDelegate . $getByHandle ( 'mmaModForumPostsScroll' ) ;
@@ -312,12 +332,6 @@ angular.module('mm.addons.mod_forum')
312332
313333 // New post added.
314334 $scope . postListChanged = function ( ) {
315- $scope . newpost . replyingto = undefined ;
316- $scope . newpost . editing = undefined ;
317- $scope . newpost . subject = $scope . defaultSubject ;
318- $scope . newpost . text = '' ;
319- $scope . newpost . isEditing = false ;
320-
321335 notifyPostListChanged ( ) ;
322336
323337 $scope . discussionLoaded = false ;
0 commit comments