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