Skip to content

Commit ba1b489

Browse files
committed
MOBILE-1966 files: Move common offline files functions to helper
1 parent d610cc5 commit ba1b489

File tree

9 files changed

+107
-103
lines changed

9 files changed

+107
-103
lines changed

www/addons/mod/assign/submission/file/directive.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ angular.module('mm.addons.mod_assign')
2222
* @name mmaModAssignSubmissionFile
2323
*/
2424
.directive('mmaModAssignSubmissionFile', function($mmaModAssign, $mmaModAssignSubmissionFileSession, $mmaModAssignHelper,
25-
$mmaModAssignOffline, mmaModAssignSubmissionFileName) {
25+
$mmaModAssignOffline, mmaModAssignSubmissionFileName, $mmFileUploaderHelper) {
2626
return {
2727
restrict: 'A',
2828
priority: 100,
@@ -40,13 +40,9 @@ angular.module('mm.addons.mod_assign')
4040
offlineData.plugindata.files_filemanager.offline) {
4141
// Has offline files.
4242
return $mmaModAssignHelper.getStoredSubmissionFiles(scope.assign.id, mmaModAssignSubmissionFileName)
43-
.then(function(result) {
44-
// Mark the files as pending offline.
45-
angular.forEach(result, function(file) {
46-
file.offline = true;
47-
file.filename = file.name;
48-
});
49-
scope.files = scope.files.concat(result);
43+
.then(function(files) {
44+
files = $mmFileUploaderHelper.markOfflineFiles(files);
45+
scope.files = scope.files.concat(files);
5046
});
5147
}
5248
}).finally(function() {

www/addons/mod/assign/submission/file/handlers.js

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ angular.module('mm.addons.mod_assign')
2424
* @name $mmaModAssignSubmissionFileHandler
2525
*/
2626
.factory('$mmaModAssignSubmissionFileHandler', function($mmaModAssignSubmissionFileSession, $mmaModAssign, $mmSite, $q,
27-
$mmaModAssignHelper, $mmWS, $mmFS, $mmFilepool, $mmUtil, $mmaModAssignOffline, mmaModAssignSubmissionFileName) {
27+
$mmaModAssignHelper, $mmWS, $mmFS, $mmFilepool, $mmUtil, $mmaModAssignOffline, mmaModAssignSubmissionFileName,
28+
$mmFileUploaderHelper) {
2829

2930
var self = {};
3031

@@ -42,13 +43,8 @@ angular.module('mm.addons.mod_assign')
4243

4344
// Clear the files in session for this assign.
4445
$mmaModAssignSubmissionFileSession.clearFiles(assign.id);
45-
4646
// Now delete the local files from the tmp folder.
47-
files.forEach(function(file) {
48-
if (!file.offline && file.remove) {
49-
file.remove();
50-
}
51-
});
47+
$mmFileUploaderHelper.clearTmpFiles(files);
5248
};
5349

5450
/**

www/addons/mod/forum/controllers/discussion.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ angular.module('mm.addons.mod_forum')
2424
.controller('mmaModForumDiscussionCtrl', function($q, $scope, $stateParams, $mmaModForum, $mmSite, $mmUtil, $translate, $mmEvents,
2525
$ionicScrollDelegate, mmaModForumComponent, mmaModForumReplyDiscussionEvent, $mmaModForumOffline, $mmaModForumSync,
2626
mmaModForumAutomSyncedEvent, mmaModForumManualSyncedEvent, $mmApp, $ionicPlatform, mmCoreEventOnlineStatusChanged,
27-
$mmaModForumHelper) {
27+
$mmaModForumHelper, $mmFileUploaderHelper) {
2828

2929
var discussionId = $stateParams.discussionid,
3030
courseId = $stateParams.cid,
@@ -319,7 +319,7 @@ angular.module('mm.addons.mod_forum')
319319

320320
return promise.then(function() {
321321
// Delete the local files from the tmp folder.
322-
$mmaModForumHelper.clearTmpFiles($scope.newPost.files);
322+
$mmFileUploaderHelper.clearTmpFiles($scope.newPost.files);
323323
});
324324
}
325325

www/addons/mod/forum/controllers/newdiscussion.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ angular.module('mm.addons.mod_forum')
2323
*/
2424
.controller('mmaModForumNewDiscussionCtrl', function($scope, $stateParams, $mmGroups, $q, $mmaModForum, $mmEvents, $ionicPlatform,
2525
$mmUtil, $ionicHistory, $translate, mmaModForumNewDiscussionEvent, $mmaModForumOffline, $mmSite, mmaModForumComponent,
26-
mmaModForumAutomSyncedEvent, $mmSyncBlock, $mmaModForumSync, $mmText, $mmaModForumHelper) {
26+
mmaModForumAutomSyncedEvent, $mmSyncBlock, $mmaModForumSync, $mmText, $mmaModForumHelper, $mmFileUploaderHelper) {
2727

2828
var courseId = $stateParams.cid,
2929
forumId = $stateParams.forumid,
@@ -250,7 +250,7 @@ angular.module('mm.addons.mod_forum')
250250
$mmEvents.trigger(mmaModForumNewDiscussionEvent, data);
251251

252252
// Delete the local files from the tmp folder.
253-
$mmaModForumHelper.clearTmpFiles($scope.newDiscussion.files);
253+
$mmFileUploaderHelper.clearTmpFiles($scope.newDiscussion.files);
254254

255255
if ($ionicPlatform.isTablet()) {
256256
// Empty form.
@@ -278,7 +278,7 @@ angular.module('mm.addons.mod_forum')
278278

279279
return promise.then(function() {
280280
// Delete the local files from the tmp folder.
281-
$mmaModForumHelper.clearTmpFiles($scope.newDiscussion.files);
281+
$mmFileUploaderHelper.clearTmpFiles($scope.newDiscussion.files);
282282
});
283283
}
284284

www/addons/mod/forum/directives/discussionpost.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ angular.module('mm.addons.mod_forum')
4141
* @param {Object} [originalData] Original newpost data. Used to detect if data has changed.
4242
*/
4343
.directive('mmaModForumDiscussionPost', function($mmaModForum, $mmUtil, $translate, $q, $mmaModForumOffline, $mmSyncBlock,
44-
mmaModForumComponent, $mmaModForumSync, $mmText, $mmaModForumHelper, $ionicScrollDelegate) {
44+
mmaModForumComponent, $mmaModForumSync, $mmText, $mmaModForumHelper, $ionicScrollDelegate, $mmFileUploaderHelper) {
4545

4646
// Confirm discard changes if any.
4747
function confirmDiscard(scope) {
@@ -56,7 +56,7 @@ angular.module('mm.addons.mod_forum')
5656
// Set data to new post, clearing tmp files and updating original data.
5757
function setPostData(scope, scrollView, replyingTo, editing, isEditing, subject, text, files) {
5858
// Delete the local files from the tmp folder if any.
59-
$mmaModForumHelper.clearTmpFiles(scope.newpost.files);
59+
$mmFileUploaderHelper.clearTmpFiles(scope.newpost.files);
6060

6161
scope.newpost.replyingto = replyingTo;
6262
scope.newpost.editing = editing;

www/addons/mod/forum/services/helper.js

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,11 @@ angular.module('mm.addons.mod_forum')
2121
* @ngdoc service
2222
* @name $mmaModForumHelper
2323
*/
24-
.factory('$mmaModForumHelper', function($mmaModForumOffline, $mmSite, $mmFileUploader, $mmFS, mmaModForumComponent, $mmUser, $q) {
24+
.factory('$mmaModForumHelper', function($mmaModForumOffline, $mmSite, $mmFileUploader, $mmFS, mmaModForumComponent, $mmUser, $q,
25+
$mmFileUploaderHelper) {
2526

2627
var self = {};
2728

28-
/**
29-
* Clear temporary attachments because a new discussion or post was cancelled.
30-
* Attachments already saved in an offline discussion or post will NOT be deleted.
31-
*
32-
* @module mm.addons.mod_forum
33-
* @ngdoc method
34-
* @name $mmaModForumHelper#clearTmpFiles
35-
* @param {Object[]} files List of current files.
36-
* @return {Void}
37-
*/
38-
self.clearTmpFiles = function(files) {
39-
// Delete the local files from the tmp folder.
40-
files.forEach(function(file) {
41-
if (!file.offline && file.remove) {
42-
file.remove();
43-
}
44-
});
45-
};
46-
4729
/**
4830
* Convert offline reply to online format in order to be compatible with them.
4931
*
@@ -150,7 +132,7 @@ angular.module('mm.addons.mod_forum')
150132
*/
151133
self.getNewDiscussionStoredFiles = function(forumId, timecreated, siteId) {
152134
return $mmaModForumOffline.getNewDiscussionFolder(forumId, timecreated, siteId).then(function(folderPath) {
153-
return getStoredFiles(folderPath);
135+
return $mmFileUploaderHelper.getStoredFiles(folderPath);
154136
});
155137
};
156138

@@ -168,27 +150,10 @@ angular.module('mm.addons.mod_forum')
168150
*/
169151
self.getReplyStoredFiles = function(forumId, postId, siteId, userId) {
170152
return $mmaModForumOffline.getReplyFolder(forumId, postId, siteId, userId).then(function(folderPath) {
171-
return getStoredFiles(folderPath);
153+
return $mmFileUploaderHelper.getStoredFiles(folderPath);
172154
});
173155
};
174156

175-
/**
176-
* Get the files stored in a folder, marking them as offline.
177-
*
178-
* @param {String} folderPath Folder where to get the files.
179-
* @return {Promise} Promise resolved with the list of files.
180-
*/
181-
function getStoredFiles(folderPath) {
182-
return $mmFS.getDirectoryContents(folderPath).then(function(files) {
183-
// Mark the files as pending offline.
184-
angular.forEach(files, function(file) {
185-
file.offline = true;
186-
file.filename = file.name;
187-
});
188-
return files;
189-
});
190-
}
191-
192157
/**
193158
* Check if the data of a post/discussion has changed.
194159
*
@@ -205,20 +170,11 @@ angular.module('mm.addons.mod_forum')
205170
return false;
206171
}
207172

208-
var postFiles = post.files || [],
209-
originalFiles = original.files || [];
210-
211-
if (original.subject != post.subject || original.text != post.text || postFiles.length != originalFiles.length) {
173+
if (original.subject != post.subject || original.text != post.text) {
212174
return true;
213175
}
214176

215-
for (var i = 0; i < postFiles.length; i++) {
216-
if (postFiles[i].name != originalFiles[i].name) {
217-
return true;
218-
}
219-
}
220-
221-
return false;
177+
return $mmFileUploaderHelper.areFileListDifferent(post.files, original.files);
222178
};
223179

224180
/**

www/addons/mod/glossary/controllers/edit.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ angular.module('mm.addons.mod_glossary')
2222
* @name mmaModGlossaryEditCtrl
2323
*/
2424
.controller('mmaModGlossaryEditCtrl', function($stateParams, $scope, mmaModGlossaryComponent, $mmUtil, $q, $mmaModGlossary, $mmText,
25-
$translate, $ionicHistory, $mmEvents, mmaModGlossaryAddEntryEvent, $mmaModGlossaryOffline, $mmaModGlossaryHelper) {
25+
$translate, $ionicHistory, $mmEvents, mmaModGlossaryAddEntryEvent, $mmaModGlossaryOffline, $mmaModGlossaryHelper,
26+
$mmFileUploaderHelper) {
2627

2728
var module = $stateParams.module,
2829
courseId = $stateParams.courseid,
@@ -101,7 +102,7 @@ angular.module('mm.addons.mod_glossary')
101102

102103
return promise.then(function() {
103104
// Delete the local files from the tmp folder.
104-
$mmaModGlossaryHelper.clearTmpFiles($scope.attachments);
105+
$mmFileUploaderHelper.clearTmpFiles($scope.attachments);
105106
});
106107
}
107108

@@ -198,7 +199,7 @@ angular.module('mm.addons.mod_glossary')
198199
entry: $scope.entry
199200
};
200201

201-
$mmaModGlossaryHelper.clearTmpFiles($scope.attachments);
202+
$mmFileUploaderHelper.clearTmpFiles($scope.attachments);
202203

203204
$mmEvents.trigger(mmaModGlossaryAddEntryEvent, data);
204205

www/addons/mod/glossary/services/helper.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,11 @@ angular.module('mm.addons.mod_glossary')
2121
* @ngdoc service
2222
* @name $mmaModGlossaryHelper
2323
*/
24-
.factory('$mmaModGlossaryHelper', function($mmaModGlossaryOffline, $mmSite, $mmFileUploader, $mmFS, mmaModGlossaryComponent) {
24+
.factory('$mmaModGlossaryHelper', function($mmaModGlossaryOffline, $mmSite, $mmFileUploader, $mmFS, mmaModGlossaryComponent,
25+
$mmFileUploaderHelper) {
2526

2627
var self = {};
2728

28-
/**
29-
* Clear temporary attachments because a new discussion or post was cancelled.
30-
* Attachments already saved in an offline discussion or post will NOT be deleted.
31-
*
32-
* @module mm.addons.mod_glossary
33-
* @ngdoc method
34-
* @name $mmaModGlossaryHelper#clearTmpFiles
35-
* @param {Object[]} files List of current files.
36-
* @return {Void}
37-
*/
38-
self.clearTmpFiles = function(files) {
39-
// Delete the local files from the tmp folder.
40-
files.forEach(function(file) {
41-
if (!file.offline && file.remove) {
42-
file.remove();
43-
}
44-
});
45-
};
46-
4729
/**
4830
* Delete stored attachment files for a new discussion.
4931
*
@@ -78,14 +60,7 @@ angular.module('mm.addons.mod_glossary')
7860
siteId = siteId || $mmSite.getId();
7961

8062
return $mmaModGlossaryOffline.getEntryFolder(glossaryId, entryName, siteId).then(function(folderPath) {
81-
return $mmFS.getDirectoryContents(folderPath).then(function(files) {
82-
// Mark the files as pending offline.
83-
angular.forEach(files, function(file) {
84-
file.offline = true;
85-
file.filename = file.name;
86-
});
87-
return files;
88-
});
63+
return $mmFileUploaderHelper.getStoredFiles(folderPath);
8964
});
9065
};
9166

www/core/components/fileuploader/services/helper.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,53 @@ angular.module('mm.core.fileuploader')
2727
filePickerDeferred,
2828
hideActionSheet;
2929

30+
/**
31+
* Compares two file lists and returns if they are different.
32+
*
33+
* @module mm.core
34+
* @ngdoc method
35+
* @name $mmFileUploaderHelper#areFileListDifferent
36+
* @param {Array} a First file list.
37+
* @param {Array} b Second file list.
38+
* @return {Boolean} If both lists are different.
39+
*/
40+
self.areFileListDifferent = function(a, b) {
41+
a = a || [];
42+
b = b || [];
43+
if (a.length != b.length) {
44+
return true;
45+
}
46+
47+
// Currently we are going to compare the order of the files as well.
48+
// This function can be improved comparing more fields or not comparing the order.
49+
for (var i = 0; i < a.length; i++) {
50+
if (a[i].name != b[i].name) {
51+
return true;
52+
}
53+
}
54+
55+
return false;
56+
};
57+
58+
/**
59+
* Clear temporary attachments to be uploaded.
60+
* Attachments already saved in an offline store will NOT be deleted.
61+
*
62+
* @module mm.core
63+
* @ngdoc method
64+
* @name $mmFileUploaderHelper#clearTmpFiles
65+
* @param {Object[]} files List of current files.
66+
* @return {Void}
67+
*/
68+
self.clearTmpFiles = function(files) {
69+
// Delete the local files from the tmp folder.
70+
files.forEach(function(file) {
71+
if (!file.offline && file.remove) {
72+
file.remove();
73+
}
74+
});
75+
};
76+
3077
/**
3178
* Show a confirmation modal to the user if he is using a limited connection or the file size is higher than 5MB.
3279
*
@@ -161,6 +208,39 @@ angular.module('mm.core.fileuploader')
161208
}
162209
};
163210

211+
/**
212+
* Get the files stored in a folder, marking them as offline.
213+
*
214+
* @module mm.core.fileuploader
215+
* @ngdoc method
216+
* @name $mmFileUploaderHelper#getStoredFiles
217+
* @param {String} folderPath Folder where to get the files.
218+
* @return {Promise} Promise resolved with the list of files.
219+
*/
220+
self.getStoredFiles = function(folderPath) {
221+
return $mmFS.getDirectoryContents(folderPath).then(function(files) {
222+
return self.markOfflineFiles(files);
223+
});
224+
};
225+
226+
/**
227+
* Mark files as offline.
228+
*
229+
* @module mm.core.fileuploader
230+
* @ngdoc method
231+
* @name $mmFileUploaderHelper#markOfflineFiles
232+
* @param {Array} files Files to mark as offline.
233+
* @return {Array} Files marked as offline.
234+
*/
235+
self.markOfflineFiles = function(files) {
236+
// Mark the files as pending offline.
237+
angular.forEach(files, function(file) {
238+
file.offline = true;
239+
file.filename = file.name;
240+
});
241+
return files;
242+
};
243+
164244
/**
165245
* Open the view to select and upload a file.
166246
*

0 commit comments

Comments
 (0)