]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/js/common.dev.js
Wordpress 3.2.1
[autoinstalls/wordpress.git] / wp-admin / js / common.dev.js
1 var showNotice, adminMenu, columns, validateForm, screenMeta;
2 (function($){
3 // sidebar admin menu
4 adminMenu = {
5         init : function() {
6                 var menu = $('#adminmenu');
7
8                 $('.wp-menu-toggle', menu).each( function() {
9                         var t = $(this), sub = t.siblings('.wp-submenu');
10                         if ( sub.length )
11                                 t.click(function(){ adminMenu.toggle( sub ); });
12                         else
13                                 t.hide();
14                 });
15
16                 this.favorites();
17
18                 $('#collapse-menu', menu).click(function(){
19                         if ( $('body').hasClass('folded') ) {
20                                 adminMenu.fold(1);
21                                 deleteUserSetting( 'mfold' );
22                         } else {
23                                 adminMenu.fold();
24                                 setUserSetting( 'mfold', 'f' );
25                         }
26                         return false;
27                 });
28
29                 if ( $('body').hasClass('folded') )
30                         this.fold();
31         },
32
33         restoreMenuState : function() {
34                 // (perhaps) needed for back-compat
35         },
36
37         toggle : function(el) {
38                 el.slideToggle(150, function() {
39                         var id = el.css('display','').parent().toggleClass( 'wp-menu-open' ).attr('id');
40                         if ( id ) {
41                                 $('li.wp-has-submenu', '#adminmenu').each(function(i, e) {
42                                         if ( id == e.id ) {
43                                                 var v = $(e).hasClass('wp-menu-open') ? 'o' : 'c';
44                                                 setUserSetting( 'm'+i, v );
45                                         }
46                                 });
47                         }
48                 });
49
50                 return false;
51         },
52
53         fold : function(off) {
54                 if (off) {
55                         $('body').removeClass('folded');
56                         $('#adminmenu li.wp-has-submenu').unbind();
57                 } else {
58                         $('body').addClass('folded');
59                         $('#adminmenu li.wp-has-submenu').hoverIntent({
60                                 over: function(e){
61                                         var m, b, h, o, f;
62                                         m = $(this).find('.wp-submenu');
63                                         b = $(this).offset().top + m.height() + 1; // Bottom offset of the menu
64                                         h = $('#wpwrap').height(); // Height of the entire page
65                                         o = 60 + b - h;
66                                         f = $(window).height() + $(window).scrollTop() - 15; // The fold
67                                         if ( f < (b - o) ) {
68                                                 o = b - f;
69                                         }
70                                         if ( o > 1 ) {
71                                                 m.css({'marginTop':'-'+o+'px'});
72                                         } else if ( m.css('marginTop') ) {
73                                                 m.css({'marginTop':''});
74                                         }
75                                         m.addClass('sub-open');
76                                 },
77                                 out: function(){
78                                         $(this).find('.wp-submenu').removeClass('sub-open');
79                                 },
80                                 timeout: 220,
81                                 sensitivity: 8,
82                                 interval: 100
83                         });
84
85                 }
86         },
87
88         favorites : function() {
89                 $('#favorite-inside').width( $('#favorite-actions').width() - 4 );
90                 $('#favorite-toggle, #favorite-inside').bind('mouseenter', function() {
91                         $('#favorite-inside').removeClass('slideUp').addClass('slideDown');
92                         setTimeout(function() {
93                                 if ( $('#favorite-inside').hasClass('slideDown') ) {
94                                         $('#favorite-inside').slideDown(100);
95                                         $('#favorite-first').addClass('slide-down');
96                                 }
97                         }, 200);
98                 }).bind('mouseleave', function() {
99                         $('#favorite-inside').removeClass('slideDown').addClass('slideUp');
100                         setTimeout(function() {
101                                 if ( $('#favorite-inside').hasClass('slideUp') ) {
102                                         $('#favorite-inside').slideUp(100, function() {
103                                                 $('#favorite-first').removeClass('slide-down');
104                                         });
105                                 }
106                         }, 300);
107                 });
108         }
109 };
110
111 $(document).ready(function(){ adminMenu.init(); });
112
113 // show/hide/save table columns
114 columns = {
115         init : function() {
116                 var that = this;
117                 $('.hide-column-tog', '#adv-settings').click( function() {
118                         var $t = $(this), column = $t.val();
119                         if ( $t.prop('checked') )
120                                 that.checked(column);
121                         else
122                                 that.unchecked(column);
123
124                         columns.saveManageColumnsState();
125                 });
126         },
127
128         saveManageColumnsState : function() {
129                 var hidden = this.hidden();
130                 $.post(ajaxurl, {
131                         action: 'hidden-columns',
132                         hidden: hidden,
133                         screenoptionnonce: $('#screenoptionnonce').val(),
134                         page: pagenow
135                 });
136         },
137
138         checked : function(column) {
139                 $('.column-' + column).show();
140                 this.colSpanChange(+1);
141         },
142
143         unchecked : function(column) {
144                 $('.column-' + column).hide();
145                 this.colSpanChange(-1);
146         },
147
148         hidden : function() {
149                 return $('.manage-column').filter(':hidden').map(function() { return this.id; }).get().join(',');
150         },
151
152         useCheckboxesForHidden : function() {
153                 this.hidden = function(){
154                         return $('.hide-column-tog').not(':checked').map(function() {
155                                 var id = this.id;
156                                 return id.substring( id, id.length - 5 );
157                         }).get().join(',');
158                 };
159         },
160
161         colSpanChange : function(diff) {
162                 var $t = $('table').find('.colspanchange'), n;
163                 if ( !$t.length )
164                         return;
165                 n = parseInt( $t.attr('colspan'), 10 ) + diff;
166                 $t.attr('colspan', n.toString());
167         }
168 }
169
170 $(document).ready(function(){columns.init();});
171
172 validateForm = function( form ) {
173         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();
174 }
175
176 // stub for doing better warnings
177 showNotice = {
178         warn : function() {
179                 var msg = commonL10n.warnDelete || '';
180                 if ( confirm(msg) ) {
181                         return true;
182                 }
183
184                 return false;
185         },
186
187         note : function(text) {
188                 alert(text);
189         }
190 };
191
192 screenMeta = {
193         links: {
194                 'screen-options-link-wrap': 'screen-options-wrap',
195                 'contextual-help-link-wrap': 'contextual-help-wrap'
196         },
197         init: function() {
198                 $('.screen-meta-toggle').click( screenMeta.toggleEvent );
199         },
200         toggleEvent: function( e ) {
201                 var panel;
202                 e.preventDefault();
203
204                 // Check to see if we found a panel.
205                 if ( ! screenMeta.links[ this.id ] )
206                         return;
207
208                 panel = $('#' + screenMeta.links[ this.id ]);
209
210                 if ( panel.is(':visible') )
211                         screenMeta.close( panel, $(this) );
212                 else
213                         screenMeta.open( panel, $(this) );
214         },
215         open: function( panel, link ) {
216                 $('.screen-meta-toggle').not( link ).css('visibility', 'hidden');
217
218                 panel.slideDown( 'fast', function() {
219                         link.addClass('screen-meta-active');
220                 });
221         },
222         close: function( panel, link ) {
223                 panel.slideUp( 'fast', function() {
224                         link.removeClass('screen-meta-active');
225                         $('.screen-meta-toggle').css('visibility', '');
226                 });
227         }
228 };
229
230 $(document).ready( function() {
231         var lastClicked = false, checks, first, last, checked, dropdown,
232                 pageInput = $('input[name="paged"]'), currentPage;
233
234         // Move .updated and .error alert boxes. Don't move boxes designed to be inline.
235         $('div.wrap h2:first').nextAll('div.updated, div.error').addClass('below-h2');
236         $('div.updated, div.error').not('.below-h2, .inline').insertAfter( $('div.wrap h2:first') );
237
238         // Init screen meta
239         screenMeta.init();
240
241         // User info dropdown.
242         dropdown = {
243                 doc: $(document),
244                 element: $('#user_info'),
245                 open: function() {
246                         if ( ! dropdown.element.hasClass('active') ) {
247                                 dropdown.element.addClass('active');
248                                 dropdown.doc.one( 'click', dropdown.close );
249                                 return false;
250                         }
251                 },
252                 close: function() {
253                         dropdown.element.removeClass('active');
254                 }
255         };
256
257         dropdown.element.click( dropdown.open );
258
259         // check all checkboxes
260         $('tbody').children().children('.check-column').find(':checkbox').click( function(e) {
261                 if ( 'undefined' == e.shiftKey ) { return true; }
262                 if ( e.shiftKey ) {
263                         if ( !lastClicked ) { return true; }
264                         checks = $( lastClicked ).closest( 'form' ).find( ':checkbox' );
265                         first = checks.index( lastClicked );
266                         last = checks.index( this );
267                         checked = $(this).prop('checked');
268                         if ( 0 < first && 0 < last && first != last ) {
269                                 checks.slice( first, last ).prop( 'checked', function(){
270                                         if ( $(this).closest('tr').is(':visible') )
271                                                 return checked;
272
273                                         return false;
274                                 });
275                         }
276                 }
277                 lastClicked = this;
278                 return true;
279         });
280
281         $('thead, tfoot').find('.check-column :checkbox').click( function(e) {
282                 var c = $(this).prop('checked'),
283                         kbtoggle = 'undefined' == typeof toggleWithKeyboard ? false : toggleWithKeyboard,
284                         toggle = e.shiftKey || kbtoggle;
285
286                 $(this).closest( 'table' ).children( 'tbody' ).filter(':visible')
287                 .children().children('.check-column').find(':checkbox')
288                 .prop('checked', function() {
289                         if ( $(this).closest('tr').is(':hidden') )
290                                 return false;
291                         if ( toggle )
292                                 return $(this).prop( 'checked' );
293                         else if (c)
294                                 return true;
295                         return false;
296                 });
297
298                 $(this).closest('table').children('thead,  tfoot').filter(':visible')
299                 .children().children('.check-column').find(':checkbox')
300                 .prop('checked', function() {
301                         if ( toggle )
302                                 return false;
303                         else if (c)
304                                 return true;
305                         return false;
306                 });
307         });
308
309         $('#default-password-nag-no').click( function() {
310                 setUserSetting('default_password_nag', 'hide');
311                 $('div.default-password-nag').hide();
312                 return false;
313         });
314
315         // tab in textareas
316         $('#newcontent').bind('keydown.wpevent_InsertTab', function(e) {
317                 if ( e.keyCode != 9 )
318                         return true;
319
320                 var el = e.target, selStart = el.selectionStart, selEnd = el.selectionEnd, val = el.value, scroll, sel;
321
322                 try {
323                         this.lastKey = 9; // not a standard DOM property, lastKey is to help stop Opera tab event.  See blur handler below.
324                 } catch(err) {}
325
326                 if ( document.selection ) {
327                         el.focus();
328                         sel = document.selection.createRange();
329                         sel.text = '\t';
330                 } else if ( selStart >= 0 ) {
331                         scroll = this.scrollTop;
332                         el.value = val.substring(0, selStart).concat('\t', val.substring(selEnd) );
333                         el.selectionStart = el.selectionEnd = selStart + 1;
334                         this.scrollTop = scroll;
335                 }
336
337                 if ( e.stopPropagation )
338                         e.stopPropagation();
339                 if ( e.preventDefault )
340                         e.preventDefault();
341         });
342
343         $('#newcontent').bind('blur.wpevent_InsertTab', function(e) {
344                 if ( this.lastKey && 9 == this.lastKey )
345                         this.focus();
346         });
347
348         if ( pageInput.length ) {
349                 currentPage = pageInput.val();
350                 pageInput.closest('form').submit( function(){
351                         // Reset paging var for new filters/searches. See #17685.
352                         if ( pageInput.val() == currentPage )
353                                 pageInput.val('1');
354                 });
355         }
356
357 });
358
359 // internal use
360 $(document).bind( 'wp_CloseOnEscape', function( e, data ) {
361         if ( typeof(data.cb) != 'function' )
362                 return;
363
364         if ( typeof(data.condition) != 'function' || data.condition() )
365                 data.cb();
366
367         return true;
368 });
369
370 })(jQuery);