Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(283)

Unified Diff: pkg/web_components/lib/created_watcher.js

Issue 319263002: Add "created-watcher" to the web_components package. This was adapted from (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/web_components/lib/created_watcher.js
diff --git a/pkg/web_components/lib/created_watcher.js b/pkg/web_components/lib/created_watcher.js
new file mode 100644
index 0000000000000000000000000000000000000000..ff9db337ba3dfc5e4b5e888e3a065149376c7f4c
--- /dev/null
+++ b/pkg/web_components/lib/created_watcher.js
@@ -0,0 +1,69 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// Updates document.registerElement so Dart can see when Javascript custom
+// elements are created, and wrap them to provide a Dart friendly API.
+(function (doc) {
Jennifer Messerly 2014/06/09 17:36:07 can we include this in dart_support.js? all polyme
Siggi Cherem (dart-lang) 2014/06/09 19:29:02 I debated about it earlier :) - will do when we a
Jennifer Messerly 2014/06/09 19:33:52 sounds good!
+ var registeredWatchers = {};
+ var originalRegisterElement = doc.registerElement;
+ if (!originalRegisterElement) {
+ throw new Error('document.registerElement is not present.');
+ }
+
+ function registerElement(name, options) {
+ var proto, extendsOption;
+ if (options !== undefined) {
+ proto = options.prototype;
+ } else {
+ proto = Object.create(HTMLElement.prototype);
+ options = {protoptype: proto};
+ }
+
+ var original = proto.createdCallback;
+ var newCallback = function() {
+ original.call(this);
+ var name = this.getAttribute('is') || this.tagName;
Jennifer Messerly 2014/06/09 17:36:07 fyi: CE polyfill uses localName and toLowerCase in
Siggi Cherem (dart-lang) 2014/06/09 19:29:02 Done.
+ var watchers = registeredWatchers[name.toUpperCase()];
+ if (!watchers) return;
+ for (var i in watchers) {
Jennifer Messerly 2014/06/09 17:36:07 based on my other suggestion, it will be possible
Siggi Cherem (dart-lang) 2014/06/09 19:29:02 Done.
+ watchers[i](this);
+ }
+ };
+
+ var descriptor = Object.getOwnPropertyDescriptor(proto, 'createdCallback');
+ if (!descriptor || descriptor.writable) {
+ proto.createdCallback = newCallback;
+ } else if (descriptor.configurable) {
+ descriptor['value'] = newCallback;
+ Object.defineProperty(proto, 'createdCallback', descriptor);
+ } else {
+ console.warn("Couldn't patch prototype to notify Dart when " + name +
Jennifer Messerly 2014/06/09 17:36:07 console.error?
Siggi Cherem (dart-lang) 2014/06/09 19:29:02 Done.
+ " elements are created. This can be fixed by making the " +
+ "createdCallback in " + name + " a configurable property.");
+ }
+ return originalRegisterElement.call(doc, name, options);
Jennifer Messerly 2014/06/09 17:36:07 this, name, options?
Siggi Cherem (dart-lang) 2014/06/09 19:29:02 Done.
+ }
+
+ function registerElementCreatedWatcher(name, watcherId, watcher) {
+ if (!watcher) return;
+ name = name.toUpperCase();
+ var watchers = registeredWatchers[name];
+ if (!watchers) {
+ registeredWatchers[name] = watchers = [];
+ }
+ watchers[watcherId] = watcher;
+ }
+
+ function unregisterElementCreatedWatcher(name, watcherId) {
+ name = name.toUpperCase();
+ var watchers = registeredWatchers[name];
+ if (watchers) {
+ delete watchers[watcherId];
+ }
+ }
+
+ doc.registerElementCreatedWatcher = registerElementCreatedWatcher;
Jennifer Messerly 2014/06/09 17:36:07 it might be worth keeping these private, and only
Siggi Cherem (dart-lang) 2014/06/09 19:29:02 Done.
+ doc.unregisterElementCreatedWatcher = unregisterElementCreatedWatcher;
+ doc.registerElement = registerElement;
+})(document);

Powered by Google App Engine
This is Rietveld 408576698