LDAPAccountManager/lam/templates/lib/extra/ckeditor/plugins/maximize/plugin.js

315 lines
12 KiB
JavaScript
Raw Normal View History

2014-04-11 20:07:18 +00:00
/**
2018-06-17 16:07:19 +00:00
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
2014-04-11 20:07:18 +00:00
*/
( function() {
function protectFormStyles( formElement ) {
if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
return [];
var hijackRecord = [],
hijackNames = [ 'style', 'className' ];
for ( var i = 0; i < hijackNames.length; i++ ) {
var name = hijackNames[ i ];
var $node = formElement.$.elements.namedItem( name );
if ( $node ) {
var hijackNode = new CKEDITOR.dom.element( $node );
hijackRecord.push( [ hijackNode, hijackNode.nextSibling ] );
hijackNode.remove();
}
}
return hijackRecord;
}
function restoreFormStyles( formElement, hijackRecord ) {
if ( !formElement || formElement.type != CKEDITOR.NODE_ELEMENT || formElement.getName() != 'form' )
return;
if ( hijackRecord.length > 0 ) {
for ( var i = hijackRecord.length - 1; i >= 0; i-- ) {
var node = hijackRecord[ i ][ 0 ];
var sibling = hijackRecord[ i ][ 1 ];
if ( sibling )
node.insertBefore( sibling );
else
node.appendTo( formElement );
}
}
}
function saveStyles( element, isInsideEditor ) {
var data = protectFormStyles( element );
var retval = {};
var $element = element.$;
if ( !isInsideEditor ) {
retval[ 'class' ] = $element.className || '';
$element.className = '';
}
retval.inline = $element.style.cssText || '';
2018-06-17 16:07:19 +00:00
if ( !isInsideEditor ) // Reset any external styles that might interfere. (https://dev.ckeditor.com/ticket/2474)
2014-04-11 20:07:18 +00:00
$element.style.cssText = 'position: static; overflow: visible';
restoreFormStyles( data );
return retval;
}
function restoreStyles( element, savedStyles ) {
var data = protectFormStyles( element );
var $element = element.$;
if ( 'class' in savedStyles )
$element.className = savedStyles[ 'class' ];
if ( 'inline' in savedStyles )
$element.style.cssText = savedStyles.inline;
restoreFormStyles( data );
}
function refreshCursor( editor ) {
if ( editor.editable().isInline() )
return;
2018-06-17 16:07:19 +00:00
// Refresh all editor instances on the page (https://dev.ckeditor.com/ticket/5724).
2014-04-11 20:07:18 +00:00
var all = CKEDITOR.instances;
for ( var i in all ) {
var one = all[ i ];
if ( one.mode == 'wysiwyg' && !one.readOnly ) {
var body = one.document.getBody();
// Refresh 'contentEditable' otherwise
2018-06-17 16:07:19 +00:00
// DOM lifting breaks design mode. (https://dev.ckeditor.com/ticket/5560)
2014-04-11 20:07:18 +00:00
body.setAttribute( 'contentEditable', false );
body.setAttribute( 'contentEditable', true );
}
}
if ( editor.editable().hasFocus ) {
editor.toolbox.focus();
editor.focus();
}
}
CKEDITOR.plugins.add( 'maximize', {
2018-06-17 16:07:19 +00:00
// jscs:disable maximumLineLength
lang: 'af,ar,az,bg,bn,bs,ca,cs,cy,da,de,de-ch,el,en,en-au,en-ca,en-gb,eo,es,es-mx,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,oc,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%
// jscs:enable maximumLineLength
2014-04-11 20:07:18 +00:00
icons: 'maximize', // %REMOVE_LINE_CORE%
hidpi: true, // %REMOVE_LINE_CORE%
init: function( editor ) {
// Maximize plugin isn't available in inline mode yet.
if ( editor.elementMode == CKEDITOR.ELEMENT_MODE_INLINE )
return;
var lang = editor.lang;
var mainDocument = CKEDITOR.document,
mainWindow = mainDocument.getWindow();
// Saved selection and scroll position for the editing area.
var savedSelection, savedScroll;
// Saved scroll position for the outer window.
var outerScroll;
// Saved resize handler function.
function resizeHandler() {
var viewPaneSize = mainWindow.getViewPaneSize();
editor.resize( viewPaneSize.width, viewPaneSize.height, null, true );
}
// Retain state after mode switches.
var savedState = CKEDITOR.TRISTATE_OFF;
editor.addCommand( 'maximize', {
2018-06-17 16:07:19 +00:00
// Disabled on iOS (https://dev.ckeditor.com/ticket/8307).
2014-04-11 20:07:18 +00:00
modes: { wysiwyg: !CKEDITOR.env.iOS, source: !CKEDITOR.env.iOS },
readOnly: 1,
editorFocus: false,
exec: function() {
2018-06-17 16:07:19 +00:00
var container = editor.container.getFirst( function( node ) {
return node.type == CKEDITOR.NODE_ELEMENT && node.hasClass( 'cke_inner' );
} );
2014-04-11 20:07:18 +00:00
var contents = editor.ui.space( 'contents' );
// Save current selection and scroll position in editing area.
if ( editor.mode == 'wysiwyg' ) {
var selection = editor.getSelection();
savedSelection = selection && selection.getRanges();
savedScroll = mainWindow.getScrollPosition();
} else {
var $textarea = editor.editable().$;
savedSelection = !CKEDITOR.env.ie && [ $textarea.selectionStart, $textarea.selectionEnd ];
savedScroll = [ $textarea.scrollLeft, $textarea.scrollTop ];
}
2018-06-17 16:07:19 +00:00
// Go fullscreen if the state is off.
if ( this.state == CKEDITOR.TRISTATE_OFF ) {
2014-04-11 20:07:18 +00:00
// Add event handler for resizing.
mainWindow.on( 'resize', resizeHandler );
// Save the scroll bar position.
outerScroll = mainWindow.getScrollPosition();
// Save and reset the styles for the entire node tree.
var currentNode = editor.container;
while ( ( currentNode = currentNode.getParent() ) ) {
currentNode.setCustomData( 'maximize_saved_styles', saveStyles( currentNode ) );
// Show under floatpanels (-1) and context menu (-2).
currentNode.setStyle( 'z-index', editor.config.baseFloatZIndex - 5 );
}
contents.setCustomData( 'maximize_saved_styles', saveStyles( contents, true ) );
container.setCustomData( 'maximize_saved_styles', saveStyles( container, true ) );
// Hide scroll bars.
var styles = {
2018-06-17 16:07:19 +00:00
overflow: CKEDITOR.env.webkit ? '' : 'hidden', // https://dev.ckeditor.com/ticket/6896
2014-04-11 20:07:18 +00:00
width: 0,
height: 0
};
mainDocument.getDocumentElement().setStyles( styles );
!CKEDITOR.env.gecko && mainDocument.getDocumentElement().setStyle( 'position', 'fixed' );
!( CKEDITOR.env.gecko && CKEDITOR.env.quirks ) && mainDocument.getBody().setStyles( styles );
2018-06-17 16:07:19 +00:00
// Scroll to the top left (IE needs some time for it - https://dev.ckeditor.com/ticket/4923).
2014-04-11 20:07:18 +00:00
CKEDITOR.env.ie ? setTimeout( function() {
mainWindow.$.scrollTo( 0, 0 );
}, 0 ) : mainWindow.$.scrollTo( 0, 0 );
// Resize and move to top left.
2018-06-17 16:07:19 +00:00
// Special treatment for FF Quirks (https://dev.ckeditor.com/ticket/7284)
2014-04-11 20:07:18 +00:00
container.setStyle( 'position', CKEDITOR.env.gecko && CKEDITOR.env.quirks ? 'fixed' : 'absolute' );
2018-06-17 16:07:19 +00:00
container.$.offsetLeft; // SAFARI BUG: See https://dev.ckeditor.com/ticket/2066.
2014-04-11 20:07:18 +00:00
container.setStyles( {
// Show under floatpanels (-1) and context menu (-2).
'z-index': editor.config.baseFloatZIndex - 5,
left: '0px',
top: '0px'
} );
2018-06-17 16:07:19 +00:00
// Add cke_maximized class before resize handle since that will change things sizes (https://dev.ckeditor.com/ticket/5580)
2014-04-11 20:07:18 +00:00
container.addClass( 'cke_maximized' );
resizeHandler();
2018-06-17 16:07:19 +00:00
// Still not top left? Fix it. (Bug https://dev.ckeditor.com/ticket/174)
2014-04-11 20:07:18 +00:00
var offset = container.getDocumentPosition();
container.setStyles( {
left: ( -1 * offset.x ) + 'px',
top: ( -1 * offset.y ) + 'px'
} );
2018-06-17 16:07:19 +00:00
// Fixing positioning editor chrome in Firefox break design mode. (https://dev.ckeditor.com/ticket/5149)
2014-04-11 20:07:18 +00:00
CKEDITOR.env.gecko && refreshCursor( editor );
2018-06-17 16:07:19 +00:00
}
// Restore from fullscreen if the state is on.
else if ( this.state == CKEDITOR.TRISTATE_ON ) {
2014-04-11 20:07:18 +00:00
// Remove event handler for resizing.
mainWindow.removeListener( 'resize', resizeHandler );
// Restore CSS styles for the entire node tree.
var editorElements = [ contents, container ];
for ( var i = 0; i < editorElements.length; i++ ) {
restoreStyles( editorElements[ i ], editorElements[ i ].getCustomData( 'maximize_saved_styles' ) );
editorElements[ i ].removeCustomData( 'maximize_saved_styles' );
}
currentNode = editor.container;
while ( ( currentNode = currentNode.getParent() ) ) {
restoreStyles( currentNode, currentNode.getCustomData( 'maximize_saved_styles' ) );
currentNode.removeCustomData( 'maximize_saved_styles' );
}
// Restore the window scroll position.
CKEDITOR.env.ie ? setTimeout( function() {
mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
}, 0 ) : mainWindow.$.scrollTo( outerScroll.x, outerScroll.y );
// Remove cke_maximized class.
container.removeClass( 'cke_maximized' );
2018-06-17 16:07:19 +00:00
// Webkit requires a re-layout on editor chrome. (https://dev.ckeditor.com/ticket/6695)
2014-04-11 20:07:18 +00:00
if ( CKEDITOR.env.webkit ) {
container.setStyle( 'display', 'inline' );
setTimeout( function() {
container.setStyle( 'display', 'block' );
}, 0 );
}
// Emit a resize event, because this time the size is modified in
// restoreStyles.
2018-06-17 16:07:19 +00:00
editor.fire( 'resize', {
outerHeight: editor.container.$.offsetHeight,
contentsHeight: contents.$.offsetHeight,
outerWidth: editor.container.$.offsetWidth
} );
2014-04-11 20:07:18 +00:00
}
this.toggleState();
// Toggle button label.
var button = this.uiItems[ 0 ];
2018-06-17 16:07:19 +00:00
// Only try to change the button if it exists (https://dev.ckeditor.com/ticket/6166)
2014-04-11 20:07:18 +00:00
if ( button ) {
var label = ( this.state == CKEDITOR.TRISTATE_OFF ) ? lang.maximize.maximize : lang.maximize.minimize;
var buttonNode = CKEDITOR.document.getById( button._.id );
buttonNode.getChild( 1 ).setHtml( label );
buttonNode.setAttribute( 'title', label );
2018-06-17 16:07:19 +00:00
buttonNode.setAttribute( 'href', 'javascript:void("' + label + '");' ); // jshint ignore:line
2014-04-11 20:07:18 +00:00
}
// Restore selection and scroll position in editing area.
if ( editor.mode == 'wysiwyg' ) {
if ( savedSelection ) {
2018-06-17 16:07:19 +00:00
// Fixing positioning editor chrome in Firefox break design mode. (https://dev.ckeditor.com/ticket/5149)
2014-04-11 20:07:18 +00:00
CKEDITOR.env.gecko && refreshCursor( editor );
editor.getSelection().selectRanges( savedSelection );
var element = editor.getSelection().getStartElement();
element && element.scrollIntoView( true );
2018-06-17 16:07:19 +00:00
} else {
2014-04-11 20:07:18 +00:00
mainWindow.$.scrollTo( savedScroll.x, savedScroll.y );
2018-06-17 16:07:19 +00:00
}
2014-04-11 20:07:18 +00:00
} else {
if ( savedSelection ) {
$textarea.selectionStart = savedSelection[ 0 ];
$textarea.selectionEnd = savedSelection[ 1 ];
}
$textarea.scrollLeft = savedScroll[ 0 ];
$textarea.scrollTop = savedScroll[ 1 ];
}
savedSelection = savedScroll = null;
savedState = this.state;
editor.fire( 'maximize', this.state );
},
canUndo: false
} );
editor.ui.addButton && editor.ui.addButton( 'Maximize', {
label: lang.maximize.maximize,
command: 'maximize',
toolbar: 'tools,10'
} );
2018-06-17 16:07:19 +00:00
// Restore the command state after mode change, unless it has been changed to disabled (https://dev.ckeditor.com/ticket/6467)
2014-04-11 20:07:18 +00:00
editor.on( 'mode', function() {
var command = editor.getCommand( 'maximize' );
command.setState( command.state == CKEDITOR.TRISTATE_DISABLED ? CKEDITOR.TRISTATE_DISABLED : savedState );
}, null, null, 100 );
}
} );
} )();
/**
* Event fired when the maximize command is called.
* It also indicates whether an editor is maximized or not.
*
* @event maximize
* @member CKEDITOR.editor
* @param {CKEDITOR.editor} editor This editor instance.
* @param {Number} data Current state of the command. See {@link CKEDITOR#TRISTATE_ON} and {@link CKEDITOR#TRISTATE_OFF}.
*/