Chromium Code Reviews| Index: pkg/mdv/lib/src/select_element.dart |
| diff --git a/pkg/mdv/lib/src/select_element.dart b/pkg/mdv/lib/src/select_element.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c84dadb27bc3d066a9735677060d7c2e99f622d4 |
| --- /dev/null |
| +++ b/pkg/mdv/lib/src/select_element.dart |
| @@ -0,0 +1,89 @@ |
| +// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +part of mdv; |
| + |
| +/** Extensions to the [SelectElement] API. */ |
| +class _SelectElementExtension extends _ElementExtension { |
| + _SelectElementExtension(SelectElement node) : super(node); |
| + |
| + 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
|
| + |
| + _SelectedIndexBinding _valueBinding; |
| + |
| + void bind(String name, model, String path) { |
| + switch (name.toLowerCase()) { |
| + 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!
|
| + unbind('selectedindex'); |
| + node.attributes.remove('selectedindex'); |
| + _valueBinding = new _SelectedIndexBinding(node, model, path); |
| + break; |
| + default: |
| + super.bind(name, model, path); |
| + break; |
| + } |
| + } |
| + |
| + void unbind(String name) { |
| + switch (name.toLowerCase()) { |
| + case 'selectedindex': |
| + if (_valueBinding != null) { |
| + _valueBinding.unbind(); |
| + _valueBinding = null; |
| + } |
| + break; |
| + default: |
| + super.unbind(name); |
| + break; |
| + } |
| + } |
| + |
| + void unbindAll() { |
| + unbind('selectedindex'); |
| + super.unbindAll(); |
| + } |
| +} |
| + |
| + |
| +class _SelectedIndexBinding extends _InputBinding { |
| + _SelectedIndexBinding(element, model, path) : super(element, model, path); |
| + |
| + SelectElement get element => super.element; |
| + |
| + void valueChanged(value) { |
| + 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
|
| + if (newValue <= element.length) { |
| + element.selectedIndex = newValue; |
| + return; |
| + } |
| + |
| + // The binding may wish to bind to an <option> which has not yet been |
| + // produced by a child <template>. Delay a maximum of twice -- once for |
| + // 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
|
| + var maxRetries = 2; |
| + delaySetSelectedIndex() { |
| + if (newValue > element.length && --maxRetries >= 0) { |
| + runAsync(delaySetSelectedIndex); |
| + } else { |
| + element.selectedIndex = newValue; |
| + } |
| + } |
| + |
| + // TODO(jmesserly): we aren't matching MDV here. They schedule "model=" to |
| + // happen async and then delay using the same scheduler. By using "runAsync" |
| + // we're going to happen quite a bit later. |
| + // We need to port the "ensureScheduled" function: |
| + // https://github.com/Polymer/mdv/commit/9a51ad7ed74a292bf71662cea28acbd151ff65c8 |
| + runAsync(delaySetSelectedIndex); |
| + } |
| + |
| + void updateBinding(e) { |
| + binding.value = element.selectedIndex; |
| + } |
| + |
| + static int _toInt(value) { |
| + 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.
|
| + return value is int ? value : null; |
| + } |
| +} |