summaryrefslogtreecommitdiffstats
path: root/app/common/directives/click-outside.js
blob: 35248ee05c04fb4ea0e3c3bdd67bbf8ef1ed959b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
window.angular && (function(angular) {
  'use strict';

  angular.module('app.common.directives').directive('clickOutside', [
    '$document',
    function($document) {
      return {
        restrict: 'A', scope: {clickOutside: '&'},
            link: function(scope, el, attr) {
              function clickEvent(e) {
                if (el !== e.target && !el[0].contains(e.target)) {
                  scope.$apply(function() {
                    scope.$eval(scope.clickOutside);
                  });
                }
              }
              $document.bind('click', clickEvent);

              scope.$on('$destroy', function() {
                $document.unbind('click', clickEvent);
              });
            }
      }
    }
  ]);
})(window.angular);
OpenPOWER on IntegriCloud