Index: pkg/element_created/lib/element_created.js |
diff --git a/pkg/element_created/lib/element_created.js b/pkg/element_created/lib/element_created.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..243794501d9946bdf2fb2d4a541e1cd74f2bb89d |
--- /dev/null |
+++ b/pkg/element_created/lib/element_created.js |
@@ -0,0 +1,67 @@ |
+(function (doc) { |
+ |
+ var originalRegisterElement = document.registerElement; |
+ if (!originalRegisterElement) { |
+ throw new Error('document.registerElement is not present.'); |
+ } |
+ |
+ function registerElement(name, options) { |
+ var newOptions = Object.create(options); |
+ for (var attr in options) { |
+ if (options.hasOwnProperty(attr)) { |
+ newOptions[attr] = options[attr]; |
+ } |
+ } |
+ |
+ var originalCreated = newOptions.prototype.createdCallback; |
+ |
+ newOptions.prototype.createdCallback = function() { |
+ originalCreated.call(this); |
+ |
+ var key = getTagKey(this.tagName, this.getAttribute('is')); |
+ var watchers = createdWatchers[key]; |
+ if (!watchers) { |
+ return; |
+ } |
+ for (var i = 0, w; (w = watchers[i]); i++) { |
+ invokeWatcher(this, w); |
+ } |
+ }; |
+ |
+ return originalRegisterElement.call(doc, name, newOptions); |
+ } |
+ |
+ function invokeWatcher(node, watcher) { |
+ if (watcher.createdCallback) { |
+ watcher.createdCallback.call(node); |
+ } |
+ } |
+ |
+ // Unique key for the tagName & extends combo |
+ function getTagKey(tagName, isTag) { |
+ if (isTag) { |
+ // '.' is a reserved character for XML names, so safe to use. |
+ return tagName.toUpperCase() + '.' + isTag.toUpperCase(); |
+ } |
+ return tagName.toUpperCase(); |
+ } |
+ |
+ var createdWatchers = {}; |
+ |
+ |
+ function registerElementCreatedWatcher(name, options) { |
+ if (!options) return; |
+ |
+ var key = getTagKey(name, options.extends); |
+ if (key in createdWatchers) { |
+ createdWatchers[key].push(options); |
+ } else { |
+ createdWatchers[key] = [options]; |
+ } |
+ } |
+ |
+ |
+ |
+ doc.registerElementCreatedWatcher = registerElementCreatedWatcher; |
+ doc.registerElement = registerElement; |
+})(document); |