]> git.proxmox.com Git - proxmox-widget-toolkit.git/commitdiff
button/Button.js: imported from pve-manager
authorDietmar Maurer <dietmar@proxmox.com>
Sat, 25 Feb 2017 13:35:29 +0000 (14:35 +0100)
committerDietmar Maurer <dietmar@proxmox.com>
Sat, 25 Feb 2017 13:35:29 +0000 (14:35 +0100)
Makefile
button/Button.js [new file with mode: 0644]

index da69925adcd4f09b735644a9f9d00b42c79a7830..380b7ec40fc6aff8843a7fa9eb29fc4b2681e94d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,6 +24,7 @@ JSSRC=                                        \
        form/TextField.js               \
        form/Checkbox.js                \
        form/KVComboBox.js              \
+       button/Button.js                \
        grid/ObjectGrid.js              \
        grid/PendingObjectGrid.js       \
        panel/InputPanel.js             \
diff --git a/button/Button.js b/button/Button.js
new file mode 100644 (file)
index 0000000..15619a3
--- /dev/null
@@ -0,0 +1,76 @@
+/* Button features:
+ * - observe selection changes to enable/disable the button using enableFn()
+ * - pop up confirmation dialog using confirmMsg()
+ */
+Ext.define('Proxmox.button.Button', {
+    extend: 'Ext.button.Button',
+    alias: 'widget.proxmoxButton',
+
+    // the selection model to observe
+    selModel: undefined,
+
+    // if 'false' handler will not be called (button disabled)
+    enableFn: function(record) { },
+
+    // function(record) or text
+    confirmMsg: false,
+
+    // take special care in confirm box (select no as default).
+    dangerous: false,
+
+    initComponent: function() {
+       /*jslint confusion: true */
+
+        var me = this;
+
+       if (me.handler) {
+           me.realHandler = me.handler;
+
+           me.handler = function(button, event) {
+               var rec, msg;
+               if (me.selModel) {
+                   rec = me.selModel.getSelection()[0];
+                   if (!rec || (me.enableFn(rec) === false)) {
+                       return;
+                   }
+               }
+
+               if (me.confirmMsg) {
+                   msg = me.confirmMsg;
+                   if (Ext.isFunction(me.confirmMsg)) {
+                       msg = me.confirmMsg(rec);
+                   }
+                   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,
+                       callback: function(btn) {
+                           if (btn !== 'yes') {
+                               return;
+                           }
+                           me.realHandler(button, event, rec);
+                       }
+                   });
+               } else {
+                   me.realHandler(button, event, rec);
+               }
+           };
+       }
+
+       me.callParent();
+
+       if (me.selModel) {
+
+           me.mon(me.selModel, "selectionchange", function() {
+               var rec = me.selModel.getSelection()[0];
+               if (!rec || (me.enableFn(rec) === false)) {
+                   me.setDisabled(true);
+               } else  {
+                   me.setDisabled(false);
+               }
+           });
+       }
+    }
+});