1window.angular && (function(angular) {
2  'use strict';
3
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                  });
15                }
16              }
17              $document.bind('click', clickEvent);
18
19              scope.$on('$destroy', function() {
20                $document.unbind('click', clickEvent);
21              });
22            }
23      }
24    }
25  ]);
26})(window.angular);