| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 /// Provides support for associating a Dart type for Javascript Custom Elements. | 5 /// Provides support for associating a Dart type for Javascript Custom Elements. |
| 6 /// This will not work unless `dart_support.js` is loaded. | 6 /// This will not work unless `dart_support.js` is loaded. |
| 7 library web_components.interop; | 7 library web_components.interop; |
| 8 | 8 |
| 9 import 'dart:async' show Stream, StreamController; | 9 import 'dart:async' show Stream, StreamController; |
| 10 import 'dart:html' show document, Element; | 10 import 'dart:html' show document, Element; |
| 11 import 'dart:js' show JsObject, JsFunction; | 11 import 'dart:js' show JsObject, JsFunction; |
| 12 | 12 |
| 13 final _doc = new JsObject.fromBrowserObject(document); | 13 final _doc = new JsObject.fromBrowserObject(document); |
| 14 | 14 |
| 15 /// Returns whether [registerDartType] is supported, which requires to have | 15 /// Returns whether [registerDartType] is supported, which requires to have |
| 16 /// `dart_support.js` already loaded in the page. | 16 /// `dart_support.js` already loaded in the page. |
| 17 bool get isSupported => _doc.hasProperty('_registerDartTypeUpgrader'); | 17 bool get isSupported => _doc.hasProperty('_registerDartTypeUpgrader'); |
| 18 | 18 |
| 19 /// Watches when Javascript custom elements named [tagName] are created and | 19 /// Watches when Javascript custom elements named [tagName] are created and |
| 20 /// associates the created element with the given [dartType]. Only one Dart type | 20 /// associates the created element with the given [dartType]. Only one Dart type |
| 21 /// can be registered for a given tag name. | 21 /// can be registered for a given tag name. |
| 22 void registerDartType(String tagName, Type dartType, {String extendsTag}) { | 22 void registerDartType(String tagName, Type dartType, {String extendsTag}) { |
| 23 if (!isSupported) { | 23 if (!isSupported) { |
| 24 throw new UnsupportedError("Couldn't find " | 24 throw new UnsupportedError("Couldn't find " |
| 25 "`document._registerDartTypeUpgrader`. Please make sure that " | 25 "`document._registerDartTypeUpgrader`. Please make sure that " |
| 26 "`packages/web_components/dart_support.js` is loaded and available " | 26 "`packages/web_components/interop_support.html` is loaded and " |
| 27 "before calling this function."); | 27 "available before calling this function."); |
| 28 } | 28 } |
| 29 | 29 |
| 30 var upgrader = document.createElementUpgrader( | 30 var upgrader = document.createElementUpgrader( |
| 31 dartType, extendsTag: extendsTag); | 31 dartType, extendsTag: extendsTag); |
| 32 | 32 |
| 33 // Unfortunately the dart:html upgrader will throw on an already-upgraded | 33 // Unfortunately the dart:html upgrader will throw on an already-upgraded |
| 34 // element, so we need to duplicate the type check to prevent that. | 34 // element, so we need to duplicate the type check to prevent that. |
| 35 // An element can be upgraded twice if it extends another element and calls | 35 // An element can be upgraded twice if it extends another element and calls |
| 36 // createdCallback on the superclass. Since that's a valid use case we must | 36 // createdCallback on the superclass. Since that's a valid use case we must |
| 37 // wrap at both levels, and guard against it here. | 37 // wrap at both levels, and guard against it here. |
| 38 upgradeElement(e) { | 38 upgradeElement(e) { |
| 39 if (e.runtimeType != dartType) upgrader.upgrade(e); | 39 if (e.runtimeType != dartType) upgrader.upgrade(e); |
| 40 } | 40 } |
| 41 | 41 |
| 42 _doc.callMethod('_registerDartTypeUpgrader', [tagName, upgradeElement]); | 42 _doc.callMethod('_registerDartTypeUpgrader', [tagName, upgradeElement]); |
| 43 } | 43 } |
| 44 | 44 |
| 45 /// This function is mainly used to save resources. By default, we save a log of | 45 /// This function is mainly used to save resources. By default, we save a log of |
| 46 /// elements that are created but have no Dart type associated with them. This | 46 /// elements that are created but have no Dart type associated with them. This |
| 47 /// is so we can upgrade them as soon as [registerDartType] is invoked. This | 47 /// is so we can upgrade them as soon as [registerDartType] is invoked. This |
| 48 /// function can be called to indicate that we no longer are interested in | 48 /// function can be called to indicate that we no longer are interested in |
| 49 /// logging element creations and that it is sufficient to only upgrade new | 49 /// logging element creations and that it is sufficient to only upgrade new |
| 50 /// elements as they are being created. Typically this is called after the last | 50 /// elements as they are being created. Typically this is called after the last |
| 51 /// call to [registerDartType] or as soon as you know that no element will be | 51 /// call to [registerDartType] or as soon as you know that no element will be |
| 52 /// created until the call to [registerDartType] is made. | 52 /// created until the call to [registerDartType] is made. |
| 53 void onlyUpgradeNewElements() => _doc.callMethod('_onlyUpgradeNewElements'); | 53 void onlyUpgradeNewElements() => _doc.callMethod('_onlyUpgradeNewElements'); |
| OLD | NEW |