aboutsummaryrefslogtreecommitdiffstats
path: root/ui/tests/ui.testmouse.js
blob: 5c4d4120479c02a86fe2fb8ba28793da7fcad256 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * jQuery UI testMouse
 *
 * Copyright (c) 2008 Richard D. Worth (rdworth.org)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * Revision: $Id: $
 */
;(function($) {
	
	var mouseX = 0;
	var mouseY = 0;
	
	$(function() {
		$(document).mousemove(function(e) {
			if (e.isTrusted !== false) {
				mouseX = e.pageX;
				mouseY = e.pageY;
			}
		});
	});
	
	$.widget("ui.testMouse", {
		trackMouse: function() {},
		init: function() {
		},
		destroy: function() {
		},
		center: function(offset) {
			var o = this.element.offset();
			return {
				x: (o.left + (offset || [0, 0])[0] || 0) + this.element.width() / 2,
				y: (o.top + (offset || [0, 0])[1] || 0) + this.element.height() / 2
			};
		},
		drag: function(dx, dy) {
			var self = this;			

			var center = this.center();
			this.left = center.x;
			this.top = center.y;
		
			var OS = (/(win|mac|linux)/i.exec(navigator.platform) || ['other'])[0].toLowerCase();
		
			var defaultUrl = ['cursors', OS == 'other' ? 'win' : OS, 'default.png'].join('/');
			var cursorUrl = function() {
				return ['cursors', OS == 'other' ? 'win' : OS, self.element.css('cursor') + '.png'].join('/');
			}
			var noneUrl = ['cursors', OS == 'other' ? 'win' : OS, 'none' + ($.browser.safari ? '.png' : '.cur')].join('/');
		
			var fakemouse = $('<img src="' + defaultUrl + '" />');
			var realmouse = $('<img src="' + defaultUrl + '" />');
			if ($.browser.msie && $.browser.version == 6) {
				fakemouse = $('<div style="height:32;width:32;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + defaultUrl + '\', sizingMethod=\'scale\');" ></div>');
				realmouse = $('<div><div style="height:32;width:32;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + defaultUrl + '\', sizingMethod=\'scale\');" ></div></div>');
			}
			var mousescreen = $('<div/>');
		
			var updateCursor = function() {
				if ($.browser.msie && $.browser.version == 6) {
					fakemouse.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + cursorUrl() + '\', sizingMethod=\'scale\'');
				} else {
					fakemouse.attr('src', cursorUrl());
				}
			}
			var resetCursor = function() {
				if ($.browser.msie && $.browser.version == 6) {
					fakemouse.css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'' + defaultUrl + '\', sizingMethod=\'scale\'');
				} else {
					fakemouse.attr('src', defaultUrl);
				}
			}
			
			var testStart = function() {
				self.element.bind("mouseover", updateCursor).bind("mouseout", resetCursor);
				fakemouse.appendTo('body').css({ position: 'absolute', left: mouseX, top: mouseY, zIndex: 5000 });
				realmouse.appendTo('body').css({ position: 'absolute', left: mouseX, top: mouseY, zIndex: 5000, opacity: 0.1 });
				mousescreen.appendTo('body').css({ width: '100%', height: '100%', position: 'absolute', top: 0, left: 0, zIndex: 5000 })
					.mousemove(function(e) { realmouse.css({ left: e.pageX, top: e.pageY }); return false; })
					.mousedown(function() { return false; })
					.mouseup(function() { return false; });
				mousescreen.css('cursor', 'url(' + noneUrl + '), crosshair');
				($.browser.opera && mousescreen.css('cursor', 'crosshair'));
			}
			var testStop = function() {
				self.element.unbind("mouseover", updateCursor).unbind("mouseout", resetCursor);
				mousescreen.remove();
				mouseX = realmouse.css("left");
				mouseY = realmouse.css("top");
				realmouse.remove();
				fakemouse.remove();
				self.options.complete.apply();
			}
			
			testStart();
		
			this.lastX = null;

			var before = function() {
				self.element.triggerHandler('mouseover');
				self.element.simulate("mousedown", { clientX: self.left, clientY: self.top });
				self.element.simulate("mousemove", { clientX: self.left, clientY: self.top });
			}

			var during = function(xory) {
				if (!self.lastX) {
					self.lastX = xory;
				} else {
					var x = self.lastX, y = xory;
					self.element.simulate("mousemove", { clientX: x, clientY: y });
					self.lastX = null;
				}				
			}

			var after = function() {
				self.element.triggerHandler('mouseout');
				self.element.simulate("mouseup", { clientX: 0, clientY: 0 });
				testStop();
			}
			
			fakemouse
				.animate({ left: this.left, top: this.top }, this.options.speed, before)
				.animate({ left: this.left + dx, top: this.top + dy }, {
					speed: this.options.speed,
					easing: "swing",
					step: during
				})
				.animate({ left: this.left + dx, top: this.top + dy }, {
					speed: this.options.speed,
					easing: "swing",
					complete: after
				});
		
		}
	});

	$.ui.testMouse.defaults = {
		speed: "slow"
	}

})(jQuery);