Skip to content

Commit 47e555e

Browse files
committed
MOBILE-1966 glossary: Check original data before leaving edition
1 parent ba1b489 commit 47e555e

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ angular.module('mm.addons.mod_glossary')
3030
cmid = $stateParams.cmid,
3131
glossaryId = $stateParams.glossaryid,
3232
glossary = $stateParams.glossary || {},
33+
originalData = null,
3334
entry = $stateParams.entry || false;
3435

3536
$scope.entry = {
@@ -52,6 +53,12 @@ angular.module('mm.addons.mod_glossary')
5253
if (entry) {
5354
$scope.entry.concept = entry.concept || '';
5455
$scope.entry.text = entry.definition || '';
56+
57+
originalData = {};
58+
originalData.text = $scope.entry.text;
59+
originalData.concept = $scope.entry.concept;
60+
originalData.files = [];
61+
5562
if (entry.options) {
5663
$scope.options.categories = entry.options.categories || null;
5764
$scope.options.aliases = entry.options.aliases || "";
@@ -64,6 +71,7 @@ angular.module('mm.addons.mod_glossary')
6471
if (entry.attachments && entry.attachments.offline) {
6572
$mmaModGlossaryHelper.getStoredFiles(glossaryId, entry.concept).then(function(files) {
6673
$scope.attachments = files;
74+
originalData.files = angular.copy(files);
6775
});
6876
}
6977
}
@@ -93,7 +101,7 @@ angular.module('mm.addons.mod_glossary')
93101
function cancel() {
94102
var promise;
95103

96-
if (!$mmaModGlossaryHelper.hasEntryDataChanged($scope.entry, $scope.attachments)) {
104+
if (!$mmaModGlossaryHelper.hasEntryDataChanged($scope.entry, $scope.attachments, originalData)) {
97105
promise = $q.when();
98106
} else {
99107
// Show confirmation if some data has been modified.

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ angular.module('mm.addons.mod_glossary')
3838
* @return {Promise} Promise resolved when deleted.
3939
*/
4040
self.deleteStoredFiles = function(glossaryId, entryName, siteId) {
41-
siteId = siteId || $mmSite.getId();
42-
4341
return $mmaModGlossaryOffline.getEntryFolder(glossaryId, entryName, siteId).then(function(folderPath) {
4442
return $mmFS.removeDir(folderPath);
4543
});
@@ -57,8 +55,6 @@ angular.module('mm.addons.mod_glossary')
5755
* @return {Promise} Promise resolved with the files.
5856
*/
5957
self.getStoredFiles = function(glossaryId, entryName, siteId) {
60-
siteId = siteId || $mmSite.getId();
61-
6258
return $mmaModGlossaryOffline.getEntryFolder(glossaryId, entryName, siteId).then(function(folderPath) {
6359
return $mmFileUploaderHelper.getStoredFiles(folderPath);
6460
});
@@ -70,12 +66,22 @@ angular.module('mm.addons.mod_glossary')
7066
* @module mm.addons.mod_glossary
7167
* @ngdoc method
7268
* @name $mmaModGlossaryHelper#hasEntryDataChanged
73-
* @param {Object} entry Current data.
74-
* @param {Object} files Files attached.
69+
* @param {Object} entry Current data.
70+
* @param {Object} files Files attached.
71+
* @param {Object} original Original content.
7572
* @return {Boolean} True if data has changed, false otherwise.
7673
*/
77-
self.hasEntryDataChanged = function(entry, files) {
78-
return entry.text || entry.concept || files.length > 0;
74+
self.hasEntryDataChanged = function(entry, files, original) {
75+
if (!original || typeof original.concept == 'undefined') {
76+
// There is no original data.
77+
return entry.text || entry.concept || files.length > 0;
78+
}
79+
80+
if (original.text != entry.text || original.concept != entry.concept) {
81+
return true;
82+
}
83+
84+
return $mmFileUploaderHelper.areFileListDifferent(files, original.files);
7985
};
8086

8187
/**
@@ -92,8 +98,6 @@ angular.module('mm.addons.mod_glossary')
9298
* @return {Promise} Promise resolved if success, rejected otherwise.
9399
*/
94100
self.storeFiles = function(glossaryId, entryName, files, siteId) {
95-
siteId = siteId || $mmSite.getId();
96-
97101
// Get the folder where to store the files.
98102
return $mmaModGlossaryOffline.getEntryFolder(glossaryId, entryName, siteId).then(function(folderPath) {
99103
return $mmFileUploader.storeFilesToUpload(folderPath, files);

0 commit comments

Comments
 (0)