|
1 |
| -/* |
2 |
| - * dropdownToggle - Provides dropdown menu functionality in place of bootstrap js |
3 |
| - * @restrict class or attribute |
4 |
| - * @example: |
5 |
| - <li class="dropdown"> |
6 |
| - <a class="dropdown-toggle">My Dropdown Menu</a> |
7 |
| - <ul class="dropdown-menu"> |
8 |
| - <li ng-repeat="choice in dropChoices"> |
9 |
| - <a ng-href="{{choice.href}}">{{choice.text}}</a> |
10 |
| - </li> |
11 |
| - </ul> |
12 |
| - </li> |
13 |
| - */ |
14 |
| - |
15 |
| -angular.module('ui.bootstrap.dropdownToggle', []).directive('dropdownToggle', ['$document', function ($document) { |
16 |
| - var openElement = null, |
17 |
| - closeMenu = angular.noop; |
| 1 | +angular.module('ui.bootstrap.dropdownToggle', []) |
| 2 | + |
| 3 | +.constant('dropdownConfig', { |
| 4 | + openClass: 'open' |
| 5 | +}) |
| 6 | + |
| 7 | +.service('dropdownService', ['$document', function($document) { |
| 8 | + var self = this, openScope = null; |
| 9 | + |
| 10 | + this.open = function( dropdownScope ) { |
| 11 | + if ( !openScope ) { |
| 12 | + $document.bind('click', closeDropdown); |
| 13 | + } |
| 14 | + |
| 15 | + if ( openScope && openScope !== dropdownScope ) { |
| 16 | + openScope.isOpen = false; |
| 17 | + } |
| 18 | + |
| 19 | + openScope = dropdownScope; |
| 20 | + }; |
| 21 | + |
| 22 | + this.close = function( dropdownScope ) { |
| 23 | + if ( openScope === dropdownScope ) { |
| 24 | + openScope = null; |
| 25 | + $document.unbind('click', closeDropdown); |
| 26 | + } |
| 27 | + }; |
| 28 | + |
| 29 | + var closeDropdown = function() { |
| 30 | + openScope.$apply(function() { |
| 31 | + openScope.isOpen = false; |
| 32 | + }); |
| 33 | + }; |
| 34 | +}]) |
| 35 | + |
| 36 | +.controller('DropdownController', ['$scope', '$attrs', 'dropdownConfig', 'dropdownService', function($scope, $attrs, dropdownConfig, dropdownService) { |
| 37 | + var self = this, openClass = dropdownConfig.openClass; |
| 38 | + |
| 39 | + this.init = function( element ) { |
| 40 | + self.$element = element; |
| 41 | + $scope.isOpen = angular.isDefined($attrs.isOpen) ? $scope.$parent.$eval($attrs.isOpen) : false; |
| 42 | + }; |
| 43 | + |
| 44 | + this.toggle = function( open ) { |
| 45 | + return $scope.isOpen = arguments.length ? !!open : !$scope.isOpen; |
| 46 | + }; |
| 47 | + |
| 48 | + $scope.$watch('isOpen', function( value ) { |
| 49 | + self.$element.toggleClass( openClass, value ); |
| 50 | + |
| 51 | + if ( value ) { |
| 52 | + dropdownService.open( $scope ); |
| 53 | + } else { |
| 54 | + dropdownService.close( $scope ); |
| 55 | + } |
| 56 | + |
| 57 | + $scope.onToggle({ open: !!value }); |
| 58 | + }); |
| 59 | + |
| 60 | + $scope.$on('$locationChangeSuccess', function() { |
| 61 | + $scope.isOpen = false; |
| 62 | + }); |
| 63 | +}]) |
| 64 | + |
| 65 | +.directive('dropdown', function() { |
18 | 66 | return {
|
19 | 67 | restrict: 'CA',
|
20 |
| - link: function(scope, element, attrs) { |
21 |
| - scope.$on('$locationChangeSuccess', function() { closeMenu(); }); |
22 |
| - element.parent().bind('click', function() { closeMenu(); }); |
23 |
| - element.bind('click', function (event) { |
| 68 | + controller: 'DropdownController', |
| 69 | + scope: { |
| 70 | + isOpen: '=?', |
| 71 | + onToggle: '&' |
| 72 | + }, |
| 73 | + link: function(scope, element, attrs, dropdownCtrl) { |
| 74 | + dropdownCtrl.init( element ); |
| 75 | + } |
| 76 | + }; |
| 77 | +}) |
24 | 78 |
|
25 |
| - var elementWasOpen = (element === openElement); |
| 79 | +.directive('dropdownToggle', function() { |
| 80 | + return { |
| 81 | + restrict: 'CA', |
| 82 | + require: '?^dropdown', |
| 83 | + link: function(scope, element, attrs, dropdownCtrl) { |
| 84 | + if ( !dropdownCtrl ) { |
| 85 | + return; |
| 86 | + } |
26 | 87 |
|
| 88 | + element.bind('click', function(event) { |
27 | 89 | event.preventDefault();
|
28 | 90 | event.stopPropagation();
|
29 | 91 |
|
30 |
| - if (!!openElement) { |
31 |
| - closeMenu(); |
32 |
| - } |
33 |
| - |
34 |
| - if (!elementWasOpen && !element.hasClass('disabled') && !element.prop('disabled')) { |
35 |
| - element.parent().addClass('open'); |
36 |
| - openElement = element; |
37 |
| - closeMenu = function (event) { |
38 |
| - if (event) { |
39 |
| - event.preventDefault(); |
40 |
| - event.stopPropagation(); |
41 |
| - } |
42 |
| - $document.unbind('click', closeMenu); |
43 |
| - element.parent().removeClass('open'); |
44 |
| - closeMenu = angular.noop; |
45 |
| - openElement = null; |
46 |
| - }; |
47 |
| - $document.bind('click', closeMenu); |
| 92 | + if ( !element.hasClass('disabled') && !element.prop('disabled') ) { |
| 93 | + scope.$apply(function() { |
| 94 | + dropdownCtrl.toggle(); |
| 95 | + }); |
48 | 96 | }
|
49 | 97 | });
|
50 | 98 | }
|
51 | 99 | };
|
52 |
| -}]); |
| 100 | +}); |
0 commit comments