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 img'),
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 ( s.has_tinymce && 'tinymce' === s.mode ) {
486 tinyMCE.execCommand('WP_Medialib');
488 var href = $('#wp-' + s.editor_id + '-media-buttons a.thickbox').attr('href') || '';
495 api.refresh_buttons = function( fade ) {
496 fade = fade || false;
498 if ( s.mode === 'html' ) {
499 $('#wp-fullscreen-mode-bar').removeClass('wp-tmce-mode').addClass('wp-html-mode');
502 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
503 $(this).addClass('wp-html-mode').fadeIn( 150 );
506 $('#wp-fullscreen-button-bar').addClass('wp-html-mode');
508 } else if ( s.mode === 'tinymce' ) {
509 $('#wp-fullscreen-mode-bar').removeClass('wp-html-mode').addClass('wp-tmce-mode');
512 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
513 $(this).removeClass('wp-html-mode').fadeIn( 150 );
516 $('#wp-fullscreen-button-bar').removeClass('wp-html-mode');
523 * Used for transitioning between states.
527 var topbar = $('#fullscreen-topbar'), txtarea = $('#wp_mce_fullscreen'), last = 0;
529 s.toolbars = topbar.add( $('#wp-fullscreen-status') );
530 s.element = $('#fullscreen-fader');
531 s.textarea_obj = txtarea[0];
532 s.has_tinymce = typeof(tinymce) != 'undefined';
534 if ( !s.has_tinymce )
535 $('#wp-fullscreen-mode-bar').hide();
537 if ( wptitlehint && $('#wp-fullscreen-title').length )
538 wptitlehint('wp-fullscreen-title');
540 $(document).keyup(function(e){
541 var c = e.keyCode || e.charCode, a, data;
543 if ( !fullscreen.settings.visible )
546 if ( navigator.platform && navigator.platform.indexOf('Mac') != -1 )
547 a = e.ctrlKey; // Ctrl key for Mac
549 a = e.altKey; // Alt key for Win & Linux
551 if ( 27 == c ) { // Esc
556 condition: function(){
557 if ( $('#TB_window').is(':visible') || $('.wp-dialog').is(':visible') )
563 if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [data] ) )
567 if ( a && (61 == c || 107 == c || 187 == c) ) // +
570 if ( a && (45 == c || 109 == c || 189 == c) ) // -
573 if ( a && 48 == c ) // 0
579 // word count in HTML mode
580 if ( typeof(wpWordCount) != 'undefined' ) {
582 txtarea.keyup( function(e) {
583 var k = e.keyCode || e.charCode;
588 if ( 13 == k || 8 == last || 46 == last )
589 $(document).triggerHandler('wpcountwords', [ txtarea.val() ]);
596 topbar.mouseenter(function(e){
597 s.toolbars.addClass('fullscreen-make-sticky');
598 $( document ).unbind( '.fullscreen' );
599 clearTimeout( s.timer );
601 }).mouseleave(function(e){
602 s.toolbars.removeClass('fullscreen-make-sticky');
605 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
609 fade: function( before, during, after ) {
613 // If any callback bound to before returns false, bail.
614 if ( before && ! ps.publish( before ) )
617 api.fade.In( s.element, 600, function() {
619 ps.publish( during );
621 api.fade.Out( s.element, 600, function() {
630 transitionend: 'transitionend webkitTransitionEnd oTransitionEnd',
632 // Sensitivity to allow browsers to render the blank element before animating.
635 In: function( element, speed, callback, stop ) {
637 callback = callback || $.noop;
638 speed = speed || 400;
639 stop = stop || false;
641 if ( api.fade.transitions ) {
642 if ( element.is(':visible') ) {
643 element.addClass( 'fade-trigger' );
648 element.first().one( this.transitionend, function() {
651 setTimeout( function() { element.addClass( 'fade-trigger' ); }, this.sensitivity );
656 element.css( 'opacity', 1 );
657 element.first().fadeIn( speed, callback );
659 if ( element.length > 1 )
660 element.not(':first').fadeIn( speed );
666 Out: function( element, speed, callback, stop ) {
668 callback = callback || $.noop;
669 speed = speed || 400;
670 stop = stop || false;
672 if ( ! element.is(':visible') )
675 if ( api.fade.transitions ) {
676 element.first().one( api.fade.transitionend, function() {
677 if ( element.hasClass('fade-trigger') )
683 setTimeout( function() { element.removeClass( 'fade-trigger' ); }, this.sensitivity );
688 element.first().fadeOut( speed, callback );
690 if ( element.length > 1 )
691 element.not(':first').fadeOut( speed );
697 transitions: (function() { // Check if the browser supports CSS 3.0 transitions
698 var s = document.documentElement.style;
700 return ( typeof ( s.WebkitTransition ) == 'string' ||
701 typeof ( s.MozTransition ) == 'string' ||
702 typeof ( s.OTransition ) == 'string' ||
703 typeof ( s.transition ) == 'string' );
710 * Automatically updates textarea height.
713 api.bind_resize = function() {
714 $(s.textarea_obj).bind('keypress.grow click.grow paste.grow', function(){
715 setTimeout( api.resize_textarea, 200 );
720 api.resize_textarea = function() {
721 var txt = s.textarea_obj, newheight;
723 newheight = txt.scrollHeight > 300 ? txt.scrollHeight : 300;
725 if ( newheight != api.oldheight ) {
726 txt.style.height = newheight + 'px';
727 api.oldheight = newheight;