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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 // Updates document.registerElement so Dart can see when Javascript custom
6 // elements are created, and wrap them to provide a Dart friendly API.
7 (function (doc) {
8 var originalRegisterElement = doc.registerElement;
Siggi Cherem (dart-lang) 2014/06/06 22:31:47 this changed from document to doc
9 if (!originalRegisterElement) {
10 throw new Error('document.registerElement is not present.');
11 }
12
13 function registerElement(name, options) {
14 var newOptions = Object.create(options);
justinfagnani 2014/06/06 23:33:05 Why setup a prototype and copy ownProperties? I wo
Siggi Cherem (dart-lang) 2014/06/09 17:10:27 very good questions. I had many discussions here w
15 for (var attr in options) {
16 if (options.hasOwnProperty(attr)) {
17 newOptions[attr] = options[attr];
18 }
19 }
20
21 var originalCreated = newOptions.prototype.createdCallback;
22 newOptions.prototype.createdCallback = function() {
23 originalCreated.call(this);
24
25 var key = getTagKey(this.tagName, this.getAttribute('is'));
26 var watchers = createdWatchers[key];
27 if (!watchers) {
28 return;
29 }
30 for (var i = 0, w; (w = watchers[i]); i++) {
31 invokeWatcher(this, w);
32 }
33 };
34
35 return originalRegisterElement.call(doc, name, newOptions);
36 }
37
38 function invokeWatcher(node, watcher) {
39 if (watcher.createdCallback) {
40 watcher.createdCallback.call(node);
41 }
42 }
43
44 // Unique key for the tagName & extends combo
45 function getTagKey(tagName, isTag) {
46 if (isTag) {
47 // '.' is a reserved character for XML names, so safe to use.
justinfagnani 2014/06/06 23:33:05 I think this will be simpler if you just use isTag
Siggi Cherem (dart-lang) 2014/06/09 17:10:28 Good idea. Done.
48 return tagName.toUpperCase() + '.' + isTag.toUpperCase();
49 }
50 return tagName.toUpperCase();
51 }
52
53 var createdWatchers = {};
justinfagnani 2014/06/06 23:33:05 what's our style here, var declarations in the mid
Siggi Cherem (dart-lang) 2014/06/09 17:10:27 I moved it up because it is used above, but I hone
54
55
56 function registerElementCreatedWatcher(name, options) {
57 if (!options) return;
58
59 var tagName = options.extends || name;
60 var key = getTagKey(tagName, options.extends ? name : undefined);
Siggi Cherem (dart-lang) 2014/06/06 22:31:47 this changed from: getTagKey(name, options.exten
justinfagnani 2014/06/06 23:33:05 Same as the comment above, except just always use
Siggi Cherem (dart-lang) 2014/06/09 17:10:27 Done.
61 if (key in createdWatchers) {
62 createdWatchers[key].push(options);
63 } else {
64 createdWatchers[key] = [options];
65 }
66 }
67
68 doc.registerElementCreatedWatcher = registerElementCreatedWatcher;
69 doc.registerElement = registerElement;
70 })(document);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698