define("dojo/_base/connect", ["./kernel", "../on", "../topic", "../aspect", "./event", "../mouse", "./sniff", "./lang", "../keys"], function(kernel, on, hub, aspect, eventModule, mouse, has, lang){ // module: // dojo/_base/connect // summary: // This module defines the dojo.connect API. // This modules also provides keyboard event handling helpers. // This module exports an extension event for emulating Firefox's keypress handling. // However, this extension event exists primarily for backwards compatibility and // is not recommended. WebKit and IE uses an alternate keypress handling (only // firing for printable characters, to distinguish from keydown events), and most // consider the WebKit/IE behavior more desirable. has.add("events-keypress-typed", function(){ // keypresses should only occur a printable character is hit var testKeyEvent = {charCode: 0}; try{ testKeyEvent = document.createEvent("KeyboardEvent"); (testKeyEvent.initKeyboardEvent || testKeyEvent.initKeyEvent).call(testKeyEvent, "keypress", true, true, null, false, false, false, false, 9, 3); }catch(e){} return testKeyEvent.charCode == 0 && !has("opera"); }); function connect_(obj, event, context, method, dontFix){ method = lang.hitch(context, method); if(!obj || !(obj.addEventListener || obj.attachEvent)){ // it is a not a DOM node and we are using the dojo.connect style of treating a // method like an event, must go right to aspect return aspect.after(obj || kernel.global, event, method, true); } if(typeof event == "string" && event.substring(0, 2) == "on"){ event = event.substring(2); } if(!obj){ obj = kernel.global; } if(!dontFix){ switch(event){ // dojo.connect has special handling for these event types case "keypress": event = keypress; break; case "mouseenter": event = mouse.enter; break; case "mouseleave": event = mouse.leave; break; } } return on(obj, event, method, dontFix); } var _punctMap = { 106:42, 111:47, 186:59, 187:43, 188:44, 189:45, 190:46, 191:47, 192:96, 219:91, 220:92, 221:93, 222:39, 229:113 }; var evtCopyKey = has("mac") ? "metaKey" : "ctrlKey"; var _synthesizeEvent = function(evt, props){ var faux = lang.mixin({}, evt, props); setKeyChar(faux); // FIXME: would prefer to use lang.hitch: lang.hitch(evt, evt.preventDefault); // but it throws an error when preventDefault is invoked on Safari // does Event.preventDefault not support "apply" on Safari? faux.preventDefault = function(){ evt.preventDefault(); }; faux.stopPropagation = function(){ evt.stopPropagation(); }; return faux; }; function setKeyChar(evt){ evt.keyChar = evt.charCode ? String.fromCharCode(evt.charCode) : ''; evt.charOrCode = evt.keyChar || evt.keyCode; } var keypress; if(has("events-keypress-typed")){ // this emulates Firefox's keypress behavior where every keydown can correspond to a keypress var _trySetKeyCode = function(e, code){ try{ // squelch errors when keyCode is read-only // (e.g. if keyCode is ctrl or shift) return (e.keyCode = code); }catch(e){ return 0; } }; keypress = function(object, listener){ var keydownSignal = on(object, "keydown", function(evt){ // munge key/charCode var k=evt.keyCode; // These are Windows Virtual Key Codes // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp var unprintable = (k!=13 || (has("ie") >= 9 && !has("quirks"))) && k!=32 && (k!=27||!has("ie")) && (k<48||k>90) && (k<96||k>111) && (k<186||k>192) && (k<219||k>222) && k!=229; // synthesize keypress for most unprintables and CTRL-keys if(unprintable||evt.ctrlKey){ var c = unprintable ? 0 : k; if(evt.ctrlKey){ if(k==3 || k==13){ return listener.call(evt.currentTarget, evt); // IE will post CTRL-BREAK, CTRL-ENTER as keypress natively }else if(c>95 && c<106){ c -= 48; // map CTRL-[numpad 0-9] to ASCII }else if((!evt.shiftKey)&&(c>=65&&c<=90)){ c += 32; // map CTRL-[A-Z] to lowercase }else{ c = _punctMap[c] || c; // map other problematic CTRL combinations to ASCII } } // simulate a keypress event var faux = _synthesizeEvent(evt, {type: 'keypress', faux: true, charCode: c}); listener.call(evt.currentTarget, faux); if(has("ie")){ _trySetKeyCode(evt, faux.keyCode); } } }); var keypressSignal = on(object, "keypress", function(evt){ var c = evt.charCode; c = c>=32 ? c : 0; evt = _synthesizeEvent(evt, {charCode: c, faux: true}); return listener.call(this, evt); }); return { remove: function(){ keydownSignal.remove(); keypressSignal.remove(); } }; }; }else{ if(has("opera")){ keypress = function(object, listener){ return on(object, "keypress", function(evt){ var c = evt.which; if(c==3){ c=99; // Mozilla maps CTRL-BREAK to CTRL-c } // can't trap some keys at all, like INSERT and DELETE // there is no differentiating info between DELETE and ".", or INSERT and "-" c = c<32 && !evt.shiftKey ? 0 : c; if(evt.ctrlKey && !evt.shiftKey && c>=65 && c<=90){ // lowercase CTRL-[A-Z] keys c += 32; } return listener.call(this, _synthesizeEvent(evt, { charCode: c })); }); }; }else{ keypress = function(object, listener){ return on(object, "keypress", function(evt){ setKeyChar(evt); return listener.call(this, evt); }); }; } } var connect = { _keypress:keypress, connect:function(obj, event, context, method, dontFix){ // normalize arguments var a=arguments, args=[], i=0; // if a[0] is a String, obj was omitted args.push(typeof a[0] == "string" ? null : a[i++], a[i++]); // if the arg-after-next is a String or Function, context was NOT omitted var a1 = a[i+1]; args.push(typeof a1 == "string" || typeof a1 == "function" ? a[i++] : null, a[i++]); // absorb any additional arguments for(var l=a.length; i