');
- if (item === null) {
- $li.addClass('divider');
- } else if (typeof item[0] === "object") {
- custom.initialize($li, item);
- } else {
- processItem($scope, event, modelValue, item, $ul, $li, $promises, $q, $, level);
- }
- $ul.append($li);
- });
+ handlePromises($ul, level, event, $promises);
- var height = Math.max(
- document.body.scrollHeight, document.documentElement.scrollHeight,
- document.body.offsetHeight, document.documentElement.offsetHeight,
- document.body.clientHeight, document.documentElement.clientHeight
- );
- $(document).find('body').append($ul);
+ function removeOnScrollEvent(e) {
+ removeAllContextMenus(e);
+ }
- handlePromises($ul, level, event, $promises);
+ function removeOnClickEvent(e) {
+ if (!$(e.target).parents().hasClass("dropdown-menu")) {
+ removeAllContextMenus(e);
+ }
+ }
- function removeOnScrollEvent(e) {
- removeAllContextMenus(e);
- }
- function removeOnClickEvent(e) {
- if( !$(e.target).parents().hasClass("dropdown-menu") ) {
- removeAllContextMenus(e);
- }
- }
+ function removeAllContextMenus(e) {
+ $(document.body).unbind('mousedown.contextmenu');
+ $(document).unbind('scroll.contextmenu');
+ if ($(event.currentTarget).hasClass('context')) {
+ $(event.currentTarget).removeClass('context');
+ removeContextMenus();
+ }
+ }
+ if (level === 0) {
+ $(document.body).bind('mousedown.contextmenu', removeOnClickEvent);
+ /// remove the menu when the scroll moves
+ $(document).bind('scroll.contextmenu', removeOnScrollEvent);
+ }
- function removeAllContextMenus(e) {
- $(document.body).unbind('mousedown.contextmenu');
- $(document).unbind('scroll.contextmenu');
- if ( $(event.currentTarget).hasClass('context') ) {
- $(event.currentTarget).removeClass('context');
+ $scope.$on("$destroy", function () {
removeContextMenus();
- }
- }
+ });
- if(level === 0) {
- $(document.body).bind('mousedown.contextmenu', removeOnClickEvent);
- /// remove the menu when the scroll moves
- $(document).bind('scroll.contextmenu', removeOnScrollEvent);
+ contextMenus.push($ul);
+ };
+
+ function isTouchDevice() {
+ return 'ontouchstart' in window || navigator.maxTouchPoints; // works on most browsers | works on IE10/11 and Surface
}
- $scope.$on("$destroy", function () {
- removeContextMenus();
- });
+ return function ($scope, element, attrs) {
+ var openMenuEvent = "contextmenu";
- contextMenus.push($ul);
- };
+ if (attrs.contextMenuOn && typeof (attrs.contextMenuOn) === "string") {
+ openMenuEvent = attrs.contextMenuOn;
+ }
- function isTouchDevice() {
- return 'ontouchstart' in window || navigator.maxTouchPoints; // works on most browsers | works on IE10/11 and Surface
- }
+ element.on(openMenuEvent, function (event) {
+ var contextMenuEnabled = $scope.$eval(attrs.contextMenuEnabled);
- return function ($scope, element, attrs) {
- var openMenuEvent = "contextmenu";
- if(attrs.contextMenuOn && typeof(attrs.contextMenuOn) === "string"){
- openMenuEvent = attrs.contextMenuOn;
- }
- element.on(openMenuEvent, function (event) {
- if(!attrs.allowEventPropagation) {
- event.stopPropagation();
- event.preventDefault();
- }
+ // default to true
+ if (contextMenuEnabled !== true && contextMenuEnabled !== false) {
+ contextMenuEnabled = true;
+ }
- // Don't show context menu if on touch device and element is draggable
- if(isTouchDevice() && element.attr('draggable') === 'true') {
- return false;
- }
+ console.log("enabled:", contextMenuEnabled);
- $scope.$apply(function () {
- var options = $scope.$eval(attrs.contextMenu);
- var customClass = attrs.contextMenuClass;
- var modelValue = $scope.$eval(attrs.model);
- if (options instanceof Array) {
- if (options.length === 0) { return; }
- renderContextMenu($scope, event, options, modelValue, undefined, customClass);
- } else {
- throw '"' + attrs.contextMenu + '" not an array';
+ if (contextMenuEnabled) {
+ if (!attrs.allowEventPropagation) {
+ event.stopPropagation();
+ event.preventDefault();
+ }
+
+ // Don't show context menu if on touch device and element is draggable
+ if (isTouchDevice() && element.attr('draggable') === 'true') {
+ return false;
+ }
+
+ $scope.$apply(function () {
+ var options = $scope.$eval(attrs.contextMenu);
+ var customClass = attrs.contextMenuClass;
+ var modelValue = $scope.$eval(attrs.model);
+ if (options instanceof Array) {
+ if (options.length === 0) {
+ return;
+ }
+ renderContextMenu($scope, event, options, modelValue, undefined, customClass);
+ } else {
+ throw '"' + attrs.contextMenu + '" not an array';
+ }
+ });
}
});
- });
- };
-}]);
+
+ angular.element(document).bind('click', function () {
+ removeContextMenus();
+ });
+ };
+ }]);
\ No newline at end of file
diff --git a/demo/demo.js b/demo/demo.js
index 3ff142d..7e6b774 100644
--- a/demo/demo.js
+++ b/demo/demo.js
@@ -9,24 +9,29 @@ angular.module('app', ['ui.bootstrap.contextMenu'])
gold: 100
};
- $scope.items = [
- { name: 'Small Health Potion', cost: 4 },
- { name: 'Small Mana Potion', cost: 5 },
- { name: 'Iron Short Sword', cost: 12 }
- ];
+ $scope.items = [{
+ name: 'Small Health Potion',
+ cost: 4
+ }, {
+ name: 'Small Mana Potion',
+ cost: 5
+ }, {
+ name: 'Iron Short Sword',
+ cost: 12
+ }];
+
+ $scope.enabled = false;
$scope.menuOptions = [
['Buy', function ($itemScope) {
$scope.player.gold -= $itemScope.item.cost;
}],
- null,
- ['Sell', function ($itemScope) {
+ null, ['Sell', function ($itemScope) {
$scope.player.gold += $itemScope.item.cost;
}, function ($itemScope) {
return $itemScope.item.name.match(/Iron/) == null;
}],
- null,
- ['More...', [
+ null, ['More...', [
['Alert Cost', function ($itemScope) {
alert($itemScope.item.cost);
}],
@@ -49,7 +54,8 @@ angular.module('app', ['ui.bootstrap.contextMenu'])
var customHtml = ' Testing Custom
';
var customItem = {
- html: customHtml, click: function ($itemScope, event, modelValue, text, $li) {
+ html: customHtml,
+ click: function ($itemScope, event, modelValue, text, $li) {
alert("custom html");
console.info($itemScope);
console.info(event);
@@ -70,11 +76,9 @@ angular.module('app', ['ui.bootstrap.contextMenu'])
}
};
- $scope.customHTMLOptions = [customItem, customDisabledItem,
- ['Example 1', function ($itemScope, $event, value) {
- alert("Example 1");
- }]
- ];
+ $scope.customHTMLOptions = [customItem, customDisabledItem, ['Example 1', function ($itemScope, $event, value) {
+ alert("Example 1");
+ }]];
}
]);
\ No newline at end of file
diff --git a/demo/index.html b/demo/index.html
index d20f88e..b9c12d6 100644
--- a/demo/index.html
+++ b/demo/index.html
@@ -17,11 +17,18 @@
+ context-menu="menuOptions"
+ context-menu-enabled="enabled">
{{item.cost}}
{{item.name}}
+
+
+
+
+
+