connect.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. //>>built
  7. define("dojo/_base/connect",["./kernel","../on","../topic","../aspect","./event","../mouse","./sniff","./lang","../keys"],function(_1,on,_2,_3,_4,_5,_6,_7){
  8. _6.add("events-keypress-typed",function(){
  9. var _8={charCode:0};
  10. try{
  11. _8=document.createEvent("KeyboardEvent");
  12. (_8.initKeyboardEvent||_8.initKeyEvent).call(_8,"keypress",true,true,null,false,false,false,false,9,3);
  13. }
  14. catch(e){
  15. }
  16. return _8.charCode==0&&!_6("opera");
  17. });
  18. function _9(_a,_b,_c,_d,_e){
  19. _d=_7.hitch(_c,_d);
  20. if(!_a||!(_a.addEventListener||_a.attachEvent)){
  21. return _3.after(_a||_1.global,_b,_d,true);
  22. }
  23. if(typeof _b=="string"&&_b.substring(0,2)=="on"){
  24. _b=_b.substring(2);
  25. }
  26. if(!_a){
  27. _a=_1.global;
  28. }
  29. if(!_e){
  30. switch(_b){
  31. case "keypress":
  32. _b=_f;
  33. break;
  34. case "mouseenter":
  35. _b=_5.enter;
  36. break;
  37. case "mouseleave":
  38. _b=_5.leave;
  39. break;
  40. }
  41. }
  42. return on(_a,_b,_d,_e);
  43. };
  44. var _10={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};
  45. var _11=_6("mac")?"metaKey":"ctrlKey";
  46. var _12=function(evt,_13){
  47. var _14=_7.mixin({},evt,_13);
  48. _15(_14);
  49. _14.preventDefault=function(){
  50. evt.preventDefault();
  51. };
  52. _14.stopPropagation=function(){
  53. evt.stopPropagation();
  54. };
  55. return _14;
  56. };
  57. function _15(evt){
  58. evt.keyChar=evt.charCode?String.fromCharCode(evt.charCode):"";
  59. evt.charOrCode=evt.keyChar||evt.keyCode;
  60. };
  61. var _f;
  62. if(_6("events-keypress-typed")){
  63. var _16=function(e,_17){
  64. try{
  65. return (e.keyCode=_17);
  66. }
  67. catch(e){
  68. return 0;
  69. }
  70. };
  71. _f=function(_18,_19){
  72. var _1a=on(_18,"keydown",function(evt){
  73. var k=evt.keyCode;
  74. var _1b=(k!=13)&&k!=32&&(k!=27||!_6("ie"))&&(k<48||k>90)&&(k<96||k>111)&&(k<186||k>192)&&(k<219||k>222)&&k!=229;
  75. if(_1b||evt.ctrlKey){
  76. var c=_1b?0:k;
  77. if(evt.ctrlKey){
  78. if(k==3||k==13){
  79. return _19.call(evt.currentTarget,evt);
  80. }else{
  81. if(c>95&&c<106){
  82. c-=48;
  83. }else{
  84. if((!evt.shiftKey)&&(c>=65&&c<=90)){
  85. c+=32;
  86. }else{
  87. c=_10[c]||c;
  88. }
  89. }
  90. }
  91. }
  92. var _1c=_12(evt,{type:"keypress",faux:true,charCode:c});
  93. _19.call(evt.currentTarget,_1c);
  94. if(_6("ie")){
  95. _16(evt,_1c.keyCode);
  96. }
  97. }
  98. });
  99. var _1d=on(_18,"keypress",function(evt){
  100. var c=evt.charCode;
  101. c=c>=32?c:0;
  102. evt=_12(evt,{charCode:c,faux:true});
  103. return _19.call(this,evt);
  104. });
  105. return {remove:function(){
  106. _1a.remove();
  107. _1d.remove();
  108. }};
  109. };
  110. }else{
  111. if(_6("opera")){
  112. _f=function(_1e,_1f){
  113. return on(_1e,"keypress",function(evt){
  114. var c=evt.which;
  115. if(c==3){
  116. c=99;
  117. }
  118. c=c<32&&!evt.shiftKey?0:c;
  119. if(evt.ctrlKey&&!evt.shiftKey&&c>=65&&c<=90){
  120. c+=32;
  121. }
  122. return _1f.call(this,_12(evt,{charCode:c}));
  123. });
  124. };
  125. }else{
  126. _f=function(_20,_21){
  127. return on(_20,"keypress",function(evt){
  128. _15(evt);
  129. return _21.call(this,evt);
  130. });
  131. };
  132. }
  133. }
  134. var _22={_keypress:_f,connect:function(obj,_23,_24,_25,_26){
  135. var a=arguments,_27=[],i=0;
  136. _27.push(typeof a[0]=="string"?null:a[i++],a[i++]);
  137. var a1=a[i+1];
  138. _27.push(typeof a1=="string"||typeof a1=="function"?a[i++]:null,a[i++]);
  139. for(var l=a.length;i<l;i++){
  140. _27.push(a[i]);
  141. }
  142. return _9.apply(this,_27);
  143. },disconnect:function(_28){
  144. if(_28){
  145. _28.remove();
  146. }
  147. },subscribe:function(_29,_2a,_2b){
  148. return _2.subscribe(_29,_7.hitch(_2a,_2b));
  149. },publish:function(_2c,_2d){
  150. return _2.publish.apply(_2,[_2c].concat(_2d));
  151. },connectPublisher:function(_2e,obj,_2f){
  152. var pf=function(){
  153. _22.publish(_2e,arguments);
  154. };
  155. return _2f?_22.connect(obj,_2f,pf):_22.connect(obj,pf);
  156. },isCopyKey:function(e){
  157. return e[_11];
  158. }};
  159. _22.unsubscribe=_22.disconnect;
  160. 1&&_7.mixin(_1,_22);
  161. return _22;
  162. });