4 * A lightweight publish/subscribe implementation.
7 var PubSub, fullscreen, wptitlehint;
13 PubSub.prototype.subscribe = function( topic, callback ) {
14 if ( ! this.topics[ topic ] )
15 this.topics[ topic ] = [];
17 this.topics[ topic ].push( callback );
21 PubSub.prototype.unsubscribe = function( topic, callback ) {
23 topics = this.topics[ topic ];
26 return callback || [];
28 // Clear matching callbacks
30 for ( i = 0, l = topics.length; i < l; i++ ) {
31 if ( callback == topics[i] )
32 topics.splice( i, 1 );
36 // Clear all callbacks
38 this.topics[ topic ] = [];
43 PubSub.prototype.publish = function( topic, args ) {
45 topics = this.topics[ topic ];
52 for ( i = 0, l = topics.length; i < l; i++ ) {
53 broken = ( topics[i].apply( null, args ) === false || broken );
59 * Distraction Free Writing
62 * Access the API globally using the fullscreen variable.
66 var api, ps, bounder, s;
68 // Initialize the fullscreen/api object
69 fullscreen = api = {};
71 // Create the PubSub (publish/subscribe) interface.
72 ps = api.pubsub = new PubSub();
76 s = api.settings = { // Settings
79 editor_id : 'content',
88 * Creates a function that publishes start/stop topics.
89 * Used to throttle events.
91 bounder = api.bounder = function( start, stop, delay, e ) {
94 delay = delay || 1250;
97 y = e.pageY || e.clientY || e.offsetY;
98 top = $(document).scrollTop();
100 if ( !e.isDefaultPrevented ) // test if e ic jQuery normalized
112 setTimeout( function() {
117 clearTimeout( s.timer );
126 s.timer = setTimeout( timed, delay );
132 * Turns fullscreen on.
134 * @param string mode Optional. Switch to the given mode before opening.
136 api.on = function() {
140 // Settings can be added or changed by defining "wp_fullscreen_settings" JS object.
141 if ( typeof(wp_fullscreen_settings) == 'object' )
142 $.extend( s, wp_fullscreen_settings );
144 s.editor_id = wpActiveEditor || 'content';
146 if ( $('input#title').length && s.editor_id == 'content' )
147 s.title_id = 'title';
148 else if ( $('input#' + s.editor_id + '-title').length ) // the title input field should have [editor_id]-title HTML ID to be auto detected
149 s.title_id = s.editor_id + '-title';
151 $('#wp-fullscreen-title, #wp-fullscreen-title-prompt-text').hide();
153 s.mode = $('#' + s.editor_id).is(':hidden') ? 'tinymce' : 'html';
154 s.qt_canvas = $('#' + s.editor_id).get(0);
159 s.is_mce_on = s.has_tinymce && typeof( tinyMCE.get(s.editor_id) ) != 'undefined';
161 api.ui.fade( 'show', 'showing', 'shown' );
167 * Turns fullscreen off.
169 api.off = function() {
173 api.ui.fade( 'hide', 'hiding', 'hidden' );
179 * @return string - The current mode.
181 * @param string to - The fullscreen mode to switch to.
183 * @eventparam string to - The new mode.
184 * @eventparam string from - The old mode.
186 api.switchmode = function( to ) {
189 if ( ! to || ! s.visible || ! s.has_tinymce )
192 // Don't switch if the mode is the same.
196 ps.publish( 'switchMode', [ from, to ] );
198 ps.publish( 'switchedMode', [ from, to ] );
207 api.save = function() {
208 var hidden = $('#hiddenaction'), old = hidden.val(), spinner = $('#wp-fullscreen-save .spinner'),
209 message = $('#wp-fullscreen-save span');
214 hidden.val('wp-fullscreen-save-post');
216 $.post( ajaxurl, $('form#post').serialize(), function(r){
220 setTimeout( function(){
221 message.fadeOut(1000);
225 $('#wp-fullscreen-save input').attr( 'title', r.last_edited );
232 api.savecontent = function() {
236 $('#' + s.title_id).val( $('#wp-fullscreen-title').val() );
238 if ( s.mode === 'tinymce' && (ed = tinyMCE.get('wp_mce_fullscreen')) ) {
241 content = $('#wp_mce_fullscreen').val();
244 $('#' + s.editor_id).val( content );
245 $(document).triggerHandler('wpcountwords', [ content ]);
248 set_title_hint = function( title ) {
249 if ( ! title.val().length )
250 title.siblings('label').css( 'visibility', '' );
252 title.siblings('label').css( 'visibility', 'hidden' );
255 api.dfw_width = function(n) {
256 var el = $('#wp-fullscreen-wrap'), w = el.width();
258 if ( !n ) { // reset to theme width
259 el.width( $('#wp-fullscreen-central-toolbar').width() );
260 deleteUserSetting('dfw_width');
266 if ( w < 200 || w > 1200 ) // sanity check
270 setUserSetting('dfw_width', w);
273 ps.subscribe( 'showToolbar', function() {
274 s.toolbars.removeClass('fade-1000').addClass('fade-300');
275 api.fade.In( s.toolbars, 300, function(){ ps.publish('toolbarShown'); }, true );
276 $('#wp-fullscreen-body').addClass('wp-fullscreen-focus');
277 s.toolbar_shown = true;
280 ps.subscribe( 'hideToolbar', function() {
281 s.toolbars.removeClass('fade-300').addClass('fade-1000');
282 api.fade.Out( s.toolbars, 1000, function(){ ps.publish('toolbarHidden'); }, true );
283 $('#wp-fullscreen-body').removeClass('wp-fullscreen-focus');
286 ps.subscribe( 'toolbarShown', function() {
287 s.toolbars.removeClass('fade-300');
290 ps.subscribe( 'toolbarHidden', function() {
291 s.toolbars.removeClass('fade-1000');
292 s.toolbar_shown = false;
295 ps.subscribe( 'show', function() { // This event occurs before the overlay blocks the UI.
299 title = $('#wp-fullscreen-title').val( $('#' + s.title_id).val() );
300 set_title_hint( title );
303 $('#wp-fullscreen-save input').attr( 'title', $('#last-edit').text() );
305 s.textarea_obj.value = s.qt_canvas.value;
307 if ( s.has_tinymce && s.mode === 'tinymce' )
308 tinyMCE.execCommand('wpFullScreenInit');
310 s.orig_y = $(window).scrollTop();
313 ps.subscribe( 'showing', function() { // This event occurs while the DFW overlay blocks the UI.
314 $( document.body ).addClass( 'fullscreen-active' );
315 api.refresh_buttons();
317 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
318 bounder( 'showToolbar', 'hideToolbar', 2000 );
321 setTimeout( api.resize_textarea, 200 );
323 // scroll to top so the user is not disoriented
326 // needed it for IE7 and compat mode
327 $('#wpadminbar').hide();
330 ps.subscribe( 'shown', function() { // This event occurs after the DFW overlay is shown
335 // init the standard TinyMCE instance if missing
336 if ( s.has_tinymce && ! s.is_mce_on ) {
338 interim_init = function(mce, ed) {
339 var el = ed.getElement(), old_val = el.value, settings = tinyMCEPreInit.mceInit[s.editor_id];
341 if ( settings && settings.wpautop && typeof(switchEditors) != 'undefined' )
342 el.value = switchEditors.wpautop( el.value );
344 ed.onInit.add(function(ed) {
346 ed.getElement().value = old_val;
347 tinymce.onAddEditor.remove(interim_init);
351 tinymce.onAddEditor.add(interim_init);
352 tinyMCE.init(tinyMCEPreInit.mceInit[s.editor_id]);
357 wpActiveEditor = 'wp_mce_fullscreen';
360 ps.subscribe( 'hide', function() { // This event occurs before the overlay blocks DFW.
361 var htmled_is_hidden = $('#' + s.editor_id).is(':hidden');
362 // Make sure the correct editor is displaying.
363 if ( s.has_tinymce && s.mode === 'tinymce' && !htmled_is_hidden ) {
364 switchEditors.go(s.editor_id, 'tmce');
365 } else if ( s.mode === 'html' && htmled_is_hidden ) {
366 switchEditors.go(s.editor_id, 'html');
369 // Save content must be after switchEditors or content will be overwritten. See #17229.
372 $( document ).unbind( '.fullscreen' );
373 $(s.textarea_obj).unbind('.grow');
375 if ( s.has_tinymce && s.mode === 'tinymce' )
376 tinyMCE.execCommand('wpFullScreenSave');
379 set_title_hint( $('#' + s.title_id) );
381 s.qt_canvas.value = s.textarea_obj.value;
384 ps.subscribe( 'hiding', function() { // This event occurs while the overlay blocks the DFW UI.
386 $( document.body ).removeClass( 'fullscreen-active' );
387 scrollTo(0, s.orig_y);
388 $('#wpadminbar').show();
391 ps.subscribe( 'hidden', function() { // This event occurs after DFW is removed.
393 $('#wp_mce_fullscreen, #wp-fullscreen-title').removeAttr('style');
395 if ( s.has_tinymce && s.is_mce_on )
396 tinyMCE.execCommand('wpFullScreenClose');
398 s.textarea_obj.value = '';
400 wpActiveEditor = s.editor_id;
403 ps.subscribe( 'switchMode', function( from, to ) {
406 if ( !s.has_tinymce || !s.is_mce_on )
409 ed = tinyMCE.get('wp_mce_fullscreen');
411 if ( from === 'html' && to === 'tinymce' ) {
413 if ( tinyMCE.get(s.editor_id).getParam('wpautop') && typeof(switchEditors) != 'undefined' )
414 s.textarea_obj.value = switchEditors.wpautop( s.textarea_obj.value );
416 if ( 'undefined' == typeof(ed) )
417 tinyMCE.execCommand('wpFullScreenInit');
421 } else if ( from === 'tinymce' && to === 'html' ) {
427 ps.subscribe( 'switchedMode', function( from, to ) {
428 api.refresh_buttons(true);
431 setTimeout( api.resize_textarea, 200 );
438 if ( s.has_tinymce && 'tinymce' === s.mode )
439 tinyMCE.execCommand('Bold');
443 if ( s.has_tinymce && 'tinymce' === s.mode )
444 tinyMCE.execCommand('Italic');
447 api.ul = function() {
448 if ( s.has_tinymce && 'tinymce' === s.mode )
449 tinyMCE.execCommand('InsertUnorderedList');
452 api.ol = function() {
453 if ( s.has_tinymce && 'tinymce' === s.mode )
454 tinyMCE.execCommand('InsertOrderedList');
457 api.link = function() {
458 if ( s.has_tinymce && 'tinymce' === s.mode )
459 tinyMCE.execCommand('WP_Link');
464 api.unlink = function() {
465 if ( s.has_tinymce && 'tinymce' === s.mode )
466 tinyMCE.execCommand('unlink');
469 api.atd = function() {
470 if ( s.has_tinymce && 'tinymce' === s.mode )
471 tinyMCE.execCommand('mceWritingImprovementTool');
474 api.help = function() {
475 if ( s.has_tinymce && 'tinymce' === s.mode )
476 tinyMCE.execCommand('WP_Help');
479 api.blockquote = function() {
480 if ( s.has_tinymce && 'tinymce' === s.mode )
481 tinyMCE.execCommand('mceBlockQuote');
484 api.medialib = function() {
485 if ( typeof wp !== 'undefined' && wp.media && wp.media.editor )
486 wp.media.editor.open(s.editor_id);
489 api.refresh_buttons = function( fade ) {
490 fade = fade || false;
492 if ( s.mode === 'html' ) {
493 $('#wp-fullscreen-mode-bar').removeClass('wp-tmce-mode').addClass('wp-html-mode');
496 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
497 $(this).addClass('wp-html-mode').fadeIn( 150 );
500 $('#wp-fullscreen-button-bar').addClass('wp-html-mode');
502 } else if ( s.mode === 'tinymce' ) {
503 $('#wp-fullscreen-mode-bar').removeClass('wp-html-mode').addClass('wp-tmce-mode');
506 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
507 $(this).removeClass('wp-html-mode').fadeIn( 150 );
510 $('#wp-fullscreen-button-bar').removeClass('wp-html-mode');
517 * Used for transitioning between states.
521 var topbar = $('#fullscreen-topbar'), txtarea = $('#wp_mce_fullscreen'), last = 0;
523 s.toolbars = topbar.add( $('#wp-fullscreen-status') );
524 s.element = $('#fullscreen-fader');
525 s.textarea_obj = txtarea[0];
526 s.has_tinymce = typeof(tinymce) != 'undefined';
528 if ( !s.has_tinymce )
529 $('#wp-fullscreen-mode-bar').hide();
531 if ( wptitlehint && $('#wp-fullscreen-title').length )
532 wptitlehint('wp-fullscreen-title');
534 $(document).keyup(function(e){
535 var c = e.keyCode || e.charCode, a, data;
537 if ( !fullscreen.settings.visible )
540 if ( navigator.platform && navigator.platform.indexOf('Mac') != -1 )
541 a = e.ctrlKey; // Ctrl key for Mac
543 a = e.altKey; // Alt key for Win & Linux
545 if ( 27 == c ) { // Esc
550 condition: function(){
551 if ( $('#TB_window').is(':visible') || $('.wp-dialog').is(':visible') )
557 if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [data] ) )
561 if ( a && (61 == c || 107 == c || 187 == c) ) // +
564 if ( a && (45 == c || 109 == c || 189 == c) ) // -
567 if ( a && 48 == c ) // 0
573 // word count in Text mode
574 if ( typeof(wpWordCount) != 'undefined' ) {
576 txtarea.keyup( function(e) {
577 var k = e.keyCode || e.charCode;
582 if ( 13 == k || 8 == last || 46 == last )
583 $(document).triggerHandler('wpcountwords', [ txtarea.val() ]);
590 topbar.mouseenter(function(e){
591 s.toolbars.addClass('fullscreen-make-sticky');
592 $( document ).unbind( '.fullscreen' );
593 clearTimeout( s.timer );
595 }).mouseleave(function(e){
596 s.toolbars.removeClass('fullscreen-make-sticky');
599 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
603 fade: function( before, during, after ) {
607 // If any callback bound to before returns false, bail.
608 if ( before && ! ps.publish( before ) )
611 api.fade.In( s.element, 600, function() {
613 ps.publish( during );
615 api.fade.Out( s.element, 600, function() {
624 transitionend: 'transitionend webkitTransitionEnd oTransitionEnd',
626 // Sensitivity to allow browsers to render the blank element before animating.
629 In: function( element, speed, callback, stop ) {
631 callback = callback || $.noop;
632 speed = speed || 400;
633 stop = stop || false;
635 if ( api.fade.transitions ) {
636 if ( element.is(':visible') ) {
637 element.addClass( 'fade-trigger' );
642 element.first().one( this.transitionend, function() {
645 setTimeout( function() { element.addClass( 'fade-trigger' ); }, this.sensitivity );
650 element.css( 'opacity', 1 );
651 element.first().fadeIn( speed, callback );
653 if ( element.length > 1 )
654 element.not(':first').fadeIn( speed );
660 Out: function( element, speed, callback, stop ) {
662 callback = callback || $.noop;
663 speed = speed || 400;
664 stop = stop || false;
666 if ( ! element.is(':visible') )
669 if ( api.fade.transitions ) {
670 element.first().one( api.fade.transitionend, function() {
671 if ( element.hasClass('fade-trigger') )
677 setTimeout( function() { element.removeClass( 'fade-trigger' ); }, this.sensitivity );
682 element.first().fadeOut( speed, callback );
684 if ( element.length > 1 )
685 element.not(':first').fadeOut( speed );
691 transitions: (function() { // Check if the browser supports CSS 3.0 transitions
692 var s = document.documentElement.style;
694 return ( typeof ( s.WebkitTransition ) == 'string' ||
695 typeof ( s.MozTransition ) == 'string' ||
696 typeof ( s.OTransition ) == 'string' ||
697 typeof ( s.transition ) == 'string' );
704 * Automatically updates textarea height.
707 api.bind_resize = function() {
708 $(s.textarea_obj).bind('keypress.grow click.grow paste.grow', function(){
709 setTimeout( api.resize_textarea, 200 );
714 api.resize_textarea = function() {
715 var txt = s.textarea_obj, newheight;
717 newheight = txt.scrollHeight > 300 ? txt.scrollHeight : 300;
719 if ( newheight != api.oldheight ) {
720 txt.style.height = newheight + 'px';
721 api.oldheight = newheight;