Skip to content

Commit e0a8f09

Browse files
committed
MOBILE-1859 sidemenu: Allow embedded custom items
1 parent d0edfee commit e0a8f09

File tree

7 files changed

+82
-4
lines changed

7 files changed

+82
-4
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// (C) Copyright 2015 Martin Dougiamas
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
angular.module('mm.core.sidemenu')
16+
17+
/**
18+
* Controller of the iframe viewer.
19+
*
20+
* @module mm.core.sidemenu
21+
* @ngdoc controller
22+
* @name mmSideMenuIframeViewCtrl
23+
*/
24+
.controller('mmSideMenuIframeViewCtrl', function($scope, $stateParams, $sce) {
25+
$scope.title = $stateParams.title;
26+
$scope.url = $sce.trustAsResourceUrl($stateParams.url);
27+
});

www/core/components/sidemenu/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@ angular.module('mm.core.sidemenu', [])
3333
$state.go('mm_login.init');
3434
}
3535
}
36+
})
37+
38+
.state('site.iframe-view', {
39+
url: '/iframe-view',
40+
params: {
41+
title: null,
42+
url: null
43+
},
44+
views: {
45+
'site': {
46+
templateUrl: 'core/components/sidemenu/templates/iframe.html',
47+
controller: 'mmSideMenuIframeViewCtrl'
48+
}
49+
}
3650
});
3751

3852
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<ion-view>
2+
<ion-nav-title>{{ title }}</ion-nav-title>
3+
<ion-content mm-state-class>
4+
<mm-iframe src="url"></mm-iframe>
5+
</ion-content>
6+
</ion-view>

www/core/components/sidemenu/templates/menu.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ <h2>{{siteinfo.fullname}}</h2>
4545
</a>
4646
</li>
4747
<li ng-repeat="item in customItems" class="mm-sidemenu-customitem">
48-
<a menu-close class="item item-icon-left" ng-href="{{item.url}}" mm-link in-app="{{item.type == 'inappbrowser'}}" title="{{item.label}}">
48+
<a ng-if="item.type != 'embedded'" menu-close class="item item-icon-left" ng-href="{{item.url}}" mm-link in-app="{{item.type == 'inappbrowser'}}" title="{{item.label}}">
49+
<i class="icon {{item.icon}}"></i>{{item.label}}
50+
</a>
51+
<a ng-if="item.type == 'embedded'" menu-close class="item item-icon-left" ui-sref="site.iframe-view({title: item.label, url: item.url})" title="{{item.label}}">
4952
<i class="icon {{item.icon}}"></i>{{item.label}}
5053
</a>
5154
</li>

www/core/directives/iframe.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
angular.module('mm.core')
1616

17+
.constant('mmCoreIframeTimeout', 15000)
18+
1719
/**
1820
* Directive to display content in an iframe.
1921
*
@@ -27,7 +29,7 @@ angular.module('mm.core')
2729
* @param {Mixed} [width=100%] Width of the iframe. If not defined, use 100%.
2830
* @param {Mixed} [height=100%] Height of the iframe. If not defined, use 100%.
2931
*/
30-
.directive('mmIframe', function($log, $mmUtil, $mmText, $mmSite, $mmFS) {
32+
.directive('mmIframe', function($log, $mmUtil, $mmText, $mmSite, $mmFS, $timeout, mmCoreIframeTimeout) {
3133
$log = $log.getInstance('mmIframe');
3234

3335
var tags = ['iframe', 'frame', 'object', 'embed'];
@@ -195,17 +197,38 @@ angular.module('mm.core')
195197

196198
return {
197199
restrict: 'E',
198-
template: '<div class="iframe-wrapper"><iframe class="mm-iframe" ng-style="{\'width\': width, \'height\': height}" ng-src="{{src}}"></iframe></div>',
200+
templateUrl: 'core/templates/iframe.html',
199201
scope: {
200202
src: '='
201203
},
202204
link: function(scope, element, attrs) {
205+
var url = (scope.src && scope.src.toString()) || '', // Convert $sce URLs to string URLs.
206+
iframe = angular.element(element.find('iframe')[0]);
207+
203208
scope.width = $mmUtil.formatPixelsSize(attrs.iframeWidth) || '100%';
204209
scope.height = $mmUtil.formatPixelsSize(attrs.iframeHeight) || '100%';
205210

206-
var iframe = angular.element(element.find('iframe')[0]);
211+
// Show loading only with external URLs.
212+
scope.loading = !!url.match(/^https?:\/\//i);
213+
207214
treatFrame(iframe);
208215

216+
if (scope.loading) {
217+
iframe.on('load', function() {
218+
scope.loading = false;
219+
$timeout(); // Use $timeout to force a digest and update the view.
220+
});
221+
222+
iframe.on('error', function() {
223+
scope.loading = false;
224+
$mmUtil.showErrorModal('mm.core.errorloadingcontent', true);
225+
$timeout(); // Use $timeout to force a digest and update the view.
226+
});
227+
228+
$timeout(function() {
229+
scope.loading = false;
230+
}, mmCoreIframeTimeout);
231+
}
209232
}
210233
};
211234
});

www/core/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"errorfileexistssamename": "There's already a file with this name.",
6161
"errorinvalidform": "The form contains invalid data. Please make sure to fill all required fields and that the data is valid.",
6262
"errorinvalidresponse": "Invalid response received. Please contact your Moodle site administrator if the error persists.",
63+
"errorloadingcontent": "Error loading content.",
6364
"erroropenfilenoapp": "Error opening the file: no app found to open this kind of file.",
6465
"erroropenfilenoextension": "Error opening the file: the file doesn't have extension.",
6566
"erroropenpopup": "This activity is trying to open a popup. This is not supported in this app.",

www/core/templates/iframe.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="iframe-wrapper" ng-class="{'mm-loading-container': loading}">
2+
<iframe ng-show="!loading" class="mm-iframe" ng-style="{'width': width, 'height': height}" ng-src="{{src}}"></iframe>
3+
<ion-spinner ng-if="loading"></ion-spinner>
4+
</div>

0 commit comments

Comments
 (0)