From 7cf0cfb2eb14ec3396bc5cbc4e110610a50496d5 Mon Sep 17 00:00:00 2001 From: Wind Li Date: Sat, 14 Mar 2020 16:37:43 +0800 Subject: [PATCH 1/5] Add submenu support --- README.md | 6 ++ render/index.html | 9 ++- src/knockout.contextmenu.js | 112 ++++++++++++++++++++++------------ src/knockout.contextmenu.less | 22 +++++++ 4 files changed, 110 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index e5a7dfd..923a01e 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,12 @@ Example: "visible": someMethod() == '1', "action": $root.someAction }, + "Sub menu": { + submenu: { + "Sub Option one": function() { alert("submenu,oh, you clicked me"); }, + "Sub Option two": { action: function() { alert("submenu,you are on fire"); } } + } + }, "One method": $root.test } ``` diff --git a/render/index.html b/render/index.html index e363ca1..9a914a3 100644 --- a/render/index.html +++ b/render/index.html @@ -11,9 +11,16 @@ var menu = { 'Option one': function() { alert('oh, you clicked me'); }, 'Option two': { action: function() { alert('you are on fire'); } }, + 'Option 3': { action: function() { alert('you are on fire'); } }, 'a separator': { separator: true }, + 'Sub menu': { submenu: { + 'Sub Option one': function() { alert('submenu,oh, you clicked me'); }, + 'Sub Option two': { action: function() { alert('submenu,you are on fire'); } } + } }, 'Some boolean': ko.observable(true), - 'Option three': { text: 'Final action', action: function() { /* nothing to do */ } } + '2ed separator': { separator: true }, + 'Option 4': { action: function() { alert('you are on fire'); } }, + 'Option three': { text: 'Final action', action: function() { /* nothing to do */ } }, }; ko.applyBindings({}); diff --git a/src/knockout.contextmenu.js b/src/knockout.contextmenu.js index 38aa8c0..1bd3219 100644 --- a/src/knockout.contextmenu.js +++ b/src/knockout.contextmenu.js @@ -154,12 +154,15 @@ function bindContextMenu(ko) { ); props.forEach(function (eventNameOutsideClosure) { - pushItem(eventNameOutsideClosure); + pushItem(eventNameOutsideClosure,eventsToHandle,elements,items,actions); }); if (elements.length) { menu = document.createElement('div'); menu.className = defaultClass; + items.forEach(function (item) { + hasChecks = hasChecks || (item.isBoolean && item.isVisible); + }); // you may need padding to menus that has checks menu.innerHTML = '