|
| 1 | +<!doctype html> |
| 2 | +<html> |
| 3 | +<head> |
| 4 | + |
| 5 | + <title>observeReNodes</title> |
| 6 | + |
| 7 | + <meta charset="utf-8"> |
| 8 | + <meta name="viewport" content="width=device-width, initial-scale=1.0"> |
| 9 | + |
| 10 | + <script src="../../../webcomponentsjs/webcomponents-lite.js"></script> |
| 11 | + <link rel="import" href="../../polymer.html"> |
| 12 | + |
| 13 | +</head> |
| 14 | +<body> |
| 15 | + |
| 16 | + <dom-module id='test-inner'> |
| 17 | + <template> |
| 18 | + ~<content id="c" select=".a"></content>~ |
| 19 | + </template> |
| 20 | + <script> |
| 21 | + Polymer({ |
| 22 | + is:'test-inner', |
| 23 | + ready: function() { |
| 24 | + this.observe(); |
| 25 | + }, |
| 26 | + |
| 27 | + observe: function() { |
| 28 | + this._childObserver = Polymer.dom(this).observeNodes(function(info) { |
| 29 | + info.addedNodes.forEach(function(n) { |
| 30 | + if (n.nodeType === Node.ELEMENT_NODE) { |
| 31 | + console.log('added:', n.localName, n.textContent); |
| 32 | + } |
| 33 | + }); |
| 34 | + info.removedNodes.forEach(function(n) { |
| 35 | + if (n.nodeType === Node.ELEMENT_NODE) { |
| 36 | + console.log('removed:', n.localName, n.textContent); |
| 37 | + } |
| 38 | + }); |
| 39 | + }); |
| 40 | + this._contentObserver = Polymer.dom(this.$.c).observeNodes(function(info) { |
| 41 | + info.addedNodes.forEach(function(n) { |
| 42 | + if (n.nodeType === Node.ELEMENT_NODE) { |
| 43 | + console.log('%c content added:', 'color: blue;', n.localName, n.textContent); |
| 44 | + } |
| 45 | + }); |
| 46 | + info.removedNodes.forEach(function(n) { |
| 47 | + if (n.nodeType === Node.ELEMENT_NODE) { |
| 48 | + console.log('%c content removed:', 'color: blue;', n.localName, n.textContent); |
| 49 | + } |
| 50 | + }); |
| 51 | + }, true); |
| 52 | + }, |
| 53 | + |
| 54 | + unobserve: function() { |
| 55 | + Polymer.dom(this).unobserveNodes(this._childObserver); |
| 56 | + Polymer.dom(this).unobserveNodes(this._contentObserver); |
| 57 | + } |
| 58 | + }); |
| 59 | + </script> |
| 60 | + </dom-module> |
| 61 | + |
| 62 | + <dom-module id='test-content'> |
| 63 | + <template> |
| 64 | + <test-inner id="inner">[<content id="ip" select=".b"></content>]</test-inner> |
| 65 | + </template> |
| 66 | + <script> |
| 67 | + (function() { |
| 68 | + Polymer({ |
| 69 | + is:'test-content' |
| 70 | + }); |
| 71 | + })(); |
| 72 | + </script> |
| 73 | + </dom-module> |
| 74 | + |
| 75 | + <test-content id="content"> |
| 76 | + <div class="a b">content A</div> |
| 77 | + <div class="a b">content B</div> |
| 78 | + </test-content> |
| 79 | + |
| 80 | + <br><br> |
| 81 | + |
| 82 | + <script> |
| 83 | + |
| 84 | + function makeNode(text) { |
| 85 | + var d = document.createElement('div'); |
| 86 | + d.textContent = text;; |
| 87 | + return d; |
| 88 | + } |
| 89 | + |
| 90 | + |
| 91 | + Polymer.dom.flush(); |
| 92 | + |
| 93 | + // setTimeout(function() { |
| 94 | + console.group('test dynamic'); |
| 95 | + var d = makeNode('dynamic!'); |
| 96 | + Polymer.dom.flush(); |
| 97 | + Polymer.dom(content).appendChild(d); |
| 98 | + Polymer.dom.flush(); |
| 99 | + Polymer.dom(content).removeChild(d); |
| 100 | + Polymer.dom.flush(); |
| 101 | + d = makeNode('1'); |
| 102 | + Polymer.dom(d).classList.add('b'); |
| 103 | + Polymer.dom(content).appendChild(d); |
| 104 | + Polymer.dom.flush(); |
| 105 | + Polymer.dom(d).classList.add('a'); |
| 106 | + Polymer.dom(Polymer.dom(d).parentNode).notifyObservers(); |
| 107 | + Polymer.dom(content.$.inner).appendChild(makeNode('2')); |
| 108 | + Polymer.dom.flush(); |
| 109 | + d = makeNode('-1'); |
| 110 | + Polymer.dom(d).classList.add('b'); |
| 111 | + Polymer.dom(d).classList.add('a'); |
| 112 | + Polymer.dom(content).insertBefore(d, Polymer.dom(content).firstChild); |
| 113 | + Polymer.dom.flush(); |
| 114 | + Polymer.dom(content.$.inner).insertBefore(makeNode('-2'), Polymer.dom(content.$.inner).firstChild); |
| 115 | + Polymer.dom.flush(); |
| 116 | + Polymer.dom(content.$.inner).removeChild(Polymer.dom(content.$.inner).firstChild); |
| 117 | + Polymer.dom(content.$.inner).removeChild(Polymer.dom(content.$.inner).lastChild); |
| 118 | + Polymer.dom(content).removeChild(Polymer.dom(content).firstChild); |
| 119 | + Polymer.dom(content).removeChild(Polymer.dom(content).lastChild); |
| 120 | + Polymer.dom.flush(); |
| 121 | + console.groupEnd('test dynamic'); |
| 122 | + // }, 1000); |
| 123 | + </script> |
| 124 | + |
| 125 | +</body> |
| 126 | +</html> |
0 commit comments