bf413370dc97284ea2f94568b12e6c8d923422de
[autoinstalls/wordpress.git] / wp-admin / js / common.js
1 /* global setUserSetting, ajaxurl, commonL10n, alert, confirm, toggleWithKeyboard, pagenow */
2 var showNotice, adminMenu, columns, validateForm, screenMeta;
3 ( function( $, window, undefined ) {
4 // Removed in 3.3.
5 // (perhaps) needed for back-compat
6 adminMenu = {
7         init : function() {},
8         fold : function() {},
9         restoreMenuState : function() {},
10         toggle : function() {},
11         favorites : function() {}
12 };
13
14 // show/hide/save table columns
15 columns = {
16         init : function() {
17                 var that = this;
18                 $('.hide-column-tog', '#adv-settings').click( function() {
19                         var $t = $(this), column = $t.val();
20                         if ( $t.prop('checked') )
21                                 that.checked(column);
22                         else
23                                 that.unchecked(column);
24
25                         columns.saveManageColumnsState();
26                 });
27         },
28
29         saveManageColumnsState : function() {
30                 var hidden = this.hidden();
31                 $.post(ajaxurl, {
32                         action: 'hidden-columns',
33                         hidden: hidden,
34                         screenoptionnonce: $('#screenoptionnonce').val(),
35                         page: pagenow
36                 });
37         },
38
39         checked : function(column) {
40                 $('.column-' + column).show();
41                 this.colSpanChange(+1);
42         },
43
44         unchecked : function(column) {
45                 $('.column-' + column).hide();
46                 this.colSpanChange(-1);
47         },
48
49         hidden : function() {
50                 return $('.manage-column').filter(':hidden').map(function() { return this.id; }).get().join(',');
51         },
52
53         useCheckboxesForHidden : function() {
54                 this.hidden = function(){
55                         return $('.hide-column-tog').not(':checked').map(function() {
56                                 var id = this.id;
57                                 return id.substring( id, id.length - 5 );
58                         }).get().join(',');
59                 };
60         },
61
62         colSpanChange : function(diff) {
63                 var $t = $('table').find('.colspanchange'), n;
64                 if ( !$t.length )
65                         return;
66                 n = parseInt( $t.attr('colspan'), 10 ) + diff;
67                 $t.attr('colspan', n.toString());
68         }
69 };
70
71 $(document).ready(function(){columns.init();});
72
73 validateForm = function( form ) {
74         return !$( form )
75                 .find( '.form-required' )
76                 .filter( function() { return $( 'input:visible', this ).val() === ''; } )
77                 .addClass( 'form-invalid' )
78                 .find( 'input:visible' )
79                 .change( function() { $( this ).closest( '.form-invalid' ).removeClass( 'form-invalid' ); } )
80                 .size();
81 };
82
83 // stub for doing better warnings
84 showNotice = {
85         warn : function() {
86                 var msg = commonL10n.warnDelete || '';
87                 if ( confirm(msg) ) {
88                         return true;
89                 }
90
91                 return false;
92         },
93
94         note : function(text) {
95                 alert(text);
96         }
97 };
98
99 screenMeta = {
100         element: null, // #screen-meta
101         toggles: null, // .screen-meta-toggle
102         page:    null, // #wpcontent
103
104         init: function() {
105                 this.element = $('#screen-meta');
106                 this.toggles = $('.screen-meta-toggle a');
107                 this.page    = $('#wpcontent');
108
109                 this.toggles.click( this.toggleEvent );
110         },
111
112         toggleEvent: function( e ) {
113                 var panel = $( this.href.replace(/.+#/, '#') );
114                 e.preventDefault();
115
116                 if ( !panel.length )
117                         return;
118
119                 if ( panel.is(':visible') )
120                         screenMeta.close( panel, $(this) );
121                 else
122                         screenMeta.open( panel, $(this) );
123         },
124
125         open: function( panel, link ) {
126
127                 $('.screen-meta-toggle').not( link.parent() ).css('visibility', 'hidden');
128
129                 panel.parent().show();
130                 panel.slideDown( 'fast', function() {
131                         panel.focus();
132                         link.addClass('screen-meta-active').attr('aria-expanded', true);
133                 });
134         },
135
136         close: function( panel, link ) {
137                 panel.slideUp( 'fast', function() {
138                         link.removeClass('screen-meta-active').attr('aria-expanded', false);
139                         $('.screen-meta-toggle').css('visibility', '');
140                         panel.parent().hide();
141                 });
142         }
143 };
144
145 /**
146  * Help tabs.
147  */
148 $('.contextual-help-tabs').delegate('a', 'click focus', function(e) {
149         var link = $(this),
150                 panel;
151
152         e.preventDefault();
153
154         // Don't do anything if the click is for the tab already showing.
155         if ( link.is('.active a') )
156                 return false;
157
158         // Links
159         $('.contextual-help-tabs .active').removeClass('active');
160         link.parent('li').addClass('active');
161
162         panel = $( link.attr('href') );
163
164         // Panels
165         $('.help-tab-content').not( panel ).removeClass('active').hide();
166         panel.addClass('active').show();
167 });
168
169 $(document).ready( function() {
170         var checks, first, last, checked, sliced, mobileEvent, transitionTimeout, focusedRowActions,
171                 lastClicked = false,
172                 menu = $('#adminmenu'),
173                 pageInput = $('input.current-page'),
174                 currentPage = pageInput.val();
175
176         // when the menu is folded, make the fly-out submenu header clickable
177         menu.on('click.wp-submenu-head', '.wp-submenu-head', function(e){
178                 $(e.target).parent().siblings('a').get(0).click();
179         });
180
181         $('#collapse-menu').on('click.collapse-menu', function() {
182                 var body = $( document.body ), respWidth;
183
184                 // reset any compensation for submenus near the bottom of the screen
185                 $('#adminmenu div.wp-submenu').css('margin-top', '');
186
187                 if ( window.innerWidth ) {
188                         // window.innerWidth is affected by zooming on phones
189                         respWidth = Math.max( window.innerWidth, document.documentElement.clientWidth );
190                 } else {
191                         // IE < 9 doesn't support @media CSS rules
192                         respWidth = 901;
193                 }
194
195                 if ( respWidth && respWidth < 900 ) {
196                         if ( body.hasClass('auto-fold') ) {
197                                 body.removeClass('auto-fold').removeClass('folded');
198                                 setUserSetting('unfold', 1);
199                                 setUserSetting('mfold', 'o');
200                         } else {
201                                 body.addClass('auto-fold');
202                                 setUserSetting('unfold', 0);
203                         }
204                 } else {
205                         if ( body.hasClass('folded') ) {
206                                 body.removeClass('folded');
207                                 setUserSetting('mfold', 'o');
208                         } else {
209                                 body.addClass('folded');
210                                 setUserSetting('mfold', 'f');
211                         }
212                 }
213         });
214
215         if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
216                 // iOS Safari works with touchstart, the rest work with click
217                 mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
218
219                 // close any open submenus when touch/click is not on the menu
220                 $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
221                         if ( menu.data('wp-responsive') ) {
222                                 return;
223                         }
224
225                         if ( ! $(e.target).closest('#adminmenu').length ) {
226                                 menu.find('li.wp-has-submenu.opensub').removeClass('opensub');
227                         }
228                 });
229
230                 menu.find('a.wp-has-submenu').on( mobileEvent+'.wp-mobile-hover', function(e) {
231                         var el = $(this), parent = el.parent();
232
233                         if ( menu.data('wp-responsive') ) {
234                                 return;
235                         }
236
237                         // Show the sub instead of following the link if:
238                         //      - the submenu is not open
239                         //      - the submenu is not shown inline or the menu is not folded
240                         if ( !parent.hasClass('opensub') && ( !parent.hasClass('wp-menu-open') || parent.width() < 40 ) ) {
241                                 e.preventDefault();
242                                 menu.find('li.opensub').removeClass('opensub');
243                                 parent.addClass('opensub');
244                         }
245                 });
246         }
247
248         menu.find('li.wp-has-submenu').hoverIntent({
249                 over: function() {
250                         var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop, top = parseInt( m.css('top'), 10 );
251
252                         if ( isNaN(top) || top > -5 ) // meaning the submenu is visible
253                                 return;
254
255                         if ( menu.data('wp-responsive') ) {
256                                 // The menu is in responsive mode, bail
257                                 return;
258                         }
259
260                         menutop = $(this).offset().top;
261                         wintop = $(window).scrollTop();
262                         maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
263
264                         b = menutop + m.height() + 1; // Bottom offset of the menu
265                         h = $('#wpwrap').height(); // Height of the entire page
266                         o = 60 + b - h;
267                         f = $(window).height() + wintop - 15; // The fold
268
269                         if ( f < (b - o) )
270                                 o = b - f;
271
272                         if ( o > maxtop )
273                                 o = maxtop;
274
275                         if ( o > 1 )
276                                 m.css('margin-top', '-'+o+'px');
277                         else
278                                 m.css('margin-top', '');
279
280                         menu.find('li.menu-top').removeClass('opensub');
281                         $(this).addClass('opensub');
282                 },
283                 out: function(){
284                         if ( menu.data('wp-responsive') ) {
285                                 // The menu is in responsive mode, bail
286                                 return;
287                         }
288
289                         $(this).removeClass('opensub').find('.wp-submenu').css('margin-top', '');
290                 },
291                 timeout: 200,
292                 sensitivity: 7,
293                 interval: 90
294         });
295
296         menu.on('focus.adminmenu', '.wp-submenu a', function(e){
297                 if ( menu.data('wp-responsive') ) {
298                         // The menu is in responsive mode, bail
299                         return;
300                 }
301
302                 $(e.target).closest('li.menu-top').addClass('opensub');
303         }).on('blur.adminmenu', '.wp-submenu a', function(e){
304                 if ( menu.data('wp-responsive') ) {
305                         // The menu is in responsive mode, bail
306                         return;
307                 }
308
309                 $(e.target).closest('li.menu-top').removeClass('opensub');
310         });
311
312         // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
313         $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
314         $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
315
316         // Init screen meta
317         screenMeta.init();
318
319         // check all checkboxes
320         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
321                 if ( 'undefined' == e.shiftKey ) { return true; }
322                 if ( e.shiftKey ) {
323                         if ( !lastClicked ) { return true; }
324                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
325                         first = checks.index( lastClicked );
326                         last = checks.index( this );
327                         checked = $(this).prop('checked');
328                         if ( 0 < first && 0 < last && first != last ) {
329                                 sliced = ( last > first ) ? checks.slice( first, last ) : checks.slice( last, first );
330                                 sliced.prop( 'checked', function() {
331                                         if ( $(this).closest('tr').is(':visible') )
332                                                 return checked;
333
334                                         return false;
335                                 });
336                         }
337                 }
338                 lastClicked = this;
339
340                 // toggle "check all" checkboxes
341                 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
342                 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
343                         return ( 0 === unchecked.length );
344                 });
345
346                 return true;
347         });
348
349         $('thead, tfoot').find('.check-column :checkbox').click( function(e) {
350                 var c = $(this).prop('checked'),
351                         kbtoggle = 'undefined' == typeof toggleWithKeyboard ? false : toggleWithKeyboard,
352                         toggle = e.shiftKey || kbtoggle;
353
354                 $(this).closest( 'table' ).children( 'tbody' ).filter(':visible')
355                 .children().children('.check-column').find(':checkbox')
356                 .prop('checked', function() {
357                         if ( $(this).is(':hidden') )
358                                 return false;
359                         if ( toggle )
360                                 return $(this).prop( 'checked' );
361                         else if (c)
362                                 return true;
363                         return false;
364                 });
365
366                 $(this).closest('table').children('thead,  tfoot').filter(':visible')
367                 .children().children('.check-column').find(':checkbox')
368                 .prop('checked', function() {
369                         if ( toggle )
370                                 return false;
371                         else if (c)
372                                 return true;
373                         return false;
374                 });
375         });
376
377         // Show row actions on keyboard focus of its parent container element or any other elements contained within
378         $( 'td.post-title, td.title, td.comment, .bookmarks td.column-name, td.blogname, td.username, .dashboard-comment-wrap' ).focusin(function(){
379                 clearTimeout( transitionTimeout );
380                 focusedRowActions = $(this).find( '.row-actions' );
381                 focusedRowActions.addClass( 'visible' );
382         }).focusout(function(){
383                 // Tabbing between post title and .row-actions links needs a brief pause, otherwise
384                 // the .row-actions div gets hidden in transit in some browsers (ahem, Firefox).
385                 transitionTimeout = setTimeout(function(){
386                         focusedRowActions.removeClass( 'visible' );
387                 }, 30);
388         });
389
390         $('#default-password-nag-no').click( function() {
391                 setUserSetting('default_password_nag', 'hide');
392                 $('div.default-password-nag').hide();
393                 return false;
394         });
395
396         // tab in textareas
397         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
398                 var el = e.target, selStart, selEnd, val, scroll, sel;
399
400                 if ( e.keyCode == 27 ) { // escape key
401                         $(el).data('tab-out', true);
402                         return;
403                 }
404
405                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
406                         return;
407
408                 if ( $(el).data('tab-out') ) {
409                         $(el).data('tab-out', false);
410                         return;
411                 }
412
413                 selStart = el.selectionStart;
414                 selEnd = el.selectionEnd;
415                 val = el.value;
416
417                 try {
418                         this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event. See blur handler below.
419                 } catch(err) {}
420
421                 if ( document.selection ) {
422                         el.focus();
423                         sel = document.selection.createRange();
424                         sel.text = '\t';
425                 } else if ( selStart >= 0 ) {
426                         scroll = this.scrollTop;
427                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
428                         el.selectionStart = el.selectionEnd = selStart + 1;
429                         this.scrollTop = scroll;
430                 }
431
432                 if ( e.stopPropagation )
433                         e.stopPropagation();
434                 if ( e.preventDefault )
435                         e.preventDefault();
436         });
437
438         $('#newcontent').bind('blur.wpevent_InsertTab', function() {
439                 if ( this.lastKey && 9 == this.lastKey )
440                         this.focus();
441         });
442
443         if ( pageInput.length ) {
444                 pageInput.closest('form').submit( function() {
445
446                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
447                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
448                                 pageInput.val('1');
449                 });
450         }
451
452         $('.search-box input[type="search"], .search-box input[type="submit"]').mousedown(function () {
453                 $('select[name^="action"]').val('-1');
454         });
455
456         // Scroll into view when focused
457         $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
458                 if ( e.target.scrollIntoView )
459                         e.target.scrollIntoView(false);
460         });
461
462         // Disable upload buttons until files are selected
463         (function(){
464                 var button, input, form = $('form.wp-upload-form');
465                 if ( ! form.length )
466                         return;
467                 button = form.find('input[type="submit"]');
468                 input = form.find('input[type="file"]');
469
470                 function toggleUploadButton() {
471                         button.prop('disabled', '' === input.map( function() {
472                                 return $(this).val();
473                         }).get().join(''));
474                 }
475                 toggleUploadButton();
476                 input.on('change', toggleUploadButton);
477         })();
478 });
479
480 // Fire a custom jQuery event at the end of window resize
481 ( function() {
482         var timeout;
483
484         function triggerEvent() {
485                 $(document).trigger( 'wp-window-resized' );
486         }
487
488         function fireOnce() {
489                 window.clearTimeout( timeout );
490                 timeout = window.setTimeout( triggerEvent, 200 );
491         }
492
493         $(window).on( 'resize.wp-fire-once', fireOnce );
494 }());
495
496 $(document).ready( function() {
497         var $document = $( document ),
498                 $window = $( window ),
499                 $body = $( document.body ),
500                 $adminMenuWrap = $( '#adminmenuwrap' ),
501                 $collapseMenu = $( '#collapse-menu' ),
502                 $wpwrap = $( '#wpwrap' ),
503                 $adminmenu = $( '#adminmenu' ),
504                 $overlay = $( '#wp-responsive-overlay' ),
505                 $toolbar = $( '#wp-toolbar' ),
506                 $toolbarPopups = $toolbar.find( 'a[aria-haspopup="true"]' ),
507                 $sortables = $('.meta-box-sortables'),
508                 stickyMenuActive = false,
509                 wpResponsiveActive = false;
510
511         window.stickyMenu = {
512                 enable: function() {
513                         if ( ! stickyMenuActive ) {
514                                 $document.on( 'wp-window-resized.sticky-menu', $.proxy( this.update, this ) );
515                                 $collapseMenu.on( 'click.sticky-menu', $.proxy( this.update, this ) );
516                                 this.update();
517                                 stickyMenuActive = true;
518                         }
519                 },
520
521                 disable: function() {
522                         if ( stickyMenuActive ) {
523                                 $window.off( 'resize.sticky-menu' );
524                                 $collapseMenu.off( 'click.sticky-menu' );
525                                 $body.removeClass( 'sticky-menu' );
526                                 stickyMenuActive = false;
527                         }
528                 },
529
530                 update: function() {
531                         // Make the admin menu sticky if the viewport is taller than it
532                         if ( $window.height() > $adminMenuWrap.height() + 32 ) {
533                                 if ( ! $body.hasClass( 'sticky-menu' ) ) {
534                                         $body.addClass( 'sticky-menu' );
535                                 }
536                         } else {
537                                 if ( $body.hasClass( 'sticky-menu' ) ) {
538                                         $body.removeClass( 'sticky-menu' );
539                                 }
540                         }
541                 }
542         };
543
544         window.wpResponsive = {
545                 init: function() {
546                         var self = this,
547                                 scrollStart = 0;
548
549                         // Modify functionality based on custom activate/deactivate event
550                         $document.on( 'wp-responsive-activate.wp-responsive', function() {
551                                 self.activate();
552                         }).on( 'wp-responsive-deactivate.wp-responsive', function() {
553                                 self.deactivate();
554                         });
555
556                         // Toggle sidebar when toggle is clicked
557                         $( '#wp-admin-bar-menu-toggle' ).on( 'click.wp-responsive', function( event ) {
558                                 event.preventDefault();
559                                 $wpwrap.toggleClass( 'wp-responsive-open' );
560                         } );
561
562                         // Add menu events
563                         $adminmenu.on( 'touchstart.wp-responsive', 'li.wp-has-submenu > a', function() {
564                                 scrollStart = $window.scrollTop();
565                         }).on( 'touchend.wp-responsive click.wp-responsive', 'li.wp-has-submenu > a', function( event ) {
566                                 if ( ! $adminmenu.data('wp-responsive') ||
567                                         ( event.type === 'touchend' && $window.scrollTop() !== scrollStart ) ) {
568
569                                         return;
570                                 }
571
572                                 $( this ).parent( 'li' ).toggleClass( 'selected' );
573                                 event.preventDefault();
574                         });
575
576                         self.trigger();
577                         $document.on( 'wp-window-resized.wp-responsive', $.proxy( this.trigger, this ) );
578
579                         // This needs to run later as UI Sortable may be initialized later on $(document).ready()
580                         $window.on( 'load.wp-responsive', function() {
581                                 var width = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $window.width() : window.innerWidth;
582
583                                 if ( width <= 782 ) {
584                                         self.disableSortables();
585                                 }
586                         });
587                 },
588
589                 activate: function() {
590                         window.stickyMenu.disable();
591
592                         if ( ! $body.hasClass( 'auto-fold' ) ) {
593                                 $body.addClass( 'auto-fold' );
594                         }
595
596                         $adminmenu.data( 'wp-responsive', 1 );
597                         this.disableSortables();
598                 },
599
600                 deactivate: function() {
601                         window.stickyMenu.enable();
602                         $adminmenu.removeData('wp-responsive');
603                         this.enableSortables();
604                 },
605
606                 trigger: function() {
607                         var width;
608
609                         if ( window.innerWidth ) {
610                                 // window.innerWidth is affected by zooming on phones
611                                 width = Math.max( window.innerWidth, document.documentElement.clientWidth );
612                         } else {
613                                 // Exclude IE < 9, it doesn't support @media CSS rules
614                                 return;
615                         }
616
617                         if ( width <= 782 ) {
618                                 if ( ! wpResponsiveActive ) {
619                                         $document.trigger( 'wp-responsive-activate' );
620                                         wpResponsiveActive = true;
621                                 }
622                         } else {
623                                 if ( wpResponsiveActive ) {
624                                         $document.trigger( 'wp-responsive-deactivate' );
625                                         wpResponsiveActive = false;
626                                 }
627                         }
628
629                         if ( width <= 480 ) {
630                                 this.enableOverlay();
631                         } else {
632                                 this.disableOverlay();
633                         }
634                 },
635
636                 enableOverlay: function() {
637                         if ( $overlay.length === 0 ) {
638                                 $overlay = $( '<div id="wp-responsive-overlay"></div>' )
639                                         .insertAfter( '#wpcontent' )
640                                         .hide()
641                                         .on( 'click.wp-responsive', function() {
642                                                 $toolbar.find( '.menupop.hover' ).removeClass( 'hover' );
643                                                 $( this ).hide();
644                                         });
645                         }
646
647                         $toolbarPopups.on( 'click.wp-responsive', function() {
648                                 $overlay.show();
649                         });
650                 },
651
652                 disableOverlay: function() {
653                         $toolbarPopups.off( 'click.wp-responsive' );
654                         $overlay.hide();
655                 },
656
657                 disableSortables: function() {
658                         if ( $sortables.length ) {
659                                 try {
660                                         $sortables.sortable('disable');
661                                 } catch(e) {}
662                         }
663                 },
664
665                 enableSortables: function() {
666                         if ( $sortables.length ) {
667                                 try {
668                                         $sortables.sortable('enable');
669                                 } catch(e) {}
670                         }
671                 }
672         };
673
674         window.stickyMenu.enable();
675         window.wpResponsive.init();
676 });
677
678 // make Windows 8 devices playing along nicely
679 (function(){
680         if ( '-ms-user-select' in document.documentElement.style && navigator.userAgent.match(/IEMobile\/10\.0/) ) {
681                 var msViewportStyle = document.createElement( 'style' );
682                 msViewportStyle.appendChild(
683                         document.createTextNode( '@-ms-viewport{width:auto!important}' )
684                 );
685                 document.getElementsByTagName( 'head' )[0].appendChild( msViewportStyle );
686         }
687 })();
688
689 // internal use
690 $(document).bind( 'wp_CloseOnEscape', function( e, data ) {
691         if ( typeof(data.cb) != 'function' )
692                 return;
693
694         if ( typeof(data.condition) != 'function' || data.condition() )
695                 data.cb();
696
697         return true;
698 });
699
700 }( jQuery, window ));