Browse Source

Add draggable feature.

Dante 12 years ago
parent
commit
f0d07e1622

+ 36 - 2
assets/bootstrap-dialog/js/bootstrap-dialog.js

@@ -27,10 +27,15 @@ var BootstrapDialog = null;
             data: {},
             onshow: null,
             onhide: null,
-            autodestroy: true
+            autodestroy: true,
+            draggable: false
         };
         this.indexedButtons = {};
         this.registeredButtonHotkeys = {};
+        this.draggableData = {
+            isMouseDown: false,
+            mouseOffset: {}
+        };
         this.realized = false;
         this.opened = false;
         this.initOptions(options);
@@ -626,6 +631,34 @@ var BootstrapDialog = null;
 
             return this;
         },
+        makeModalDraggable: function() {
+            if (this.options.draggable) {
+                this.getModalHeader().addClass(this.getNamespace('draggable')).on('mousedown', {dialog: this}, function(event) {
+                    var dialog = event.data.dialog;
+                    dialog.draggableData.isMouseDown = true;
+                    var dialogOffset = dialog.getModalContent().offset();
+                    dialog.draggableData.mouseOffset = {
+                        top: event.clientY - dialogOffset.top,
+                        left: event.clientX - dialogOffset.left
+                    };
+                });
+                this.getModal().on('mouseup mouseleave', {dialog: this}, function(event) {
+                    event.data.dialog.draggableData.isMouseDown = false;
+                });
+                $('body').on('mousemove', {dialog: this}, function(event) {
+                    var dialog = event.data.dialog;
+                    if (!dialog.draggableData.isMouseDown) {
+                        return;
+                    }
+                    dialog.getModalContent().offset({
+                        top: event.clientY - dialog.draggableData.mouseOffset.top,
+                        left: event.clientX - dialog.draggableData.mouseOffset.left
+                    });
+                });
+            }
+
+            return this;
+        },
         showPageScrollBar: function(show) {
             $(document.body).toggleClass('modal-open', show);
         },
@@ -643,6 +676,7 @@ var BootstrapDialog = null;
                 keyboard: false,
                 show: false
             });
+            this.makeModalDraggable();
             this.handleModalEvents();
             this.setRealized(true);
             this.updateTitle();
@@ -752,4 +786,4 @@ var BootstrapDialog = null;
                 }]
         }).open();
     };
-}(window.jQuery);
+}(window.jQuery);

File diff suppressed because it is too large
+ 1 - 1
assets/bootstrap-dialog/js/bootstrap-dialog.min.js


+ 21 - 0
index.html

@@ -454,6 +454,27 @@
         -->
     </div>
     
+    <a name="dialog-draggable"></a>
+    <h3>Dialog Draggable</h3>
+    <p>
+        When option 'draggable' is set to <strong>true</strong>, your dialog can be moved by dragging on its header. <br />
+        If you would like to change the cursor shape of hovering on dialog header, you can try the following css lines: <br />
+        <div class="source-code">
+            .bootstrap-dialog .modal-header.bootstrap-dialog-draggable {
+                cursor: move;
+            }
+        </div>
+    </p>
+    <div class="source-code runnable">
+        <!--
+        BootstrapDialog.show({
+            title: 'Draggable Dialog',
+            message: 'Try to drag on dialog title to move your dialog.',
+            draggable: true
+        });
+        -->
+    </div>
+    
     <h3>Manipulating your dialog</h3>
     <div class="source-code runnable">
         <!--