| 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 mdv; | 5 part of mdv; |
| 6 | 6 |
| 7 // This code is a port of Model-Driven-Views: | 7 // This code is a port of Model-Driven-Views: |
| 8 // https://github.com/polymer-project/mdv | 8 // https://github.com/polymer-project/mdv |
| 9 // The code mostly comes from src/template_element.js | 9 // The code mostly comes from src/template_element.js |
| 10 | 10 |
| 11 typedef void _ChangeHandler(value); | |
| 12 | |
| 13 abstract class _InputBinding { | |
| 14 // InputElement or SelectElement | |
| 15 final element; | |
| 16 PathObserver binding; | |
| 17 StreamSubscription _pathSub; | |
| 18 StreamSubscription _eventSub; | |
| 19 | |
| 20 _InputBinding(this.element, model, String path) { | |
| 21 binding = new PathObserver(model, path); | |
| 22 _pathSub = binding.bindSync(valueChanged); | |
| 23 _eventSub = _getStreamForInputType(element).listen(updateBinding); | |
| 24 } | |
| 25 | |
| 26 void valueChanged(newValue); | |
| 27 | |
| 28 void updateBinding(e); | |
| 29 | |
| 30 void unbind() { | |
| 31 binding = null; | |
| 32 _pathSub.cancel(); | |
| 33 _eventSub.cancel(); | |
| 34 } | |
| 35 | |
| 36 static EventStreamProvider<Event> _checkboxEventType = () { | |
| 37 // Attempt to feature-detect which event (change or click) is fired first | |
| 38 // for checkboxes. | |
| 39 var div = new DivElement(); | |
| 40 var checkbox = div.append(new InputElement()); | |
| 41 checkbox.type = 'checkbox'; | |
| 42 var fired = []; | |
| 43 checkbox.onClick.listen((e) { | |
| 44 fired.add(Element.clickEvent); | |
| 45 }); | |
| 46 checkbox.onChange.listen((e) { | |
| 47 fired.add(Element.changeEvent); | |
| 48 }); | |
| 49 checkbox.dispatchEvent(new MouseEvent('click', view: window)); | |
| 50 // WebKit/Blink don't fire the change event if the element is outside the | |
| 51 // document, so assume 'change' for that case. | |
| 52 return fired.length == 1 ? Element.changeEvent : fired.first; | |
| 53 }(); | |
| 54 | |
| 55 static Stream<Event> _getStreamForInputType(element) { | |
| 56 switch (element.type) { | |
| 57 case 'checkbox': | |
| 58 return _checkboxEventType.forTarget(element); | |
| 59 case 'radio': | |
| 60 case 'select-multiple': | |
| 61 case 'select-one': | |
| 62 return element.onChange; | |
| 63 default: | |
| 64 return element.onInput; | |
| 65 } | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 | |
| 70 // TODO(jmesserly): not sure what kind of boolean conversion rules to | 11 // TODO(jmesserly): not sure what kind of boolean conversion rules to |
| 71 // apply for template data-binding. HTML attributes are true if they're | 12 // apply for template data-binding. HTML attributes are true if they're |
| 72 // present. However Dart only treats "true" as true. Since this is HTML we'll | 13 // present. However Dart only treats "true" as true. Since this is HTML we'll |
| 73 // use something closer to the HTML rules: null (missing) and false are false, | 14 // use something closer to the HTML rules: null (missing) and false are false, |
| 74 // everything else is true. See: https://github.com/polymer-project/mdv/issues/5
9 | 15 // everything else is true. See: https://github.com/polymer-project/mdv/issues/5
9 |
| 75 bool _toBoolean(value) => null != value && false != value; | 16 bool _toBoolean(value) => null != value && false != value; |
| 76 | 17 |
| 77 Node _createDeepCloneAndDecorateTemplates(Node node, String syntax) { | 18 Node _createDeepCloneAndDecorateTemplates(Node node, String syntax) { |
| 78 var clone = node.clone(false); // Shallow clone. | 19 var clone = node.clone(false); // Shallow clone. |
| 79 if (clone is Element && clone.isTemplate) { | 20 if (clone is Element && clone.isTemplate) { |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 421 nodeExt._templateIterator = null; | 362 nodeExt._templateIterator = null; |
| 422 } | 363 } |
| 423 } | 364 } |
| 424 | 365 |
| 425 _nodeOrCustom(node).unbindAll(); | 366 _nodeOrCustom(node).unbindAll(); |
| 426 for (var c = node.firstChild; c != null; c = c.nextNode) { | 367 for (var c = node.firstChild; c != null; c = c.nextNode) { |
| 427 _unbindAllRecursively(c); | 368 _unbindAllRecursively(c); |
| 428 } | 369 } |
| 429 } | 370 } |
| 430 } | 371 } |
| OLD | NEW |