]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.dev.js
Wordpress 3.3
[autoinstalls/wordpress.git] / wp-admin / js / common.dev.js
1 var showNotice, adminMenu, columns, validateForm, screenMeta, autofold_menu;
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                         link.addClass('screen-meta-active');
125                 });
126         },
127
128         close: function( panel, link ) {
129                 panel.slideUp( 'fast', function() {
130                         link.removeClass('screen-meta-active');
131                         $('.screen-meta-toggle').css('visibility', '');
132                         panel.parent().hide();
133                 });
134         }
135 };
136
137 /**
138  * Help tabs.
139  */
140 $('.contextual-help-tabs').delegate('a', 'click focus', function(e) {
141         var link = $(this),
142                 panel;
143
144         e.preventDefault();
145
146         // Don't do anything if the click is for the tab already showing.
147         if ( link.is('.active a') )
148                 return false;
149
150         // Links
151         $('.contextual-help-tabs .active').removeClass('active');
152         link.parent('li').addClass('active');
153
154         panel = $( link.attr('href') );
155
156         // Panels
157         $('.help-tab-content').not( panel ).removeClass('active').hide();
158         panel.addClass('active').show();
159 });
160
161 $(document).ready( function() {
162         var lastClicked = false, checks, first, last, checked, menu = $('#adminmenu'),
163                 pageInput = $('input.current-page'), currentPage = pageInput.val(), folded, refresh;
164
165         // admin menu
166         refresh = function(i, el){ // force the browser to refresh the tabbing index
167                 var node = $(el), tab = node.attr('tabindex');
168                 if ( tab )
169                         node.attr('tabindex', '0').attr('tabindex', tab);
170         };
171
172         $('#collapse-menu', menu).click(function(){
173                 var body = $(document.body);
174
175                 if ( body.hasClass('folded') ) {
176                         body.removeClass('folded');
177                         setUserSetting('mfold', 'o');
178                 } else {
179                         body.addClass('folded');
180                         setUserSetting('mfold', 'f');
181                 }
182                 return false;
183         });
184
185         $('li.wp-has-submenu', menu).hoverIntent({
186                 over: function(e){
187                         var b, h, o, f, m = $(this).find('.wp-submenu'), menutop, wintop, maxtop;
188
189                         if ( !$(document.body).hasClass('folded') && $(this).hasClass('wp-menu-open') )
190                                 return;
191
192                         menutop = $(this).offset().top;
193                         wintop = $(window).scrollTop();
194                         maxtop = menutop - wintop - 30; // max = make the top of the sub almost touch admin bar
195
196                         b = menutop + m.height() + 1; // Bottom offset of the menu
197                         h = $('#wpwrap').height(); // Height of the entire page
198                         o = 60 + b - h;
199                         f = $(window).height() + wintop - 15; // The fold
200
201                         if ( f < (b - o) )
202                                 o = b - f;
203
204                         if ( o > maxtop )
205                                 o = maxtop;
206
207                         if ( o > 1 )
208                                 m.css({'marginTop':'-'+o+'px'});
209                         else if ( m.css('marginTop') )
210                                 m.css({'marginTop':''});
211
212                         m.addClass('sub-open');
213                 },
214                 out: function(){
215                         $(this).find('.wp-submenu').removeClass('sub-open');
216                 },
217                 timeout: 200,
218                 sensitivity: 7,
219                 interval: 90
220         });
221
222         // Tab to select, Enter to open sub, Esc to close sub and focus the top menu
223         $('li.wp-has-submenu > a.wp-not-current-submenu', menu).bind('keydown.adminmenu', function(e){
224                 if ( e.which != 13 )
225                         return;
226
227                 var target = $(e.target);
228
229                 e.stopPropagation();
230                 e.preventDefault();
231
232                 menu.find('.wp-submenu').removeClass('sub-open');
233                 target.siblings('.wp-submenu').toggleClass('sub-open').find('a[role="menuitem"]').each(refresh);
234         }).each(refresh);
235
236         $('a[role="menuitem"]', menu).bind('keydown.adminmenu', function(e){
237                 if ( e.which != 27 )
238                         return;
239
240                 var target = $(e.target);
241
242                 e.stopPropagation();
243                 e.preventDefault();
244
245                 target.add( target.siblings() ).closest('.sub-open').removeClass('sub-open').siblings('a.wp-not-current-submenu').focus();
246         });
247
248         // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
249         $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
250         $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
251
252         // Init screen meta
253         screenMeta.init();
254
255         // check all checkboxes
256         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
257                 if ( 'undefined' == e.shiftKey ) { return true; }
258                 if ( e.shiftKey ) {
259                         if ( !lastClicked ) { return true; }
260                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
261                         first = checks.index( lastClicked );
262                         last = checks.index( this );
263                         checked = $(this).prop('checked');
264                         if ( 0 < first && 0 < last && first != last ) {
265                                 checks.slice( first, last ).prop( 'checked', function(){
266                                         if ( $(this).closest('tr').is(':visible') )
267                                                 return checked;
268
269                                         return false;
270                                 });
271                         }
272                 }
273                 lastClicked = this;
274                 return true;
275         });
276
277         $('thead, tfoot').find('.check-column :checkbox').click( function(e) {
278                 var c = $(this).prop('checked'),
279                         kbtoggle = 'undefined' == typeof toggleWithKeyboard ? false : toggleWithKeyboard,
280                         toggle = e.shiftKey || kbtoggle;
281
282                 $(this).closest( 'table' ).children( 'tbody' ).filter(':visible')
283                 .children().children('.check-column').find(':checkbox')
284                 .prop('checked', function() {
285                         if ( $(this).closest('tr').is(':hidden') )
286                                 return false;
287                         if ( toggle )
288                                 return $(this).prop( 'checked' );
289                         else if (c)
290                                 return true;
291                         return false;
292                 });
293
294                 $(this).closest('table').children('thead,  tfoot').filter(':visible')
295                 .children().children('.check-column').find(':checkbox')
296                 .prop('checked', function() {
297                         if ( toggle )
298                                 return false;
299                         else if (c)
300                                 return true;
301                         return false;
302                 });
303         });
304
305         $('#default-password-nag-no').click( function() {
306                 setUserSetting('default_password_nag', 'hide');
307                 $('div.default-password-nag').hide();
308                 return false;
309         });
310
311         // tab in textareas
312         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
313                 if ( e.keyCode != 9 )
314                         return true;
315
316                 var el = e.target, selStart = el.selectionStart, selEnd = el.selectionEnd, val = el.value, scroll, sel;
317
318                 try {
319                         this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event.  See blur handler below.
320                 } catch(err) {}
321
322                 if ( document.selection ) {
323                         el.focus();
324                         sel = document.selection.createRange();
325                         sel.text = '\t';
326                 } else if ( selStart >= 0 ) {
327                         scroll = this.scrollTop;
328                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
329                         el.selectionStart = el.selectionEnd = selStart + 1;
330                         this.scrollTop = scroll;
331                 }
332
333                 if ( e.stopPropagation )
334                         e.stopPropagation();
335                 if ( e.preventDefault )
336                         e.preventDefault();
337         });
338
339         $('#newcontent').bind('blur.wpevent_InsertTab', function(e) {
340                 if ( this.lastKey && 9 == this.lastKey )
341                         this.focus();
342         });
343
344         if ( pageInput.length ) {
345                 pageInput.closest('form').submit( function(e){
346
347                         // Reset paging var for new filters/searches but not for bulk actions. See #17685.
348                         if ( $('select[name="action"]').val() == -1 && $('select[name="action2"]').val() == -1 && pageInput.val() == currentPage )
349                                 pageInput.val('1');
350                 });
351         }
352
353         // auto-fold the menu when screen is under 800px
354         $(window).bind('resize.autofold', function(){
355                 if ( getUserSetting('mfold') == 'f' )
356                         return;
357
358                 var width = $(window).width();
359
360                 // fold admin menu
361                 if ( width <= 800 ) {
362                         if ( !folded ) {
363                                 $(document.body).addClass('folded');
364                                 folded = true;
365                         }
366                 } else {
367                         if ( folded ) {
368                                 $(document.body).removeClass('folded');
369                                 folded = false;
370                         }
371                 }
372
373         }).triggerHandler('resize');
374
375 });
376
377 // internal use
378 $(document).bind( 'wp_CloseOnEscape', function( e, data ) {
379         if ( typeof(data.cb) != 'function' )
380                 return;
381
382         if ( typeof(data.condition) != 'function' || data.condition() )
383                 data.cb();
384
385         return true;
386 });
387
388 })(jQuery);