/* global getUserSetting, setUserSetting */
( function( tinymce ) {
// Set the minimum value for the modals z-index higher than #wpadminbar (100000)
if ( ! tinymce.ui.FloatPanel.zIndex || tinymce.ui.FloatPanel.zIndex < 100100 ) {
tinymce.ui.FloatPanel.zIndex = 100100;
}
tinymce.PluginManager.add( 'wordpress', function( editor ) {
var wpAdvButton, style,
DOM = tinymce.DOM,
each = tinymce.each,
__ = editor.editorManager.i18n.translate,
$ = window.jQuery,
wp = window.wp,
hasWpautop = ( wp && wp.editor && wp.editor.autop && editor.getParam( 'wpautop', true ) );
if ( $ ) {
$( document ).triggerHandler( 'tinymce-editor-setup', [ editor ] );
}
function toggleToolbars( state ) {
var iframe, initial, toolbars,
pixels = 0;
initial = ( state === 'hide' );
if ( editor.theme.panel ) {
toolbars = editor.theme.panel.find('.toolbar:not(.menubar)');
}
if ( ! toolbars || toolbars.length < 2 || ( state === 'hide' && ! toolbars[1].visible() ) ) {
return;
}
if ( ! state && toolbars[1].visible() ) {
state = 'hide';
}
each( toolbars, function( toolbar, i ) {
if ( i > 0 ) {
if ( state === 'hide' ) {
toolbar.hide();
pixels += 30;
} else {
toolbar.show();
pixels -= 30;
}
}
});
if ( pixels && ! initial ) {
// Resize iframe, not needed in iOS
if ( ! tinymce.Env.iOS ) {
iframe = editor.getContentAreaContainer().firstChild;
DOM.setStyle( iframe, 'height', iframe.clientHeight + pixels );
}
if ( state === 'hide' ) {
setUserSetting('hidetb', '0');
wpAdvButton && wpAdvButton.active( false );
} else {
setUserSetting('hidetb', '1');
wpAdvButton && wpAdvButton.active( true );
}
}
editor.fire( 'wp-toolbar-toggle' );
}
// Add the kitchen sink button :)
editor.addButton( 'wp_adv', {
tooltip: 'Toolbar Toggle',
cmd: 'WP_Adv',
onPostRender: function() {
wpAdvButton = this;
wpAdvButton.active( getUserSetting( 'hidetb' ) === '1' ? true : false );
}
});
// Hide the toolbars after loading
editor.on( 'PostRender', function() {
if ( editor.getParam( 'wordpress_adv_hidden', true ) && getUserSetting( 'hidetb', '0' ) === '0' ) {
toggleToolbars( 'hide' );
}
});
editor.addCommand( 'WP_Adv', function() {
toggleToolbars();
});
editor.on( 'focus', function() {
window.wpActiveEditor = editor.id;
});
editor.on( 'BeforeSetContent', function( event ) {
var title;
if ( event.content ) {
if ( event.content.indexOf( '/g, function( match, moretext ) {
return '';
});
}
if ( event.content.indexOf( '' ) !== -1 ) {
title = __( 'Page break' );
event.content = event.content.replace( //g,
'' );
}
if ( event.load && event.format !== 'raw' && hasWpautop ) {
event.content = wp.editor.autop( event.content );
}
if ( event.content.indexOf( '