|
| 1 | +angular.module('ui.bootstrap.dropdown', []) |
| 2 | + |
| 3 | +.constant('dropdownConfig', { |
| 4 | + openClass: 'open' |
| 5 | +}) |
| 6 | + |
| 7 | +.service('dropdownService', ['$document', '$rootScope', function($document, $rootScope) { |
| 8 | + var openScope = null; |
| 9 | + |
| 10 | + this.open = function( dropdownScope ) { |
| 11 | + if ( !openScope ) { |
| 12 | + $document.bind('click', closeDropdown); |
| 13 | + $document.bind('keydown', escapeKeyBind); |
| 14 | + } |
| 15 | + |
| 16 | + if ( openScope && openScope !== dropdownScope ) { |
| 17 | + openScope.isOpen = false; |
| 18 | + } |
| 19 | + |
| 20 | + openScope = dropdownScope; |
| 21 | + }; |
| 22 | + |
| 23 | + this.close = function( dropdownScope ) { |
| 24 | + if ( openScope === dropdownScope ) { |
| 25 | + openScope = null; |
| 26 | + $document.unbind('click', closeDropdown); |
| 27 | + $document.unbind('keydown', escapeKeyBind); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + var closeDropdown = function( evt ) { |
| 32 | + // This method may still be called during the same mouse event that |
| 33 | + // unbound this event handler. So check openScope before proceeding. |
| 34 | + if (!openScope) { return; } |
| 35 | + |
| 36 | + if( evt && openScope.getAutoClose() === 'disabled' ) { return ; } |
| 37 | + |
| 38 | + var toggleElement = openScope.getToggleElement(); |
| 39 | + if ( evt && toggleElement && toggleElement[0].contains(evt.target) ) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + var $element = openScope.getElement(); |
| 44 | + if( evt && openScope.getAutoClose() === 'outsideClick' && $element && $element[0].contains(evt.target) ) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + openScope.isOpen = false; |
| 49 | + |
| 50 | + if (!$rootScope.$$phase) { |
| 51 | + openScope.$apply(); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + var escapeKeyBind = function( evt ) { |
| 56 | + if ( evt.which === 27 ) { |
| 57 | + openScope.focusToggleElement(); |
| 58 | + closeDropdown(); |
| 59 | + } |
| 60 | + }; |
| 61 | +}]) |
| 62 | + |
| 63 | +.controller('DropdownController', ['$scope', '$attrs', '$parse', 'dropdownConfig', 'dropdownService', '$animate', function($scope, $attrs, $parse, dropdownConfig, dropdownService, $animate) { |
| 64 | + var self = this, |
| 65 | + scope = $scope.$new(), // create a child scope so we are not polluting original one |
| 66 | + openClass = dropdownConfig.openClass, |
| 67 | + getIsOpen, |
| 68 | + setIsOpen = angular.noop, |
| 69 | + toggleInvoker = $attrs.onToggle ? $parse($attrs.onToggle) : angular.noop; |
| 70 | + |
| 71 | + this.init = function( element ) { |
| 72 | + self.$element = element; |
| 73 | + |
| 74 | + if ( $attrs.isOpen ) { |
| 75 | + getIsOpen = $parse($attrs.isOpen); |
| 76 | + setIsOpen = getIsOpen.assign; |
| 77 | + |
| 78 | + $scope.$watch(getIsOpen, function(value) { |
| 79 | + scope.isOpen = !!value; |
| 80 | + }); |
| 81 | + } |
| 82 | + }; |
| 83 | + |
| 84 | + this.toggle = function( open ) { |
| 85 | + return scope.isOpen = arguments.length ? !!open : !scope.isOpen; |
| 86 | + }; |
| 87 | + |
| 88 | + // Allow other directives to watch status |
| 89 | + this.isOpen = function() { |
| 90 | + return scope.isOpen; |
| 91 | + }; |
| 92 | + |
| 93 | + scope.getToggleElement = function() { |
| 94 | + return self.toggleElement; |
| 95 | + }; |
| 96 | + |
| 97 | + scope.getAutoClose = function() { |
| 98 | + return $attrs.autoClose || 'always'; //or 'outsideClick' or 'disabled' |
| 99 | + }; |
| 100 | + |
| 101 | + scope.getElement = function() { |
| 102 | + return self.$element; |
| 103 | + }; |
| 104 | + |
| 105 | + scope.focusToggleElement = function() { |
| 106 | + if ( self.toggleElement ) { |
| 107 | + self.toggleElement[0].focus(); |
| 108 | + } |
| 109 | + }; |
| 110 | + |
| 111 | + scope.$watch('isOpen', function( isOpen, wasOpen ) { |
| 112 | + $animate[isOpen ? 'addClass' : 'removeClass'](self.$element, openClass); |
| 113 | + |
| 114 | + if ( isOpen ) { |
| 115 | + scope.focusToggleElement(); |
| 116 | + dropdownService.open( scope ); |
| 117 | + } else { |
| 118 | + dropdownService.close( scope ); |
| 119 | + } |
| 120 | + |
| 121 | + setIsOpen($scope, isOpen); |
| 122 | + if (angular.isDefined(isOpen) && isOpen !== wasOpen) { |
| 123 | + toggleInvoker($scope, { open: !!isOpen }); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + $scope.$on('$locationChangeSuccess', function() { |
| 128 | + scope.isOpen = false; |
| 129 | + }); |
| 130 | + |
| 131 | + $scope.$on('$destroy', function() { |
| 132 | + scope.$destroy(); |
| 133 | + }); |
| 134 | +}]) |
| 135 | + |
| 136 | +.directive('dropdown', function() { |
| 137 | + return { |
| 138 | + controller: 'DropdownController', |
| 139 | + link: function(scope, element, attrs, dropdownCtrl) { |
| 140 | + dropdownCtrl.init( element ); |
| 141 | + } |
| 142 | + }; |
| 143 | +}) |
| 144 | + |
| 145 | +.directive('dropdownToggle', function() { |
| 146 | + return { |
| 147 | + require: '?^dropdown', |
| 148 | + link: function(scope, element, attrs, dropdownCtrl) { |
| 149 | + if ( !dropdownCtrl ) { |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + dropdownCtrl.toggleElement = element; |
| 154 | + |
| 155 | + var toggleDropdown = function(event) { |
| 156 | + event.preventDefault(); |
| 157 | + |
| 158 | + if ( !element.hasClass('disabled') && !attrs.disabled ) { |
| 159 | + scope.$apply(function() { |
| 160 | + dropdownCtrl.toggle(); |
| 161 | + }); |
| 162 | + } |
| 163 | + }; |
| 164 | + |
| 165 | + element.bind('click', toggleDropdown); |
| 166 | + |
| 167 | + // WAI-ARIA |
| 168 | + element.attr({ 'aria-haspopup': true, 'aria-expanded': false }); |
| 169 | + scope.$watch(dropdownCtrl.isOpen, function( isOpen ) { |
| 170 | + element.attr('aria-expanded', !!isOpen); |
| 171 | + }); |
| 172 | + |
| 173 | + scope.$on('$destroy', function() { |
| 174 | + element.unbind('click', toggleDropdown); |
| 175 | + }); |
| 176 | + } |
| 177 | + }; |
| 178 | +}); |
0 commit comments