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