OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // DO NOT EDIT | 5 // DO NOT EDIT |
6 // Auto-generated dart:html library. | 6 // Auto-generated dart:html library. |
7 | 7 |
8 /** | 8 /** |
9 * HTML elements and other resources for web-based applications that need to | 9 * HTML elements and other resources for web-based applications that need to |
10 * interact with the browser and the DOM (Document Object Model). | 10 * interact with the browser and the DOM (Document Object Model). |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 return null; | 243 return null; |
244 } | 244 } |
245 | 245 |
246 Type _getSvgType(String key) { | 246 Type _getSvgType(String key) { |
247 if (svgBlinkMap.containsKey(key)) { | 247 if (svgBlinkMap.containsKey(key)) { |
248 return svgBlinkMap[key](); | 248 return svgBlinkMap[key](); |
249 } | 249 } |
250 return null; | 250 return null; |
251 } | 251 } |
252 | 252 |
253 | |
254 $if JSINTEROP | 253 $if JSINTEROP |
255 // FIXME: Can we make this private? | 254 // FIXME: Can we make this private? |
256 final htmlBlinkFunctionMap = { | 255 final htmlBlinkFunctionMap = { |
257 $!TYPE_FUNCTION_MAP | 256 $!TYPE_FUNCTION_MAP |
258 }; | 257 }; |
259 | 258 |
260 // TODO(terry): We may want to move this elsewhere if html becomes | 259 // TODO(terry): We may want to move this elsewhere if html becomes |
261 // a package to avoid dartium depending on pkg:html. | 260 // a package to avoid dartium depending on pkg:html. |
262 getHtmlCreateFunction(String key) { | 261 getHtmlCreateFunction(String key) { |
263 var result; | 262 var result; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 | 295 |
297 // Check the svg library. | 296 // Check the svg library. |
298 result = _getSvgFunction(key); | 297 result = _getSvgFunction(key); |
299 if (result != null) { | 298 if (result != null) { |
300 return result; | 299 return result; |
301 } | 300 } |
302 | 301 |
303 return null; | 302 return null; |
304 } | 303 } |
305 | 304 |
306 Type _getHtmlFunction(String key) { | 305 Function _getHtmlFunction(String key) { |
307 if (htmlBlinkFunctionMap.containsKey(key)) { | 306 if (htmlBlinkFunctionMap.containsKey(key)) { |
308 return htmlBlinkFunctionMap[key](); | 307 return htmlBlinkFunctionMap[key](); |
309 } | 308 } |
310 return null; | 309 return null; |
311 } | 310 } |
312 | 311 |
313 Type _getWebGlFunction(String key) { | 312 Function _getWebGlFunction(String key) { |
314 if (web_glBlinkFunctionMap.containsKey(key)) { | 313 if (web_glBlinkFunctionMap.containsKey(key)) { |
315 return web_glBlinkFunctionMap[key](); | 314 return web_glBlinkFunctionMap[key](); |
316 } | 315 } |
317 return null; | 316 return null; |
318 } | 317 } |
319 | 318 |
320 Type _getIndexDbFunction(String key) { | 319 Function _getIndexDbFunction(String key) { |
321 if (indexed_dbBlinkFunctionMap.containsKey(key)) { | 320 if (indexed_dbBlinkFunctionMap.containsKey(key)) { |
322 return indexed_dbBlinkFunctionMap[key](); | 321 return indexed_dbBlinkFunctionMap[key](); |
323 } | 322 } |
324 return null; | 323 return null; |
325 } | 324 } |
326 | 325 |
327 Type _getWebAudioFunction(String key) { | 326 Function _getWebAudioFunction(String key) { |
328 if (web_audioBlinkFunctionMap.containsKey(key)) { | 327 if (web_audioBlinkFunctionMap.containsKey(key)) { |
329 return web_audioBlinkFunctionMap[key](); | 328 return web_audioBlinkFunctionMap[key](); |
330 } | 329 } |
331 return null; | 330 return null; |
332 } | 331 } |
333 | 332 |
334 Type _getWebSqlFunction(String key) { | 333 Function _getWebSqlFunction(String key) { |
335 if (web_sqlBlinkFunctionMap.containsKey(key)) { | 334 if (web_sqlBlinkFunctionMap.containsKey(key)) { |
336 return web_sqlBlinkFunctionMap[key](); | 335 return web_sqlBlinkFunctionMap[key](); |
337 } | 336 } |
338 return null; | 337 return null; |
339 } | 338 } |
340 | 339 |
341 Type _getSvgFunction(String key) { | 340 Function _getSvgFunction(String key) { |
342 if (svgBlinkFunctionMap.containsKey(key)) { | 341 if (svgBlinkFunctionMap.containsKey(key)) { |
343 return svgBlinkFunctionMap[key](); | 342 return svgBlinkFunctionMap[key](); |
344 } | 343 } |
345 return null; | 344 return null; |
346 } | 345 } |
347 | 346 |
348 | 347 |
349 /****************************************************************************** | 348 /****************************************************************************** |
350 ********** ********** | 349 ********** ********** |
351 ********** JS Interop Support ********** | 350 ********** JS Interop Support ********** |
352 ********** ********** | 351 ********** ********** |
353 ******************************************************************************/ | 352 ******************************************************************************/ |
354 | 353 |
355 Rectangle make_dart_rectangle(r) => new Rectangle(r['top'], r['left'], r['width'
], r['height']); | 354 Rectangle make_dart_rectangle(r) => |
| 355 r == null ? null : new Rectangle(r['left'], r['top'], r['width'], r['height'
]); |
356 | 356 |
| 357 // Need a default constructor for constructing classes with mixins that are |
| 358 // also extending NativeFieldWrapperClass2. Defining JsoNativeFieldWrapper |
| 359 // extending NativeFieldWrapperClass2 creates a default constructor. |
| 360 class JsoNativeFieldWrapper extends NativeFieldWrapperClass2 {} |
| 361 |
| 362 // Flag to disable JS interop asserts. Setting to false will speed up the |
| 363 // wrap_jso calls. |
| 364 bool __interop_checks = true; |
357 | 365 |
358 /** Expando for JsObject, used by every Dart class associated with a Javascript | 366 /** Expando for JsObject, used by every Dart class associated with a Javascript |
359 * class (e.g., DOM, WebAudio, etc.). | 367 * class (e.g., DOM, WebAudio, etc.). |
360 */ | 368 */ |
361 | 369 |
362 /** | 370 /** |
363 * Return the JsObject associated with a Dart class [dartClass_instance]. | 371 * Return the JsObject associated with a Dart class [dartClass_instance]. |
364 */ | 372 */ |
365 js.JsObject unwrap_jso(dartClass_instance) { | 373 unwrap_jso(dartClass_instance) { |
366 try { | 374 try { |
367 if (dartClass_instance != null) | 375 if (dartClass_instance != null) |
368 return dartClass_instance is! Function ? dartClass_instance.blink_jsOb
ject : dartClass_instance; | 376 return dartClass_instance is NativeFieldWrapperClass2 ? |
369 else | 377 dartClass_instance.blink_jsObject : dartClass_instance; |
370 return null; | 378 else |
371 // return dartClass_instance.dartium_expando[dartClass_instance.expandoJsOb
ject]; | 379 return null; |
372 } catch(NoSuchMethodException) { | 380 } catch(NoSuchMethodException) { |
373 // No blink_jsObject then return the dartClass_instance is probably an | 381 // No blink_jsObject then return the dartClass_instance is probably an |
374 // array that was already converted to a Dart class e.g., Uint8ClampedList
. | 382 // array that was already converted to a Dart class e.g., Uint8ClampedList. |
375 return dartClass_instance; | 383 return dartClass_instance; |
376 } | 384 } |
377 } | 385 } |
378 | 386 |
379 /** | 387 /** |
380 * Create Dart class that maps to the JS Type, add the JsObject as an expando | 388 * Create Dart class that maps to the JS Type, add the JsObject as an expando |
381 * on the Dart class and return the created Dart class. | 389 * on the Dart class and return the created Dart class. |
382 */ | 390 */ |
383 wrap_jso(jsObject) { | 391 wrap_jso(jsObject) { |
384 try { | 392 try { |
385 // debug_or_assert("jsObject != null", jsObject != null); | 393 if (jsObject is! js.JsObject) { |
386 if (jsObject is! js.JsObject) { | |
387 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. | 394 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. |
388 return jsObject; | 395 return jsObject; |
| 396 } |
| 397 var constructor = jsObject['constructor']; |
| 398 if (__interop_checks) { |
| 399 if (jsObject is js.JsArray) { |
| 400 return jsObject; |
| 401 } |
| 402 |
| 403 debug_or_assert("constructor != null", constructor != null); |
| 404 } |
| 405 if (constructor == js.context['Object']) { |
| 406 return convertNativeObjectToDartMap(jsObject); |
| 407 } |
| 408 if (constructor == js.context['Promise']) { |
| 409 return convertNativePromiseToDartFuture(jsObject); |
| 410 } |
| 411 var jsTypeName = constructor['name']; |
| 412 if (__interop_checks) { |
| 413 debug_or_assert("constructor != null && jsTypeName.length > 0", constructo
r != null && jsTypeName.length > 0); |
| 414 } |
| 415 |
| 416 var dartClass_instance; |
| 417 if (jsObject.hasProperty('dart_class')) { |
| 418 // Got a dart_class (it's a custom element) use it it's already set up. |
| 419 dartClass_instance = jsObject['dart_class']; |
| 420 } else { |
| 421 var func = getHtmlCreateFunction(jsTypeName); |
| 422 if (func != null) { |
| 423 dartClass_instance = func(); |
| 424 dartClass_instance.blink_jsObject = jsObject; |
| 425 } |
| 426 } |
| 427 return dartClass_instance; |
| 428 } catch(e, stacktrace){ |
| 429 if (__interop_checks) { |
| 430 if (e is DebugAssertException) |
| 431 window.console.log("${e.message}\n ${stacktrace}"); |
| 432 else |
| 433 window.console.log("${stacktrace}"); |
| 434 } |
389 } | 435 } |
390 var constructor = jsObject['constructor']; | 436 |
391 debug_or_assert("constructor != null", constructor != null); | 437 return null; |
392 var jsTypeName = constructor['name']; | |
393 debug_or_assert("constructor != null && jsTypeName.length > 0", constructor !=
null && jsTypeName.length > 0); | |
394 var func = getHtmlCreateFunction(jsTypeName); | |
395 debug_or_assert("func != null name = ${jsTypeName}", func != null); | |
396 var dartClass_instance = func(); | |
397 dartClass_instance.blink_jsObject = jsObject; | |
398 // dartClass_instance.dartium_expando[dartClass_instance.expandoJsObject] = jsO
bject; | |
399 return dartClass_instance; | |
400 } catch(e, stacktrace){ | |
401 if (e is DebugAssertException) | |
402 window.console.log("${e.message}\n ${stacktrace}"); | |
403 else | |
404 window.console.log("${stacktrace}"); | |
405 } | 438 } |
| 439 |
| 440 /** |
| 441 * Create Dart class that maps to the JS Type that is the JS type being |
| 442 * extended using JS interop createCallback (we need the base type of the |
| 443 * custom element) not the Dart created constructor. |
| 444 */ |
| 445 wrap_jso_custom_element(jsObject) { |
| 446 try { |
| 447 if (jsObject is! js.JsObject) { |
| 448 // JS Interop converted the object to a Dart class e.g., Uint8ClampedList. |
| 449 return jsObject; |
| 450 } |
| 451 |
| 452 // Find out what object we're extending. |
| 453 var objectName = jsObject.toString(); |
| 454 // Expect to see something like '[object HTMLElement]'. |
| 455 if (!objectName.startsWith('[object ')) { |
| 456 return jsObject; |
| 457 } |
| 458 |
| 459 var extendsClass = objectName.substring(8, objectName.length - 1); |
| 460 var func = getHtmlCreateFunction(extendsClass); |
| 461 if (__interop_checks) |
| 462 debug_or_assert("func != null name = ${extendsClass}", func != null); |
| 463 var dartClass_instance = func(); |
| 464 dartClass_instance.blink_jsObject = jsObject; |
| 465 return dartClass_instance; |
| 466 } catch(e, stacktrace){ |
| 467 if (__interop_checks) { |
| 468 if (e is DebugAssertException) |
| 469 window.console.log("${e.message}\n ${stacktrace}"); |
| 470 else |
| 471 window.console.log("${stacktrace}"); |
| 472 } |
| 473 |
| 474 // Problem? |
| 475 return null; |
| 476 } |
406 } | 477 } |
407 | 478 |
408 class DebugAssertException implements Exception { | 479 class DebugAssertException implements Exception { |
409 String message; | 480 String message; |
410 DebugAssertException(this.message); | 481 DebugAssertException(this.message); |
411 } | 482 } |
412 | 483 |
413 debug_or_assert(message, expression) { | 484 debug_or_assert(message, expression) { |
414 if (!expression) { | 485 if (!expression) { |
415 throw new DebugAssertException("$message"); | 486 throw new DebugAssertException("$message"); |
416 } | 487 } |
417 } | 488 } |
418 | 489 |
419 // Wrap JsObject node list to return a List<node>. | 490 // TODO(terry): Manage JS interop JsFunctions for each listener used for add/ |
420 List<Node> wrap_jso_list(jso_nodes) { | 491 // removeEventListener. These JsFunctions will leak look at |
421 List<Node> nodes = new List<Node>(); | 492 // fixing with weak-refs in C++. The key are the hashcodes of the |
422 var collectionLen = jso_nodes['length']; | 493 // user's this (this is needed for futures) and listener function. |
423 for (var i = 0; i < collectionLen; i++) { | 494 Map<int, Map<int, js.JsFunction>> _knownListeners = {}; |
424 nodes.add(wrap_jso(jso_nodes.callMethod('item', [i]))); | 495 |
| 496 js.JsFunction wrap_event_listener(theObject, Function listener) { |
| 497 var thisHashCode = theObject.hashCode; |
| 498 var listenerHashCode = identityHashCode(listener); |
| 499 |
| 500 _knownListeners.putIfAbsent(thisHashCode, () => new Map<int, js.JsFunction>())
; |
| 501 _knownListeners[thisHashCode].putIfAbsent(listenerHashCode, () => |
| 502 new js.JsFunction.withThis((theObject, event) => listener(wrap_jso(event))))
; |
| 503 |
| 504 return _knownListeners[thisHashCode][listenerHashCode]; |
| 505 } |
| 506 |
| 507 js.JsFunction wrap_media_event_listener(Function listener) { |
| 508 var thisHashCode = theObject.hashCode; |
| 509 var listenerHashCode = identityHashCode(listener); |
| 510 |
| 511 _knownListeners.putIfAbsent(thisHashCode, () => new Map<int, js.JsFunction>())
; |
| 512 _knownListeners[thisHashCode].putIfAbsent(listenerHashCode, () => |
| 513 new js.JsFunction.withThis((theObject, eventListener) => listener(wrap_jso(e
ventListener)))); |
| 514 |
| 515 return _knownListeners[thisHashCode][listenerHashCode]; |
| 516 } |
| 517 |
| 518 Map<String, dynamic> convertNativeObjectToDartMap(js.JsObject jsObject) { |
| 519 var result = new Map(); |
| 520 var keys = js.context['Object'].callMethod('keys', [jsObject]); |
| 521 for (var key in keys) { |
| 522 result[key] = wrap_jso(jsObject[key]); |
425 } | 523 } |
426 var frozen_nodes = new _FrozenElementList._wrap(nodes); | 524 return result; |
427 frozen_nodes.dartClass_instance = jso_nodes; | |
428 return frozen_nodes; | |
429 } | 525 } |
| 526 |
| 527 // Converts a flat Dart map into a JavaScript object with properties this is |
| 528 // is the Dartium only version it uses dart:js. |
| 529 convertDartToNative_Dictionary(Map dict) { |
| 530 if (dict == null) return null; |
| 531 var jsObject = new js.JsObject(js.context['Object']); |
| 532 dict.forEach((String key, value) { |
| 533 if (value is List) { |
| 534 var jsArray = new js.JsArray(); |
| 535 value.forEach((elem) { |
| 536 jsArray.add(elem is Map ? convertDartToNative_Dictionary(elem): elem); |
| 537 }); |
| 538 jsObject[key] = jsArray; |
| 539 } else { |
| 540 jsObject[key] = value; |
| 541 } |
| 542 }); |
| 543 return jsObject; |
| 544 } |
| 545 |
| 546 // Converts a Dart list into a JsArray. For the Dartium version only. |
| 547 convertDartToNative_List(List input) => new js.JsArray()..addAll(input); |
| 548 |
| 549 // Conversion function place holder (currently not used in dart2js or dartium). |
| 550 List convertDartToNative_StringArray(List<String> input) => input; |
| 551 |
| 552 Future convertNativePromiseToDartFuture(js.JsObject promise) { |
| 553 var completer = new Completer(); |
| 554 var newPromise = promise |
| 555 .callMethod("then", [(result) => completer.complete(result)]) |
| 556 .callMethod("catch", [(result) => completer.completeError(result)]); |
| 557 return completer.future; |
| 558 } |
| 559 |
430 $else | 560 $else |
| 561 class JsoNativeFieldWrapper extends NativeFieldWrapperClass2 {} |
| 562 |
431 unwrap_jso(dartClass_instance) => dartClass_instance; | 563 unwrap_jso(dartClass_instance) => dartClass_instance; |
432 wrap_jso(jsObject) => jsObject; | 564 wrap_jso(jsObject) => jsObject; |
433 wrap_jso_list(jso_nodes) => jso_nodes; | |
434 make_dart_rectangle(r) => r; | 565 make_dart_rectangle(r) => r; |
| 566 convertDartToNative_Dictionary(Map dict) => dict; |
| 567 List convertDartToNative_StringArray(List<String> input) => input; |
| 568 convertDartToNative_List(List input) => input; |
| 569 |
435 $endif | 570 $endif |
OLD | NEW |