]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.js
WordPress 3.6.1
[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 ), respWidth;
173
174                 // reset any compensation for submenus near the bottom of the screen
175                 $('#adminmenu div.wp-submenu').css('margin-top', '');
176
177                 // WebKit excludes the width of the vertical scrollbar when applying the CSS "@media screen and (max-width: ...)"
178                 // and matches $(window).width().
179                 // Firefox and IE > 8 include the scrollbar width, so after the jQuery normalization
180                 // $(window).width() is 884px but window.innerWidth is 900px.
181                 // (using window.innerWidth also excludes IE < 9)
182                 respWidth = navigator.userAgent.indexOf('AppleWebKit/') > -1 ? $(window).width() : window.innerWidth;
183
184                 if ( respWidth && respWidth < 900 ) {
185                         if ( body.hasClass('auto-fold') ) {
186                                 body.removeClass('auto-fold').removeClass('folded');
187                                 setUserSetting('unfold', 1);
188                                 setUserSetting('mfold', 'o');
189                         } else {
190                                 body.addClass('auto-fold');
191                                 setUserSetting('unfold', 0);
192                         }
193                 } else {
194                         if ( body.hasClass('folded') ) {
195                                 body.removeClass('folded');
196                                 setUserSetting('mfold', 'o');
197                         } else {
198                                 body.addClass('folded');
199                                 setUserSetting('mfold', 'f');
200                         }
201                 }
202         });
203
204         if ( 'ontouchstart' in window || /IEMobile\/[1-9]/.test(navigator.userAgent) ) { // touch screen device
205                 // iOS Safari works with touchstart, the rest work with click
206                 mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
207
208                 // close any open submenus when touch/click is not on the menu
209                 $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
210                         if ( !$(e.target).closest('#adminmenu').length )
211                                 menu.find('li.wp-has-submenu.opensub').removeClass('opensub');
212                 });
213
214                 menu.find('a.wp-has-submenu').on( mobileEvent+'.wp-mobile-hover', function(e) {
215                         var el = $(this), parent = el.parent();
216
217                         // Show the sub instead of following the link if:
218                         //      - the submenu is not open
219                         //      - the submenu is not shown inline or the menu is not folded
220                         if ( !parent.hasClass('opensub') && ( !parent.hasClass('wp-menu-open') || parent.width() < 40 ) ) {
221                                 e.preventDefault();
222                                 menu.find('li.opensub').removeClass('opensub');
223                                 parent.addClass('opensub');
224                         }
225                 });
226         }
227
228         menu.find('li.wp-has-submenu').hoverIntent({
229                 over: function(e){
230                         var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop, top = parseInt( m.css('top'), 10 );
231
232                         if ( isNaN(top) || top > -5 ) // meaning the submenu is visible
233                                 return;
234
235                         menutop = $(this).offset().top;
236                         wintop = $(window).scrollTop();
237                         maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
238
239                         b = menutop + m.height() + 1; // Bottom offset of the menu
240                         h = $('#wpwrap').height(); // Height of the entire page
241                         o = 60 + b - h;
242                         f = $(window).height() + wintop - 15; // The fold
243
244                         if ( f < (b - o) )
245                                 o = b - f;
246
247                         if ( o > maxtop )
248                                 o = maxtop;
249
250                         if ( o > 1 )
251                                 m.css('margin-top', '-'+o+'px');
252                         else
253                                 m.css('margin-top', '');
254
255                         menu.find('li.menu-top').removeClass('opensub');
256                         $(this).addClass('opensub');
257                 },
258                 out: function(){
259                         $(this).removeClass('opensub').find('.wp-submenu').css('margin-top', '');
260                 },
261                 timeout: 200,
262                 sensitivity: 7,
263                 interval: 90
264         });
265
266         menu.on('focus.adminmenu', '.wp-submenu a', function(e){
267                 $(e.target).closest('li.menu-top').addClass('opensub');
268         }).on('blur.adminmenu', '.wp-submenu a', function(e){
269                 $(e.target).closest('li.menu-top').removeClass('opensub');
270         });
271
272         // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
273         $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
274         $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
275
276         // Init screen meta
277         screenMeta.init();
278
279         // check all checkboxes
280         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
281                 if ( 'undefined' == e.shiftKey ) { return true; }
282                 if ( e.shiftKey ) {
283                         if ( !lastClicked ) { return true; }
284                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
285                         first = checks.index( lastClicked );
286                         last = checks.index( this );
287                         checked = $(this).prop('checked');
288                         if ( 0 < first && 0 < last && first != last ) {
289                                 checks.slice( first, last ).prop( 'checked', function(){
290                                         if ( $(this).closest('tr').is(':visible') )
291                                                 return checked;
292
293                                         return false;
294                                 });
295                         }
296                 }
297                 lastClicked = this;
298
299                 // toggle "check all" checkboxes
300                 var unchecked = $(this).closest('tbody').find(':checkbox').filter(':visible').not(':checked');
301                 $(this).closest('table').children('thead, tfoot').find(':checkbox').prop('checked', function() {
302                         return ( 0 == unchecked.length );
303                 });
304
305                 return true;
306         });
307
308         $('thead, tfoot').find('.check-column :checkbox').click( function(e) {
309                 var c = $(this).prop('checked'),
310                         kbtoggle = 'undefined' == typeof toggleWithKeyboard ? false : toggleWithKeyboard,
311                         toggle = e.shiftKey || kbtoggle;
312
313                 $(this).closest( 'table' ).children( 'tbody' ).filter(':visible')
314                 .children().children('.check-column').find(':checkbox')
315                 .prop('checked', function() {
316                         if ( $(this).is(':hidden') )
317                                 return false;
318                         if ( toggle )
319                                 return $(this).prop( 'checked' );
320                         else if (c)
321                                 return true;
322                         return false;
323                 });
324
325                 $(this).closest('table').children('thead,  tfoot').filter(':visible')
326                 .children().children('.check-column').find(':checkbox')
327                 .prop('checked', function() {
328                         if ( toggle )
329                                 return false;
330                         else if (c)
331                                 return true;
332                         return false;
333                 });
334         });
335
336         $('#default-password-nag-no').click( function() {
337                 setUserSetting('default_password_nag', 'hide');
338                 $('div.default-password-nag').hide();
339                 return false;
340         });
341
342         // tab in textareas
343         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
344                 var el = e.target, selStart, selEnd, val, scroll, sel;
345
346                 if ( e.keyCode == 27 ) { // escape key
347                         $(el).data('tab-out', true);
348                         return;
349                 }
350
351                 if ( e.keyCode != 9 || e.ctrlKey || e.altKey || e.shiftKey ) // tab key
352                         return;
353
354                 if ( $(el).data('tab-out') ) {
355                         $(el).data('tab-out', false);
356                         return;
357                 }
358
359                 selStart = el.selectionStart;
360                 selEnd = el.selectionEnd;
361                 val = el.value;
362
363                 try {
364                         this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event. See blur handler below.
365                 } catch(err) {}
366
367                 if ( document.selection ) {
368                         el.focus();
369                         sel = document.selection.createRange();
370                         sel.text = '\t';
371                 } else if ( selStart >= 0 ) {
372                         scroll = this.scrollTop;
373                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
374                         el.selectionStart = el.selectionEnd = selStart + 1;
375                         this.scrollTop = scroll;
376                 }
377
378                 if ( e.stopPropagation )
379                         e.stopPropagation();
380                 if ( e.preventDefault )
381                         e.preventDefault();
382         });
383
384         $('#newcontent').bind('blur.wpevent_InsertTab', function(e) {
385                 if ( this.lastKey && 9 == this.lastKey )
386                         this.focus();
387         });
388
389         if ( pageInput.length ) {
390                 pageInput.closest('form').submit( function(e){
391
392                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
393                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
394                                 pageInput.val('1');
395                 });
396         }
397
398         // Scroll into view when focused
399         $('#contextual-help-link, #show-settings-link').on( 'focus.scroll-into-view', function(e){
400                 if ( e.target.scrollIntoView )
401                         e.target.scrollIntoView(false);
402         });
403
404         // Disable upload buttons until files are selected
405         (function(){
406                 var button, input, form = $('form.wp-upload-form');
407                 if ( ! form.length )
408                         return;
409                 button = form.find('input[type="submit"]');
410                 input = form.find('input[type="file"]');
411
412                 function toggleUploadButton() {
413                         button.prop('disabled', '' === input.map( function() {
414                                 return $(this).val();
415                         }).get().join(''));
416                 }
417                 toggleUploadButton();
418                 input.on('change', toggleUploadButton);
419         })();
420 });
421
422 // internal use
423 $(document).bind( 'wp_CloseOnEscape', function( e, data ) {
424         if ( typeof(data.cb) != 'function' )
425                 return;
426
427         if ( typeof(data.condition) != 'function' || data.condition() )
428                 data.cb();
429
430         return true;
431 });
432
433 })(jQuery);