| OLD | NEW |
| 1 /** | 1 /** |
| 2 * A custom KeyboardEvent that attempts to eliminate cross-browser | 2 * A custom KeyboardEvent that attempts to eliminate cross-browser |
| 3 * inconsistencies, and also provide both keyCode and charCode information | 3 * inconsistencies, and also provide both keyCode and charCode information |
| 4 * for all key events (when such information can be determined). | 4 * for all key events (when such information can be determined). |
| 5 * | 5 * |
| 6 * KeyEvent tries to provide a higher level, more polished keyboard event | 6 * KeyEvent tries to provide a higher level, more polished keyboard event |
| 7 * information on top of the "raw" [KeyboardEvent]. | 7 * information on top of the "raw" [KeyboardEvent]. |
| 8 * | 8 * |
| 9 * The mechanics of using KeyEvents is a little different from the underlying | 9 * The mechanics of using KeyEvents is a little different from the underlying |
| 10 * [KeyboardEvent]. To use KeyEvents, you need to create a stream and then add | 10 * [KeyboardEvent]. To use KeyEvents, you need to create a stream and then add |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 */ | 69 */ |
| 70 static final _keyboardEventDispatchRecord = _makeRecord(); | 70 static final _keyboardEventDispatchRecord = _makeRecord(); |
| 71 | 71 |
| 72 /** Helper to statically create the dispatch record. */ | 72 /** Helper to statically create the dispatch record. */ |
| 73 static _makeRecord() { | 73 static _makeRecord() { |
| 74 var interceptor = JS_INTERCEPTOR_CONSTANT(KeyboardEvent); | 74 var interceptor = JS_INTERCEPTOR_CONSTANT(KeyboardEvent); |
| 75 return makeLeafDispatchRecord(interceptor); | 75 return makeLeafDispatchRecord(interceptor); |
| 76 } | 76 } |
| 77 | 77 |
| 78 /** Construct a KeyEvent with [parent] as the event we're emulating. */ | 78 /** Construct a KeyEvent with [parent] as the event we're emulating. */ |
| 79 KeyEvent.wrap(KeyboardEvent parent): super(parent) { | 79 KeyEvent.wrap(KeyboardEvent parent) : super(parent) { |
| 80 _parent = parent; | 80 _parent = parent; |
| 81 _shadowAltKey = _realAltKey; | 81 _shadowAltKey = _realAltKey; |
| 82 _shadowCharCode = _realCharCode; | 82 _shadowCharCode = _realCharCode; |
| 83 _shadowKeyCode = _realKeyCode; | 83 _shadowKeyCode = _realKeyCode; |
| 84 _currentTarget = _parent.currentTarget; | 84 _currentTarget = _parent.currentTarget; |
| 85 } | 85 } |
| 86 | 86 |
| 87 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ | 87 /** Programmatically create a new KeyEvent (and KeyboardEvent). */ |
| 88 factory KeyEvent(String type, | 88 factory KeyEvent(String type, |
| 89 {Window view, bool canBubble: true, bool cancelable: true, int keyCode: 0, | 89 {Window view, |
| 90 int charCode: 0, int keyLocation: 1, bool ctrlKey: false, | 90 bool canBubble: true, |
| 91 bool altKey: false, bool shiftKey: false, bool metaKey: false, | 91 bool cancelable: true, |
| 92 int keyCode: 0, |
| 93 int charCode: 0, |
| 94 int keyLocation: 1, |
| 95 bool ctrlKey: false, |
| 96 bool altKey: false, |
| 97 bool shiftKey: false, |
| 98 bool metaKey: false, |
| 92 EventTarget currentTarget}) { | 99 EventTarget currentTarget}) { |
| 93 if (view == null) { | 100 if (view == null) { |
| 94 view = window; | 101 view = window; |
| 95 } | 102 } |
| 96 | 103 |
| 97 var eventObj; | 104 var eventObj; |
| 98 // In these two branches we create an underlying native KeyboardEvent, but | 105 // In these two branches we create an underlying native KeyboardEvent, but |
| 99 // we set it with our specified values. Because we are doing custom setting | 106 // we set it with our specified values. Because we are doing custom setting |
| 100 // of certain values (charCode/keyCode, etc) only in this class (as opposed | 107 // of certain values (charCode/keyCode, etc) only in this class (as opposed |
| 101 // to KeyboardEvent) and the way we set these custom values depends on the | 108 // to KeyboardEvent) and the way we set these custom values depends on the |
| (...skipping 19 matching lines...) Expand all Loading... |
| 121 // TypeError when trying to do the Object.defineProperty hack, so we avoid | 128 // TypeError when trying to do the Object.defineProperty hack, so we avoid |
| 122 // this branch if possible. | 129 // this branch if possible. |
| 123 // Also, if we want this branch to work in FF, we also need to modify | 130 // Also, if we want this branch to work in FF, we also need to modify |
| 124 // _initKeyboardEvent to also take charCode and keyCode values to | 131 // _initKeyboardEvent to also take charCode and keyCode values to |
| 125 // initialize initKeyEvent. | 132 // initialize initKeyEvent. |
| 126 | 133 |
| 127 eventObj = new Event.eventType('KeyboardEvent', type, | 134 eventObj = new Event.eventType('KeyboardEvent', type, |
| 128 canBubble: canBubble, cancelable: cancelable); | 135 canBubble: canBubble, cancelable: cancelable); |
| 129 | 136 |
| 130 // Chromium Hack | 137 // Chromium Hack |
| 131 JS('void', "Object.defineProperty(#, 'keyCode', {" | 138 JS( |
| 132 " get : function() { return this.keyCodeVal; } })", eventObj); | 139 'void', |
| 133 JS('void', "Object.defineProperty(#, 'which', {" | 140 "Object.defineProperty(#, 'keyCode', {" |
| 134 " get : function() { return this.keyCodeVal; } })", eventObj); | 141 " get : function() { return this.keyCodeVal; } })", |
| 135 JS('void', "Object.defineProperty(#, 'charCode', {" | 142 eventObj); |
| 136 " get : function() { return this.charCodeVal; } })", eventObj); | 143 JS( |
| 144 'void', |
| 145 "Object.defineProperty(#, 'which', {" |
| 146 " get : function() { return this.keyCodeVal; } })", |
| 147 eventObj); |
| 148 JS( |
| 149 'void', |
| 150 "Object.defineProperty(#, 'charCode', {" |
| 151 " get : function() { return this.charCodeVal; } })", |
| 152 eventObj); |
| 137 | 153 |
| 138 var keyIdentifier = _convertToHexString(charCode, keyCode); | 154 var keyIdentifier = _convertToHexString(charCode, keyCode); |
| 139 eventObj._initKeyboardEvent(type, canBubble, cancelable, view, | 155 eventObj._initKeyboardEvent(type, canBubble, cancelable, view, |
| 140 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey); | 156 keyIdentifier, keyLocation, ctrlKey, altKey, shiftKey, metaKey); |
| 141 JS('void', '#.keyCodeVal = #', eventObj, keyCode); | 157 JS('void', '#.keyCodeVal = #', eventObj, keyCode); |
| 142 JS('void', '#.charCodeVal = #', eventObj, charCode); | 158 JS('void', '#.charCodeVal = #', eventObj, charCode); |
| 143 } | 159 } |
| 144 // Tell dart2js that it smells like a KeyboardEvent! | 160 // Tell dart2js that it smells like a KeyboardEvent! |
| 145 setDispatchProperty(eventObj, _keyboardEventDispatchRecord); | 161 setDispatchProperty(eventObj, _keyboardEventDispatchRecord); |
| 146 | 162 |
| 147 var keyEvent = new KeyEvent.wrap(eventObj); | 163 var keyEvent = new KeyEvent.wrap(eventObj); |
| 148 if (keyEvent._currentTarget == null) { | 164 if (keyEvent._currentTarget == null) { |
| 149 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; | 165 keyEvent._currentTarget = currentTarget == null ? window : currentTarget; |
| 150 } | 166 } |
| 151 return keyEvent; | 167 return keyEvent; |
| 152 } | 168 } |
| 153 | 169 |
| 154 // Currently known to work on all browsers but IE. | 170 // Currently known to work on all browsers but IE. |
| 155 static bool get canUseDispatchEvent => | 171 static bool get canUseDispatchEvent => JS( |
| 156 JS('bool', | 172 'bool', |
| 157 '(typeof document.body.dispatchEvent == "function")' | 173 '(typeof document.body.dispatchEvent == "function")' |
| 158 '&& document.body.dispatchEvent.length > 0'); | 174 '&& document.body.dispatchEvent.length > 0'); |
| 159 | 175 |
| 160 /** The currently registered target for this event. */ | 176 /** The currently registered target for this event. */ |
| 161 EventTarget get currentTarget => _currentTarget; | 177 EventTarget get currentTarget => _currentTarget; |
| 162 | 178 |
| 163 // This is an experimental method to be sure. | 179 // This is an experimental method to be sure. |
| 164 static String _convertToHexString(int charCode, int keyCode) { | 180 static String _convertToHexString(int charCode, int keyCode) { |
| 165 if (charCode != -1) { | 181 if (charCode != -1) { |
| 166 var hex = charCode.toRadixString(16); // Convert to hexadecimal. | 182 var hex = charCode.toRadixString(16); // Convert to hexadecimal. |
| 167 StringBuffer sb = new StringBuffer('U+'); | 183 StringBuffer sb = new StringBuffer('U+'); |
| 168 for (int i = 0; i < 4 - hex.length; i++) sb.write('0'); | 184 for (int i = 0; i < 4 - hex.length; i++) sb.write('0'); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 195 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, | 211 * KeyLocation.STANDARD, KeyLocation.RIGHT, KeyLocation.LEFT, |
| 196 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). | 212 * KeyLocation.NUMPAD, KeyLocation.MOBILE, KeyLocation.JOYSTICK). |
| 197 */ | 213 */ |
| 198 int get keyLocation => _parent.keyLocation; | 214 int get keyLocation => _parent.keyLocation; |
| 199 /** True if the Meta (or Mac command) key is pressed during this event. */ | 215 /** True if the Meta (or Mac command) key is pressed during this event. */ |
| 200 bool get metaKey => _parent.metaKey; | 216 bool get metaKey => _parent.metaKey; |
| 201 /** True if the shift key was pressed during this event. */ | 217 /** True if the shift key was pressed during this event. */ |
| 202 bool get shiftKey => _parent.shiftKey; | 218 bool get shiftKey => _parent.shiftKey; |
| 203 InputDevice get sourceDevice => _parent.sourceDevice; | 219 InputDevice get sourceDevice => _parent.sourceDevice; |
| 204 Window get view => _parent.view; | 220 Window get view => _parent.view; |
| 205 void _initUIEvent(String type, bool canBubble, bool cancelable, | 221 void _initUIEvent( |
| 206 Window view, int detail) { | 222 String type, bool canBubble, bool cancelable, Window view, int detail) { |
| 207 throw new UnsupportedError("Cannot initialize a UI Event from a KeyEvent."); | 223 throw new UnsupportedError("Cannot initialize a UI Event from a KeyEvent."); |
| 208 } | 224 } |
| 225 |
| 209 String get _shadowKeyIdentifier => JS('String', '#.keyIdentifier', _parent); | 226 String get _shadowKeyIdentifier => JS('String', '#.keyIdentifier', _parent); |
| 210 | 227 |
| 211 int get _charCode => charCode; | 228 int get _charCode => charCode; |
| 212 int get _keyCode => keyCode; | 229 int get _keyCode => keyCode; |
| 213 int get _which => which; | 230 int get _which => which; |
| 214 | 231 |
| 215 String get _keyIdentifier { | 232 String get _keyIdentifier { |
| 216 throw new UnsupportedError("keyIdentifier is unsupported."); | 233 throw new UnsupportedError("keyIdentifier is unsupported."); |
| 217 } | 234 } |
| 218 void _initKeyboardEvent(String type, bool canBubble, bool cancelable, | 235 |
| 219 Window view, String keyIdentifier, int keyLocation, bool ctrlKey, | 236 void _initKeyboardEvent( |
| 220 bool altKey, bool shiftKey, bool metaKey) { | 237 String type, |
| 238 bool canBubble, |
| 239 bool cancelable, |
| 240 Window view, |
| 241 String keyIdentifier, |
| 242 int keyLocation, |
| 243 bool ctrlKey, |
| 244 bool altKey, |
| 245 bool shiftKey, |
| 246 bool metaKey) { |
| 221 throw new UnsupportedError( | 247 throw new UnsupportedError( |
| 222 "Cannot initialize a KeyboardEvent from a KeyEvent."); | 248 "Cannot initialize a KeyboardEvent from a KeyEvent."); |
| 223 } | 249 } |
| 250 |
| 224 @Experimental() // untriaged | 251 @Experimental() // untriaged |
| 225 bool getModifierState(String keyArgument) => throw new UnimplementedError(); | 252 bool getModifierState(String keyArgument) => throw new UnimplementedError(); |
| 226 @Experimental() // untriaged | 253 @Experimental() // untriaged |
| 227 int get location => throw new UnimplementedError(); | 254 int get location => throw new UnimplementedError(); |
| 228 @Experimental() // untriaged | 255 @Experimental() // untriaged |
| 229 bool get repeat => throw new UnimplementedError(); | 256 bool get repeat => throw new UnimplementedError(); |
| 230 dynamic get _get_view => throw new UnimplementedError(); | 257 dynamic get _get_view => throw new UnimplementedError(); |
| 231 } | 258 } |
| OLD | NEW |