minor tweaks.
[sgn.git] / js / jqueryui / jquery.ui.mouse.js
blob140727dc460b5be6ab695de409a2a45b4cd6ddcd
1 /*!
2  * jquery UI Mouse 1.8.4
3  *
4  * Copyright 2010, AUTHORS.txt (http://jqueryui.com/about)
5  * Dual licensed under the MIT or GPL Version 2 licenses.
6  * http://jquery.org/license
7  *
8  * http://docs.jquery.com/UI/Mouse
9  *
10  * Depends:
11  *      jquery.ui.widget.js
12  */
13 (function( $, undefined ) {
15 $.widget("ui.mouse", {
16         options: {
17                 cancel: ':input,option',
18                 distance: 1,
19                 delay: 0
20         },
21         _mouseInit: function() {
22                 var self = this;
24                 this.element
25                         .bind('mousedown.'+this.widgetName, function(event) {
26                                 return self._mouseDown(event);
27                         })
28                         .bind('click.'+this.widgetName, function(event) {
29                                 if(self._preventClickEvent) {
30                                         self._preventClickEvent = false;
31                                         event.stopImmediatePropagation();
32                                         return false;
33                                 }
34                         });
36                 this.started = false;
37         },
39         // TODO: make sure destroying one instance of mouse doesn't mess with
40         // other instances of mouse
41         _mouseDestroy: function() {
42                 this.element.unbind('.'+this.widgetName);
43         },
45         _mouseDown: function(event) {
46                 // don't let more than one widget handle mouseStart
47                 // TODO: figure out why we have to use originalEvent
48                 event.originalEvent = event.originalEvent || {};
49                 if (event.originalEvent.mouseHandled) { return; }
51                 // we may have missed mouseup (out of window)
52                 (this._mouseStarted && this._mouseUp(event));
54                 this._mouseDownEvent = event;
56                 var self = this,
57                         btnIsLeft = (event.which == 1),
58                         elIsCancel = (typeof this.options.cancel == "string" ? $(event.target).parents().add(event.target).filter(this.options.cancel).length : false);
59                 if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
60                         return true;
61                 }
63                 this.mouseDelayMet = !this.options.delay;
64                 if (!this.mouseDelayMet) {
65                         this._mouseDelayTimer = setTimeout(function() {
66                                 self.mouseDelayMet = true;
67                         }, this.options.delay);
68                 }
70                 if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
71                         this._mouseStarted = (this._mouseStart(event) !== false);
72                         if (!this._mouseStarted) {
73                                 event.preventDefault();
74                                 return true;
75                         }
76                 }
78                 // these delegates are required to keep context
79                 this._mouseMoveDelegate = function(event) {
80                         return self._mouseMove(event);
81                 };
82                 this._mouseUpDelegate = function(event) {
83                         return self._mouseUp(event);
84                 };
85                 $(document)
86                         .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
87                         .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
89                 // preventDefault() is used to prevent the selection of text here -
90                 // however, in Safari, this causes select boxes not to be selectable
91                 // anymore, so this fix is needed
92                 ($.browser.safari || event.preventDefault());
94                 event.originalEvent.mouseHandled = true;
95                 return true;
96         },
98         _mouseMove: function(event) {
99                 // IE mouseup check - mouseup happened when mouse was out of window
100                 if ($.browser.msie && !event.button) {
101                         return this._mouseUp(event);
102                 }
104                 if (this._mouseStarted) {
105                         this._mouseDrag(event);
106                         return event.preventDefault();
107                 }
109                 if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
110                         this._mouseStarted =
111                                 (this._mouseStart(this._mouseDownEvent, event) !== false);
112                         (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
113                 }
115                 return !this._mouseStarted;
116         },
118         _mouseUp: function(event) {
119                 $(document)
120                         .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
121                         .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
123                 if (this._mouseStarted) {
124                         this._mouseStarted = false;
125                         this._preventClickEvent = (event.target == this._mouseDownEvent.target);
126                         this._mouseStop(event);
127                 }
129                 return false;
130         },
132         _mouseDistanceMet: function(event) {
133                 return (Math.max(
134                                 Math.abs(this._mouseDownEvent.pageX - event.pageX),
135                                 Math.abs(this._mouseDownEvent.pageY - event.pageY)
136                         ) >= this.options.distance
137                 );
138         },
140         _mouseDelayMet: function(event) {
141                 return this.mouseDelayMet;
142         },
144         // These are placeholder methods, to be overriden by extending plugin
145         _mouseStart: function(event) {},
146         _mouseDrag: function(event) {},
147         _mouseStop: function(event) {},
148         _mouseCapture: function(event) { return true; }
151 })(jquery);