@@ -69,8 +69,7 @@ angular.module('mm.core')
6969 }
7070
7171 if ( ionContentEl . nodeName == 'ION-CONTENT' ) {
72- ionContentHeight = ionContentEl . offsetHeight || ionContentEl . height || ionContentEl . clientHeight ;
73- return $window . innerHeight - ionContentHeight ;
72+ return $window . innerHeight - $mmUtil . getElementHeight ( ionContentEl ) ;
7473 } else {
7574 return 0 ;
7675 }
@@ -106,6 +105,31 @@ angular.module('mm.core')
106105 }
107106 }
108107
108+ /**
109+ * Get the height of the surrounding elements from the current to the top element.
110+ *
111+ * @param {Object } element Directive DOM element to get surroundings elements from.
112+ * @param {Object } top Top DOM element to stop getting elements.
113+ * @return {Number } Surrounding height in px.
114+ */
115+ function getSurroundingHeight ( element , top ) {
116+ var height = 0 ;
117+
118+ while ( element . parentNode && element . parentNode . tagName != "ION-CONTENT" && ( ! top || element != top ) ) {
119+ var parent = element . parentNode ;
120+ angular . forEach ( parent . childNodes , function ( child ) {
121+ if ( child . tagName && element . tagName && element . tagName != 'MM-LOADING' && child != element ) {
122+ height += $mmUtil . getElementHeight ( child , false , true , true ) ;
123+ }
124+ } ) ;
125+ element = parent ;
126+ }
127+ var cs = getComputedStyle ( element ) ;
128+ height += ( parseInt ( cs . paddingTop , 10 ) + parseInt ( cs . paddingBottom , 10 ) ) ;
129+
130+ return height ;
131+ }
132+
109133 /**
110134 * Search WYSIWYG iframe and format its contents.
111135 *
@@ -293,26 +317,60 @@ angular.module('mm.core')
293317
294318 // Check if we should use rich text editor.
295319 $mmUtil . isRichTextEditorEnabled ( ) . then ( function ( enabled ) {
320+ var promise ;
321+
296322 scope . richTextEditor = ! ! enabled ;
297323 renderTime = new Date ( ) . getTime ( ) ;
298324
299325 if ( enabled ) {
300326 // Get current language to configure the editor.
301- $mmLang . getCurrentLanguage ( ) . then ( function ( lang ) {
327+ promise = $mmLang . getCurrentLanguage ( ) . then ( function ( lang ) {
302328 defaultOptions . language = changeLanguageCode ( lang ) ;
303-
304- if ( $ionicPlatform . isTablet ( ) ) {
305- scope . editorOptions = angular . extend ( defaultOptions , scope . options , scope . tabletOptions ) ;
306- } else {
307- scope . editorOptions = angular . extend ( defaultOptions , scope . options , scope . phoneOptions ) ;
308- }
309-
310- editorInitialHeight = scope . editorOptions . height ;
311- adjustHeight = scope . editorOptions . adjustHeight ;
312329 } ) ;
330+ } else {
331+ promise = $q . when ( ) ;
313332 }
333+
334+ promise . then ( function ( ) {
335+ if ( $ionicPlatform . isTablet ( ) ) {
336+ scope . editorOptions = angular . extend ( defaultOptions , scope . options , scope . tabletOptions ) ;
337+ } else {
338+ scope . editorOptions = angular . extend ( defaultOptions , scope . options , scope . phoneOptions ) ;
339+ }
340+
341+ editorInitialHeight = scope . editorOptions . height ;
342+ adjustHeight = scope . editorOptions . adjustHeight ;
343+
344+ if ( ! enabled ) {
345+ textareaReady ( ) ;
346+ }
347+ } ) ;
314348 } ) ;
315349
350+ // Function called when textarea is ready.
351+ function textareaReady ( ) {
352+ var editorEl ;
353+ $timeout ( function ( ) {
354+ if ( firstChange ) {
355+ firstChange = false ;
356+ editorEl = element . querySelector ( '.mm-textarea' ) ;
357+ resizeContentTextarea ( editorEl ) ;
358+
359+ // Listen for event resize.
360+ ionic . on ( 'resize' , onResize , window ) ;
361+ }
362+ } , 1000 ) ;
363+
364+ scope . $on ( '$destroy' , function ( ) {
365+ ionic . off ( 'resize' , onResize , window ) ;
366+ } ) ;
367+
368+ // Window resized.
369+ function onResize ( ) {
370+ resizeContentTextarea ( editorEl ) ;
371+ }
372+ }
373+
316374 // Function called when editor is ready.
317375 scope . editorReady = function ( ) {
318376 // Editor is ready, setup listeners.
@@ -427,9 +485,60 @@ angular.module('mm.core')
427485 }
428486 } ;
429487
430- // Resize the editor to fill the visible screen size (except top bar).
488+ /**
489+ * Resize the textarea to fill the visible screen size (except top bar).
490+ *
491+ * @param {Object } editorEl Textare element
492+ */
493+ function resizeContentTextarea ( editorEl ) {
494+ var editorHeight = editorInitialHeight ,
495+ contentVisibleHeight ,
496+ screenSmallerThanEditor ;
497+
498+ if ( typeof fixedBarsHeight == 'undefined' ) {
499+ fixedBarsHeight = calculateFixedBarsHeight ( editorEl ) ;
500+ }
501+
502+ contentVisibleHeight = $window . innerHeight - fixedBarsHeight ;
503+
504+ // Editor is ready, adjust Height if needed.
505+ if ( adjustHeight && contentVisibleHeight > 0 ) {
506+ var topElement ,
507+ height ;
508+
509+ if ( adjustHeight !== true ) {
510+ topElement = document . getElementById ( adjustHeight ) ;
511+ contentVisibleHeight = $mmUtil . getElementHeight ( topElement ) || contentVisibleHeight ;
512+ }
513+
514+ height = getSurroundingHeight ( element , topElement ) ;
515+ if ( contentVisibleHeight > height ) {
516+ editorHeight = contentVisibleHeight - height ;
517+ editorInitialHeight = editorHeight ;
518+ }
519+ }
520+ screenSmallerThanEditor = contentVisibleHeight > 0 && contentVisibleHeight < editorHeight ;
521+
522+ if ( resized && ! screenSmallerThanEditor ) {
523+ // The editor was resized but now it isn't needed anymore, undo the resize.
524+ undoResize ( editorEl ) ;
525+ } else if ( editorHeight > 60 && ( resized || screenSmallerThanEditor || adjustHeight ) ) {
526+ // The visible screen size is lower than the editor size, resize editor to fill the screen.
527+ // We don't resize if the content new height is too small.
528+ angular . element ( editorEl ) . css ( 'height' , editorHeight + 'px' ) ;
529+ resized = true ;
530+ }
531+ }
532+
533+ /**
534+ * Resize the editor to fill the visible screen size (except top bar).
535+ *
536+ * @param {Object } editorEl Editor element of CKE (the biggest element .cke)
537+ * @param {Object } contentsEl Just the writtable part of CKE (.cke_contents)
538+ * @param {Object } toolbar Toolbar of CKE editor (.cke_bottom)
539+ */
431540 function resizeContent ( editorEl , contentsEl , toolbar ) {
432- var toolbarHeight = toolbar . offsetHeight || toolbar . height || toolbar . clientHeight || 0 ,
541+ var toolbarHeight = $mmUtil . getElementHeight ( toolbar ) ,
433542 editorWithToolbarHeight = editorInitialHeight + toolbarHeight ,
434543 contentVisibleHeight ,
435544 editorContentNewHeight ,
@@ -445,31 +554,15 @@ angular.module('mm.core')
445554
446555 // Editor is ready, adjust Height if needed.
447556 if ( adjustHeight && ! editorMaximized && contentVisibleHeight > 0 ) {
448- var currentElement = element ,
449- topElement ,
450- height = 0 ;
557+ var topElement ,
558+ height ;
451559
452560 if ( adjustHeight !== true ) {
453561 topElement = document . getElementById ( adjustHeight ) ;
454- contentVisibleHeight = topElement . offsetHeight || topElement . height || topElement . clientHeight ||
455- contentVisibleHeight ;
562+ contentVisibleHeight = $mmUtil . getElementHeight ( topElement ) || contentVisibleHeight ;
456563 }
457- while ( currentElement . parentNode && currentElement . parentNode . tagName != "ION-CONTENT" &&
458- ( ! topElement || currentElement != topElement ) ) {
459-
460- var parent = currentElement . parentNode ;
461- angular . forEach ( parent . childNodes , function ( child ) {
462- if ( child . tagName && currentElement . tagName &&
463- currentElement . tagName != 'MM-LOADING' && child != currentElement ) {
464- height += ( child . offsetHeight || child . height || child . clientHeight ) ;
465- var cs = getComputedStyle ( child ) ;
466- height += ( parseInt ( cs . borderTop , 10 ) + parseInt ( cs . borderBottom , 10 ) ) ;
467- }
468- } ) ;
469- currentElement = parent ;
470- }
471- var cs = getComputedStyle ( currentElement ) ;
472- height += ( parseInt ( cs . paddingTop , 10 ) + parseInt ( cs . paddingBottom , 10 ) ) ;
564+
565+ height = getSurroundingHeight ( element , topElement ) ;
473566 if ( contentVisibleHeight > height ) {
474567 editorWithToolbarHeight = contentVisibleHeight - height ;
475568 editorInitialHeight = editorWithToolbarHeight - toolbarHeight ;
@@ -504,8 +597,10 @@ angular.module('mm.core')
504597
505598 // Undo resize.
506599 function undoResize ( editorEl , contentsEl ) {
507- angular . element ( editorEl ) . css ( 'height' , 'auto' ) ;
508- angular . element ( contentsEl ) . css ( 'height' , editorInitialHeight + 'px' ) ;
600+ angular . element ( editorEl ) . css ( 'height' , '' ) ;
601+ if ( contentsEl ) {
602+ angular . element ( contentsEl ) . css ( 'height' , editorInitialHeight + 'px' ) ;
603+ }
509604 resized = false ;
510605 }
511606 }
0 commit comments