Skip to content

Commit

Permalink
fix: remove memory leak in click-outside
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Jun 29, 2020
1 parent 7d313d2 commit fc0a7d4
Showing 1 changed file with 26 additions and 29 deletions.
55 changes: 26 additions & 29 deletions app/assets/javascripts/directives/functional/click-outside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,35 @@ export function clickOutside($document: ng.IDocumentService) {
return {
restrict: 'A',
replace: false,
link: function ($scope: ng.IScope, $element: JQLite, attrs: any) {
// Causes memory leak as-is:
// let didApplyClickOutside = false;
link($scope: ng.IScope, $element: JQLite, attrs: any) {
let didApplyClickOutside = false;

// $scope.$on('$destroy', () => {
// attrs.clickOutside = null;
// $element.unbind('click', $scope.onElementClick);
// $document.unbind('click', $scope.onDocumentClick);
// $scope.onElementClick = null;
// $scope.onDocumentClick = null;
// });
function onElementClick(event: JQueryEventObject) {
didApplyClickOutside = false;
if (attrs.isOpen) {
event.stopPropagation();
}
}

// $scope.onElementClick = (event) => {
// didApplyClickOutside = false;
// if (attrs.isOpen) {
// event.stopPropagation();
// }
// };

// $scope.onDocumentClick = (event) => {
// /* Ignore click if on SKAlert */
// if (event.target.closest('.sk-modal')) {
// return;
// }
// if (!didApplyClickOutside) {
// $scope.$apply(attrs.clickOutside);
// didApplyClickOutside = true;
// }
// };
function onDocumentClick(event: JQueryEventObject) {
/** Ignore click if on SKAlert */
if (event.target.closest('.sk-modal')) {
return;
}
if (!didApplyClickOutside) {
$scope.$apply(attrs.clickOutside);
didApplyClickOutside = true;
}
};

// $element.bind('click', $scope.onElementClick);
// $document.bind('click', $scope.onDocumentClick);
$scope.$on('$destroy', () => {
attrs.clickOutside = undefined;
$element.unbind('click', onElementClick);
$document.unbind('click', onDocumentClick);
});

$element.bind('click', onElementClick);
$document.bind('click', onDocumentClick);
}
};
}

0 comments on commit fc0a7d4

Please sign in to comment.