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');
249 if ( w < 200 || w > 1200 ) // sanity check
253 setUserSetting('dfw_width', n + w);
256 ps.subscribe( 'showToolbar', function() {
257 s.toolbars.removeClass('fade-1000').addClass('fade-300');
258 api.fade.In( s.toolbars, 300, function(){ ps.publish('toolbarShown'); }, true );
259 $('#wp-fullscreen-body').addClass('wp-fullscreen-focus');
260 s.toolbar_shown = true;
263 ps.subscribe( 'hideToolbar', function() {
264 s.toolbars.removeClass('fade-300').addClass('fade-1000');
265 api.fade.Out( s.toolbars, 1000, function(){ ps.publish('toolbarHidden'); }, true );
266 $('#wp-fullscreen-body').removeClass('wp-fullscreen-focus');
269 ps.subscribe( 'toolbarShown', function() {
270 s.toolbars.removeClass('fade-300');
273 ps.subscribe( 'toolbarHidden', function() {
274 s.toolbars.removeClass('fade-1000');
275 s.toolbar_shown = false;
278 ps.subscribe( 'show', function() { // This event occurs before the overlay blocks the UI.
279 var title = $('#wp-fullscreen-title').val( $('#' + s.title_id).val() );
281 set_title_hint( title );
282 $('#wp-fullscreen-save input').attr( 'title', $('#last-edit').text() );
284 s.textarea_obj.value = edCanvas.value;
286 if ( s.has_tinymce && s.mode === 'tinymce' )
287 tinyMCE.execCommand('wpFullScreenInit');
289 s._edCanvas = edCanvas;
290 edCanvas = s.textarea_obj;
292 s.orig_y = $(window).scrollTop();
295 ps.subscribe( 'showing', function() { // This event occurs while the DFW overlay blocks the UI.
296 $( document.body ).addClass( 'fullscreen-active' );
297 api.refresh_buttons();
299 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
300 bounder( 'showToolbar', 'hideToolbar', 2000 );
303 setTimeout( api.resize_textarea, 200 );
305 // scroll to top so the user is not disoriented
308 // needed it for IE7 and compat mode
309 $('#wpadminbar').hide();
312 ps.subscribe( 'shown', function() { // This event occurs after the DFW overlay is shown
315 // init the standard TinyMCE instance if missing
316 if ( s.has_tinymce && ! s.is_mce_on ) {
317 htmled = document.getElementById(s.editor_id), old_val = htmled.value;
319 htmled.value = switchEditors.wpautop( old_val );
321 tinyMCE.settings.setup = function(ed) {
322 ed.onInit.add(function(ed) {
324 delete tinyMCE.settings.setup;
325 ed.getElement().value = old_val;
329 tinyMCE.execCommand("mceAddControl", false, s.editor_id);
334 ps.subscribe( 'hide', function() { // This event occurs before the overlay blocks DFW.
336 // Make sure the correct editor is displaying.
337 if ( s.has_tinymce && s.mode === 'tinymce' && $('#' + s.editor_id).is(':visible') ) {
338 switchEditors.go( s.editor_id, 'tinymce' );
339 } else if ( s.mode === 'html' && $('#' + s.editor_id).is(':hidden') ) {
340 switchEditors.go( s.editor_id, 'html' );
343 // Save content must be after switchEditors or content will be overwritten. See #17229.
346 $( document ).unbind( '.fullscreen' );
347 $(s.textarea_obj).unbind('.grow');
349 if ( s.has_tinymce && s.mode === 'tinymce' )
350 tinyMCE.execCommand('wpFullScreenSave');
352 set_title_hint( $('#' + s.title_id) );
354 // Restore and update edCanvas.
355 edCanvas = s._edCanvas;
356 edCanvas.value = s.textarea_obj.value;
359 ps.subscribe( 'hiding', function() { // This event occurs while the overlay blocks the DFW UI.
361 $( document.body ).removeClass( 'fullscreen-active' );
362 scrollTo(0, s.orig_y);
363 $('#wpadminbar').show();
366 ps.subscribe( 'hidden', function() { // This event occurs after DFW is removed.
368 $('#wp_mce_fullscreen').removeAttr('style');
370 if ( s.has_tinymce && s.is_mce_on )
371 tinyMCE.execCommand('wpFullScreenClose');
373 s.textarea_obj.value = '';
377 ps.subscribe( 'switchMode', function( from, to ) {
380 if ( !s.has_tinymce || !s.is_mce_on )
383 ed = tinyMCE.get('wp_mce_fullscreen');
385 if ( from === 'html' && to === 'tinymce' ) {
386 s.textarea_obj.value = switchEditors.wpautop( s.textarea_obj.value );
388 if ( 'undefined' == typeof(ed) )
389 tinyMCE.execCommand('wpFullScreenInit');
393 } else if ( from === 'tinymce' && to === 'html' ) {
399 ps.subscribe( 'switchedMode', function( from, to ) {
400 api.refresh_buttons(true);
403 setTimeout( api.resize_textarea, 200 );
410 if ( s.has_tinymce && 'tinymce' === s.mode )
411 tinyMCE.execCommand('Bold');
415 if ( s.has_tinymce && 'tinymce' === s.mode )
416 tinyMCE.execCommand('Italic');
419 api.ul = function() {
420 if ( s.has_tinymce && 'tinymce' === s.mode )
421 tinyMCE.execCommand('InsertUnorderedList');
424 api.ol = function() {
425 if ( s.has_tinymce && 'tinymce' === s.mode )
426 tinyMCE.execCommand('InsertOrderedList');
429 api.link = function() {
430 if ( s.has_tinymce && 'tinymce' === s.mode )
431 tinyMCE.execCommand('WP_Link');
436 api.unlink = function() {
437 if ( s.has_tinymce && 'tinymce' === s.mode )
438 tinyMCE.execCommand('unlink');
441 api.atd = function() {
442 if ( s.has_tinymce && 'tinymce' === s.mode )
443 tinyMCE.execCommand('mceWritingImprovementTool');
446 api.help = function() {
447 if ( s.has_tinymce && 'tinymce' === s.mode )
448 tinyMCE.execCommand('WP_Help');
451 api.blockquote = function() {
452 if ( s.has_tinymce && 'tinymce' === s.mode )
453 tinyMCE.execCommand('mceBlockQuote');
456 api.refresh_buttons = function( fade ) {
457 fade = fade || false;
459 if ( s.mode === 'html' ) {
460 $('#wp-fullscreen-mode-bar').removeClass('wp-tmce-mode').addClass('wp-html-mode');
463 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
464 $(this).addClass('wp-html-mode').fadeIn( 150 );
467 $('#wp-fullscreen-button-bar').addClass('wp-html-mode');
469 } else if ( s.mode === 'tinymce' ) {
470 $('#wp-fullscreen-mode-bar').removeClass('wp-html-mode').addClass('wp-tmce-mode');
473 $('#wp-fullscreen-button-bar').fadeOut( 150, function(){
474 $(this).removeClass('wp-html-mode').fadeIn( 150 );
477 $('#wp-fullscreen-button-bar').removeClass('wp-html-mode');
484 * Used for transitioning between states.
488 var topbar = $('#fullscreen-topbar'), txtarea = $('#wp_mce_fullscreen'), last = 0;
489 s.toolbars = topbar.add( $('#wp-fullscreen-status') );
490 s.element = $('#fullscreen-fader');
491 s.textarea_obj = txtarea[0];
492 s.has_tinymce = typeof(tinyMCE) != 'undefined';
494 if ( !s.has_tinymce )
495 $('#wp-fullscreen-mode-bar').hide();
498 wptitlehint('wp-fullscreen-title');
500 $(document).keyup(function(e){
501 var c = e.keyCode || e.charCode, a, data;
503 if ( !fullscreen.settings.visible )
506 if ( navigator.platform && navigator.platform.indexOf('Mac') != -1 )
507 a = e.ctrlKey; // Ctrl key for Mac
509 a = e.altKey; // Alt key for Win & Linux
511 if ( 27 == c ) { // Esc
516 condition: function(){
517 if ( $('#TB_window').is(':visible') || $('.wp-dialog').is(':visible') )
523 if ( ! jQuery(document).triggerHandler( 'wp_CloseOnEscape', [data] ) )
527 if ( a && (61 == c || 107 == c || 187 == c) ) // +
530 if ( a && (45 == c || 109 == c || 189 == c) ) // -
533 if ( a && 48 == c ) // 0
539 // word count in HTML mode
540 if ( typeof(wpWordCount) != 'undefined' ) {
542 txtarea.keyup( function(e) {
543 var k = e.keyCode || e.charCode;
548 if ( 13 == k || 8 == last || 46 == last )
549 $(document).triggerHandler('wpcountwords', [ txtarea.val() ]);
556 topbar.mouseenter(function(e){
557 s.toolbars.addClass('fullscreen-make-sticky');
558 $( document ).unbind( '.fullscreen' );
559 clearTimeout( s.timer );
561 }).mouseleave(function(e){
562 s.toolbars.removeClass('fullscreen-make-sticky');
565 $( document ).bind( 'mousemove.fullscreen', function(e) { bounder( 'showToolbar', 'hideToolbar', 2000, e ); } );
569 fade: function( before, during, after ) {
573 // If any callback bound to before returns false, bail.
574 if ( before && ! ps.publish( before ) )
577 api.fade.In( s.element, 600, function() {
579 ps.publish( during );
581 api.fade.Out( s.element, 600, function() {
590 transitionend: 'transitionend webkitTransitionEnd oTransitionEnd',
592 // Sensitivity to allow browsers to render the blank element before animating.
595 In: function( element, speed, callback, stop ) {
597 callback = callback || $.noop;
598 speed = speed || 400;
599 stop = stop || false;
601 if ( api.fade.transitions ) {
602 if ( element.is(':visible') ) {
603 element.addClass( 'fade-trigger' );
608 element.first().one( this.transitionend, function() {
611 setTimeout( function() { element.addClass( 'fade-trigger' ); }, this.sensitivity );
616 element.css( 'opacity', 1 );
617 element.first().fadeIn( speed, callback );
619 if ( element.length > 1 )
620 element.not(':first').fadeIn( speed );
626 Out: function( element, speed, callback, stop ) {
628 callback = callback || $.noop;
629 speed = speed || 400;
630 stop = stop || false;
632 if ( ! element.is(':visible') )
635 if ( api.fade.transitions ) {
636 element.first().one( api.fade.transitionend, function() {
637 if ( element.hasClass('fade-trigger') )
643 setTimeout( function() { element.removeClass( 'fade-trigger' ); }, this.sensitivity );
648 element.first().fadeOut( speed, callback );
650 if ( element.length > 1 )
651 element.not(':first').fadeOut( speed );
657 transitions: (function() { // Check if the browser supports CSS 3.0 transitions
658 var s = document.documentElement.style;
660 return ( typeof ( s.WebkitTransition ) == 'string' ||
661 typeof ( s.MozTransition ) == 'string' ||
662 typeof ( s.OTransition ) == 'string' ||
663 typeof ( s.transition ) == 'string' );
671 * Automatically updates textarea height.
674 api.bind_resize = function() {
675 $(s.textarea_obj).bind('keypress.grow click.grow paste.grow', function(){
676 setTimeout( api.resize_textarea, 200 );
681 api.resize_textarea = function() {
682 var txt = s.textarea_obj, newheight;
684 newheight = txt.scrollHeight > 300 ? txt.scrollHeight : 300;
686 if ( newheight != api.oldheight ) {
687 txt.style.height = newheight + 'px';
688 api.oldheight = newheight;