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

Side by Side Diff: tools/dom/src/ModelTreeObserver.dart

Issue 14732003: Implement Model-Driven-Views spec for Dart (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: try upload again Created 7 years, 7 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) 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 * Currently this does not support propagation through Shadow DOMs.
15 */
16 static void initialize() {
17 if (!_initialized) {
18 _initialized = true;
19
20 if (MutationObserver.supported) {
21 var observer = new MutationObserver(_processTreeChange);
22 observer.observe(document, childList: true, subtree: true);
23 } else {
24 document.on['DOMNodeInserted'].listen(_handleNodeInserted);
25 document.on['DOMNodeRemoved'].listen(_handleNodeRemoved);
26 }
27 }
28 }
29
30 static void _processTreeChange(List<MutationRecord> mutations,
31 MutationObserver observer) {
32 for (var record in mutations) {
33 for (var node in record.addedNodes) {
34 // When nodes enter the document we need to make sure that all of the
35 // models are properly propagated through the entire sub-tree.
36 propagateModel(node, _calculatedModel(node), true);
37 }
38 for (var node in record.removedNodes) {
39 propagateModel(node, _calculatedModel(node), false);
40 }
41 }
42 }
43
44 static void _handleNodeInserted(MutationEvent e) {
45 var node = e.target;
46 window.setImmediate(() {
47 propagateModel(node, _calculatedModel(node), true);
48 });
49 }
50
51 static void _handleNodeRemoved(MutationEvent e) {
52 var node = e.target;
53 window.setImmediate(() {
54 propagateModel(node, _calculatedModel(node), false);
55 });
56 }
57
58 /**
59 * Figures out what the model should be for a node, avoiding any cached
60 * model values.
61 */
62 static _calculatedModel(node) {
63 if (node._hasLocalModel == true) {
64 return node._model;
65 } else if (node.parentNode != null) {
66 return node.parentNode._model;
67 }
68 return null;
69 }
70
71 /**
72 * Pushes model changes down through the tree.
73 *
74 * Set fullTree to true if the state of the tree is unknown and model changes
75 * should be propagated through the entire tree.
76 */
77 static void propagateModel(Node node, model, bool fullTree) {
78 // Calling into user code with the != call could generate exceptions.
79 // Catch and report them a global exceptions.
80 try {
81 if (node._hasLocalModel != true && node._model != model &&
82 node._modelChangedStreams != null &&
83 !node._modelChangedStreams.isEmpty) {
84 node._model = model;
85 node._modelChangedStreams.toList()
86 .forEach((controller) => controller.add(node));
87 }
88 } catch (e, s) {
89 new Future.error(e, s);
90 }
91 for (var child = node.$dom_firstChild; child != null;
92 child = child.nextNode) {
93 if (child._hasLocalModel != true) {
94 propagateModel(child, model, fullTree);
95 } else if (fullTree) {
96 propagateModel(child, child._model, true);
97 }
98 }
99 }
100 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698