]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.js
WordPress 3.5-scripts
[autoinstalls/wordpress.git] / wp-admin / js / common.js
1 var showNotice, adminMenu, columns, validateForm, screenMeta;
2 (function($){
3 // Removed in 3.3.
4 // (perhaps) needed for back-compat
5 adminMenu = {
6         init : function() {},
7         fold : function() {},
8         restoreMenuState : function() {},
9         toggle : function() {},
10         favorites : function() {}
11 };
12
13 // show/hide/save table columns
14 columns = {
15         init : function() {
16                 var that = this;
17                 $('.hide-column-tog', '#adv-settings').click( function() {
18                         var $t = $(this), column = $t.val();
19                         if ( $t.prop('checked') )
20                                 that.checked(column);
21                         else
22                                 that.unchecked(column);
23
24                         columns.saveManageColumnsState();
25                 });
26         },
27
28         saveManageColumnsState : function() {
29                 var hidden = this.hidden();
30                 $.post(ajaxurl, {
31                         action: 'hidden-columns',
32                         hidden: hidden,
33                         screenoptionnonce: $('#screenoptionnonce').val(),
34                         page: pagenow
35                 });
36         },
37
38         checked : function(column) {
39                 $('.column-' + column).show();
40                 this.colSpanChange(+1);
41         },
42
43         unchecked : function(column) {
44                 $('.column-' + column).hide();
45                 this.colSpanChange(-1);
46         },
47
48         hidden : function() {
49                 return $('.manage-column').filter(':hidden').map(function() { return this.id; }).get().join(',');
50         },
51
52         useCheckboxesForHidden : function() {
53                 this.hidden = function(){
54                         return $('.hide-column-tog').not(':checked').map(function() {
55                                 var id = this.id;
56                                 return id.substring( id, id.length - 5 );
57                         }).get().join(',');
58                 };
59         },
60
61         colSpanChange : function(diff) {
62                 var $t = $('table').find('.colspanchange'), n;
63                 if ( !$t.length )
64                         return;
65                 n = parseInt( $t.attr('colspan'), 10 ) + diff;
66                 $t.attr('colspan', n.toString());
67         }
68 }
69
70 $(document).ready(function(){columns.init();});
71
72 validateForm = function( form ) {
73         return !$( form ).find('.form-required').filter( function() { return $('input:visible', this).val() == ''; } ).addClass( 'form-invalid' ).find('input:visible').change( function() { $(this).closest('.form-invalid').removeClass( 'form-invalid' ); } ).size();
74 }
75
76 // stub for doing better warnings
77 showNotice = {
78         warn : function() {
79                 var msg = commonL10n.warnDelete || '';
80                 if ( confirm(msg) ) {
81                         return true;
82                 }
83
84                 return false;
85         },
86
87         note : function(text) {
88                 alert(text);
89         }
90 };
91
92 screenMeta = {
93         element: null, // #screen-meta
94         toggles: null, // .screen-meta-toggle
95         page:    null, // #wpcontent
96
97         init: function() {
98                 this.element = $('#screen-meta');
99                 this.toggles = $('.screen-meta-toggle a');
100                 this.page    = $('#wpcontent');
101
102                 this.toggles.click( this.toggleEvent );
103         },
104
105         toggleEvent: function( e ) {
106                 var panel = $( this.href.replace(/.+#/, '#') );
107                 e.preventDefault();
108
109                 if ( !panel.length )
110                         return;
111
112                 if ( panel.is(':visible') )
113                         screenMeta.close( panel, $(this) );
114                 else
115                         screenMeta.open( panel, $(this) );
116         },
117
118         open: function( panel, link ) {
119
120                 $('.screen-meta-toggle').not( link.parent() ).css('visibility', 'hidden');
121
122                 panel.parent().show();
123                 panel.slideDown( 'fast', function() {
124                         panel.focus();
125                         link.addClass('screen-meta-active').attr('aria-expanded', true);
126                 });
127         },
128
129         close: function( panel, link ) {
130                 panel.slideUp( 'fast', function() {
131                         link.removeClass('screen-meta-active').attr('aria-expanded', false);
132                         $('.screen-meta-toggle').css('visibility', '');
133                         panel.parent().hide();
134                 });
135         }
136 };
137
138 /**
139  * Help tabs.
140  */
141 $('.contextual-help-tabs').delegate('a', 'click focus', function(e) {
142         var link = $(this),
143                 panel;
144
145         e.preventDefault();
146
147         // Don't do anything if the click is for the tab already showing.
148         if ( link.is('.active a') )
149                 return false;
150
151         // Links
152         $('.contextual-help-tabs .active').removeClass('active');
153         link.parent('li').addClass('active');
154
155         panel = $( link.attr('href') );
156
157         // Panels
158         $('.help-tab-content').not( panel ).removeClass('active').hide();
159         panel.addClass('active').show();
160 });
161
162 $(document).ready( function() {
163         var lastClicked = false, checks, first, last, checked, menu = $('#adminmenu'), mobileEvent,
164                 pageInput = $('input.current-page'), currentPage = pageInput.val();
165
166         // when the menu is folded, make the fly-out submenu header clickable
167         menu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
168                 $(e.target).parent().siblings('a').get(0).click();
169         });
170
171         $('#collapse-menu').on('click.collapse-menu', function(e){
172                 var body = $(document.body);
173
174                 // reset any compensation for submenus near the bottom of the screen
175                 $('#adminmenu div.wp-submenu').css('margin-top', '');
176
177                 if ( $(window).width() < 900 ) {
178                         if ( body.hasClass('auto-fold') ) {
179                                 body.removeClass('auto-fold');
180                                 setUserSetting('unfold', 1);
181                                 body.removeClass('folded');
182                                 deleteUserSetting('mfold');
183                         } else {
184                                 body.addClass('auto-fold');
185                                 deleteUserSetting('unfold');
186                         }
187                 } else {
188                         if ( body.hasClass('folded') ) {
189                                 body.removeClass('folded');
190                                 deleteUserSetting('mfold');
191                         } else {
192                                 body.addClass('folded');
193                                 setUserSetting('mfold', 'f');
194                         }
195                 }
196         });
197
198         if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
199                 // iOS Safari works with touchstart, the rest work with click
200                 mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
201
202                 // close any open submenus when touch/click is not on the menu
203                 $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
204                         if ( !$(e.target).closest('#adminmenu').length )
205                                 menu.find('li.wp-has-submenu.opensub').removeClass('opensub');
206                 });
207
208                 menu.find('a.wp-has-submenu').on( mobileEvent+'.wp-mobile-hover', function(e) {
209                         var el = $(this), parent = el.parent();
210
211                         // Show the sub instead of following the link if:
212                         //      - the submenu is not open
213                         //      - the submenu is not shown inline or the menu is not folded
214                         if ( !parent.hasClass('opensub') && ( !parent.hasClass('wp-menu-open') || parent.width() < 40 ) ) {
215                                 e.preventDefault();
216                                 menu.find('li.opensub').removeClass('opensub');
217                                 parent.addClass('opensub');
218                         }
219                 });
220         }
221
222         menu.find('li.wp-has-submenu').hoverIntent({
223                 over: function(e){
224                         var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop, top = parseInt( m.css('top'), 10 );
225
226                         if ( isNaN(top) || top > -5 ) // meaning the submenu is visible
227                                 return;
228
229                         menutop = $(this).offset().top;
230                         wintop = $(window).scrollTop();
231                         maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
232
233                         b = menutop + m.height() + 1; // Bottom offset of the menu
234                         h = $('#wpwrap').height(); // Height of the entire page
235                         o = 60 + b - h;
236                         f = $(window).height() + wintop - 15; // The fold
237
238                         if ( f < (b - o) )
239                                 o = b - f;
240
241                         if ( o > maxtop )
242                                 o = maxtop;
243
244                         if ( o > 1 )
245                                 m.css('margin-top', '-'+o+'px');
246                         else
247                                 m.css('margin-top', '');
248
249                         menu.find('li.menu-top').removeClass('opensub');
250                         $(this).addClass('opensub');
251                 },
252                 out: function(){
253                         $(this).removeClass('opensub').find('.wp-submenu').css('margin-top', '');
254                 },
255                 timeout: 200,
256                 sensitivity: 7,
257                 interval: 90
258         });
259
260         menu.on('focus.adminmenu', '.wp-submenu a', function(e){
261                 $(e.target).closest('li.menu-top').addClass('opensub');
262         }).on('blur.adminmenu', '.wp-submenu a', function(e){
263                 $(e.target).closest('li.menu-top').removeClass('opensub');
264         });
265
266         // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
267         $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
268         $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
269
270         // Init screen meta
271         screenMeta.init();
272
273         // check all checkboxes
274         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
275                 if ( 'undefined' == e.shiftKey ) { return true; }
276                 if ( e.shiftKey ) {
277                         if ( !lastClicked ) { return true; }
278                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
279                         first = checks.index( lastClicked );
280                         last = checks.index( this );
281                         checked = $(this).prop('checked');
282                         if ( 0 < first && 0 < last && first != last ) {
283                                 checks.slice( first, last ).prop( 'checked', function(){
284                                         if ( $(this).closest('tr').is(':visible') )
285                                                 return checked;
286
287                                         return false;
288                                 });
289                         }
290                 }
291                 lastClicked = this;
292
293                 // toggle "check all" checkboxes
294                 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
295                 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
296                         return ( 0 == unchecked.length );
297                 });
298
299                 return true;
300         });
301
302         $('thead, tfoot').find('.check-column :checkbox').click( function(e) {
303                 var c = $(this).prop('checked'),
304                         kbtoggle = 'undefined' == typeof toggleWithKeyboard ? false : toggleWithKeyboard,
305                         toggle = e.shiftKey || kbtoggle;
306
307                 $(this).closest( 'table' ).children( 'tbody' ).filter(':visible')
308                 .children().children('.check-column').find(':checkbox')
309                 .prop('checked', function() {
310                         if ( $(this).closest('tr').is(':hidden') )
311                                 return false;
312                         if ( toggle )
313                                 return $(this).prop( 'checked' );
314                         else if (c)
315                                 return true;
316                         return false;
317                 });
318
319                 $(this).closest('table').children('thead,  tfoot').filter(':visible')
320                 .children().children('.check-column').find(':checkbox')
321                 .prop('checked', function() {
322                         if ( toggle )
323                                 return false;
324                         else if (c)
325                                 return true;
326                         return false;
327                 });
328         });
329
330         $('#default-password-nag-no').click( function() {
331                 setUserSetting('default_password_nag', 'hide');
332                 $('div.default-password-nag').hide();
333                 return false;
334         });
335
336         // tab in textareas
337         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
338                 var el = e.target, selStart, selEnd, val, scroll, sel;
339
340                 if ( e.keyCode == 27 ) { // escape key
341                         $(el).data('tab-out', true);
342                         return;
343                 }
344
345                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
346                         return;
347
348                 if ( $(el).data('tab-out') ) {
349                         $(el).data('tab-out', false);
350                         return;
351                 }
352
353                 selStart = el.selectionStart;
354                 selEnd = el.selectionEnd;
355                 val = el.value;
356
357                 try {
358                         this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event. See blur handler below.
359                 } catch(err) {}
360
361                 if ( document.selection ) {
362                         el.focus();
363                         sel = document.selection.createRange();
364                         sel.text = '\t';
365                 } else if ( selStart >= 0 ) {
366                         scroll = this.scrollTop;
367                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
368                         el.selectionStart = el.selectionEnd = selStart + 1;
369                         this.scrollTop = scroll;
370                 }
371
372                 if ( e.stopPropagation )
373                         e.stopPropagation();
374                 if ( e.preventDefault )
375                         e.preventDefault();
376         });
377
378         $('#newcontent').bind('blur.wpevent_InsertTab', function(e) {
379                 if ( this.lastKey && 9 == this.lastKey )
380                         this.focus();
381         });
382
383         if ( pageInput.length ) {
384                 pageInput.closest('form').submit( function(e){
385
386                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
387                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
388                                 pageInput.val('1');
389                 });
390         }
391
392         // Scroll into view when focused
393         $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
394                 if ( e.target.scrollIntoView )
395                         e.target.scrollIntoView(false);
396         });
397
398         // Disable upload buttons until files are selected
399         (function(){
400                 var button, input, form = $('form.wp-upload-form');
401                 if ( ! form.length )
402                         return;
403                 button = form.find('input[type="submit"]');
404                 input = form.find('input[type="file"]');
405
406                 function toggleUploadButton() {
407                         button.prop('disabled', '' === input.map( function() {
408                                 return $(this).val();
409                         }).get().join(''));
410                 }
411                 toggleUploadButton();
412                 input.on('change', toggleUploadButton);
413         })();
414 });
415
416 // internal use
417 $(document).bind( 'wp_CloseOnEscape', function( e, data ) {
418         if ( typeof(data.cb) != 'function' )
419                 return;
420
421         if ( typeof(data.condition) != 'function' || data.condition() )
422                 data.cb();
423
424         return true;
425 });
426
427 })(jQuery);