1414
1515angular . 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 ( / ^ h t t p s ? : \/ \/ / 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} ) ;
0 commit comments