| 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 template_binding; | |
| 6 | |
| 7 class _InstanceBindingMap { | |
| 8 final List bindings; | |
| 9 List<_InstanceBindingMap> children; | |
| 10 DocumentFragment content; | |
| 11 | |
| 12 bool get isTemplate => false; | |
| 13 | |
| 14 _InstanceBindingMap(this.bindings); | |
| 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); | |
| 28 } | |
| 29 | |
| 30 _InstanceBindingMap _createInstanceBindingMap(Node node, | |
| 31 BindingDelegate delegate) { | |
| 32 | |
| 33 _InstanceBindingMap map = _getBindings(node, delegate); | |
| 34 if (map == null) map = new _InstanceBindingMap([]); | |
| 35 | |
| 36 List children = null; | |
| 37 int index = 0; | |
| 38 for (var c = node.firstChild; c != null; c = c.nextNode, index++) { | |
| 39 var childMap = _createInstanceBindingMap(c, delegate); | |
| 40 if (childMap == null) continue; | |
| 41 | |
| 42 // TODO(jmesserly): use a sparse map instead? | |
| 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); | |
| 62 } | |
| 63 | |
| 64 if (bindings.isTemplate) { | |
| 65 TemplateBindExtension.decorate(clone, node); | |
| 66 if (delegate != null) { | |
| 67 templateBindFallback(clone).bindingDelegate = delegate; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 _processBindings(clone, bindings, model, instanceBindings); | |
| 72 return clone; | |
| 73 } | |
| 74 | |
| 75 // TODO(rafaelw): Setup a MutationObserver on content which clears the expando | |
| 76 // so that bindingMaps regenerate when template.content changes. | |
| 77 _getInstanceBindingMap(DocumentFragment content, BindingDelegate delegate) { | |
| 78 if (delegate == null) delegate = BindingDelegate._DEFAULT; | |
| 79 | |
| 80 if (delegate._bindingMaps == null) delegate._bindingMaps = new Expando(); | |
| 81 var map = delegate._bindingMaps[content]; | |
| 82 if (map == null) { | |
| 83 map = _createInstanceBindingMap(content, delegate); | |
| 84 delegate._bindingMaps[content] = map; | |
| 85 } | |
| 86 return map; | |
| 87 } | |
| OLD | NEW |