Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, 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 part of html; | |
| 6 | |
| 7 class _ModelTreeObserver { | |
| 8 static bool _initialized = false; | |
| 9 | |
| 10 /** | |
| 11 * Start an observer watching the document for tree changes to automatically | |
| 12 * propagate model changes. | |
| 13 */ | |
| 14 static void initialize() { | |
| 15 if (!_initialized) { | |
| 16 _initialized = true; | |
| 17 | |
| 18 if (MutationObserver.supported) { | |
| 19 var observer = new MutationObserver(_processTreeChange); | |
| 20 observer.observe(document, childList: true, subtree: true); | |
| 21 } else { | |
| 22 document.on['DOMNodeInserted'].listen(_handleNodeInserted); | |
| 23 document.on['DOMNodeRemoved'].listen(_handleNodeRemoved); | |
| 24 } | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 static void _processTreeChange(List<MutationRecord> mutations, | |
| 29 MutationObserver observer) { | |
| 30 for (var record in mutations) { | |
| 31 for (var node in record.addedNodes) { | |
| 32 // When nodes enter the document we need to make sure that all of the | |
| 33 // models are properly propagated through the entire sub-tree. | |
| 34 propagateModel(node, _calculatedModel(node), true); | |
| 35 } | |
| 36 for (var node in record.removedNodes) { | |
| 37 propagateModel(node, _calculatedModel(node), false); | |
| 38 } | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 static void _handleNodeInserted(MutationEvent e) { | |
| 43 var node = e.target; | |
| 44 propagateModel(node, _calculatedModel(node), true); | |
| 45 } | |
| 46 | |
| 47 static void _handleNodeRemoved(MutationEvent e) { | |
| 48 var node = e.target; | |
| 49 propagateModel(node, _calculatedModel(node), false); | |
| 50 } | |
| 51 | |
| 52 /** | |
| 53 * Figures out what the model should be for a node, avoiding any cached | |
| 54 * model values. | |
| 55 */ | |
| 56 static _calculatedModel(node) { | |
| 57 if (node._hasLocalModel == true) { | |
| 58 return node._model; | |
| 59 } else if (node.parentNode != null) { | |
| 60 return node.parentNode._model; | |
| 61 } | |
| 62 return null; | |
| 63 } | |
| 64 | |
| 65 /** | |
| 66 * Pushes model changes down through the tree. | |
| 67 * | |
| 68 * Set fullTree to true if the state of the tree is unknown and model changes | |
| 69 * should be propagated through the entire tree. | |
| 70 */ | |
| 71 static void propagateModel(Node node, model, bool fullTree) { | |
| 72 // TODO(blois): calling into user code with the != call, should catch errors | |
|
Jennifer Messerly
2013/03/15 18:49:21
FWIW, I noticed the pattern in stream_impl is to w
blois
2013/03/15 20:14:10
Looks good.
| |
| 73 // and report them a global exceptions. | |
| 74 if (node._hasLocalModel != true && node._model != model && | |
| 75 node._modelChangedStream != null) { | |
| 76 node._model = model; | |
| 77 node._modelChangedStream.add(model); | |
| 78 } | |
| 79 for (var child = node.$dom_firstChild; child != null; | |
| 80 child = child.nextNode) { | |
| 81 if (child._hasLocalModel != true) { | |
| 82 propagateModel(child, model, fullTree); | |
| 83 } else if (fullTree) { | |
| 84 propagateModel(child, child._model, true); | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 } | |
| OLD | NEW |