From e441add3695060665f7a7c29e9279f1c166271bd Mon Sep 17 00:00:00 2001 From: Pem Taira Date: Sun, 1 Oct 2017 14:29:17 +0800 Subject: [PATCH] fix: add way to stop propagation when an item is clicked Closes #105 --- contextMenu.js | 13 ++++++++++--- demo/demo.js | 8 ++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/contextMenu.js b/contextMenu.js index e2a65aa..e7934fa 100644 --- a/contextMenu.js +++ b/contextMenu.js @@ -196,9 +196,11 @@ angular.module('ui.bootstrap.contextMenu', []) if($event.which == 1) { $event.preventDefault(); $scope.$apply(function () { - $(event.currentTarget).removeClass('context'); - removeAllContextMenus(); + var cleanupFunction = function () { + $(event.currentTarget).removeClass('context'); + removeAllContextMenus(); + }; var clickFunction = angular.isFunction(item.click) ? item.click : (angular.isFunction(item[1]) @@ -206,7 +208,12 @@ angular.module('ui.bootstrap.contextMenu', []) : null); if (clickFunction) { - clickFunction.call($scope, $scope, event, modelValue, text, $li); + var res = clickFunction.call($scope, $scope, event, modelValue, text, $li); + if(res === undefined || res) { + cleanupFunction(); + } + } else { + cleanupFunction(); } }); } diff --git a/demo/demo.js b/demo/demo.js index b07e626..ab418ff 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -42,6 +42,14 @@ angular.module('app', ['ui.bootstrap.contextMenu']) alert('A new implementation based on objects'); } }, + { + text: 'Clicking this does not close the context menu', + click: function () { + // Returning false or any false-y value EXCEPT undefined will stop + // the context menu from closing. + return false; + } + }, { text: 'Object-based with Submenu', click: function() {