1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives')
5      .directive('toggleFlag', function($document) {
6        return {
7          restrict: 'A',
8          link: function(scope, element, attrs) {
9
10            function elementClick(e) {
11              e.stopPropagation();
12            }
13
14            function documentClick(e) {
15              scope[attrs.toggleFlag] = false;
16              scope.$apply();
17            }
18
19            element.on('click', elementClick);
20            $document.on('click', documentClick);
21
22            // remove event handlers when directive is destroyed
23            scope.$on('$destroy', function() {
24              element.off('click', elementClick);
25              $document.off('click', documentClick);
26            });
27          }
28        };
29      });
30})(window.angular);
31