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 s.mode = $('#' + s.editor_id).is(':hidden') ? 'tinymce' : 'html';
145 s.is_mce_on = s.has_tinymce && typeof( tinyMCE.get(s.editor_id) ) != 'undefined';
147 api.ui.fade( 'show', 'showing', 'shown' );
153 * Turns fullscreen off.
155 api.off = function() {
159 api.ui.fade( 'hide', 'hiding', 'hidden' );
165 * @return string - The current mode.
167 * @param string to - The fullscreen mode to switch to.
169 * @eventparam string to - The new mode.
170 * @eventparam string from - The old mode.
172 api.switchmode = function( to ) {
175 if ( ! to || ! s.visible || ! s.has_tinymce )
178 // Don't switch if the mode is the same.
182 ps.publish( 'switchMode', [ from, to ] );
184 ps.publish( 'switchedMode', [ from, to ] );
193 api.save = function() {
194 var hidden = $('#hiddenaction'), old = hidden.val(), spinner = $('#wp-fullscreen-save img'),
195 message = $('#wp-fullscreen-save span');
200 hidden.val('wp-fullscreen-save-post');
202 $.post( ajaxurl, $('form#post').serialize(), function(r){
206 setTimeout( function(){
207 message.fadeOut(1000);
211 $('#wp-fullscreen-save input').attr( 'title', r.last_edited );
218 api.savecontent = function() {
221 $('#' + s.title_id).val( $('#wp-fullscreen-title').val() );
223 if ( s.mode === 'tinymce' && (ed = tinyMCE.get('wp_mce_fullscreen')) ) {
226 content = $('#wp_mce_fullscreen').val();
229 $('#' + s.editor_id).val( content );
230 $(document).triggerHandler('wpcountwords', [ content ]);
233 set_title_hint = function( title ) {
234 if ( ! title.val().length )
235 title.siblings('label').css( 'visibility', '' );
237 title.siblings('label').css( 'visibility', 'hidden' );
240 api.dfw_width = function(n) {
241 var el = $('#wp-fullscreen-wrap'), w = el.width();
243 if ( !n ) { // reset to theme width
244 el.width( $('#wp-fullscreen-central-toolbar').width() );
245 deleteUserSetting('dfw_width');
251 if ( w < 200 || w > 1200 ) // sanity check
255 setUserSetting('dfw_width', w);
258 ps.subscribe( 'showToolbar', function() {
259 s.toolbars.removeClass('fade-1000').addClass('fade-300');
260 api.fade.In( s.toolbars, 300, function(){ ps.publish('toolbarShown'); }, true );
261 $('#wp-fullscreen-body').addClass('wp-fullscreen-focus');
262 s.toolbar_shown = true;
265 ps.subscribe( 'hideToolbar', function() {
266 s.toolbars.removeClass('fade-300').addClass('fade-1000');
267 api.fade.Out( s.toolbars, 1000, function(){ ps.publish('toolbarHidden'); }, true );
268 $('#wp-fullscreen-body').removeClass('wp-fullscreen-focus');
271 ps.subscribe( 'toolbarShown', function() {
272 s.toolbars.removeClass('fade-300');
275 ps.subscribe( 'toolbarHidden', function() {
276 s.toolbars.removeClass('fade-1000');
277 s.toolbar_shown = false;
280 ps.subscribe( 'show', function() { // This event occurs before the overlay blocks the UI.
281 var title = $('#wp-fullscreen-title').val( $('#' + s.title_id).val() );
283 set_title_hint( title );
284 $('#wp-fullscreen-save input').attr( 'title', $('#last-edit').text() );
286 s.textarea_obj.value = edCanvas.value;
288 if ( s.has_tinymce && s.mode === 'tinymce' )
289 tinyMCE.execCommand('wpFullScreenInit');
291 s._edCanvas = edCanvas;
292 edCanvas = s.textarea_obj;
294 s.orig_y = $(window).scrollTop();
297 ps.subscribe( 'showing', function() { // This event occurs while the DFW overlay blocks the UI.
298 $( document.body ).addClass( 'fullscreen-active' );
299 api.refresh_buttons();
301 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
302 bounder( 'showToolbar', 'hideToolbar', 2000 );
305 setTimeout( api.resize_textarea, 200 );
307 // scroll to top so the user is not disoriented
310 // needed it for IE7 and compat mode
311 $('#wpadminbar').hide();
314 ps.subscribe( 'shown', function() { // This event occurs after the DFW overlay is shown
317 // init the standard TinyMCE instance if missing
318 if ( s.has_tinymce && ! s.is_mce_on ) {
319 htmled = document.getElementById(s.editor_id), old_val = htmled.value;
321 htmled.value = switchEditors.wpautop( old_val );
323 tinyMCE.settings.setup = function(ed) {
324 ed.onInit.add(function(ed) {
326 delete tinyMCE.settings.setup;
327 ed.getElement().value = old_val;
331 tinyMCE.execCommand("mceAddControl", false, s.editor_id);
336 ps.subscribe( 'hide', function() { // This event occurs before the overlay blocks DFW.
338 // Make sure the correct editor is displaying.
339 if ( s.has_tinymce && s.mode === 'tinymce' && $('#' + s.editor_id).is(':visible') ) {
340 switchEditors.go( s.editor_id, 'tinymce' );
341 } else if ( s.mode === 'html' && $('#' + s.editor_id).is(':hidden') ) {
342 switchEditors.go( s.editor_id, 'html' );
345 // Save content must be after switchEditors or content will be overwritten. See #17229.
348 $( document ).unbind( '.fullscreen' );
349 $(s.textarea_obj).unbind('.grow');
351 if ( s.has_tinymce && s.mode === 'tinymce' )
352 tinyMCE.execCommand('wpFullScreenSave');
354 set_title_hint( $('#' + s.title_id) );
356 // Restore and update edCanvas.
357 edCanvas = s._edCanvas;
358 edCanvas.value = s.textarea_obj.value;
361 ps.subscribe( 'hiding', function() { // This event occurs while the overlay blocks the DFW UI.
363 $( document.body ).removeClass( 'fullscreen-active' );
364 scrollTo(0, s.orig_y);
365 $('#wpadminbar').show();
368 ps.subscribe( 'hidden', function() { // This event occurs after DFW is removed.
370 $('#wp_mce_fullscreen').removeAttr('style');
372 if ( s.has_tinymce && s.is_mce_on )
373 tinyMCE.execCommand('wpFullScreenClose');
375 s.textarea_obj.value = '';
379 ps.subscribe( 'switchMode', function( from, to ) {
382 if ( !s.has_tinymce || !s.is_mce_on )
385 ed = tinyMCE.get('wp_mce_fullscreen');
387 if ( from === 'html' && to === 'tinymce' ) {
388 s.textarea_obj.value = switchEditors.wpautop( s.textarea_obj.value );
390 if ( 'undefined' == typeof(ed) )
391 tinyMCE.execCommand('wpFullScreenInit');
395 } else if ( from === 'tinymce' && to === 'html' ) {
401 ps.subscribe( 'switchedMode', function( from, to ) {
402 api.refresh_buttons(true);
405 setTimeout( api.resize_textarea, 200 );
412 if ( s.has_tinymce && 'tinymce' === s.mode )
413 tinyMCE.execCommand('Bold');
417 if ( s.has_tinymce && 'tinymce' === s.mode )
418 tinyMCE.execCommand('Italic');
421 api.ul = function() {
422 if ( s.has_tinymce && 'tinymce' === s.mode )
423 tinyMCE.execCommand('InsertUnorderedList');
426 api.ol = function() {
427 if ( s.has_tinymce && 'tinymce' === s.mode )
428 tinyMCE.execCommand('InsertOrderedList');
431 api.link = function() {
432 if ( s.has_tinymce && 'tinymce' === s.mode )
433 tinyMCE.execCommand('WP_Link');
438 api.unlink = function() {
439 if ( s.has_tinymce && 'tinymce' === s.mode )
440 tinyMCE.execCommand('unlink');
443 api.atd = function() {
444 if ( s.has_tinymce && 'tinymce' === s.mode )
445 tinyMCE.execCommand('mceWritingImprovementTool');
448 api.help = function() {
449 if ( s.has_tinymce && 'tinymce' === s.mode )
450 tinyMCE.execCommand('WP_Help');
453 api.blockquote = function() {
454 if ( s.has_tinymce && 'tinymce' === s.mode )
455 tinyMCE.execCommand('mceBlockQuote');
458 api.refresh_buttons = function( fade ) {
459 fade = fade || false;
461 if ( s.mode === 'html' ) {
462 $('#wp-fullscreen-mode-bar').removeClass('wp-tmce-mode').addClass('wp-html-mode');
465 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
466 $(this).addClass('wp-html-mode').fadeIn( 150 );
469 $('#wp-fullscreen-button-bar').addClass('wp-html-mode');
471 } else if ( s.mode === 'tinymce' ) {
472 $('#wp-fullscreen-mode-bar').removeClass('wp-html-mode').addClass('wp-tmce-mode');
475 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
476 $(this).removeClass('wp-html-mode').fadeIn( 150 );
479 $('#wp-fullscreen-button-bar').removeClass('wp-html-mode');
486 * Used for transitioning between states.
490 var topbar = $('#fullscreen-topbar'), txtarea = $('#wp_mce_fullscreen'), last = 0;
491 s.toolbars = topbar.add( $('#wp-fullscreen-status') );
492 s.element = $('#fullscreen-fader');
493 s.textarea_obj = txtarea[0];
494 s.has_tinymce = typeof(tinyMCE) != 'undefined';
496 if ( !s.has_tinymce )
497 $('#wp-fullscreen-mode-bar').hide();
500 wptitlehint('wp-fullscreen-title');
502 $(document).keyup(function(e){
503 var c = e.keyCode || e.charCode, a, data;
505 if ( !fullscreen.settings.visible )
508 if ( navigator.platform && navigator.platform.indexOf('Mac') != -1 )
509 a = e.ctrlKey; // Ctrl key for Mac
511 a = e.altKey; // Alt key for Win & Linux
513 if ( 27 == c ) { // Esc
518 condition: function(){
519 if ( $('#TB_window').is(':visible') || $('.wp-dialog').is(':visible') )
525 if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [data] ) )
529 if ( a && (61 == c || 107 == c || 187 == c) ) // +
532 if ( a && (45 == c || 109 == c || 189 == c) ) // -
535 if ( a && 48 == c ) // 0
541 // word count in HTML mode
542 if ( typeof(wpWordCount) != 'undefined' ) {
544 txtarea.keyup( function(e) {
545 var k = e.keyCode || e.charCode;
550 if ( 13 == k || 8 == last || 46 == last )
551 $(document).triggerHandler('wpcountwords', [ txtarea.val() ]);
558 topbar.mouseenter(function(e){
559 s.toolbars.addClass('fullscreen-make-sticky');
560 $( document ).unbind( '.fullscreen' );
561 clearTimeout( s.timer );
563 }).mouseleave(function(e){
564 s.toolbars.removeClass('fullscreen-make-sticky');
567 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
571 fade: function( before, during, after ) {
575 // If any callback bound to before returns false, bail.
576 if ( before && ! ps.publish( before ) )
579 api.fade.In( s.element, 600, function() {
581 ps.publish( during );
583 api.fade.Out( s.element, 600, function() {
592 transitionend: 'transitionend webkitTransitionEnd oTransitionEnd',
594 // Sensitivity to allow browsers to render the blank element before animating.
597 In: function( element, speed, callback, stop ) {
599 callback = callback || $.noop;
600 speed = speed || 400;
601 stop = stop || false;
603 if ( api.fade.transitions ) {
604 if ( element.is(':visible') ) {
605 element.addClass( 'fade-trigger' );
610 element.first().one( this.transitionend, function() {
613 setTimeout( function() { element.addClass( 'fade-trigger' ); }, this.sensitivity );
618 element.css( 'opacity', 1 );
619 element.first().fadeIn( speed, callback );
621 if ( element.length > 1 )
622 element.not(':first').fadeIn( speed );
628 Out: function( element, speed, callback, stop ) {
630 callback = callback || $.noop;
631 speed = speed || 400;
632 stop = stop || false;
634 if ( ! element.is(':visible') )
637 if ( api.fade.transitions ) {
638 element.first().one( api.fade.transitionend, function() {
639 if ( element.hasClass('fade-trigger') )
645 setTimeout( function() { element.removeClass( 'fade-trigger' ); }, this.sensitivity );
650 element.first().fadeOut( speed, callback );
652 if ( element.length > 1 )
653 element.not(':first').fadeOut( speed );
659 transitions: (function() { // Check if the browser supports CSS 3.0 transitions
660 var s = document.documentElement.style;
662 return ( typeof ( s.WebkitTransition ) == 'string' ||
663 typeof ( s.MozTransition ) == 'string' ||
664 typeof ( s.OTransition ) == 'string' ||
665 typeof ( s.transition ) == 'string' );
673 * Automatically updates textarea height.
676 api.bind_resize = function() {
677 $(s.textarea_obj).bind('keypress.grow click.grow paste.grow', function(){
678 setTimeout( api.resize_textarea, 200 );
683 api.resize_textarea = function() {
684 var txt = s.textarea_obj, newheight;
686 newheight = txt.scrollHeight > 300 ? txt.scrollHeight : 300;
688 if ( newheight != api.oldheight ) {
689 txt.style.height = newheight + 'px';
690 api.oldheight = newheight;