From 41aa90fe67a714639a6a43ab1d0aa85f9312972d Mon Sep 17 00:00:00 2001 From: Pem Taira Date: Mon, 9 Oct 2017 01:42:59 +0800 Subject: [PATCH] fix: add default empty option --- contextMenu.js | 23 ++++++++++++++++++----- demo/demo.js | 5 +++-- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/contextMenu.js b/contextMenu.js index 229941b..da38531 100644 --- a/contextMenu.js +++ b/contextMenu.js @@ -108,6 +108,8 @@ function ($rootScope, ContextMenuEvents, $parse, $q, custom, $sce) { } $li.append(optionText); + + return optionText; }; /** @@ -133,7 +135,7 @@ function ($rootScope, ContextMenuEvents, $parse, $q, custom, $sce) { var text = DEFAULT_ITEM_TEXT; var currItemParam = angular.extend({}, params); var item = params.item; - + currItemParam.nestedMenu = nestedMenu; currItemParam.enabled = resolveBoolOrFunc(item.enabled || item[2], params); currItemParam.text = createAndAddOptionText(currItemParam); @@ -241,10 +243,7 @@ function ($rootScope, ContextMenuEvents, $parse, $q, custom, $sce) { }); }); } else { - $li.on('click', function ($event) { - $event.preventDefault(); - }); - $li.addClass('disabled'); + setElementDisabled($li); } }; @@ -348,6 +347,13 @@ function ($rootScope, ContextMenuEvents, $parse, $q, custom, $sce) { } }); + if ($ul.children().length === 0) { + var $emptyLi = angular.element('
  • '); + setElementDisabled($emptyLi); + $emptyLi.html('empty'); + $ul.append($emptyLi); + } + $(document).find('body').append($ul); doAfterAllPromises(params); @@ -546,6 +552,13 @@ function ($rootScope, ContextMenuEvents, $parse, $q, custom, $sce) { $ul.append($li); } + function setElementDisabled($li) { + $li.on('click', function ($event) { + $event.preventDefault(); + }); + $li.addClass('disabled'); + } + return function ($scope, element, attrs) { var openMenuEvent = "contextmenu"; if(attrs.contextMenuOn && typeof(attrs.contextMenuOn) === "string"){ diff --git a/demo/demo.js b/demo/demo.js index 88b4124..0439084 100644 --- a/demo/demo.js +++ b/demo/demo.js @@ -66,13 +66,14 @@ angular.module('app', ['ui.bootstrap.contextMenu']) text: 'Disabled object-based option', enabled: function () { return false; - } + }, }, { text: function() { return 'Text Using Function'; }, hasBottomDivider: function () { return $scope.showHiddenOption; - } + }, + children: [], }, { html: function() { return 'HTML Using Function'; },