wp/wp-admin/js/dashboard.js
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
       
     1 /* global pagenow, ajaxurl, postboxes, wpActiveEditor:true */
     1 var ajaxWidgets, ajaxPopulateWidgets, quickPressLoad;
     2 var ajaxWidgets, ajaxPopulateWidgets, quickPressLoad;
     2 
     3 
     3 jQuery(document).ready( function($) {
     4 jQuery(document).ready( function($) {
     4 	/* Dashboard Welcome Panel */
     5 	var welcomePanel = $( '#welcome-panel' ),
     5 	var welcomePanel = $('#welcome-panel'),
       
     6 		welcomePanelHide = $('#wp_welcome_panel-hide'),
     6 		welcomePanelHide = $('#wp_welcome_panel-hide'),
     7 	 	updateWelcomePanel = function( visible ) {
     7 		updateWelcomePanel;
     8 			$.post( ajaxurl, {
       
     9 				action: 'update-welcome-panel',
       
    10 				visible: visible,
       
    11 				welcomepanelnonce: $('#welcomepanelnonce').val()
       
    12 			});
       
    13 		};
       
    14 
     8 
    15 	if ( welcomePanel.hasClass('hidden') && welcomePanelHide.prop('checked') )
     9 	updateWelcomePanel = function( visible ) {
       
    10 		$.post( ajaxurl, {
       
    11 			action: 'update-welcome-panel',
       
    12 			visible: visible,
       
    13 			welcomepanelnonce: $( '#welcomepanelnonce' ).val()
       
    14 		});
       
    15 	};
       
    16 
       
    17 	if ( welcomePanel.hasClass('hidden') && welcomePanelHide.prop('checked') ) {
    16 		welcomePanel.removeClass('hidden');
    18 		welcomePanel.removeClass('hidden');
       
    19 	}
    17 
    20 
    18 	$('.welcome-panel-close, .welcome-panel-dismiss a', welcomePanel).click( function(e) {
    21 	$('.welcome-panel-close, .welcome-panel-dismiss a', welcomePanel).click( function(e) {
    19 		e.preventDefault();
    22 		e.preventDefault();
    20 		welcomePanel.addClass('hidden');
    23 		welcomePanel.addClass('hidden');
    21 		updateWelcomePanel( 0 );
    24 		updateWelcomePanel( 0 );
    26 		welcomePanel.toggleClass('hidden', ! this.checked );
    29 		welcomePanel.toggleClass('hidden', ! this.checked );
    27 		updateWelcomePanel( this.checked ? 1 : 0 );
    30 		updateWelcomePanel( this.checked ? 1 : 0 );
    28 	});
    31 	});
    29 
    32 
    30 	// These widgets are sometimes populated via ajax
    33 	// These widgets are sometimes populated via ajax
    31 	ajaxWidgets = [
    34 	ajaxWidgets = ['dashboard_primary'];
    32 		'dashboard_incoming_links',
       
    33 		'dashboard_primary',
       
    34 		'dashboard_secondary',
       
    35 		'dashboard_plugins'
       
    36 	];
       
    37 
    35 
    38 	ajaxPopulateWidgets = function(el) {
    36 	ajaxPopulateWidgets = function(el) {
    39 		function show(i, id) {
    37 		function show(i, id) {
    40 			var p, e = $('#' + id + ' div.inside:visible').find('.widget-loading');
    38 			var p, e = $('#' + id + ' div.inside:visible').find('.widget-loading');
    41 			if ( e.length ) {
    39 			if ( e.length ) {
    42 				p = e.parent();
    40 				p = e.parent();
    43 				setTimeout( function(){
    41 				setTimeout( function(){
    44 					p.load( ajaxurl + '?action=dashboard-widgets&widget=' + id, '', function() {
    42 					p.load( ajaxurl + '?action=dashboard-widgets&widget=' + id + '&pagenow=' + pagenow, '', function() {
    45 						p.hide().slideDown('normal', function(){
    43 						p.hide().slideDown('normal', function(){
    46 							$(this).css('display', '');
    44 							$(this).css('display', '');
    47 						});
    45 						});
    48 					});
    46 					});
    49 				}, i * 500 );
    47 				}, i * 500 );
    50 			}
    48 			}
    51 		}
    49 		}
    52 
    50 
    53 		if ( el ) {
    51 		if ( el ) {
    54 			el = el.toString();
    52 			el = el.toString();
    55 			if ( $.inArray(el, ajaxWidgets) != -1 )
    53 			if ( $.inArray(el, ajaxWidgets) !== -1 ) {
    56 				show(0, el);
    54 				show(0, el);
       
    55 			}
    57 		} else {
    56 		} else {
    58 			$.each( ajaxWidgets, show );
    57 			$.each( ajaxWidgets, show );
    59 		}
    58 		}
    60 	};
    59 	};
    61 	ajaxPopulateWidgets();
    60 	ajaxPopulateWidgets();
    63 	postboxes.add_postbox_toggles(pagenow, { pbshow: ajaxPopulateWidgets } );
    62 	postboxes.add_postbox_toggles(pagenow, { pbshow: ajaxPopulateWidgets } );
    64 
    63 
    65 	/* QuickPress */
    64 	/* QuickPress */
    66 	quickPressLoad = function() {
    65 	quickPressLoad = function() {
    67 		var act = $('#quickpost-action'), t;
    66 		var act = $('#quickpost-action'), t;
       
    67 
       
    68 		$( '#quick-press .submit input[type="submit"], #quick-press .submit input[type="reset"]' ).prop( 'disabled' , false );
       
    69 
    68 		t = $('#quick-press').submit( function() {
    70 		t = $('#quick-press').submit( function() {
    69 			$('#dashboard_quick_press #publishing-action .spinner').show();
    71 			$('#dashboard_quick_press #publishing-action .spinner').show();
    70 			$('#quick-press .submit input[type="submit"], #quick-press .submit input[type="reset"]').prop('disabled', true);
    72 			$('#quick-press .submit input[type="submit"], #quick-press .submit input[type="reset"]').prop('disabled', true);
    71 
    73 
    72 			if ( 'post' == act.val() ) {
    74 			$.post( t.attr( 'action' ), t.serializeArray(), function( data ) {
    73 				act.val( 'post-quickpress-publish' );
    75 				// Replace the form, and prepend the published post.
       
    76 				$('#dashboard_quick_press .inside').html( data );
       
    77 				$('#quick-press').removeClass('initial-form');
       
    78 				quickPressLoad();
       
    79 				highlightLatestPost();
       
    80 				$('#title').focus();
       
    81 			});
       
    82 
       
    83 			function highlightLatestPost () {
       
    84 				var latestPost = $('.drafts ul li').first();
       
    85 				latestPost.css('background', '#fffbe5');
       
    86 				setTimeout(function () {
       
    87 					latestPost.css('background', 'none');
       
    88 				}, 1000);
    74 			}
    89 			}
    75 
    90 
    76 			$('#dashboard_quick_press div.inside').load( t.attr( 'action' ), t.serializeArray(), function() {
       
    77 				$('#dashboard_quick_press #publishing-action .spinner').hide();
       
    78 				$('#quick-press .submit input[type="submit"], #quick-press .submit input[type="reset"]').prop('disabled', false);
       
    79 				$('#dashboard_quick_press ul').next('p').remove();
       
    80 				$('#dashboard_quick_press ul').find('li').each( function() {
       
    81 					$('#dashboard_recent_drafts ul').prepend( this );
       
    82 				} ).end().remove();
       
    83 				quickPressLoad();
       
    84 			} );
       
    85 			return false;
    91 			return false;
    86 		} );
    92 		} );
    87 
    93 
    88 		$('#publish').click( function() { act.val( 'post-quickpress-publish' ); } );
    94 		$('#publish').click( function() { act.val( 'post-quickpress-publish' ); } );
    89 
    95 
    90 		$('#title, #tags-input').each( function() {
    96 		$('#title, #tags-input, #content').each( function() {
    91 			var input = $(this), prompt = $('#' + this.id + '-prompt-text');
    97 			var input = $(this), prompt = $('#' + this.id + '-prompt-text');
    92 
    98 
    93 			if ( '' === this.value )
    99 			if ( '' === this.value ) {
    94 				prompt.removeClass('screen-reader-text');
   100 				prompt.removeClass('screen-reader-text');
       
   101 			}
    95 
   102 
    96 			prompt.click( function() {
   103 			prompt.click( function() {
    97 				$(this).addClass('screen-reader-text');
   104 				$(this).addClass('screen-reader-text');
    98 				input.focus();
   105 				input.focus();
    99 			});
   106 			});
   100 
   107 
   101 			input.blur( function() {
   108 			input.blur( function() {
   102 				if ( '' === this.value )
   109 				if ( '' === this.value ) {
   103 					prompt.removeClass('screen-reader-text');
   110 					prompt.removeClass('screen-reader-text');
       
   111 				}
   104 			});
   112 			});
   105 
   113 
   106 			input.focus( function() {
   114 			input.focus( function() {
   107 				prompt.addClass('screen-reader-text');
   115 				prompt.addClass('screen-reader-text');
   108 			});
   116 			});
   109 		});
   117 		});
   110 
   118 
   111 		$('#quick-press').on( 'click focusin', function() {
   119 		$('#quick-press').on( 'click focusin', function() {
   112 			wpActiveEditor = 'content';
   120 			wpActiveEditor = 'content';
   113 		});
   121 		});
       
   122 
       
   123 		autoResizeTextarea();
   114 	};
   124 	};
   115 	quickPressLoad();
   125 	quickPressLoad();
   116 
   126 
       
   127 	$( '.meta-box-sortables' ).sortable( 'option', 'containment', 'document' );
       
   128 
       
   129 	function autoResizeTextarea() {
       
   130 		if ( document.documentMode && document.documentMode < 9 ) {
       
   131 			return;
       
   132 		}
       
   133 
       
   134 		// Add a hidden div. We'll copy over the text from the textarea to measure its height.
       
   135 		$('body').append( '<div class="quick-draft-textarea-clone" style="display: none;"></div>' );
       
   136 
       
   137 		var clone = $('.quick-draft-textarea-clone'),
       
   138 			editor = $('#content'),
       
   139 			editorHeight = editor.height(),
       
   140 			// 100px roughly accounts for browser chrome and allows the
       
   141 			// save draft button to show on-screen at the same time.
       
   142 			editorMaxHeight = $(window).height() - 100;
       
   143 
       
   144 		// Match up textarea and clone div as much as possible.
       
   145 		// Padding cannot be reliably retrieved using shorthand in all browsers.
       
   146 		clone.css({
       
   147 			'font-family': editor.css('font-family'),
       
   148 			'font-size':   editor.css('font-size'),
       
   149 			'line-height': editor.css('line-height'),
       
   150 			'padding-bottom': editor.css('paddingBottom'),
       
   151 			'padding-left': editor.css('paddingLeft'),
       
   152 			'padding-right': editor.css('paddingRight'),
       
   153 			'padding-top': editor.css('paddingTop'),
       
   154 			'white-space': 'pre-wrap',
       
   155 			'word-wrap': 'break-word',
       
   156 			'display': 'none'
       
   157 		});
       
   158 
       
   159 		// propertychange is for IE < 9
       
   160 		editor.on('focus input propertychange', function() {
       
   161 			var $this = $(this),
       
   162 				// &nbsp; is to ensure that the height of a final trailing newline is included.
       
   163 				textareaContent = $this.val() + '&nbsp;',
       
   164 				// 2px is for border-top & border-bottom
       
   165 				cloneHeight = clone.css('width', $this.css('width')).text(textareaContent).outerHeight() + 2;
       
   166 
       
   167 			// Default to having scrollbars
       
   168 			editor.css('overflow-y', 'auto');
       
   169 
       
   170 			// Only change the height if it has indeed changed and both heights are below the max.
       
   171 			if ( cloneHeight === editorHeight || ( cloneHeight >= editorMaxHeight && editorHeight >= editorMaxHeight ) ) {
       
   172 				return;
       
   173 			}
       
   174 
       
   175 			// Don't allow editor to exceed height of window.
       
   176 			// This is also bound in CSS to a max-height of 1300px to be extra safe.
       
   177 			if ( cloneHeight > editorMaxHeight ) {
       
   178 				editorHeight = editorMaxHeight;
       
   179 			} else {
       
   180 				editorHeight = cloneHeight;
       
   181 			}
       
   182 
       
   183 			// No scrollbars as we change height, not for IE < 9
       
   184 			editor.css('overflow', 'hidden');
       
   185 
       
   186 			$this.css('height', editorHeight + 'px');
       
   187 		});
       
   188 	}
       
   189 
   117 } );
   190 } );