Skip to content

Commit 81eae62

Browse files
committed
Merge pull request #362 from dpalou/MOBILE-1392
Mobile 1392
2 parents 10fd2f5 + 786ea76 commit 81eae62

File tree

7 files changed

+111
-44
lines changed

7 files changed

+111
-44
lines changed

www/core/components/course/controllers/sections.js

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@ angular.module('mm.core.course')
2323
*/
2424
.controller('mmCourseSectionsCtrl', function($mmCourse, $mmUtil, $scope, $stateParams, $translate, $mmCourseHelper, $mmEvents,
2525
$mmSite, $mmCoursePrefetchDelegate, $mmCourses, $q, $ionicHistory, $ionicPlatform, mmCoreCourseAllSectionsId,
26-
mmCoreEventSectionStatusChanged) {
27-
var courseid = $stateParams.courseid;
26+
mmCoreEventSectionStatusChanged, $mmConfig, mmCoreSettingsDownloadSection) {
27+
var courseid = $stateParams.courseid,
28+
downloadSectionsEnabled;
2829

2930
$scope.courseid = courseid;
3031

32+
function checkDownloadSectionsEnabled() {
33+
return $mmConfig.get(mmCoreSettingsDownloadSection, true).then(function(enabled) {
34+
downloadSectionsEnabled = enabled;
35+
}).catch(function() {
36+
// Shouldn't happen.
37+
downloadSectionsEnabled = false;
38+
});
39+
}
40+
3141
function loadSections(refresh) {
3242
// Get full course data. If not refreshing we'll try to get it from cache to speed up the response.
3343
return $mmCourses.getUserCourse(courseid).then(function(course) {
@@ -42,27 +52,28 @@ angular.module('mm.core.course')
4252
id: mmCoreCourseAllSectionsId
4353
}].concat(sections);
4454

45-
// Calculate status of the sections.
46-
return $mmCourseHelper.calculateSectionsStatus(result, courseid, true, refresh).catch(function() {
47-
// Ignore errors (shouldn't happen).
48-
}).then(function(downloadpromises) {
49-
// If we restored any download we'll recalculate the status once all of them have finished.
50-
if (downloadpromises && downloadpromises.length) {
51-
$mmUtil.allPromises(downloadpromises).catch(function() {
52-
if (!$scope.$$destroyed) {
53-
$mmUtil.showErrorModal('mm.course.errordownloadingsection', true);
54-
}
55-
}).finally(function() {
56-
if (!$scope.$$destroyed) {
57-
// Recalculate the status.
58-
$mmCourseHelper.calculateSectionsStatus($scope.sections, courseid, false);
59-
}
60-
});
61-
}
62-
}).finally(function() {
63-
// Show the sections even if some calculation fails (it shouldn't).
64-
$scope.sections = result;
65-
});
55+
$scope.sections = result;
56+
57+
if (downloadSectionsEnabled) {
58+
// Calculate status of the sections.
59+
return $mmCourseHelper.calculateSectionsStatus(result, courseid, true, refresh).catch(function() {
60+
// Ignore errors (shouldn't happen).
61+
}).then(function(downloadpromises) {
62+
// If we restored any download we'll recalculate the status once all of them have finished.
63+
if (downloadpromises && downloadpromises.length) {
64+
$mmUtil.allPromises(downloadpromises).catch(function() {
65+
if (!$scope.$$destroyed) {
66+
$mmUtil.showErrorModal('mm.course.errordownloadingsection', true);
67+
}
68+
}).finally(function() {
69+
if (!$scope.$$destroyed) {
70+
// Recalculate the status.
71+
$mmCourseHelper.calculateSectionsStatus($scope.sections, courseid, false);
72+
}
73+
});
74+
}
75+
});
76+
}
6677
});
6778
});
6879
}).catch(function(error) {
@@ -120,13 +131,16 @@ angular.module('mm.core.course')
120131
});
121132
};
122133

