wp/wp-content/themes/twentythirteen/js/functions.js
author ymh <ymh.work@gmail.com>
Tue, 09 Jun 2015 03:35:32 +0200
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
permissions -rw-r--r--
upgrade wordpress + plugins
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
 * Functionality specific to Twenty Thirteen.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * Provides helper functions to enhance the theme experience.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
( function( $ ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
	var body    = $( 'body' ),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     9
	    _window = $( window ),
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    10
		nav, button, menu;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    11
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    12
	nav = $( '#site-navigation' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
	button = nav.find( '.menu-toggle' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    14
	menu = nav.find( '.nav-menu' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	 * Adds a top margin to the footer if the sidebar widget area is higher
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	 * than the rest of the page, to help the footer always visually clear
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
	 * the sidebar.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
	$( function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
		if ( body.is( '.sidebar' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
			var sidebar   = $( '#secondary .widget-area' ),
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    24
			    secondary = ( 0 === sidebar.length ) ? -40 : sidebar.height(),
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
			    margin    = $( '#tertiary .widget-area' ).height() - $( '#content' ).height() - secondary;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    27
			if ( margin > 0 && _window.innerWidth() > 999 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
				$( '#colophon' ).css( 'margin-top', margin + 'px' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	} );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	 * Enables menu toggle for small screens.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	( function() {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
		if ( ! nav || ! button ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
			return;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
		// Hide button if menu is missing or empty.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
		if ( ! menu || ! menu.children().length ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
			button.hide();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
		button.on( 'click.twentythirteen', function() {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
			nav.toggleClass( 'toggled-on' );
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    49
			if ( nav.hasClass( 'toggled-on' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    50
				$( this ).attr( 'aria-expanded', 'true' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    51
				menu.attr( 'aria-expanded', 'true' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    52
			} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    53
				$( this ).attr( 'aria-expanded', 'false' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    54
				menu.attr( 'aria-expanded', 'false' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    55
			}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    56
		} );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    57
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    58
		// Fix sub-menus for touch devices.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    59
		if ( 'ontouchstart' in window ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    60
			menu.find( '.menu-item-has-children > a, .page_item_has_children > a' ).on( 'touchstart.twentythirteen', function( e ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    61
				var el = $( this ).parent( 'li' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    63
				if ( ! el.hasClass( 'focus' ) ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    64
					e.preventDefault();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    65
					el.toggleClass( 'focus' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    66
					el.siblings( '.focus' ).removeClass( 'focus' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    67
				}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    68
			} );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    69
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    70
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    71
		// Better focus for hidden submenu items for accessibility.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    72
		menu.find( 'a' ).on( 'focus.twentythirteen blur.twentythirteen', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    73
			$( this ).parents( '.menu-item, .page_item' ).toggleClass( 'focus' );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
		} );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	} )();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
	/**
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    78
	 * @summary Add or remove ARIA attributes.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    79
	 * Uses jQuery's width() function to determine the size of the window and add
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    80
	 * the default ARIA attributes for the menu toggle if it's visible.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    81
	 * @since Twenty Thirteen 1.5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    82
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    83
	function onResizeARIA() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    84
		if ( 643 > _window.width() ) {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    85
			button.attr( 'aria-expanded', 'false' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    86
			menu.attr( 'aria-expanded', 'false' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    87
			button.attr( 'aria-controls', 'primary-menu' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    88
		} else {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    89
			button.removeAttr( 'aria-expanded' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    90
			menu.removeAttr( 'aria-expanded' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    91
			button.removeAttr( 'aria-controls' );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    92
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    93
	}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    95
	_window
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    96
		.on( 'load.twentythirteen', onResizeARIA )
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    97
		.on( 'resize.twentythirteen', function() {
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    98
			onResizeARIA();
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    99
	} );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   100
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   101
	/**
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	 * Makes "skip to content" link work correctly in IE9 and Chrome for better
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	 * accessibility.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	 * @link http://www.nczonline.net/blog/2013/01/15/fixing-skip-to-content-links/
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	_window.on( 'hashchange.twentythirteen', function() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		var element = document.getElementById( location.hash.substring( 1 ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
		if ( element ) {
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   111
			if ( ! /^(?:a|select|input|button|textarea)$/i.test( element.tagName ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
				element.tabIndex = -1;
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   113
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
			element.focus();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	} );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	 * Arranges footer widgets vertically.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	if ( $.isFunction( $.fn.masonry ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
		var columnWidth = body.is( '.sidebar' ) ? 228 : 245;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
		$( '#secondary .widget-area' ).masonry( {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
			itemSelector: '.widget',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
			columnWidth: columnWidth,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
			gutterWidth: 20,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
			isRTL: body.is( '.rtl' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
		} );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
} )( jQuery );