浏览代码

Improve alert window, customizing title, type and more options are now possible.

Dante 11 年之前
父节点
当前提交
2af6b417cb

+ 32 - 7
examples/assets/bootstrap-dialog/js/bootstrap-dialog.js

@@ -734,20 +734,45 @@
     /**
      * Alert window
      * 
-     * @param {type} message
-     * @param {type} callback
      * @returns the created dialog instance
      */
-    BootstrapDialog.alert = function(message, callback) {
+    BootstrapDialog.alert = function() {
+        var options = {};
+        var defaultOptions = {
+            type: BootstrapDialog.TYPE_PRIMARY,
+            title: null,
+            message: null,
+            closable: true,
+            buttonLabel: 'OK',
+            callback: null
+        };
+
+        if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
+            options = $.extend(true, defaultOptions, arguments[0]);
+        } else {
+            options = $.extend(true, defaultOptions, {
+                message: arguments[0],
+                closable: false,
+                buttonLabel: 'OK',
+                callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
+            });
+        }
+
         return new BootstrapDialog({
-            message: message,
+            type: options.type,
+            title: options.title,
+            message: options.message,
+            closable: options.closable,
             data: {
-                'callback': callback
+                callback: options.callback
+            },
+            onhide: function(dialog) {
+                !dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
             },
-            closable: false,
             buttons: [{
-                    label: 'OK',
+                    label: options.buttonLabel,
                     action: function(dialog) {
+                        dialog.setData('btnClicked', true);
                         typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
                         dialog.close();
                     }

文件差异内容过多而无法显示
+ 1 - 1
examples/assets/bootstrap-dialog/js/bootstrap-dialog.min.js


+ 19 - 0
examples/index.html

@@ -592,6 +592,25 @@
         -->
     </div>
     
+    <a name="advanced-alert-window"></a>
+    <h3>Customizing dialog type, title, and more.</h3>
+    <p>All options shown below are optional.</p>
+    <div class="source-code runnable">
+        <!--
+        BootstrapDialog.alert({
+            title: 'WARNING',
+            message: 'Warning! No Banana!',
+            type: BootstrapDialog.TYPE_WARNING, // <-- Default value is BootstrapDialog.TYPE_PRIMARY
+            closable: true, // <-- Default value is true
+            buttonLabel: 'Roar! Why!', // <-- Default value is 'OK',
+            callback: function(result) {
+                // result will be true if button was click, while it will be false if users close the dialog directly.
+                alert('Result is: ' + result);
+            }
+        });
+        -->
+    </div>
+    
     <h3>Confirm</h3>
     <div class="source-code runnable">
         <!--

+ 32 - 7
js/bootstrap-dialog.js

@@ -734,20 +734,45 @@
     /**
      * Alert window
      * 
-     * @param {type} message
-     * @param {type} callback
      * @returns the created dialog instance
      */
-    BootstrapDialog.alert = function(message, callback) {
+    BootstrapDialog.alert = function() {
+        var options = {};
+        var defaultOptions = {
+            type: BootstrapDialog.TYPE_PRIMARY,
+            title: null,
+            message: null,
+            closable: true,
+            buttonLabel: 'OK',
+            callback: null
+        };
+
+        if (typeof arguments[0] === 'object' && arguments[0].constructor === {}.constructor) {
+            options = $.extend(true, defaultOptions, arguments[0]);
+        } else {
+            options = $.extend(true, defaultOptions, {
+                message: arguments[0],
+                closable: false,
+                buttonLabel: 'OK',
+                callback: typeof arguments[1] !== 'undefined' ? arguments[1] : null
+            });
+        }
+
         return new BootstrapDialog({
-            message: message,
+            type: options.type,
+            title: options.title,
+            message: options.message,
+            closable: options.closable,
             data: {
-                'callback': callback
+                callback: options.callback
+            },
+            onhide: function(dialog) {
+                !dialog.getData('btnClicked') && dialog.isClosable() && typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(false);
             },
-            closable: false,
             buttons: [{
-                    label: 'OK',
+                    label: options.buttonLabel,
                     action: function(dialog) {
+                        dialog.setData('btnClicked', true);
                         typeof dialog.getData('callback') === 'function' && dialog.getData('callback')(true);
                         dialog.close();
                     }

文件差异内容过多而无法显示
+ 1 - 1
js/bootstrap-dialog.min.js