]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/admin-bar.js
Wordpress 3.5
[autoinstalls/wordpress.git] / wp-includes / js / admin-bar.js
1 // use jQuery and hoverIntent if loaded
2 if ( typeof(jQuery) != 'undefined' ) {
3         if ( typeof(jQuery.fn.hoverIntent) == 'undefined' )
4                 (function(a){a.fn.hoverIntent=function(l,j){var m={sensitivity:7,interval:100,timeout:0};m=a.extend(m,j?{over:l,out:j}:l);var o,n,h,d;var e=function(f){o=f.pageX;n=f.pageY};var c=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);if((Math.abs(h-o)+Math.abs(d-n))<m.sensitivity){a(f).unbind("mousemove",e);f.hoverIntent_s=1;return m.over.apply(f,[g])}else{h=o;d=n;f.hoverIntent_t=setTimeout(function(){c(g,f)},m.interval)}};var i=function(g,f){f.hoverIntent_t=clearTimeout(f.hoverIntent_t);f.hoverIntent_s=0;return m.out.apply(f,[g])};var b=function(q){var f=this;var g=(q.type=="mouseover"?q.fromElement:q.toElement)||q.relatedTarget;while(g&&g!=this){try{g=g.parentNode}catch(q){g=this}}if(g==this){if(a.browser.mozilla){if(q.type=="mouseout"){f.mtout=setTimeout(function(){k(q,f)},30)}else{if(f.mtout){f.mtout=clearTimeout(f.mtout)}}}return}else{if(f.mtout){f.mtout=clearTimeout(f.mtout)}k(q,f)}};var k=function(p,f){var g=jQuery.extend({},p);if(f.hoverIntent_t){f.hoverIntent_t=clearTimeout(f.hoverIntent_t)}if(p.type=="mouseover"){h=g.pageX;d=g.pageY;a(f).bind("mousemove",e);if(f.hoverIntent_s!=1){f.hoverIntent_t=setTimeout(function(){c(g,f)},m.interval)}}else{a(f).unbind("mousemove",e);if(f.hoverIntent_s==1){f.hoverIntent_t=setTimeout(function(){i(g,f)},m.timeout)}}};return this.mouseover(b).mouseout(b)}})(jQuery);
5
6         jQuery(document).ready(function($){
7                 var adminbar = $('#wpadminbar'), refresh, touchOpen, touchClose, disableHoverIntent = false;
8
9                 refresh = function(i, el){ // force the browser to refresh the tabbing index
10                         var node = $(el), tab = node.attr('tabindex');
11                         if ( tab )
12                                 node.attr('tabindex', '0').attr('tabindex', tab);
13                 };
14
15                 touchOpen = function(unbind) {
16                         adminbar.find('li.menupop').on('click.wp-mobile-hover', function(e) {
17                                 var el = $(this);
18
19                                 if ( !el.hasClass('hover') ) {
20                                         e.preventDefault();
21                                         adminbar.find('li.menupop.hover').removeClass('hover');
22                                         el.addClass('hover');
23                                 }
24
25                                 if ( unbind ) {
26                                         $('li.menupop').off('click.wp-mobile-hover');
27                                         disableHoverIntent = false;
28                                 }
29                         });
30                 };
31
32                 touchClose = function() {
33                         var mobileEvent = /Mobile\/.+Safari/.test(navigator.userAgent) ? 'touchstart' : 'click';
34                         // close any open drop-downs when the click/touch is not on the toolbar
35                         $(document.body).on( mobileEvent+'.wp-mobile-hover', function(e) {
36                                 if ( !$(e.target).closest('#wpadminbar').length )
37                                         adminbar.find('li.menupop.hover').removeClass('hover');
38                         });
39                 };
40
41                 adminbar.removeClass('nojq').removeClass('nojs');
42
43                 if ( 'ontouchstart' in window ) {
44                         adminbar.on('touchstart', function(){
45                                 touchOpen(true);
46                                 disableHoverIntent = true;
47                         });
48                         touchClose();
49                 } else if ( /IEMobile\/[1-9]/.test(navigator.userAgent) ) {
50                         touchOpen();
51                         touchClose();
52                 }
53
54                 adminbar.find('li.menupop').hoverIntent({
55                         over: function(e){
56                                 if ( disableHoverIntent )
57                                         return;
58
59                                 $(this).addClass('hover');
60                         },
61                         out: function(e){
62                                 if ( disableHoverIntent )
63                                         return;
64
65                                 $(this).removeClass('hover');
66                         },
67                         timeout: 180,
68                         sensitivity: 7,
69                         interval: 100
70                 });
71
72                 if ( window.location.hash )
73                         window.scrollBy( 0, -32 );
74
75                 $('#wp-admin-bar-get-shortlink').click(function(e){
76                         e.preventDefault();
77                         $(this).addClass('selected').children('.shortlink-input').blur(function(){
78                                 $(this).parents('#wp-admin-bar-get-shortlink').removeClass('selected');
79                         }).focus().select();
80                 });
81
82                 $('#wpadminbar li.menupop > .ab-item').bind('keydown.adminbar', function(e){
83                         if ( e.which != 13 )
84                                 return;
85
86                         var target = $(e.target), wrap = target.closest('ab-sub-wrapper');
87
88                         e.stopPropagation();
89                         e.preventDefault();
90
91                         if ( !wrap.length )
92                                 wrap = $('#wpadminbar .quicklinks');
93
94                         wrap.find('.menupop').removeClass('hover');
95                         target.parent().toggleClass('hover');
96                         target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh);
97                 }).each(refresh);
98
99                 $('#wpadminbar .ab-item').bind('keydown.adminbar', function(e){
100                         if ( e.which != 27 )
101                                 return;
102
103                         var target = $(e.target);
104
105                         e.stopPropagation();
106                         e.preventDefault();
107
108                         target.closest('.hover').removeClass('hover').children('.ab-item').focus();
109                         target.siblings('.ab-sub-wrapper').find('.ab-item').each(refresh);
110                 });
111
112                 $('#wpadminbar').click( function(e) {
113                         if ( e.target.id != 'wpadminbar' && e.target.id != 'wp-admin-bar-top-secondary' )
114                                 return;
115
116                         e.preventDefault();
117                         $('html, body').animate({ scrollTop: 0 }, 'fast');
118                 });
119
120                 // fix focus bug in WebKit
121                 $('.screen-reader-shortcut').keydown( function(e) {
122                         if ( 13 != e.which )
123                                 return;
124
125                         var id = $(this).attr('href');
126
127                         if ( $.browser.webkit && id && id.charAt(0) == '#' ) {
128                                 setTimeout(function () {
129                                         $(id).focus();
130                                 }, 100);
131                         }
132                 });
133         });
134 } else {
135         (function(d, w) {
136                 var addEvent = function( obj, type, fn ) {
137                         if ( obj.addEventListener )
138                                 obj.addEventListener(type, fn, false);
139                         else if ( obj.attachEvent )
140                                 obj.attachEvent('on' + type, function() { return fn.call(obj, window.event);});
141                 },
142
143                 aB, hc = new RegExp('\\bhover\\b', 'g'), q = [],
144                 rselected = new RegExp('\\bselected\\b', 'g'),
145
146                 /**
147                  * Get the timeout ID of the given element
148                  */
149                 getTOID = function(el) {
150                         var i = q.length;
151                         while ( i-- ) {
152                                 if ( q[i] && el == q[i][1] )
153                                         return q[i][0];
154                         }
155                         return false;
156                 },
157
158                 addHoverClass = function(t) {
159                         var i, id, inA, hovering, ul, li,
160                                 ancestors = [],
161                                 ancestorLength = 0;
162
163                         while ( t && t != aB && t != d ) {
164                                 if ( 'LI' == t.nodeName.toUpperCase() ) {
165                                         ancestors[ ancestors.length ] = t;
166                                         id = getTOID(t);
167                                         if ( id )
168                                                 clearTimeout( id );
169                                         t.className = t.className ? ( t.className.replace(hc, '') + ' hover' ) : 'hover';
170                                         hovering = t;
171                                 }
172                                 t = t.parentNode;
173                         }
174
175                         // Remove any selected classes.
176                         if ( hovering && hovering.parentNode ) {
177                                 ul = hovering.parentNode;
178                                 if ( ul && 'UL' == ul.nodeName.toUpperCase() ) {
179                                         i = ul.childNodes.length;
180                                         while ( i-- ) {
181                                                 li = ul.childNodes[i];
182                                                 if ( li != hovering )
183                                                         li.className = li.className ? li.className.replace( rselected, '' ) : '';
184                                         }
185                                 }
186                         }
187
188                         /* remove the hover class for any objects not in the immediate element's ancestry */
189                         i = q.length;
190                         while ( i-- ) {
191                                 inA = false;
192                                 ancestorLength = ancestors.length;
193                                 while( ancestorLength-- ) {
194                                         if ( ancestors[ ancestorLength ] == q[i][1] )
195                                                 inA = true;
196                                 }
197
198                                 if ( ! inA )
199                                         q[i][1].className = q[i][1].className ? q[i][1].className.replace(hc, '') : '';
200                         }
201                 },
202
203                 removeHoverClass = function(t) {
204                         while ( t && t != aB && t != d ) {
205                                 if ( 'LI' == t.nodeName.toUpperCase() ) {
206                                         (function(t) {
207                                                 var to = setTimeout(function() {
208                                                         t.className = t.className ? t.className.replace(hc, '') : '';
209                                                 }, 500);
210                                                 q[q.length] = [to, t];
211                                         })(t);
212                                 }
213                                 t = t.parentNode;
214                         }
215                 },
216
217                 clickShortlink = function(e) {
218                         var i, l, node,
219                                 t = e.target || e.srcElement;
220
221                         // Make t the shortlink menu item, or return.
222                         while ( true ) {
223                                 // Check if we've gone past the shortlink node,
224                                 // or if the user is clicking on the input.
225                                 if ( ! t || t == d || t == aB )
226                                         return;
227                                 // Check if we've found the shortlink node.
228                                 if ( t.id && t.id == 'wp-admin-bar-get-shortlink' )
229                                         break;
230                                 t = t.parentNode;
231                         }
232
233                         // IE doesn't support preventDefault, and does support returnValue
234                         if ( e.preventDefault )
235                                 e.preventDefault();
236                         e.returnValue = false;
237
238                         if ( -1 == t.className.indexOf('selected') )
239                                 t.className += ' selected';
240
241                         for ( i = 0, l = t.childNodes.length; i < l; i++ ) {
242                                 node = t.childNodes[i];
243                                 if ( node.className && -1 != node.className.indexOf('shortlink-input') ) {
244                                         node.focus();
245                                         node.select();
246                                         node.onblur = function() {
247                                                 t.className = t.className ? t.className.replace( rselected, '' ) : '';
248                                         };
249                                         break;
250                                 }
251                         }
252                         return false;
253                 },
254
255                 scrollToTop = function(t) {
256                         var distance, speed, step, steps, timer, speed_step;
257
258                         // Ensure that the #wpadminbar was the target of the click.
259                         if ( t.id != 'wpadminbar' && t.id != 'wp-admin-bar-top-secondary' )
260                                 return;
261
262                         distance    = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
263
264                         if ( distance < 1 )
265                                 return;
266
267                         speed_step = distance > 800 ? 130 : 100;
268                         speed     = Math.min( 12, Math.round( distance / speed_step ) );
269                         step      = distance > 800 ? Math.round( distance / 30  ) : Math.round( distance / 20  );
270                         steps     = [];
271                         timer     = 0;
272
273                         // Animate scrolling to the top of the page by generating steps to
274                         // the top of the page and shifting to each step at a set interval.
275                         while ( distance ) {
276                                 distance -= step;
277                                 if ( distance < 0 )
278                                         distance = 0;
279                                 steps.push( distance );
280
281                                 setTimeout( function() {
282                                         window.scrollTo( 0, steps.shift() );
283                                 }, timer * speed );
284
285                                 timer++;
286                         }
287                 };
288
289                 addEvent(w, 'load', function() {
290                         aB = d.getElementById('wpadminbar');
291
292                         if ( d.body && aB ) {
293                                 d.body.appendChild( aB );
294
295                                 if ( aB.className )
296                                         aB.className = aB.className.replace(/nojs/, '');
297
298                                 addEvent(aB, 'mouseover', function(e) {
299                                         addHoverClass( e.target || e.srcElement );
300                                 });
301
302                                 addEvent(aB, 'mouseout', function(e) {
303                                         removeHoverClass( e.target || e.srcElement );
304                                 });
305
306                                 addEvent(aB, 'click', clickShortlink );
307
308                                 addEvent(aB, 'click', function(e) {
309                                         scrollToTop( e.target || e.srcElement );
310                                 });
311                         }
312
313                         if ( w.location.hash )
314                                 w.scrollBy(0,-32);
315                 });
316         })(document, window);
317
318 }