forked from bitovi/syn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.js
315 lines (269 loc) · 8.32 KB
/
mouse.js
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
//handles mosue events
(function(){
var h = Syn.helpers;
Syn.mouse = {};
h.extend(Syn.defaults,{
mousedown: function( options ) {
Syn.trigger("focus", {}, this)
},
click: function() {
// prevents the access denied issue in IE if the click causes the element to be destroyed
var element = this;
try {
element.nodeType;
} catch(e){
return;
}
//get old values
var href,
radioChanged = Syn.data(element,"radioChanged"),
scope = Syn.helpers.getWindow(element),
nodeName = element.nodeName.toLowerCase();
if( (href = Syn.data(element,"href") ) ){
element.setAttribute('href',href)
}
//run href javascript
if(!Syn.support.linkHrefJS
&& /^\s*javascript:/.test(element.href)){
//eval js
var code = element.href.replace(/^\s*javascript:/,"")
//try{
if (code != "//" && code.indexOf("void(0)") == -1) {
if(window.selenium){
eval("with(selenium.browserbot.getCurrentWindow()){"+code+"}")
}else{
eval("with(scope){"+code+"}")
}
}
}
//submit a form
if(!(Syn.support.clickSubmits) &&
(nodeName == "input"
&& element.type == "submit" ) ||
nodeName == 'button' ){
var form = Syn.closest(element, "form");
if(form){
Syn.trigger("submit",{},form)
}
}
//follow a link, probably needs to check if in an a.
if(nodeName == "a"
&& element.href
&& !/^\s*javascript:/.test(element.href)){
scope.location.href = element.href;
}
//change a checkbox
if(nodeName == "input"
&& element.type == "checkbox"){
//if(!Syn.support.clickChecks && !Syn.support.changeChecks){
// element.checked = !element.checked;
//}
if(!Syn.support.clickChanges){
Syn.trigger("change",{}, element );
}
}
//change a radio button
if(nodeName == "input" && element.type == "radio"){ // need to uncheck others if not checked
/*if(!Syn.support.clickChecks && !Syn.support.changeChecks){
//do the checks manually
if(!element.checked){ //do nothing, no change
element.checked = true;
}
}*/
if(radioChanged && !Syn.support.radioClickChanges){
Syn.trigger("change",{}, element );
}
}
// change options
if(nodeName == "option" && Syn.data(element,"createChange")){
Syn.trigger("change",{}, element.parentNode);//does not bubble
Syn.data(element,"createChange",false)
}
}
})
//add create and setup behavior for mosue events
h.extend(Syn.create,{
mouse : {
options: function( type, options, element ) {
var doc = document.documentElement, body = document.body,
center = [options.pageX || 0, options.pageY || 0],
//browser might not be loaded yet (doing support code)
left = Syn.mouse.browser && Syn.mouse.browser.left[type],
right = Syn.mouse.browser && Syn.mouse.browser.right[type];
return h.extend({
bubbles : true,cancelable : true,
view : window,
detail : 1,
screenX : 1, screenY : 1,
clientX : options.clientX || center[0] -(doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc.clientLeft || 0),
clientY : options.clientY || center[1] -(doc && doc.scrollTop || body && body.scrollTop || 0) - (doc.clientTop || 0),
ctrlKey : !!Syn.key.ctrlKey,
altKey : !!Syn.key.altKey,
shiftKey : !!Syn.key.shiftKey,
metaKey : !!Syn.key.metaKey,
button : left && left.button != null ?
left.button :
right && right.button || (type == 'contextmenu' ? 2 : 0),
relatedTarget : document.documentElement
}, options);
},
event : document.createEvent ?
function(type, defaults, element){ //Everyone Else
var event;
try {
event = element.ownerDocument.createEvent('MouseEvents');
event.initMouseEvent(type,
defaults.bubbles, defaults.cancelable,
defaults.view,
defaults.detail,
defaults.screenX, defaults.screenY,defaults.clientX,defaults.clientY,
defaults.ctrlKey,defaults.altKey,defaults.shiftKey,defaults.metaKey,
defaults.button,defaults.relatedTarget);
} catch(e) {
event = h.createBasicStandardEvent(type,defaults)
}
event.synthetic = true;
return event;
} :
h.createEventObject
},
click : {
setup: function( type, options, element ) {
var nodeName = element.nodeName.toLowerCase(),
type;
//we need to manually 'check' in browser that can't check
//so checked has the right value
if(!Syn.support.clickChecks && !Syn.support.changeChecks && nodeName === "input"){
type = element.type.toLowerCase();//pretty sure lowercase isn't needed
if(type === 'checkbox'){
element.checked = !element.checked;
}
if(type === "radio"){
//do the checks manually
if(!element.checked){ //do nothing, no change
try{
Syn.data(element,"radioChanged", true);
}catch(e){}
element.checked = true;
}
}
}
if(
nodeName == "a"
&& element.href
&& !/^\s*javascript:/.test(element.href)){
//save href
Syn.data(element,"href", element.href)
//remove b/c safari/opera will open a new tab instead of changing the page
element.setAttribute('href','javascript://')
//however this breaks scripts using the href
//we need to listen to this and prevent the default behavior
//and run the default behavior ourselves. Boo!
}
//if select or option, save old value and mark to change
if(/option/i.test(element.nodeName)){
var child = element.parentNode.firstChild,
i = -1;
while(child){
if(child.nodeType ==1){
i++;
if(child == element) break;
}
child = child.nextSibling;
}
if(i !== element.parentNode.selectedIndex){
//shouldn't this wait on triggering
//change?
element.parentNode.selectedIndex = i;
Syn.data(element,"createChange",true)
}
}
}
},
mousedown : {
setup: function( type,options, element ) {
var nn = element.nodeName.toLowerCase();
//we have to auto prevent default to prevent freezing error in safari
if(Syn.browser.safari && (nn == "select" || nn == "option" )){
options._autoPrevent = true;
}
}
}
});
//do support code
(function(){
if(!document.body){
setTimeout(arguments.callee,1)
return;
}
var oldSynth = window.__synthTest;
window.__synthTest = function(){
Syn.support.linkHrefJS = true;
}
var div = document.createElement("div"),
checkbox,
submit,
form,
input,
select;
div.innerHTML = "<form id='outer'>"+
"<input name='checkbox' type='checkbox'/>"+
"<input name='radio' type='radio' />"+
"<input type='submit' name='submitter'/>"+
"<input type='input' name='inputter'/>"+
"<input name='one'>"+
"<input name='two'/>"+
"<a href='javascript:__synthTest()' id='synlink'></a>"+
"<select><option></option></select>"+
"</form>";
document.documentElement.appendChild(div);
form = div.firstChild
checkbox = form.childNodes[0];
submit = form.childNodes[2];
select = form.getElementsByTagName('select')[0]
checkbox.checked = false;
checkbox.onchange = function(){
Syn.support.clickChanges = true;
}
Syn.trigger("click", {}, checkbox)
Syn.support.clickChecks = checkbox.checked;
checkbox.checked = false;
Syn.trigger("change", {}, checkbox);
Syn.support.changeChecks = checkbox.checked;
form.onsubmit = function(ev){
if (ev.preventDefault)
ev.preventDefault();
Syn.support.clickSubmits = true;
return false;
}
Syn.trigger("click", {}, submit)
form.childNodes[1].onchange = function(){
Syn.support.radioClickChanges = true;
}
Syn.trigger("click", {}, form.childNodes[1])
Syn.bind(div, 'click', function(){
Syn.support.optionClickBubbles = true;
Syn.unbind(div,'click', arguments.callee)
})
Syn.trigger("click",{},select.firstChild)
Syn.support.changeBubbles = Syn.eventSupported('change');
//test if mousedown followed by mouseup causes click (opera), make sure there are no clicks after this
var clicksCount = 0
div.onclick = function(){
Syn.support.mouseDownUpClicks = true;
//we should use this to check for opera potentially, but would
//be difficult to remove element correctly
//Syn.support.mouseDownUpRepeatClicks = (2 == (++clicksCount))
}
Syn.trigger("mousedown",{},div)
Syn.trigger("mouseup",{},div)
//setTimeout(function(){
// Syn.trigger("mousedown",{},div)
// Syn.trigger("mouseup",{},div)
//},1)
document.documentElement.removeChild(div);
//check stuff
window.__synthTest = oldSynth;
Syn.support.ready++;
})();
})()