Extend 'Ext.menu.Item' with a simplified handler logic also used in
'PVE.button.Button'.

If 'confirmMsg' config is set we wrap the defined handler in a
confirm dialog, useful if the menu item just makes an API call and
does not has an own (edit) window shown.

In contrast to the 'pveButton' we do not have a selection model,
enable function and the respective logic here.

Signed-off-by: Thomas Lamprecht <t.lampre...@proxmox.com>
---
 www/manager6/Makefile         |  1 +
 www/manager6/menu/MenuItem.js | 46 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+)
 create mode 100644 www/manager6/menu/MenuItem.js

diff --git a/www/manager6/Makefile b/www/manager6/Makefile
index 0c057f1d..d2089618 100644
--- a/www/manager6/Makefile
+++ b/www/manager6/Makefile
@@ -6,6 +6,7 @@ JSSRC=                                                  \
        Parser.js                                       \
        StateProvider.js                                \
        button/Button.js                                \
+       menu/MenuItem.js                                \
        button/ConsoleButton.js                         \
        button/Split.js                                 \
        button/HelpButton.js                            \
diff --git a/www/manager6/menu/MenuItem.js b/www/manager6/menu/MenuItem.js
new file mode 100644
index 00000000..2c1beabd
--- /dev/null
+++ b/www/manager6/menu/MenuItem.js
@@ -0,0 +1,46 @@
+Ext.define('PVE.menu.Item', {
+    extend: 'Ext.menu.Item',
+    alias: 'widget.pveMenuItem',
+
+    // set to wrap the handler callback in a confirm dialog  showing this text
+    confirmMsg: false,
+
+    // set to focus 'No' instead of 'Yes' button and show a warning symbol
+    dangerous: false,
+
+    initComponent: function() {
+        var me = this;
+
+       if (me.handler) {
+           me.setHandler(me.handler, me.scope);
+       }
+
+       me.callParent();
+    },
+
+    setHandler: function(fn, scope) {
+       var me = this;
+       me.scope = scope;
+       me.handler = function(button, e) {
+           var rec, msg;
+           if (me.confirmMsg) {
+               msg = me.confirmMsg;
+               Ext.MessageBox.defaultButton = me.dangerous ? 2 : 1;
+               Ext.Msg.show({
+                   title: gettext('Confirm'),
+                   icon: me.dangerous ? Ext.Msg.WARNING : Ext.Msg.QUESTION,
+                   msg: msg,
+                   buttons: Ext.Msg.YESNO,
+                   defaultFocus: me.dangerous ? 'no' : 'yes',
+                   callback: function(btn) {
+                       if (btn === 'yes') {
+                           Ext.callback(fn, me.scope, [me, e], 0, me);
+                       }
+                   }
+               });
+           } else {
+               Ext.callback(fn, me.scope, [me, e], 0, me);
+           }
+       };
+    }
+});
-- 
2.11.0


_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to