Website Summary Files Commits Branches @commit @diff @stats
path: root / wp-content / themes / carmag / assets / src / scripts / inc / navigation.js
blob: 23327cade1e918c268ddc6d7cf3e5f3f9e8488fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
jQuery( document ).ready( function ( $ ) {
	'use strict';

	/**
	 * The actual plugin for mobile dropdown
	 */
	var mobileDropdown = (function () {

		var settings = {
			heightHeader: 80,
			heightAdminbar: 50
		};

		// The selector variables.
		var selector = {
			menuIcon: $( ".menu-toggle" ),
			dropdownArrow: $( "span.dropdown-arrow" ),
			menuNav: $('.main-navigation'),
			wavesButtonSelector: '.button'
		};

		var events = function () {

			// When header is clicked.
			selector.menuIcon.on( 'click', openMenu );


			// Dropdown menu links
			$('li.menu-item-has-children').on( 'click', $(this).closest('span.dropdown-arrow'), openDropdownMenu );

		};

		var openMenu = function ( event ) {
			var headerHeight = settings.heightHeader;
			var adminbarHeight = settings.heightAdminbar;

			// No need for this when you are not logged in.
			if ( ! $("body").hasClass("admin-bar") ) {
				adminbarHeight = 0;
			}

			// Change button state
			$( this ).toggleClass( 'is-opened' );

			// Open the main menu.
			selector.menuNav.toggleClass( 'is-extended' ).removeClass('init');
			// selector.menuNav.height( $(window).height() - headerHeight - adminbarHeight );

			$('body').toggleClass( 'is-menu-opened' );
		};

		var openDropdownMenu = function ( event ) {
			event.stopImmediatePropagation(); // Fix issue with double clicking.
			$(this).toggleClass( "is-extended" );
		};

		var buildDropdownArrow = function () {
			$('.menu-item-has-children').each(function() {
				$(this).append("<span class='dropdown-arrow'><i class='fa fa-angle-right'></i></span>");
			});
		};

		/**
		 * Run the dropdown menu.
		 */
		var initialiseDropdown = function() {
			buildDropdownArrow();
			events();
		};

		return {
			init: initialiseDropdown
		};
	})();

	mobileDropdown.init();
});