/** * @ngdoc fbClickOutside * @name fasit.component.fbClickOutside * @function * * @description * Används för att trigga en callback om kan klickar utanför det här elementet * */ angular.module('fasit').directive('fbClickOutside', ['$document', function ($document: ng.IDocumentService) { 'use strict'; var events = 'click touchstart' var link = function (scope, element: ng.IRootElementService, attrs: any) { scope.closingHandler = function (e) { // Avkommentera följande för att debugga html som inte har satt condition för fb-click-outside /*if (!attrs['condition']) { console.log('fbClickoutside', element, 'missing condition'); }*/ var skipClickOutside = attrs.triggedFromDropdownMenu && $(e.target).parents('.dropdown-menu').parent()[0] === element.parent()[0]; if (!element.is(e.target) && element.has(e.target).length === 0 && !skipClickOutside) { scope.$apply(function () { scope.fbClickOutside(); }); } }; if (_.isUndefined(attrs.condition)) { // Om inget condition är satt anropas closingHandler på varje klick i dokumentet, ej rekommenderat $document.on(events, scope.closingHandler); } else { // Lyssna endast på global click omm condition är true scope.$watch('condition', function (newVal) { if (newVal) { $document.on(events, scope.closingHandler); } else { $document.off(events, scope.closingHandler); } }); } scope.$on('$destroy', function () { $document.off(events, scope.closingHandler); }); }; return { restrict: 'A', link: link, scope: { fbClickOutside: '&', condition: '=?' } } }]);