| 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 /** Extensions to the [InputElement] API. */ | 7 /** Extensions to the [InputElement] API. */ |
| 8 class _InputElementExtension extends _ElementExtension { | 8 class _InputElementExtension extends _ElementExtension { |
| 9 _InputElementExtension(InputElement node) : super(node); | 9 _InputElementExtension(InputElement node) : super(node); |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 break; | 51 break; |
| 52 } | 52 } |
| 53 } | 53 } |
| 54 | 54 |
| 55 void unbindAll() { | 55 void unbindAll() { |
| 56 unbind('value'); | 56 unbind('value'); |
| 57 unbind('checked'); | 57 unbind('checked'); |
| 58 super.unbindAll(); | 58 super.unbindAll(); |
| 59 } | 59 } |
| 60 } | 60 } |
| 61 |
| 62 class _ValueBinding extends _InputBinding { |
| 63 _ValueBinding(element, model, path) : super(element, model, path); |
| 64 |
| 65 InputElement get element => super.element; |
| 66 |
| 67 void valueChanged(value) { |
| 68 element.value = value == null ? '' : '$value'; |
| 69 } |
| 70 |
| 71 void updateBinding(e) { |
| 72 binding.value = element.value; |
| 73 } |
| 74 } |
| 75 |
| 76 class _CheckedBinding extends _InputBinding { |
| 77 _CheckedBinding(element, model, path) : super(element, model, path); |
| 78 |
| 79 InputElement get element => super.element; |
| 80 |
| 81 void valueChanged(value) { |
| 82 element.checked = _toBoolean(value); |
| 83 } |
| 84 |
| 85 void updateBinding(e) { |
| 86 binding.value = element.checked; |
| 87 |
| 88 // Only the radio button that is getting checked gets an event. We |
| 89 // therefore find all the associated radio buttons and update their |
| 90 // CheckedBinding manually. |
| 91 if (element is InputElement && element.type == 'radio') { |
| 92 for (var r in _getAssociatedRadioButtons(element)) { |
| 93 var checkedBinding = _mdv(r)._checkedBinding; |
| 94 if (checkedBinding != null) { |
| 95 // Set the value directly to avoid an infinite call stack. |
| 96 checkedBinding.binding.value = false; |
| 97 } |
| 98 } |
| 99 } |
| 100 } |
| 101 |
| 102 // |element| is assumed to be an HTMLInputElement with |type| == 'radio'. |
| 103 // Returns an array containing all radio buttons other than |element| that |
| 104 // have the same |name|, either in the form that |element| belongs to or, |
| 105 // if no form, in the document tree to which |element| belongs. |
| 106 // |
| 107 // This implementation is based upon the HTML spec definition of a |
| 108 // "radio button group": |
| 109 // http://www.whatwg.org/specs/web-apps/current-work/multipage/number-state.
html#radio-button-group |
| 110 // |
| 111 static Iterable _getAssociatedRadioButtons(element) { |
| 112 if (!_isNodeInDocument(element)) return []; |
| 113 if (element.form != null) { |
| 114 return element.form.nodes.where((el) { |
| 115 return el != element && |
| 116 el is InputElement && |
| 117 el.type == 'radio' && |
| 118 el.name == element.name; |
| 119 }); |
| 120 } else { |
| 121 var radios = element.document.queryAll( |
| 122 'input[type="radio"][name="${element.name}"]'); |
| 123 return radios.where((el) => el != element && el.form == null); |
| 124 } |
| 125 } |
| 126 |
| 127 // TODO(jmesserly): polyfill document.contains API instead of doing it here |
| 128 static bool _isNodeInDocument(Node node) { |
| 129 // On non-IE this works: |
| 130 // return node.document.contains(node); |
| 131 var document = node.document; |
| 132 if (node == document || node.parentNode == document) return true; |
| 133 return document.documentElement.contains(node); |
| 134 } |
| 135 } |
| OLD | NEW |