|
| 1 | +<!-- Copyright 2014 Google Inc. All rights reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +--> |
| 15 | + |
| 16 | +<!-- |
| 17 | +Put this element in another element's <template> as: |
| 18 | + <cxx-get-element-by-id elemId="{{idToLookUp}}" elem="{{variableToFillIn}}"> |
| 19 | + </cxx-get-element-by-id> |
| 20 | +
|
| 21 | +Then the containing element can define a method `variableToFillInChanged` that |
| 22 | +will be called when an element with the right ID is added to the document. |
| 23 | +--> |
| 24 | +<polymer-element name="cxx-get-element-by-id" attributes="elemId elem"> |
| 25 | + <script> |
| 26 | + (function() { |
| 27 | + 'use strict'; |
| 28 | + |
| 29 | + // Maps IDs to an array of elements waiting for that ID to appear. |
| 30 | + var pendingIds = {}; |
| 31 | + |
| 32 | + // Watches for all node changes that might make an ID appear, and checks |
| 33 | + // the result against the pendingIDs. |
| 34 | + var shared_observer = new MutationObserver(function(mutations) { |
| 35 | + // We only seem to get a mutation record for the top node of a subtree |
| 36 | + // that gets added to the DOM, which means that looking at just the |
| 37 | + // addedNodes' IDs misses a bunch of IDs. Instead, we'll just search |
| 38 | + // for all the IDs we care about, any time anything changes. |
| 39 | + Object.keys(pendingIds).forEach(function(id) { |
| 40 | + var elem = document.getElementById(id); |
| 41 | + if (elem) { |
| 42 | + pendingIds[id].forEach(function(waiting) { |
| 43 | + if (waiting.elemId == id) |
| 44 | + waiting.elem = elem; |
| 45 | + }); |
| 46 | + delete pendingIds[id]; |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + if (Object.keys(pendingIds).length == 0) |
| 51 | + shared_observer.disconnect(); |
| 52 | + }); |
| 53 | + |
| 54 | + // Adds `to_notify` to the pendingIds list, waiting for `id` to appear, |
| 55 | + // and makes sure the MutationObserver is watching. |
| 56 | + function watchFor(id, to_notify) { |
| 57 | + if (!pendingIds.hasOwnProperty(id)) |
| 58 | + pendingIds[id] = []; |
| 59 | + pendingIds[id].push(to_notify); |
| 60 | + |
| 61 | + shared_observer.observe(document, { |
| 62 | + childList: true, |
| 63 | + attributes: true, |
| 64 | + subtree: true, |
| 65 | + attributeFilter: ['id'], |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + Polymer('cxx-get-element-by-id', { |
| 70 | + elemId: '', |
| 71 | + elemIdChanged: function() { |
| 72 | + if (!this.elemId) { |
| 73 | + this.elem = null; |
| 74 | + return; |
| 75 | + } |
| 76 | + this.elem = document.getElementById(this.elemId); |
| 77 | + if (!this.elem) { |
| 78 | + watchFor(this.elemId, this); |
| 79 | + } |
| 80 | + }, |
| 81 | + }); |
| 82 | + })(); |
| 83 | + </script> |
| 84 | +</polymer-element> |
0 commit comments