ソースを参照

Button hotkey supported.

Dante 12 年 前
コミット
4549e36f7c
2 ファイル変更19 行追加2 行削除
  1. 2 0
      examples/hotkey.html
  2. 17 2
      js/bootstrap-dialog.js

+ 2 - 0
examples/hotkey.html

@@ -15,12 +15,14 @@
         BootstrapDialog.show({
             buttons: [{
                     label: 'Cancel',
+                    hotkey: 81,
                     action: function(dialog){
                         alert('Canceling...');
                     }
             }, {
                     label: 'Confirm',
                     cssClass: 'btn-primary',
+                    hotkey: 13,
                     action: function(dialog){
                         alert('Confirming...');
                     }

+ 17 - 2
js/bootstrap-dialog.js

@@ -30,6 +30,7 @@ var BootstrapDialog = null;
             autodestroy: true
         };
         this.indexedButtons = {};
+        this.registeredButtonHotkeys = {};
         this.realized = false;
         this.opened = false;
         this.initOptions(options);
@@ -431,8 +432,10 @@ var BootstrapDialog = null;
                 $button.addClass('btn-default');
             }
 
-            // Dynamically add extra functions to $button
-            this.enhanceButton($button);
+            // Hotkey
+            if (typeof button.hotkey !== undefined) {
+                this.registeredButtonHotkeys[button.hotkey] = $button;
+            }
 
             // Button on click
             $button.on('click', {dialog: this, $button: $button, button: button}, function(event) {
@@ -448,6 +451,9 @@ var BootstrapDialog = null;
                 }
             });
 
+            // Dynamically add extra functions to $button
+            this.enhanceButton($button);
+
             return $button;
         },
         /**
@@ -609,6 +615,15 @@ var BootstrapDialog = null;
                 event.which === 27 && event.data.dialog.isClosable() && event.data.dialog.close();
             });
 
+            // Button hotkey
+            this.getModal().on('keyup', {dialog: this}, function(event) {
+                var dialog = event.data.dialog;
+                if (typeof dialog.registeredButtonHotkeys[event.which] !== 'undefined') {
+                    var $button = $(dialog.registeredButtonHotkeys[event.which]);
+                    !$button.prop('disabled') && $button.trigger('click');
+                }
+            });
+
             return this;
         },
         showPageScrollBar: function(show) {