1 | /*! |
---|
2 | * jQuery UI Mouse 1.8.17 |
---|
3 | * |
---|
4 | * Copyright 2011, 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 ) { |
---|
14 | |
---|
15 | var mouseHandled = false; |
---|
16 | $( document ).mouseup( function( e ) { |
---|
17 | mouseHandled = false; |
---|
18 | }); |
---|
19 | |
---|
20 | $.widget("ui.mouse", { |
---|
21 | options: { |
---|
22 | cancel: ':input,option', |
---|
23 | distance: 1, |
---|
24 | delay: 0 |
---|
25 | }, |
---|
26 | _mouseInit: function() { |
---|
27 | var self = this; |
---|
28 | |
---|
29 | this.element |
---|
30 | .bind('mousedown.'+this.widgetName, function(event) { |
---|
31 | return self._mouseDown(event); |
---|
32 | }) |
---|
33 | .bind('click.'+this.widgetName, function(event) { |
---|
34 | if (true === $.data(event.target, self.widgetName + '.preventClickEvent')) { |
---|
35 | $.removeData(event.target, self.widgetName + '.preventClickEvent'); |
---|
36 | event.stopImmediatePropagation(); |
---|
37 | return false; |
---|
38 | } |
---|
39 | }); |
---|
40 | |
---|
41 | this.started = false; |
---|
42 | }, |
---|
43 | |
---|
44 | // TODO: make sure destroying one instance of mouse doesn't mess with |
---|
45 | // other instances of mouse |
---|
46 | _mouseDestroy: function() { |
---|
47 | this.element.unbind('.'+this.widgetName); |
---|
48 | }, |
---|
49 | |
---|
50 | _mouseDown: function(event) { |
---|
51 | // don't let more than one widget handle mouseStart |
---|
52 | if( mouseHandled ) { return }; |
---|
53 | |
---|
54 | // we may have missed mouseup (out of window) |
---|
55 | (this._mouseStarted && this._mouseUp(event)); |
---|
56 | |
---|
57 | this._mouseDownEvent = event; |
---|
58 | |
---|
59 | var self = this, |
---|
60 | btnIsLeft = (event.which == 1), |
---|
61 | // event.target.nodeName works around a bug in IE 8 with |
---|
62 | // disabled inputs (#7620) |
---|
63 | elIsCancel = (typeof this.options.cancel == "string" && event.target.nodeName ? $(event.target).closest(this.options.cancel).length : false); |
---|
64 | if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) { |
---|
65 | return true; |
---|
66 | } |
---|
67 | |
---|
68 | this.mouseDelayMet = !this.options.delay; |
---|
69 | if (!this.mouseDelayMet) { |
---|
70 | this._mouseDelayTimer = setTimeout(function() { |
---|
71 | self.mouseDelayMet = true; |
---|
72 | }, this.options.delay); |
---|
73 | } |
---|
74 | |
---|
75 | if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) { |
---|
76 | this._mouseStarted = (this._mouseStart(event) !== false); |
---|
77 | if (!this._mouseStarted) { |
---|
78 | event.preventDefault(); |
---|
79 | return true; |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | // Click event may never have fired (Gecko & Opera) |
---|
84 | if (true === $.data(event.target, this.widgetName + '.preventClickEvent')) { |
---|
85 | $.removeData(event.target, this.widgetName + '.preventClickEvent'); |
---|
86 | } |
---|
87 | |
---|
88 | // these delegates are required to keep context |
---|
89 | this._mouseMoveDelegate = function(event) { |
---|
90 | return self._mouseMove(event); |
---|
91 | }; |
---|
92 | this._mouseUpDelegate = function(event) { |
---|
93 | return self._mouseUp(event); |
---|
94 | }; |
---|
95 | $(document) |
---|
96 | .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate) |
---|
97 | .bind('mouseup.'+this.widgetName, this._mouseUpDelegate); |
---|
98 | |
---|
99 | event.preventDefault(); |
---|
100 | |
---|
101 | mouseHandled = true; |
---|
102 | return true; |
---|
103 | }, |
---|
104 | |
---|
105 | _mouseMove: function(event) { |
---|
106 | // IE mouseup check - mouseup happened when mouse was out of window |
---|
107 | if ($.browser.msie && !(document.documentMode >= 9) && !event.button) { |
---|
108 | return this._mouseUp(event); |
---|
109 | } |
---|
110 | |
---|
111 | if (this._mouseStarted) { |
---|
112 | this._mouseDrag(event); |
---|
113 | return event.preventDefault(); |
---|
114 | } |
---|
115 | |
---|
116 | if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) { |
---|
117 | this._mouseStarted = |
---|
118 | (this._mouseStart(this._mouseDownEvent, event) !== false); |
---|
119 | (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event)); |
---|
120 | } |
---|
121 | |
---|
122 | return !this._mouseStarted; |
---|
123 | }, |
---|
124 | |
---|
125 | _mouseUp: function(event) { |
---|
126 | $(document) |
---|
127 | .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate) |
---|
128 | .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate); |
---|
129 | |
---|
130 | if (this._mouseStarted) { |
---|
131 | this._mouseStarted = false; |
---|
132 | |
---|
133 | if (event.target == this._mouseDownEvent.target) { |
---|
134 | $.data(event.target, this.widgetName + '.preventClickEvent', true); |
---|
135 | } |
---|
136 | |
---|
137 | this._mouseStop(event); |
---|
138 | } |
---|
139 | |
---|
140 | return false; |
---|
141 | }, |
---|
142 | |
---|
143 | _mouseDistanceMet: function(event) { |
---|
144 | return (Math.max( |
---|
145 | Math.abs(this._mouseDownEvent.pageX - event.pageX), |
---|
146 | Math.abs(this._mouseDownEvent.pageY - event.pageY) |
---|
147 | ) >= this.options.distance |
---|
148 | ); |
---|
149 | }, |
---|
150 | |
---|
151 | _mouseDelayMet: function(event) { |
---|
152 | return this.mouseDelayMet; |
---|
153 | }, |
---|
154 | |
---|
155 | // These are placeholder methods, to be overriden by extending plugin |
---|
156 | _mouseStart: function(event) {}, |
---|
157 | _mouseDrag: function(event) {}, |
---|
158 | _mouseStop: function(event) {}, |
---|
159 | _mouseCapture: function(event) { return true; } |
---|
160 | }); |
---|
161 | |
---|
162 | })(jQuery); |
---|