123-
loadSections().finally(function() {
124-
$scope.sectionsLoaded = true;
134+
checkDownloadSectionsEnabled().then(function() {
135+
loadSections().finally(function() {
136+
$scope.sectionsLoaded = true;
137+
});
125138
});
126139

127140
// Listen for section status changes.
128141
var statusObserver = $mmEvents.on(mmCoreEventSectionStatusChanged, function(data) {
129-
if ($scope.sections && $scope.sections.length && data.siteid === $mmSite.getId() && !$scope.$$destroyed && data.sectionid) {
142+
if (downloadSectionsEnabled && $scope.sections && $scope.sections.length && data.siteid === $mmSite.getId() &&
143+
!$scope.$$destroyed&& data.sectionid) {
130144
// Check if the affected section is being downloaded. If so, we don't update section status
131145
// because it'll already be updated when the download finishes.
132146
if ($mmCoursePrefetchDelegate.isBeingDownloaded($mmCourseHelper.getSectionDownloadId({id: data.sectionid}))) {

www/core/components/course/templates/sections.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<ion-view>
22
<ion-nav-title><mm-format-text watch="true">{{fullname}}</mm-format-text></ion-nav-title>
3-
<mm-split-view load-when="sectionsLoaded" component="mmCoreCourseSectionsList">
3+
<mm-split-view load-when="sectionsLoaded" load="2" component="mmCoreCourseSectionsList">
44
<ion-content>
55
<ion-refresher on-refresh="doRefresh()" pulling-text="{{ 'mm.core.pulltorefresh' | translate }}" ng-if="sectionsLoaded"></ion-refresher>
66
<mm-loading hide-until="sectionsLoaded">

www/core/components/settings/controllers/general.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ angular.module('mm.core.settings')
2121
* @ngdoc controller
2222
* @name mmSettingsGeneralCtrl
2323
*/
24-
.controller('mmSettingsGeneralCtrl', function($scope, $mmLang, $ionicHistory, $mmEvents, mmCoreEventLanguageChanged,
25-
mmCoreSettingsReportInBackground, mmCoreConfigConstants) {
24+
.controller('mmSettingsGeneralCtrl', function($scope, $mmLang, $ionicHistory, $mmEvents, $mmConfig, mmCoreEventLanguageChanged,
25+
mmCoreSettingsReportInBackground, mmCoreConfigConstants, mmCoreSettingsDownloadSection) {
2626

2727
$scope.langs = mmCoreConfigConstants.languages;
2828

@@ -38,6 +38,14 @@ angular.module('mm.core.settings')
3838
});
3939
};
4040

41+
$mmConfig.get(mmCoreSettingsDownloadSection, true).then(function(downloadSectionEnabled) {
42+
$scope.downloadSection = downloadSectionEnabled;
43+
});
44+
45+
$scope.downloadSectionChanged = function(downloadSection) {
46+
$mmConfig.set(mmCoreSettingsDownloadSection, downloadSection);
47+
};
48+
4149
if (localStorage && localStorage.getItem && localStorage.setItem) {
4250
$scope.showReport = true;
4351
$scope.reportInBackground = parseInt(localStorage.getItem(mmCoreSettingsReportInBackground), 10) === 1;

www/core/components/settings/lang/en.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"displayformat": "Display format",
1818
"devicewebworkers": "Device Web Workers supported",
1919
"enabledebugging": "Enable debugging",
20+
"enabledownloadsection": "Enable download sections",
21+
"enabledownloadsectiondescription": "Disable this option to speed up the loading of course sections.",
2022
"enablesyncwifi": "Allow sync only when on Wi-Fi",
2123
"errordeletesitefiles": "Error deleting site files.",
2224
"errorsyncsite": "Error synchronizing the site data, please check your internet connection and try again.",

www/core/components/settings/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
angular.module('mm.core.settings', [])
1616

17+
.constant('mmCoreSettingsDownloadSection', 'mmCoreSettingsDownloadSection')
1718
.constant('mmCoreSettingsReportInBackground', 'mmCoreReportInBackground')
1819
.constant('mmCoreSettingsSyncOnlyOnWifi', 'mmCoreSyncOnlyOnWifi')
1920

www/core/components/settings/templates/general.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@
77
</div>
88
<select ng-model="selectedLanguage" ng-options="code as name for (code, name) in langs" ng-change="languageChanged(selectedLanguage)" aria-labelledby="mm-settings-languagelabel">
99
</select>
10+
</li>
11+
<li class="item item-toggle item-text-wrap">
12+
<p id="mm-settings-downloadsectionlabel" class="item-heading">{{ 'mm.settings.enabledownloadsection' | translate }}</p>
13+
<p>{{ 'mm.settings.enabledownloadsectiondescription' | translate }}</p>
14+
<label class="toggle" aria-labelledby="mm-settings-downloadsectionlabel">
15+
<input id="mm-settings-downloadsectioncheckbox" type="checkbox" ng-model="downloadSection" ng-change="downloadSectionChanged(downloadSection)">
16+
<div class="track">
17+
<div class="handle"></div>
18+
</div>
19+
</label>
1020
</li>
1121
<li class="item item-toggle item-text-wrap" ng-if="showReport">
1222
<p id="mm-settings-reportlabel" class="item-heading">{{ 'mm.settings.reportinbackground' | translate }}</p>

www/core/directives/splitview.js

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,32 @@ angular.module('mm.core')
2424
* @name mmSplitView
2525
* @description
2626
* Usage:
27-
* <mm-split-view>
27+
* <mm-split-view component="mmaCalendarEventsList">
2828
* <!-- CONTENT TO SHOW ON THE LEFT PANEL (MENU) -->
2929
* </mm-split-view>
3030
*
3131
* To change the right pane contents (content pane), mmSplitViewLink directive is needed.
32-
* mmSplitView will try to load the first mmSplitViewLink when the view is loaded.
32+
* mmSplitView will automatically try to load a mmSplitViewLink when the view is loaded. This can be configured using
33+
* the attributes "load" and "loadWhen".
34+
*
35+
* If you don't have access to the directive's scope but you still want to configure when should the data be loaded and which
36+
* element should it load you can use the mmCoreSplitViewLoad event. When the directive receives this event it will try to
37+
* immediately load the link set (if no link is set it will load the first link found). Example:
38+
* $rootScope.$broadcast(mmCoreSplitViewLoad, {load: 2});
3339
*
3440
* Accepts the following params:
3541
*
3642
* @param {String} [menuWidth] Width of the left menu. Can be specified in pixels ('200px') or in percentage ('30%').
3743
*
38-
* @param {String} [loadWhen] Name of a scope variable. When that variable is set to true, the first mm-split-view-link
39-
* found will be loaded in the contents pane. If not set, try to load it right at the start.
44+
* @param {String} [loadWhen] Name of a scope variable. When that variable is set to true, a mm-split-view-link will be loaded in
45+
* in the contents pane. If not set, try to load it right at the start. See "load" param.
4046
*
4147
* @param {String} component Component. In tablet, the new view will be named after the component.
48+
*
49+
* @param {Number} [load] Link to load. If not set then the first link will be loaded by default. If it's set then it will
50+
* try to load the nth link. E.g. load=2 will load the second link in the page.
4251
*/
43-
.directive('mmSplitView', function($log, $state, $ionicPlatform, $timeout, $mmUtil, mmCoreSplitViewLoad) {
52+
.directive('mmSplitView', function($log, $state, $ionicPlatform, $timeout, $mmUtil, $interpolate, mmCoreSplitViewLoad) {
4453

4554
$log = $log.getInstance('mmSplitView');
4655

@@ -94,21 +103,40 @@ angular.module('mm.core')
94103
/**
95104
* Load a mm-split-view-link.
96105
*
97-
* @param {Boolean} retrying True if we're retrying because the function failed (link wasn't ready), false otherwise.
106+
* @param {Object} [scope] Directive's scope.
107+
* @param {String|Number} [loadAttr] Number of link to load.
108+
* @param {Boolean} retrying True if we're retrying because the function failed (link wasn't ready).
98109
*/
99-
this.loadLink = function(retrying) {
110+
this.loadLink = function(scope, loadAttr, retrying) {
100111
if ($ionicPlatform.isTablet()) {
101112
if (!linkToLoad) {
102-
// No link set. Get first link inside the directive.
103-
linkToLoad = angular.element(element.querySelector('[mm-split-view-link]'));
113+
// No link set. Let's determine if loadAttr is set and its real value.
114+
if (typeof loadAttr != 'undefined') {
115+
var position = parseInt(loadAttr);
116+
if (!position) {
117+
// Seems it's not a number. Try to interpolate it.
118+
position = parseInt($interpolate(loadAttr)(scope), 10); // "Evaluate" scope variables.
119+
}
120+
if (position) {
121+
var links = element.querySelectorAll('[mm-split-view-link]');
122+
position = position > links.length ? 0 : position - 1;
123+
linkToLoad = angular.element(links[position]);
124+
} else {
125+
// Load first link
126+
linkToLoad = angular.element(element.querySelector('[mm-split-view-link]'));
127+
}
128+
} else {
129+
// Load first link
130+
linkToLoad = angular.element(element.querySelector('[mm-split-view-link]'));
131+
}
104132
}
105133

106134
if (!triggerClick(linkToLoad)) {
107135
// Link not found. Let's retry once in the next digest.
108136
if (!retrying) {
109137
linkToLoad = undefined;
110138
$timeout(function() {
111-
self.loadLink(true);
139+
self.loadLink(scope, loadAttr, true);
112140
});
113141
}
114142
}
@@ -185,25 +213,29 @@ angular.module('mm.core')
185213
// Load link when variable is set to true.
186214
scope.$watch(attrs.loadWhen, function(newValue) {
187215
if (newValue) {
188-
controller.loadLink();
216+
controller.loadLink(scope, attrs.load);
189217
}
190218
});
191219
} else {
192-
controller.loadLink();
220+
controller.loadLink(scope, attrs.load);
193221
}
194222

195223
// Load last opened link when we re-enter the same state. We use $stateChangeSuccess instead of $ionicView.enter
196224
// because $ionicView.enter is not triggered when going to the same state.
197225
scope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
198226
// Compare that name and params are similar. We'll only compare 1st level of params, it's not a deep compare.
199227
if (toState.name === menuState && $mmUtil.basicLeftCompare(toParams, menuParams, 1)) {
200-
controller.loadLink();
228+
controller.loadLink(); // No need to pass scope and load, link should be set.
201229
}
202230
});
203231

204232
// Listen for event to load link.
205-
scope.$on(mmCoreSplitViewLoad, function() {
206-
controller.loadLink();
233+
scope.$on(mmCoreSplitViewLoad, function(e, data) {
234+
if (data && data.load) {
235+
controller.loadLink(scope, data.load);
236+
} else {
237+
controller.loadLink(scope, attrs.load);
238+
}
207239
});
208240
}
209241
};

0 commit comments

Comments
 (0)