Chromium Code Reviews| 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 mdv; | |
| 6 | |
| 7 /** Extensions to the [SelectElement] API. */ | |
| 8 class _SelectElementExtension extends _ElementExtension { | |
| 9 _SelectElementExtension(SelectElement node) : super(node); | |
| 10 | |
| 11 SelectElement get node => super.node; | |
|
Siggi Cherem (dart-lang)
2013/06/28 20:36:13
is this needed for functionality or just for speci
Jennifer Messerly
2013/07/01 22:07:25
just specializing the type. better type checks, mm
| |
| 12 | |
| 13 _SelectedIndexBinding _valueBinding; | |
| 14 | |
| 15 void bind(String name, model, String path) { | |
| 16 switch (name.toLowerCase()) { | |
| 17 case 'selectedindex': | |
|
Siggi Cherem (dart-lang)
2013/06/28 20:36:13
seems strange to have a switch statement for just
Jennifer Messerly
2013/07/01 22:07:25
good point. fixed!
| |
| 18 unbind('selectedindex'); | |
| 19 node.attributes.remove('selectedindex'); | |
| 20 _valueBinding = new _SelectedIndexBinding(node, model, path); | |
| 21 break; | |
| 22 default: | |
| 23 super.bind(name, model, path); | |
| 24 break; | |
| 25 } | |
| 26 } | |
| 27 | |
| 28 void unbind(String name) { | |
| 29 switch (name.toLowerCase()) { | |
| 30 case 'selectedindex': | |
| 31 if (_valueBinding != null) { | |
| 32 _valueBinding.unbind(); | |
| 33 _valueBinding = null; | |
| 34 } | |
| 35 break; | |
| 36 default: | |
| 37 super.unbind(name); | |
| 38 break; | |
| 39 } | |
| 40 } | |
| 41 | |
| 42 void unbindAll() { | |
| 43 unbind('selectedindex'); | |
| 44 super.unbindAll(); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 | |
| 49 class _SelectedIndexBinding extends _InputBinding { | |
| 50 _SelectedIndexBinding(element, model, path) : super(element, model, path); | |
| 51 | |
| 52 SelectElement get element => super.element; | |
| 53 | |
| 54 void valueChanged(value) { | |
| 55 var newValue = _toInt(value); | |
|
Siggi Cherem (dart-lang)
2013/06/28 20:36:13
not for now, but I wonder how many bindings typica
Jennifer Messerly
2013/07/01 22:07:25
it's a good point. I added a TODO to highlight thi
| |
| 56 if (newValue <= element.length) { | |
| 57 element.selectedIndex = newValue; | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 // The binding may wish to bind to an <option> which has not yet been | |
| 62 // produced by a child <template>. Delay a maximum of twice -- once for | |
| 63 // iterating <optgroup> and once for <option>. | |
|
Siggi Cherem (dart-lang)
2013/06/28 20:36:13
is this to address this bug: https://github.com/da
Jennifer Messerly
2013/07/01 22:07:25
i don't think it is related. it's to make sure <te
| |
| 64 var maxRetries = 2; | |
| 65 delaySetSelectedIndex() { | |
| 66 if (newValue > element.length && --maxRetries >= 0) { | |
| 67 runAsync(delaySetSelectedIndex); | |
| 68 } else { | |
| 69 element.selectedIndex = newValue; | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 // TODO(jmesserly): we aren't matching MDV here. They schedule "model=" to | |
| 74 // happen async and then delay using the same scheduler. By using "runAsync" | |
| 75 // we're going to happen quite a bit later. | |
| 76 // We need to port the "ensureScheduled" function: | |
| 77 // https://github.com/Polymer/mdv/commit/9a51ad7ed74a292bf71662cea28acbd151f f65c8 | |
| 78 runAsync(delaySetSelectedIndex); | |
| 79 } | |
| 80 | |
| 81 void updateBinding(e) { | |
| 82 binding.value = element.selectedIndex; | |
| 83 } | |
| 84 | |
| 85 static int _toInt(value) { | |
| 86 if (value is String) return int.parse(value, onError: (_) {}); | |
|
Siggi Cherem (dart-lang)
2013/06/28 20:36:13
maybe make it more explicit that the onerror defau
Jennifer Messerly
2013/07/01 22:07:25
Done.
| |
| 87 return value is int ? value : null; | |
| 88 } | |
| 89 } | |
| OLD | NEW |