/* Black Studio TinyMCE Widget - JS */ /* global bstw_data, tinymce, tinyMCEPreInit, isRtl */ (function( $ ) { // Returns bstw instance given the textarea ID or any jQuery object inside the widget object function bstw( arg ) { var id = null; // ID initialization if ( typeof arg === 'string' ) { id = arg; } // jQuery object initialization else if ( typeof arg === 'object' && arg instanceof jQuery ) { id = $( 'textarea[id^=widget-black-studio-tinymce]', arg.closest( bstw_data.container_selectors ) ).attr( 'id' ); } // Create and return instance return { // Activate visual editor activate: function () { $( '#' + id ).addClass( 'mceEditor' ); if ( typeof tinymce === 'object' && typeof tinymce.execCommand === 'function' ) { this.deactivate(); tinyMCEPreInit.mceInit[id] = tinyMCEPreInit.mceInit['black-studio-tinymce-widget']; tinyMCEPreInit.mceInit[id].selector = '#' + id; try { // Instantiate new TinyMCE editor tinymce.init( tinymce.extend( {}, tinyMCEPreInit.mceInit['black-studio-tinymce-widget'], tinyMCEPreInit.mceInit[id] ) ); tinymce.execCommand( 'mceAddControl', false, id ); } catch( e ) { window.alert( e ); } // Real time preview (Theme customizer) if ( this.is_tinymce_active() ) { if ( typeof tinymce.get( id ).on === 'function' ) { tinymce.get( id ).on( 'keyup change', function() { var content = tinymce.get( id ).getContent(); $( '#' + id ).val( content ).change(); }); } } } return this; }, // Deactivate visual editor deactivate: function() { if ( typeof tinymce === 'object' && typeof tinymce.execCommand === 'function' ) { if ( this.is_tinymce_active() ) { var content = tinymce.get( id ).getContent(); // tinymce.execCommand('mceRemoveControl', false, id); tinymce.get( id ).remove(); $( '#' + id ).val( content ); } } return this; }, // Activate editor deferred (after widget opening) activate_after_open: function() { // Activate only if type is set to visual if ( this.get_mode() === 'visual' ) { // If textarea is visible and animation/ajax has completed (or in accessibility mode) then trigger a click to Visual button and enable the editor if ( $('div.widget:has(#' + id + ') :animated' ).size() === 0 && ! this.is_tinymce_active() && this.is_textearea_visible() ) { this.set_mode( 'visual' ); } // Otherwise wait and retry later (animation ongoing) else if ( ! this.is_tinymce_active() ) { setTimeout(function() { bstw( id ).activate_after_open(); }, 100 ); } // If editor instance is already existing (i.e. dragged from another sidebar) just activate it else { this.set_mode( 'visual' ); } } return this; }, // Activate editor deferred (after ajax requests) activate_after_ajax: function () { // Activate only if type is set to visual if ( this.get_mode() === 'visual' ) { // If textarea is visible and animation/ajax has completed then trigger a click to Visual button and enable the editor if ( $.active === 0 && ! this.is_tinymce_active() && this.is_textearea_visible() ) { this.set_mode( 'visual' ); } // Otherwise wait and retry later (animation ongoing) else if ( this.is_widget_inside_visible() && ! this.is_tinymce_active() ) { setTimeout(function() { bstw( id ).activate_after_ajax(); }, 100 ); } } return this; }, // Get the div.widget jQuery object containing the instance get_widget: function() { return $( '#' + id ).closest( 'div.widget' ); }, // Get the div.widget-inside jQuery object containing the instance get_widget_inside: function() { return $( '#' + id ).closest( 'div.widget-inside' ); }, // Get the div.wp-editor-wrap jQuery object containing the instance get_editor_wrap: function() { return $( '#' + id ).closest( 'div.wp-editor-wrap' ); }, // Get the textarea jQuery object related to the instance get_textarea: function() { return $( '#' + id ); }, // Get the textarea ID related to the instance get_id: function() { return id; }, // Get the tinymce instance related to the instance get_tinymce: function() { return tinymce.get( id ); }, // Get the current editor mode ( visual / html ) get_mode: function() { return $( 'input[id^=widget-black-studio-tinymce][id$=type]', this.get_widget_inside() ).val(); }, // Set editor mode ( visual / html ) set_mode: function( value ) { if ( value === 'visual' ) { this.get_editor_wrap().removeClass( 'html-active' ).addClass( 'tmce-active' ); this.activate(); } if ( value === 'html' ) { this.get_editor_wrap().removeClass( 'tmce-active' ).addClass( 'html-active' ); this.deactivate(); } $( 'input[id^=widget-black-studio-tinymce][id$=type]', this.get_widget_inside() ).val( value ); return this; }, // Check if the connected tinymce instance is active is_tinymce_active: function() { return typeof tinymce === 'object' && typeof tinymce.get( id ) === 'object' && tinymce.get( id ) !== null; }, // Check if the textarea is visible is_textearea_visible: function() { return $( '#' + id ).is( ':visible' ); }, // Check if the widget inside is visible is_widget_inside_visible: function() { return $( ' div.widget-inside:has(#' + id + ')' ).is( ':visible' ); }, // Check for widgets with duplicate ids check_duplicates: function() { if ( $( '[name="' + this.get_textarea().attr('name') + '"]' ).size() > 1) { if ( $( 'div.error', this.get_widget_inside() ).length === 0 ) { this.get_widget_inside().prepend('