github.com/justinjmoses/evergreen@v0.0.0-20170530173719-1d50e381ff0d/public/static/js/directives/directives.confirm.js (about) 1 var directives = directives || {}; 2 3 directives.confirm = angular.module('directives.confirm', []); 4 5 directives.confirm.directive('confirmOnExit', function() { 6 return { 7 scope: { 8 confirmOnExit: '&', 9 confirmMessageWindow: '@', 10 confirmMessageRoute: '@', 11 confirmMessage: '@' 12 }, 13 link: function($scope, elem, attrs) { 14 window.onbeforeunload = function(){ 15 if ($scope.confirmOnExit()) { 16 return $scope.confirmMessageWindow || $scope.confirmMessage; 17 } 18 } 19 var $locationChangeStartUnbind = $scope.$on('$locationChangeStart', function(event, next, current) { 20 if ($scope.confirmOnExit()) { 21 if(! confirm($scope.confirmMessageRoute || $scope.confirmMessage)) { 22 event.preventDefault(); 23 } 24 } 25 }); 26 27 $scope.$on('$destroy', function() { 28 window.onbeforeunload = null; 29 $locationChangeStartUnbind(); 30 }); 31 } 32 }; 33 });