Ryan Arnell | 84e7a93 | 2018-12-13 10:50:17 -0600 | [diff] [blame] | 1 | window.angular && (function(angular) { |
| 2 | 'use strict'; |
| 3 | |
beccabroek | 4733a11 | 2019-04-10 14:08:49 -0500 | [diff] [blame] | 4 | angular.module('app.common.directives').directive('clickOutside', [ |
| 5 | '$document', |
| 6 | function($document) { |
| 7 | return { |
| 8 | restrict: 'A', scope: {clickOutside: '&'}, |
| 9 | link: function(scope, el, attr) { |
| 10 | function clickEvent(e) { |
| 11 | if (el !== e.target && !el[0].contains(e.target)) { |
| 12 | scope.$apply(function() { |
| 13 | scope.$eval(scope.clickOutside); |
| 14 | }); |
Ryan Arnell | 84e7a93 | 2018-12-13 10:50:17 -0600 | [diff] [blame] | 15 | } |
Ryan Arnell | 84e7a93 | 2018-12-13 10:50:17 -0600 | [diff] [blame] | 16 | } |
beccabroek | 4733a11 | 2019-04-10 14:08:49 -0500 | [diff] [blame] | 17 | $document.bind('click', clickEvent); |
| 18 | |
| 19 | scope.$on('$destroy', function() { |
| 20 | $document.unbind('click', clickEvent); |
| 21 | }); |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | ]); |
Ryan Arnell | 84e7a93 | 2018-12-13 10:50:17 -0600 | [diff] [blame] | 26 | })(window.angular); |