| 1 |
<?php |
| 2 |
|
| 3 |
namespace ResponsiveMenu\Mappers; |
| 4 |
use ResponsiveMenu\Collections\OptionsCollection as OptionsCollection; |
| 5 |
|
| 6 |
class JsMapper |
| 7 |
{ |
| 8 |
public function __construct(OptionsCollection $options) |
| 9 |
{ |
| 10 |
$this->options = $options; |
| 11 |
} |
| 12 |
|
| 13 |
public function map() |
| 14 |
{ |
| 15 |
$animation_speed = $this->options['animation_speed'] ? $this->options['animation_speed']->getValue() * 1000 : 500; |
| 16 |
|
| 17 |
$js = <<<JS |
| 18 |
|
| 19 |
jQuery(document).ready(function($) { |
| 20 |
|
| 21 |
var ResponsiveMenu = { |
| 22 |
trigger: '{$this->options['button_click_trigger']}', |
| 23 |
animationSpeed: {$animation_speed}, |
| 24 |
breakpoint: {$this->options['breakpoint']}, |
| 25 |
pushButton: '{$this->options['button_push_with_animation']}', |
| 26 |
animationType: '{$this->options['animation_type']}', |
| 27 |
animationSide: '{$this->options['menu_appear_from']}', |
| 28 |
pageWrapper: '{$this->options['page_wrapper']}', |
| 29 |
isOpen: false, |
| 30 |
triggerTypes: 'click', |
| 31 |
activeClass: 'is-active', |
| 32 |
container: '#responsive-menu-container', |
| 33 |
openClass: 'responsive-menu-open', |
| 34 |
accordion: '{$this->options['accordion_animation']}', |
| 35 |
activeArrow: '{$this->options->getActiveArrow()}', |
| 36 |
inactiveArrow: '{$this->options->getInActiveArrow()}', |
| 37 |
wrapper: '#responsive-menu-wrapper', |
| 38 |
closeOnBodyClick: '{$this->options['menu_close_on_body_click']}', |
| 39 |
closeOnLinkClick: '{$this->options['menu_close_on_link_click']}', |
| 40 |
itemTriggerSubMenu: '{$this->options['menu_item_click_to_trigger_submenu']}', |
| 41 |
linkElement: '.responsive-menu-item-link', |
| 42 |
openMenu: function() { |
| 43 |
$(this.trigger).addClass(this.activeClass); |
| 44 |
$('html').addClass(this.openClass); |
| 45 |
$('.responsive-menu-button-icon-active').hide(); |
| 46 |
$('.responsive-menu-button-icon-inactive').show(); |
| 47 |
this.setWrapperTranslate(); |
| 48 |
this.isOpen = true; |
| 49 |
}, |
| 50 |
closeMenu: function() { |
| 51 |
$(this.trigger).removeClass(this.activeClass); |
| 52 |
$('html').removeClass(this.openClass); |
| 53 |
$('.responsive-menu-button-icon-inactive').hide(); |
| 54 |
$('.responsive-menu-button-icon-active').show(); |
| 55 |
this.clearWrapperTranslate(); |
| 56 |
this.isOpen = false; |
| 57 |
}, |
| 58 |
triggerMenu: function() { |
| 59 |
this.isOpen ? this.closeMenu() : this.openMenu(); |
| 60 |
}, |
| 61 |
triggerSubArrow: function(subarrow) { |
| 62 |
var sub_menu = $(subarrow).parent().next('.responsive-menu-submenu'); |
| 63 |
var self = this; |
| 64 |
if(this.accordion == 'on') { |
| 65 |
/* Get Top Most Parent and the siblings */ |
| 66 |
var top_siblings = sub_menu.parents('.responsive-menu-item-has-children').last().siblings('.responsive-menu-item-has-children'); |
| 67 |
var first_siblings = sub_menu.parents('.responsive-menu-item-has-children').first().siblings('.responsive-menu-item-has-children'); |
| 68 |
/* Close up just the top level parents to key the rest as it was */ |
| 69 |
top_siblings.children('.responsive-menu-submenu').slideUp(200, 'linear').removeClass('responsive-menu-submenu-open'); |
| 70 |
/* Set each parent arrow to inactive */ |
| 71 |
top_siblings.each(function() { |
| 72 |
$(this).find('.responsive-menu-subarrow').first().html(self.inactiveArrow); |
| 73 |
}); |
| 74 |
/* Now Repeat for the current item siblings */ |
| 75 |
first_siblings.children('.responsive-menu-submenu').slideUp(200, 'linear').removeClass('responsive-menu-submenu-open'); |
| 76 |
first_siblings.each(function() { |
| 77 |
$(this).find('.responsive-menu-subarrow').first().html(self.inactiveArrow); |
| 78 |
}); |
| 79 |
} |
| 80 |
if(sub_menu.hasClass('responsive-menu-submenu-open')) { |
| 81 |
sub_menu.slideUp(200, 'linear').removeClass('responsive-menu-submenu-open'); |
| 82 |
$(subarrow).html(this.inactiveArrow); |
| 83 |
} else { |
| 84 |
sub_menu.slideDown(200, 'linear').addClass('responsive-menu-submenu-open'); |
| 85 |
$(subarrow).html(this.activeArrow); |
| 86 |
} |
| 87 |
}, |
| 88 |
menuHeight: function() { |
| 89 |
return $(this.container).height(); |
| 90 |
}, |
| 91 |
menuWidth: function() { |
| 92 |
return $(this.container).width(); |
| 93 |
}, |
| 94 |
wrapperHeight: function() { |
| 95 |
return $(this.wrapper).height(); |
| 96 |
}, |
| 97 |
setWrapperTranslate: function() { |
| 98 |
switch(this.animationSide) { |
| 99 |
case 'left': |
| 100 |
translate = 'translateX(' + this.menuWidth() + 'px)'; break; |
| 101 |
case 'right': |
| 102 |
translate = 'translateX(-' + this.menuWidth() + 'px)'; break; |
| 103 |
case 'top': |
| 104 |
translate = 'translateY(' + this.wrapperHeight() + 'px)'; break; |
| 105 |
case 'bottom': |
| 106 |
translate = 'translateY(-' + this.menuHeight() + 'px)'; break; |
| 107 |
} |
| 108 |
if(this.animationType == 'push') { |
| 109 |
$(this.pageWrapper).css({'transform':translate}); |
| 110 |
$('html, body').css('overflow-x', 'hidden'); |
| 111 |
} |
| 112 |
if(this.pushButton == 'on') { |
| 113 |
$('#responsive-menu-button').css({'transform':translate}); |
| 114 |
} |
| 115 |
}, |
| 116 |
clearWrapperTranslate: function() { |
| 117 |
self = this; |
| 118 |
if(this.animationType == 'push') { |
| 119 |
$(this.pageWrapper).css({'transform':''}); |
| 120 |
setTimeout(function() { |
| 121 |
$('html, body').css('overflow-x', ''); |
| 122 |
}, self.animationSpeed); |
| 123 |
} |
| 124 |
if(this.pushButton == 'on') { |
| 125 |
$('#responsive-menu-button').css({'transform':''}); |
| 126 |
} |
| 127 |
}, |
| 128 |
init: function() { |
| 129 |
var self = this; |
| 130 |
$(this.trigger).on(this.triggerTypes, function(e){ |
| 131 |
e.stopPropagation(); |
| 132 |
self.triggerMenu(); |
| 133 |
}); |
| 134 |
$('.responsive-menu-subarrow').on('click', function(e) { |
| 135 |
e.preventDefault(); |
| 136 |
e.stopPropagation(); |
| 137 |
self.triggerSubArrow(this); |
| 138 |
}); |
| 139 |
$(window).resize(function() { |
| 140 |
if($(window).width() > self.breakpoint) { |
| 141 |
if(self.isOpen){ |
| 142 |
self.closeMenu(); |
| 143 |
} |
| 144 |
} else { |
| 145 |
if($('.responsive-menu-open').length>0){ |
| 146 |
self.setWrapperTranslate(); |
| 147 |
} |
| 148 |
} |
| 149 |
}); |
| 150 |
if(this.closeOnLinkClick == 'on') { |
| 151 |
$(this.linkElement).on('click', function(e) { |
| 152 |
e.preventDefault(); |
| 153 |
old_href = $(this).attr('href'); |
| 154 |
if(self.isOpen) { |
| 155 |
if($(e.target).closest('.responsive-menu-subarrow').length) { |
| 156 |
return; |
| 157 |
} |
| 158 |
self.closeMenu(); |
| 159 |
setTimeout(function() { |
| 160 |
window.location = old_href; |
| 161 |
}, self.animationSpeed); |
| 162 |
} |
| 163 |
}); |
| 164 |
} |
| 165 |
if(this.closeOnBodyClick == 'on') { |
| 166 |
$(document).on('click', 'body', function(e) { |
| 167 |
if(self.isOpen) { |
| 168 |
if($(e.target).closest('#responsive-menu-container').length || $(e.target).closest('#responsive-menu-button').length) { |
| 169 |
return; |
| 170 |
} |
| 171 |
} |
| 172 |
self.closeMenu(); |
| 173 |
}); |
| 174 |
} |
| 175 |
if(this.itemTriggerSubMenu == 'on') { |
| 176 |
$('.responsive-menu-item-has-children > ' + this.linkElement).on('click', function(e) { |
| 177 |
e.preventDefault(); |
| 178 |
self.triggerSubArrow($(this).children('.responsive-menu-subarrow').first()); |
| 179 |
}); |
| 180 |
} |
| 181 |
} |
| 182 |
}; |
| 183 |
ResponsiveMenu.init(); |
| 184 |
}); |
| 185 |
|
| 186 |
JS; |
| 187 |
|
| 188 |
return $js; |
| 189 |
|
| 190 |
} |
| 191 |
|
| 192 |
} |
| 193 |
|