plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /**
  2. * @license Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. ( function() {
  6. function protectFormStyles( formElement ) {
  7. if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
  8. return [];
  9. var hijackRecord = [],
  10. hijackNames = [ 'style', 'className' ];
  11. for ( var i = 0; i < hijackNames.length; i++ ) {
  12. var name = hijackNames[ i ];
  13. var $node = formElement.$.elements.namedItem( name );
  14. if ( $node ) {
  15. var hijackNode = new CKEDITOR.dom.element( $node );
  16. hijackRecord.push( [ hijackNode, hijackNode.nextSibling ] );
  17. hijackNode.remove();
  18. }
  19. }
  20. return hijackRecord;
  21. }
  22. function restoreFormStyles( formElement, hijackRecord ) {
  23. if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
  24. return;
  25. if ( hijackRecord.length > 0 ) {
  26. for ( var i = hijackRecord.length - 1; i >= 0; i-- ) {
  27. var node = hijackRecord[ i ][ 0 ];
  28. var sibling = hijackRecord[ i ][ 1 ];
  29. if ( sibling )
  30. node.insertBefore( sibling );
  31. else
  32. node.appendTo( formElement );
  33. }
  34. }
  35. }
  36. function saveStyles( element, isInsideEditor ) {
  37. var data = protectFormStyles( element );
  38. var retval = {};
  39. var $element = element.$;
  40. if ( !isInsideEditor ) {
  41. retval[ 'class' ] = $element.className || '';
  42. $element.className = '';
  43. }
  44. retval.inline = $element.style.cssText || '';
  45. if ( !isInsideEditor ) // Reset any external styles that might interfere. (#2474)
  46. $element.style.cssText = 'position: static; overflow: visible';
  47. restoreFormStyles( data );
  48. return retval;
  49. }
  50. function restoreStyles( element, savedStyles ) {
  51. var data = protectFormStyles( element );
  52. var $element = element.$;
  53. if ( 'class' in savedStyles )
  54. $element.className = savedStyles[ 'class' ];
  55. if ( 'inline' in savedStyles )
  56. $element.style.cssText = savedStyles.inline;
  57. restoreFormStyles( data );
  58. }
  59. function refreshCursor( editor ) {
  60. if ( editor.editable().isInline() )
  61. return;
  62. // Refresh all editor instances on the page (#5724).
  63. var all = CKEDITOR.instances;
  64. for ( var i in all ) {
  65. var one = all[ i ];
  66. if ( one.mode == 'wysiwyg' && !one.readOnly ) {
  67. var body = one.document.getBody();
  68. // Refresh 'contentEditable' otherwise
  69. // DOM lifting breaks design mode. (#5560)
  70. body.setAttribute( 'contentEditable', false );
  71. body.setAttribute( 'contentEditable', true );
  72. }
  73. }
  74. if ( editor.editable().hasFocus ) {
  75. editor.toolbox.focus();
  76. editor.focus();
  77. }
  78. }
  79. CKEDITOR.plugins.add( 'maximize', {
  80. lang: 'af,ar,bg,bn,bs,ca,cs,cy,da,de,el,en,en-au,en-ca,en-gb,eo,es,et,eu,fa,fi,fo,fr,fr-ca,gl,gu,he,hi,hr,hu,id,is,it,ja,ka,km,ko,ku,lt,lv,mk,mn,ms,nb,nl,no,pl,pt,pt-br,ro,ru,si,sk,sl,sq,sr,sr-latn,sv,th,tr,tt,ug,uk,vi,zh,zh-cn', // %REMOVE_LINE_CORE%
  81. icons: 'maximize', // %REMOVE_LINE_CORE%
  82. hidpi: true, // %REMOVE_LINE_CORE%
  83. init: function( editor ) {
  84. // Maximize plugin isn't available in inline mode yet.
  85. if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE )
  86. return;
  87. var lang = editor.lang;
  88. var mainDocument = CKEDITOR.document,
  89. mainWindow = mainDocument.getWindow();
  90. // Saved selection and scroll position for the editing area.
  91. var savedSelection, savedScroll;
  92. // Saved scroll position for the outer window.
  93. var outerScroll;
  94. // Saved resize handler function.
  95. function resizeHandler() {
  96. var viewPaneSize = mainWindow.getViewPaneSize();
  97. editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
  98. }
  99. // Retain state after mode switches.
  100. var savedState = CKEDITOR.TRISTATE_OFF;
  101. editor.addCommand( 'maximize', {
  102. // Disabled on iOS (#8307).
  103. modes: { wysiwyg: !CKEDITOR.env.iOS, source: !CKEDITOR.env.iOS },
  104. readOnly: 1,
  105. editorFocus: false,
  106. exec: function() {
  107. var container = editor.container.getFirst( function( node ) {
  108. return node.type == CKEDITOR.NODE_ELEMENT && node.hasClass( 'cke_inner' );
  109. } );
  110. var contents = editor.ui.space( 'contents' );
  111. // Save current selection and scroll position in editing area.
  112. if ( editor.mode == 'wysiwyg' ) {
  113. var selection = editor.getSelection();
  114. savedSelection = selection && selection.getRanges();
  115. savedScroll = mainWindow.getScrollPosition();
  116. } else {
  117. var $textarea = editor.editable().$;
  118. savedSelection = !CKEDITOR.env.ie && [ $textarea.selectionStart, $textarea.selectionEnd ];
  119. savedScroll = [ $textarea.scrollLeft, $textarea.scrollTop ];
  120. }
  121. if ( this.state == CKEDITOR.TRISTATE_OFF ) // Go fullscreen if the state is off.
  122. {
  123. // Add event handler for resizing.
  124. mainWindow.on( 'resize', resizeHandler );
  125. // Save the scroll bar position.
  126. outerScroll = mainWindow.getScrollPosition();
  127. // Save and reset the styles for the entire node tree.
  128. var currentNode = editor.container;
  129. while ( ( currentNode = currentNode.getParent() ) ) {
  130. currentNode.setCustomData( 'maximize_saved_styles', saveStyles( currentNode ) );
  131. // Show under floatpanels (-1) and context menu (-2).
  132. currentNode.setStyle( 'z-index', editor.config.baseFloatZIndex - 5 );
  133. }
  134. contents.setCustomData( 'maximize_saved_styles', saveStyles( contents, true ) );
  135. container.setCustomData( 'maximize_saved_styles', saveStyles( container, true ) );
  136. // Hide scroll bars.
  137. var styles = {
  138. overflow: CKEDITOR.env.webkit ? '' : 'hidden', // #6896
  139. width: 0,
  140. height: 0
  141. };
  142. mainDocument.getDocumentElement().setStyles( styles );
  143. !CKEDITOR.env.gecko && mainDocument.getDocumentElement().setStyle( 'position', 'fixed' );
  144. !( CKEDITOR.env.gecko && CKEDITOR.env.quirks ) && mainDocument.getBody().setStyles( styles );
  145. // Scroll to the top left (IE needs some time for it - #4923).
  146. CKEDITOR.env.ie ? setTimeout( function() {
  147. mainWindow.$.scrollTo( 0, 0 );
  148. }, 0 ) : mainWindow.$.scrollTo( 0, 0 );
  149. // Resize and move to top left.
  150. // Special treatment for FF Quirks (#7284)
  151. container.setStyle( 'position', CKEDITOR.env.gecko && CKEDITOR.env.quirks ? 'fixed' : 'absolute' );
  152. container.$.offsetLeft; // SAFARI BUG: See #2066.
  153. container.setStyles( {
  154. // Show under floatpanels (-1) and context menu (-2).
  155. 'z-index': editor.config.baseFloatZIndex - 5,
  156. left: '0px',
  157. top: '0px'
  158. } );
  159. // Add cke_maximized class before resize handle since that will change things sizes (#5580)
  160. container.addClass( 'cke_maximized' );
  161. resizeHandler();
  162. // Still not top left? Fix it. (Bug #174)
  163. var offset = container.getDocumentPosition();
  164. container.setStyles( {
  165. left: ( -1 * offset.x ) + 'px',
  166. top: ( -1 * offset.y ) + 'px'
  167. } );
  168. // Fixing positioning editor chrome in Firefox break design mode. (#5149)
  169. CKEDITOR.env.gecko && refreshCursor( editor );
  170. } else if ( this.state == CKEDITOR.TRISTATE_ON ) // Restore from fullscreen if the state is on.
  171. {
  172. // Remove event handler for resizing.
  173. mainWindow.removeListener( 'resize', resizeHandler );
  174. // Restore CSS styles for the entire node tree.
  175. var editorElements = [ contents, container ];
  176. for ( var i = 0; i < editorElements.length; i++ ) {
  177. restoreStyles( editorElements[ i ], editorElements[ i ].getCustomData( 'maximize_saved_styles' ) );
  178. editorElements[ i ].removeCustomData( 'maximize_saved_styles' );
  179. }
  180. currentNode = editor.container;
  181. while ( ( currentNode = currentNode.getParent() ) ) {
  182. restoreStyles( currentNode, currentNode.getCustomData( 'maximize_saved_styles' ) );
  183. currentNode.removeCustomData( 'maximize_saved_styles' );
  184. }
  185. // Restore the window scroll position.
  186. CKEDITOR.env.ie ? setTimeout( function() {
  187. mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
  188. }, 0 ) : mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
  189. // Remove cke_maximized class.
  190. container.removeClass( 'cke_maximized' );
  191. // Webkit requires a re-layout on editor chrome. (#6695)
  192. if ( CKEDITOR.env.webkit ) {
  193. container.setStyle( 'display', 'inline' );
  194. setTimeout( function() {
  195. container.setStyle( 'display', 'block' );
  196. }, 0 );
  197. }
  198. // Emit a resize event, because this time the size is modified in
  199. // restoreStyles.
  200. editor.fire( 'resize' );
  201. }
  202. this.toggleState();
  203. // Toggle button label.
  204. var button = this.uiItems[ 0 ];
  205. // Only try to change the button if it exists (#6166)
  206. if ( button ) {
  207. var label = ( this.state == CKEDITOR.TRISTATE_OFF ) ? lang.maximize.maximize : lang.maximize.minimize;
  208. var buttonNode = CKEDITOR.document.getById( button._.id );
  209. buttonNode.getChild( 1 ).setHtml( label );
  210. buttonNode.setAttribute( 'title', label );
  211. buttonNode.setAttribute( 'href', 'javascript:void("' + label + '");' );
  212. }
  213. // Restore selection and scroll position in editing area.
  214. if ( editor.mode == 'wysiwyg' ) {
  215. if ( savedSelection ) {
  216. // Fixing positioning editor chrome in Firefox break design mode. (#5149)
  217. CKEDITOR.env.gecko && refreshCursor( editor );
  218. editor.getSelection().selectRanges( savedSelection );
  219. var element = editor.getSelection().getStartElement();
  220. element && element.scrollIntoView( true );
  221. } else
  222. mainWindow.$.scrollTo( savedScroll.x, savedScroll.y );
  223. } else {
  224. if ( savedSelection ) {
  225. $textarea.selectionStart = savedSelection[ 0 ];
  226. $textarea.selectionEnd = savedSelection[ 1 ];
  227. }
  228. $textarea.scrollLeft = savedScroll[ 0 ];
  229. $textarea.scrollTop = savedScroll[ 1 ];
  230. }
  231. savedSelection = savedScroll = null;
  232. savedState = this.state;
  233. editor.fire( 'maximize', this.state );
  234. },
  235. canUndo: false
  236. } );
  237. editor.ui.addButton && editor.ui.addButton( 'Maximize', {
  238. label: lang.maximize.maximize,
  239. command: 'maximize',
  240. toolbar: 'tools,10'
  241. } );
  242. // Restore the command state after mode change, unless it has been changed to disabled (#6467)
  243. editor.on( 'mode', function() {
  244. var command = editor.getCommand( 'maximize' );
  245. command.setState( command.state == CKEDITOR.TRISTATE_DISABLED ? CKEDITOR.TRISTATE_DISABLED : savedState );
  246. }, null, null, 100 );
  247. }
  248. } );
  249. } )();
  250. /**
  251. * Event fired when the maximize command is called.
  252. * It also indicates whether an editor is maximized or not.
  253. *
  254. * @event maximize
  255. * @member CKEDITOR.editor
  256. * @param {CKEDITOR.editor} editor This editor instance.
  257. * @param {Number} data Current state of the command. See {@link CKEDITOR#TRISTATE_ON} and {@link CKEDITOR#TRISTATE_OFF}.
  258. */