OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of template_binding; | 5 part of template_binding; |
6 | 6 |
7 class _InstanceBindingMap { | 7 class _InstanceBindingMap { |
8 final List bindings; | 8 final List bindings; |
9 final Map<int, _InstanceBindingMap> children; | 9 List<_InstanceBindingMap> children; |
10 final Node templateRef; | 10 DocumentFragment content; |
11 | 11 |
12 // Workaround for: | 12 bool get isTemplate => false; |
13 // https://github.com/Polymer/TemplateBinding/issues/150 | |
14 final int numChildren; | |
15 | 13 |
16 _InstanceBindingMap._(this.bindings, this.children, this.templateRef, | 14 _InstanceBindingMap(this.bindings); |
17 this.numChildren); | 15 |
| 16 _InstanceBindingMap getChild(int index) { |
| 17 if (children == null || index >= children.length) return null; |
| 18 return children[index]; |
| 19 } |
| 20 } |
| 21 |
| 22 class _TemplateBindingMap extends _InstanceBindingMap { |
| 23 bool get isTemplate => true; |
| 24 |
| 25 MustacheTokens _if, _bind, _repeat; |
| 26 |
| 27 _TemplateBindingMap(List bindings) : super(bindings); |
18 } | 28 } |
19 | 29 |
20 _InstanceBindingMap _createInstanceBindingMap(Node node, | 30 _InstanceBindingMap _createInstanceBindingMap(Node node, |
21 BindingDelegate delegate) { | 31 BindingDelegate delegate) { |
22 | 32 |
23 var bindings = _getBindings(node, delegate); | 33 _InstanceBindingMap map = _getBindings(node, delegate); |
24 Node templateRef = null; | 34 if (map == null) map = new _InstanceBindingMap([]); |
25 | 35 |
26 if (isSemanticTemplate(node)) templateRef = node; | 36 List children = null; |
27 | 37 int index = 0; |
28 Map children = null; | 38 for (var c = node.firstChild; c != null; c = c.nextNode, index++) { |
29 int i = 0; | |
30 for (var c = node.firstChild; c != null; c = c.nextNode, i++) { | |
31 var childMap = _createInstanceBindingMap(c, delegate); | 39 var childMap = _createInstanceBindingMap(c, delegate); |
32 if (childMap == null) continue; | 40 if (childMap == null) continue; |
33 | 41 |
34 if (children == null) children = new HashMap(); | 42 // TODO(jmesserly): use a sparse map instead? |
35 children[i] = childMap; | 43 if (children == null) children = new List(node.nodes.length); |
| 44 children[index] = childMap; |
| 45 } |
| 46 map.children = children; |
| 47 |
| 48 return map; |
| 49 } |
| 50 |
| 51 Node _cloneAndBindInstance(Node node, Node parent, Document stagingDocument, |
| 52 _InstanceBindingMap bindings, model, BindingDelegate delegate, |
| 53 List instanceBindings, [TemplateInstance instanceRecord]) { |
| 54 |
| 55 var clone = parent.append(stagingDocument.importNode(node, false)); |
| 56 |
| 57 int i = 0; |
| 58 for (var c = node.firstChild; c != null; c = c.nextNode, i++) { |
| 59 var childMap = bindings != null ? bindings.getChild(i) : null; |
| 60 _cloneAndBindInstance(c, clone, stagingDocument, childMap, model, delegate, |
| 61 instanceBindings); |
36 } | 62 } |
37 | 63 |
38 if (bindings == null && children == null && templateRef == null) return null; | 64 if (bindings.isTemplate) { |
39 | 65 TemplateBindExtension.decorate(clone, node); |
40 return new _InstanceBindingMap._(bindings, children, templateRef, i); | |
41 } | |
42 | |
43 void _addMapBindings(Node node, _InstanceBindingMap map, model, | |
44 BindingDelegate delegate, List bound) { | |
45 if (map == null) return; | |
46 | |
47 if (map.templateRef != null) { | |
48 TemplateBindExtension.decorate(node, map.templateRef); | |
49 if (delegate != null) { | 66 if (delegate != null) { |
50 templateBindFallback(node)._bindingDelegate = delegate; | 67 templateBindFallback(clone).bindingDelegate = delegate; |
51 } | 68 } |
52 } | 69 } |
53 | 70 |
54 if (map.bindings != null) { | 71 _processBindings(clone, bindings, model, instanceBindings); |
55 _processBindings(map.bindings, node, model, bound); | 72 return clone; |
56 } | |
57 | |
58 if (map.children == null) return; | |
59 | |
60 // To workaround https://github.com/Polymer/TemplateBinding/issues/150, | |
61 // we try and detect cases where creating a custom element resulted in extra | |
62 // children compared to what we expected. We assume these new children are all | |
63 // at the beginning, because _deepCloneIgnoreTemplateContent creates the | |
64 // element then appends the template content's children to the end. | |
65 | |
66 int i = map.numChildren - node.nodes.length; | |
67 for (var c = node.firstChild; c != null; c = c.nextNode, i++) { | |
68 if (i < 0) continue; | |
69 _addMapBindings(c, map.children[i], model, delegate, bound); | |
70 } | |
71 } | 73 } |
OLD | NEW |