| 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 /** Extensions to [Element]s that behave as templates. */ | 7 /** Extensions to [Element]s that behave as templates. */ |
| 8 class TemplateBindExtension extends _ElementExtension { | 8 class TemplateBindExtension extends NodeBindExtension { |
| 9 var _model; | 9 var _model; |
| 10 BindingDelegate _bindingDelegate; | 10 BindingDelegate _bindingDelegate; |
| 11 _TemplateIterator _iterator; | 11 _TemplateIterator _iterator; |
| 12 bool _setModelScheduled = false; | 12 bool _setModelScheduled = false; |
| 13 | 13 |
| 14 Element _templateInstanceRef; | 14 Element _templateInstanceRef; |
| 15 | 15 |
| 16 // Note: only used if `this is! TemplateElement` | 16 // Note: only used if `this is! TemplateElement` |
| 17 DocumentFragment _content; | 17 DocumentFragment _content; |
| 18 bool _templateIsDecorated; | 18 bool _templateIsDecorated; |
| 19 | 19 |
| 20 HtmlDocument _stagingDocument; | 20 HtmlDocument _stagingDocument; |
| 21 | 21 |
| 22 _InstanceBindingMap _bindingMap; | 22 _InstanceBindingMap _bindingMap; |
| 23 | 23 |
| 24 Node _refContent; | 24 Node _refContent; |
| 25 | 25 |
| 26 TemplateBindExtension._(Element node) : super(node); | 26 TemplateBindExtension._(Element node) : super._(node); |
| 27 | 27 |
| 28 Element get _node => super._node; | 28 Element get _node => super._node; |
| 29 | 29 |
| 30 TemplateBindExtension get _self => super._node is TemplateBindExtension | 30 TemplateBindExtension get _self => _node is TemplateBindExtension |
| 31 ? _node : this; | 31 ? _node : this; |
| 32 | 32 |
| 33 Bindable bind(String name, value, {bool oneTime: false}) { | 33 Bindable bind(String name, value, {bool oneTime: false}) { |
| 34 if (name != 'ref') return super.bind(name, value, oneTime: oneTime); | 34 if (name != 'ref') return super.bind(name, value, oneTime: oneTime); |
| 35 | 35 |
| 36 | |
| 37 var ref = oneTime ? value : value.open((ref) { | 36 var ref = oneTime ? value : value.open((ref) { |
| 38 _node.attributes['ref'] = ref; | 37 _node.attributes['ref'] = ref; |
| 39 _refChanged(); | 38 _refChanged(); |
| 40 }); | 39 }); |
| 41 | 40 |
| 42 _node.attributes['ref'] = ref; | 41 _node.attributes['ref'] = ref; |
| 43 _refChanged(); | 42 _refChanged(); |
| 44 if (oneTime) return null; | 43 if (oneTime) return null; |
| 45 | 44 |
| 46 return _updateBindings('ref', value); | 45 if (bindings == null) bindings = {}; |
| 46 return bindings['ref'] = value; |
| 47 } | 47 } |
| 48 | 48 |
| 49 _TemplateIterator _processBindingDirectives(_TemplateBindingMap directives) { | 49 _TemplateIterator _processBindingDirectives(_TemplateBindingMap directives) { |
| 50 if (_iterator != null) _iterator._closeDependencies(); | 50 if (_iterator != null) _iterator._closeDependencies(); |
| 51 | 51 |
| 52 if (directives._if == null && | 52 if (directives._if == null && |
| 53 directives._bind == null && | 53 directives._bind == null && |
| 54 directives._repeat == null) { | 54 directives._repeat == null) { |
| 55 | 55 |
| 56 if (_iterator != null) { | 56 if (_iterator != null) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 71 | 71 |
| 72 return _iterator; | 72 return _iterator; |
| 73 } | 73 } |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * Creates an instance of the template, using the provided [model] and | 76 * Creates an instance of the template, using the provided [model] and |
| 77 * optional binding [delegate]. | 77 * optional binding [delegate]. |
| 78 * | 78 * |
| 79 * If [instanceBindings] is supplied, each [Bindable] in the returned | 79 * If [instanceBindings] is supplied, each [Bindable] in the returned |
| 80 * instance will be added to the list. This makes it easy to close all of the | 80 * instance will be added to the list. This makes it easy to close all of the |
| 81 * bindings without walking the tree. This is not normally necesssary, but is | 81 * bindings without walking the tree. This is not normally necessary, but is |
| 82 * used internally by the system. | 82 * used internally by the system. |
| 83 */ | 83 */ |
| 84 DocumentFragment createInstance([model, BindingDelegate delegate]) { | 84 DocumentFragment createInstance([model, BindingDelegate delegate]) { |
| 85 if (delegate == null) delegate = _bindingDelegate; | 85 if (delegate == null) delegate = _bindingDelegate; |
| 86 if (_refContent == null) _refContent = templateBind(_ref).content; | 86 if (_refContent == null) _refContent = templateBind(_ref).content; |
| 87 | 87 |
| 88 var content = _refContent; | 88 var content = _refContent; |
| 89 if (content.firstChild == null) return _emptyInstance; | 89 if (content.firstChild == null) return _emptyInstance; |
| 90 | 90 |
| 91 final map = _getInstanceBindingMap(content, delegate); | 91 final map = _getInstanceBindingMap(content, delegate); |
| (...skipping 427 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 } | 519 } |
| 520 | 520 |
| 521 _getInstanceRoot(node) { | 521 _getInstanceRoot(node) { |
| 522 while (node.parentNode != null) { | 522 while (node.parentNode != null) { |
| 523 node = node.parentNode; | 523 node = node.parentNode; |
| 524 } | 524 } |
| 525 _InstanceExtension instance = _instanceExtension[node]; | 525 _InstanceExtension instance = _instanceExtension[node]; |
| 526 return instance != null && instance._templateCreator != null ? node : null; | 526 return instance != null && instance._templateCreator != null ? node : null; |
| 527 } | 527 } |
| 528 | 528 |
| 529 // Note: JS code tests that getElementById is present. We can't do that |
| 530 // easily, so instead check for the types known to implement it. |
| 531 bool _hasGetElementById(Node node) => |
| 532 node is Document || node is ShadowRoot || node is SvgSvgElement; |
| 533 |
| 529 final Expando<_InstanceExtension> _instanceExtension = new Expando(); | 534 final Expando<_InstanceExtension> _instanceExtension = new Expando(); |
| 530 | 535 |
| 531 final _isStagingDocument = new Expando(); | 536 final _isStagingDocument = new Expando(); |
| OLD | NEW |