annotate jquery-ui/development-bundle/ui/jquery.ui.mouse.js @ 0:b2e4605f20b2

beta version
author dwinter
date Thu, 30 Jun 2011 09:07:49 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
1 /*!
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
2 * jQuery UI Mouse 1.8.11
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
3 *
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
4 * Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
5 * Dual licensed under the MIT or GPL Version 2 licenses.
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
6 * http://jquery.org/license
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
7 *
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
8 * http://docs.jquery.com/UI/Mouse
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
9 *
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
10 * Depends:
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
11 * jquery.ui.widget.js
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
12 */
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
13 (function( $, undefined ) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
14
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
15 $.widget("ui.mouse", {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
16 options: {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
17 cancel: ':input,option',
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
18 distance: 1,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
19 delay: 0
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
20 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
21 _mouseInit: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
22 var self = this;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
23
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
24 this.element
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
25 .bind('mousedown.'+this.widgetName, function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
26 return self._mouseDown(event);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
27 })
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
28 .bind('click.'+this.widgetName, function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
29 if (true === $.data(event.target, self.widgetName + '.preventClickEvent')) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
30 $.removeData(event.target, self.widgetName + '.preventClickEvent');
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
31 event.stopImmediatePropagation();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
32 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
33 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
34 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
35
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
36 this.started = false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
37 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
38
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
39 // TODO: make sure destroying one instance of mouse doesn't mess with
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
40 // other instances of mouse
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
41 _mouseDestroy: function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
42 this.element.unbind('.'+this.widgetName);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
43 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
44
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
45 _mouseDown: function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
46 // don't let more than one widget handle mouseStart
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
47 // TODO: figure out why we have to use originalEvent
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
48 event.originalEvent = event.originalEvent || {};
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
49 if (event.originalEvent.mouseHandled) { return; }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
50
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
51 // we may have missed mouseup (out of window)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
52 (this._mouseStarted && this._mouseUp(event));
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
53
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
54 this._mouseDownEvent = event;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
55
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
56 var self = this,
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
57 btnIsLeft = (event.which == 1),
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
58 elIsCancel = (typeof this.options.cancel == "string" ? $(event.target).parents().add(event.target).filter(this.options.cancel).length : false);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
59 if (!btnIsLeft || elIsCancel || !this._mouseCapture(event)) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
60 return true;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
61 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
62
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
63 this.mouseDelayMet = !this.options.delay;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
64 if (!this.mouseDelayMet) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
65 this._mouseDelayTimer = setTimeout(function() {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
66 self.mouseDelayMet = true;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
67 }, this.options.delay);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
68 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
69
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
70 if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
71 this._mouseStarted = (this._mouseStart(event) !== false);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
72 if (!this._mouseStarted) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
73 event.preventDefault();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
74 return true;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
75 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
76 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
77
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
78 // Click event may never have fired (Gecko & Opera)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
79 if (true === $.data(event.target, this.widgetName + '.preventClickEvent')) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
80 $.removeData(event.target, this.widgetName + '.preventClickEvent');
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
81 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
82
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
83 // these delegates are required to keep context
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
84 this._mouseMoveDelegate = function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
85 return self._mouseMove(event);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
86 };
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
87 this._mouseUpDelegate = function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
88 return self._mouseUp(event);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
89 };
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
90 $(document)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
91 .bind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
92 .bind('mouseup.'+this.widgetName, this._mouseUpDelegate);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
93
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
94 event.preventDefault();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
95 event.originalEvent.mouseHandled = true;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
96 return true;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
97 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
98
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
99 _mouseMove: function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
100 // IE mouseup check - mouseup happened when mouse was out of window
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
101 if ($.browser.msie && !(document.documentMode >= 9) && !event.button) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
102 return this._mouseUp(event);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
103 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
104
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
105 if (this._mouseStarted) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
106 this._mouseDrag(event);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
107 return event.preventDefault();
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
108 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
109
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
110 if (this._mouseDistanceMet(event) && this._mouseDelayMet(event)) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
111 this._mouseStarted =
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
112 (this._mouseStart(this._mouseDownEvent, event) !== false);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
113 (this._mouseStarted ? this._mouseDrag(event) : this._mouseUp(event));
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
114 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
115
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
116 return !this._mouseStarted;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
117 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
118
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
119 _mouseUp: function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
120 $(document)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
121 .unbind('mousemove.'+this.widgetName, this._mouseMoveDelegate)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
122 .unbind('mouseup.'+this.widgetName, this._mouseUpDelegate);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
123
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
124 if (this._mouseStarted) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
125 this._mouseStarted = false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
126
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
127 if (event.target == this._mouseDownEvent.target) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
128 $.data(event.target, this.widgetName + '.preventClickEvent', true);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
129 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
130
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
131 this._mouseStop(event);
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
132 }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
133
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
134 return false;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
135 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
136
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
137 _mouseDistanceMet: function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
138 return (Math.max(
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
139 Math.abs(this._mouseDownEvent.pageX - event.pageX),
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
140 Math.abs(this._mouseDownEvent.pageY - event.pageY)
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
141 ) >= this.options.distance
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
142 );
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
143 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
144
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
145 _mouseDelayMet: function(event) {
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
146 return this.mouseDelayMet;
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
147 },
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
148
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
149 // These are placeholder methods, to be overriden by extending plugin
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
150 _mouseStart: function(event) {},
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
151 _mouseDrag: function(event) {},
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
152 _mouseStop: function(event) {},
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
153 _mouseCapture: function(event) { return true; }
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
154 });
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
155
b2e4605f20b2 beta version
dwinter
parents:
diff changeset
156 })(jQuery);