|
| 1 | +(function() { |
| 2 | + |
| 3 | + angular.module("mega-menu", ["ui.bootstrap"]); |
| 4 | + |
| 5 | + angular.module('mega-menu').config(ExtendDropdownToggleDirective); |
| 6 | + |
| 7 | + ExtendDropdownToggleDirective.$inject = ['$provide']; |
| 8 | + |
| 9 | + function ExtendDropdownToggleDirective($provide) { |
| 10 | + |
| 11 | + $provide.decorator('dropdownToggleDirective', ["$delegate", "$document", function($delegate, $document) { |
| 12 | + |
| 13 | + var directive = $delegate[0]; |
| 14 | + |
| 15 | + var link = directive.link; |
| 16 | + |
| 17 | + directive.compile = function() { |
| 18 | + return function(scope, element, attrs, dropdownCtrl) { |
| 19 | + |
| 20 | + if ( !dropdownCtrl ) { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + dropdownCtrl.toggleElement = element; |
| 25 | + |
| 26 | + var toggleDropdown = function(evt) { |
| 27 | + evt.preventDefault(); |
| 28 | + |
| 29 | + if ( evt && element && element[0].parentElement.contains(evt.target) && dropdownCtrl.isOpen() && attrs.eventToggle == "mouseover" ) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + if ( !element.hasClass('disabled') && !attrs.disabled ) { |
| 34 | + scope.$apply(function() { |
| 35 | + dropdownCtrl.toggle(); |
| 36 | + }); |
| 37 | + } |
| 38 | + }; |
| 39 | + |
| 40 | + if(attrs.eventToggle !== "mouseover"){ |
| 41 | + element.bind('click', toggleDropdown); |
| 42 | + } else { |
| 43 | + element.bind('mouseover', toggleDropdown); |
| 44 | + } |
| 45 | + |
| 46 | + // WAI-ARIA |
| 47 | + element.attr({ 'aria-haspopup': true, 'aria-expanded': false }); |
| 48 | + scope.$watch(dropdownCtrl.isOpen, function( isOpen ) { |
| 49 | + element.attr('aria-expanded', !!isOpen); |
| 50 | + }); |
| 51 | + |
| 52 | + scope.$on('$destroy', function() { |
| 53 | + if(attrs.eventToggle !== "mouseover"){ |
| 54 | + element.unbind('click', toggleDropdown); |
| 55 | + } else { |
| 56 | + element.unbind('mouseover', toggleDropdown); |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + }; |
| 61 | + }; |
| 62 | + |
| 63 | + return $delegate; |
| 64 | + }]); |
| 65 | + |
| 66 | + $provide.decorator('dropdownService', ['$delegate', '$document', '$rootScope', function ($delegate, $document, $rootScope) { |
| 67 | + //var openBackup = $delegate.open; |
| 68 | + //var closeBackup = $delegate.close; |
| 69 | + |
| 70 | + //override open method |
| 71 | + $delegate.open = function () { |
| 72 | + open.apply($delegate, arguments); |
| 73 | + }; |
| 74 | + |
| 75 | + //override close method |
| 76 | + $delegate.close = function () { |
| 77 | + close.apply($delegate, arguments); |
| 78 | + }; |
| 79 | + |
| 80 | + |
| 81 | + var openScope = null; |
| 82 | + |
| 83 | + var open = function( dropdownScope ) { |
| 84 | + |
| 85 | + var toggleElement = dropdownScope.getToggleElement(); |
| 86 | + var eventToggle = toggleElement.attr("event-toggle"); |
| 87 | + |
| 88 | + if ( !openScope ) { |
| 89 | + if(eventToggle == 'mouseover'){ |
| 90 | + $document.bind('mouseover', closeDropdown); |
| 91 | + } else{ |
| 92 | + $document.bind('click', closeDropdown); |
| 93 | + $document.bind('keydown', keybindFilter); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ( openScope && openScope !== dropdownScope ) { |
| 98 | + openScope.isOpen = false; |
| 99 | + } |
| 100 | + |
| 101 | + openScope = dropdownScope; |
| 102 | + }; |
| 103 | + |
| 104 | + var close = function( dropdownScope ) { |
| 105 | + var toggleElement = dropdownScope.getToggleElement(); |
| 106 | + var eventToggle = toggleElement.attr("event-toggle"); |
| 107 | + |
| 108 | + if ( openScope === dropdownScope ) { |
| 109 | + openScope = null; |
| 110 | + if(eventToggle == 'mouseover'){ |
| 111 | + $document.unbind('mouseover', closeDropdown); |
| 112 | + } else{ |
| 113 | + $document.unbind('click', closeDropdown); |
| 114 | + $document.unbind('keydown', keybindFilter); |
| 115 | + } |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + var closeDropdown = function( evt ) { |
| 120 | + // This method may still be called during the same mouse event that |
| 121 | + // unbound this event handler. So check openScope before proceeding. |
| 122 | + if (!openScope) { return; } |
| 123 | + |
| 124 | + if( evt && openScope.getAutoClose() === 'disabled' ) { return ; } |
| 125 | + |
| 126 | + var toggleElement = openScope.getToggleElement(); |
| 127 | + if ( evt && toggleElement && toggleElement[0].parentElement.contains(evt.target) ) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + var $element = openScope.getElement(); |
| 132 | + if( evt && openScope.getAutoClose() === 'outsideClick' && $element && $element[0].contains(evt.target) ) { |
| 133 | + return; |
| 134 | + } |
| 135 | + |
| 136 | + openScope.isOpen = false; |
| 137 | + |
| 138 | + if (!$rootScope.$$phase) { |
| 139 | + openScope.$apply(); |
| 140 | + } |
| 141 | + }; |
| 142 | + |
| 143 | + var keybindFilter = function( evt ) { |
| 144 | + if ( evt.which === 27 ) { |
| 145 | + openScope.focusToggleElement(); |
| 146 | + closeDropdown(); |
| 147 | + } |
| 148 | + else if ( openScope.isKeynavEnabled() && /(38|40)/.test(evt.which) && openScope.isOpen ) { |
| 149 | + evt.preventDefault(); |
| 150 | + evt.stopPropagation(); |
| 151 | + openScope.focusDropdownEntry(evt.which); |
| 152 | + } |
| 153 | + }; |
| 154 | + |
| 155 | + return $delegate; |
| 156 | + }]); |
| 157 | + } |
| 158 | + |
| 159 | +})(); |
0 commit comments