plugin.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * @license Copyright (c) 2003-2015, 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. // jscs:disable maximumLineLength
  81. 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%
  82. // jscs:enable maximumLineLength
  83. icons: 'maximize', // %REMOVE_LINE_CORE%
  84. hidpi: true, // %REMOVE_LINE_CORE%
  85. init: function( editor ) {
  86. // Maximize plugin isn't available in inline mode yet.
  87. if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE )
  88. return;
  89. var lang = editor.lang;
  90. var mainDocument = CKEDITOR.document,
  91. mainWindow = mainDocument.getWindow();
  92. // Saved selection and scroll position for the editing area.
  93. var savedSelection, savedScroll;
  94. // Saved scroll position for the outer window.
  95. var outerScroll;
  96. // Saved resize handler function.
  97. function resizeHandler() {
  98. var viewPaneSize = mainWindow.getViewPaneSize();
  99. editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
  100. }
  101. // Retain state after mode switches.
  102. var savedState = CKEDITOR.TRISTATE_OFF;
  103. editor.addCommand( 'maximize', {
  104. // Disabled on iOS (#8307).
  105. modes: { wysiwyg: !CKEDITOR.env.iOS, source: !CKEDITOR.env.iOS },
  106. readOnly: 1,
  107. editorFocus: false,
  108. exec: function() {
  109. var container = editor.container.getFirst( function( node ) {
  110. return node.type == CKEDITOR.NODE_ELEMENT && node.hasClass( 'cke_inner' );
  111. } );
  112. var contents = editor.ui.space( 'contents' );
  113. // Save current selection and scroll position in editing area.
  114. if ( editor.mode == 'wysiwyg' ) {
  115. var selection = editor.getSelection();
  116. savedSelection = selection && selection.getRanges();
  117. savedScroll = mainWindow.getScrollPosition();
  118. } else {
  119. var $textarea = editor.editable().$;
  120. savedSelection = !CKEDITOR.env.ie && [ $textarea.selectionStart, $textarea.selectionEnd ];
  121. savedScroll = [ $textarea.scrollLeft, $textarea.scrollTop ];
  122. }
  123. // Go fullscreen if the state is off.
  124. if ( this.state == CKEDITOR.TRISTATE_OFF ) {
  125. // Add event handler for resizing.
  126. mainWindow.on( 'resize', resizeHandler );
  127. // Save the scroll bar position.
  128. outerScroll = mainWindow.getScrollPosition();
  129. // Save and reset the styles for the entire node tree.
  130. var currentNode = editor.container;
  131. while ( ( currentNode = currentNode.getParent() ) ) {
  132. currentNode.setCustomData( 'maximize_saved_styles', saveStyles( currentNode ) );
  133. // Show under floatpanels (-1) and context menu (-2).
  134. currentNode.setStyle( 'z-index', editor.config.baseFloatZIndex - 5 );
  135. }
  136. contents.setCustomData( 'maximize_saved_styles', saveStyles( contents, true ) );
  137. container.setCustomData( 'maximize_saved_styles', saveStyles( container, true ) );
  138. // Hide scroll bars.
  139. var styles = {
  140. overflow: CKEDITOR.env.webkit ? '' : 'hidden', // #6896
  141. width: 0,
  142. height: 0
  143. };
  144. mainDocument.getDocumentElement().setStyles( styles );
  145. !CKEDITOR.env.gecko && mainDocument.getDocumentElement().setStyle( 'position', 'fixed' );
  146. !( CKEDITOR.env.gecko && CKEDITOR.env.quirks ) && mainDocument.getBody().setStyles( styles );
  147. // Scroll to the top left (IE needs some time for it - #4923).
  148. CKEDITOR.env.ie ? setTimeout( function() {
  149. mainWindow.$.scrollTo( 0, 0 );
  150. }, 0 ) : mainWindow.$.scrollTo( 0, 0 );
  151. // Resize and move to top left.
  152. // Special treatment for FF Quirks (#7284)
  153. container.setStyle( 'position', CKEDITOR.env.gecko && CKEDITOR.env.quirks ? 'fixed' : 'absolute' );
  154. container.$.offsetLeft; // SAFARI BUG: See #2066.
  155. container.setStyles( {
  156. // Show under floatpanels (-1) and context menu (-2).
  157. 'z-index': editor.config.baseFloatZIndex - 5,
  158. left: '0px',
  159. top: '0px'
  160. } );
  161. // Add cke_maximized class before resize handle since that will change things sizes (#5580)
  162. container.addClass( 'cke_maximized' );
  163. resizeHandler();
  164. // Still not top left? Fix it. (Bug #174)
  165. var offset = container.getDocumentPosition();
  166. container.setStyles( {
  167. left: ( -1 * offset.x ) + 'px',
  168. top: ( -1 * offset.y ) + 'px'
  169. } );
  170. // Fixing positioning editor chrome in Firefox break design mode. (#5149)
  171. CKEDITOR.env.gecko && refreshCursor( editor );
  172. }
  173. // Restore from fullscreen if the state is on.
  174. else if ( this.state == CKEDITOR.TRISTATE_ON ) {
  175. // Remove event handler for resizing.
  176. mainWindow.removeListener( 'resize', resizeHandler );
  177. // Restore CSS styles for the entire node tree.
  178. var editorElements = [ contents, container ];
  179. for ( var i = 0; i < editorElements.length; i++ ) {
  180. restoreStyles( editorElements[ i ], editorElements[ i ].getCustomData( 'maximize_saved_styles' ) );
  181. editorElements[ i ].removeCustomData( 'maximize_saved_styles' );
  182. }
  183. currentNode = editor.container;
  184. while ( ( currentNode = currentNode.getParent() ) ) {
  185. restoreStyles( currentNode, currentNode.getCustomData( 'maximize_saved_styles' ) );
  186. currentNode.removeCustomData( 'maximize_saved_styles' );
  187. }
  188. // Restore the window scroll position.
  189. CKEDITOR.env.ie ? setTimeout( function() {
  190. mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
  191. }, 0 ) : mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
  192. // Remove cke_maximized class.
  193. container.removeClass( 'cke_maximized' );
  194. // Webkit requires a re-layout on editor chrome. (#6695)
  195. if ( CKEDITOR.env.webkit ) {
  196. container.setStyle( 'display', 'inline' );
  197. setTimeout( function() {
  198. container.setStyle( 'display', 'block' );
  199. }, 0 );
  200. }
  201. // Emit a resize event, because this time the size is modified in
  202. // restoreStyles.
  203. editor.fire( 'resize', {
  204. outerHeight: editor.container.$.offsetHeight,
  205. contentsHeight: contents.$.offsetHeight,
  206. outerWidth: editor.container.$.offsetWidth
  207. } );
  208. }
  209. this.toggleState();
  210. // Toggle button label.
  211. var button = this.uiItems[ 0 ];
  212. // Only try to change the button if it exists (#6166)
  213. if ( button ) {
  214. var label = ( this.state == CKEDITOR.TRISTATE_OFF ) ? lang.maximize.maximize : lang.maximize.minimize;
  215. var buttonNode = CKEDITOR.document.getById( button._.id );
  216. buttonNode.getChild( 1 ).setHtml( label );
  217. buttonNode.setAttribute( 'title', label );
  218. buttonNode.setAttribute( 'href', 'javascript:void("' + label + '");' ); // jshint ignore:line
  219. }
  220. // Restore selection and scroll position in editing area.
  221. if ( editor.mode == 'wysiwyg' ) {
  222. if ( savedSelection ) {
  223. // Fixing positioning editor chrome in Firefox break design mode. (#5149)
  224. CKEDITOR.env.gecko && refreshCursor( editor );
  225. editor.getSelection().selectRanges( savedSelection );
  226. var element = editor.getSelection().getStartElement();
  227. element && element.scrollIntoView( true );
  228. } else {
  229. mainWindow.$.scrollTo( savedScroll.x, savedScroll.y );
  230. }
  231. } else {
  232. if ( savedSelection ) {
  233. $textarea.selectionStart = savedSelection[ 0 ];
  234. $textarea.selectionEnd = savedSelection[ 1 ];
  235. }
  236. $textarea.scrollLeft = savedScroll[ 0 ];
  237. $textarea.scrollTop = savedScroll[ 1 ];
  238. }
  239. savedSelection = savedScroll = null;
  240. savedState = this.state;
  241. editor.fire( 'maximize', this.state );
  242. },
  243. canUndo: false
  244. } );
  245. editor.ui.addButton && editor.ui.addButton( 'Maximize', {
  246. label: lang.maximize.maximize,
  247. command: 'maximize',
  248. toolbar: 'tools,10'
  249. } );
  250. // Restore the command state after mode change, unless it has been changed to disabled (#6467)
  251. editor.on( 'mode', function() {
  252. var command = editor.getCommand( 'maximize' );
  253. command.setState( command.state == CKEDITOR.TRISTATE_DISABLED ? CKEDITOR.TRISTATE_DISABLED : savedState );
  254. }, null, null, 100 );
  255. }
  256. } );
  257. } )();
  258. /**
  259. * Event fired when the maximize command is called.
  260. * It also indicates whether an editor is maximized or not.
  261. *
  262. * @event maximize
  263. * @member CKEDITOR.editor
  264. * @param {CKEDITOR.editor} editor This editor instance.
  265. * @param {Number} data Current state of the command. See {@link CKEDITOR#TRISTATE_ON} and {@link CKEDITOR#TRISTATE_OFF}.
  266. */