Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: pkg/mdv/lib/src/select_element.dart

Issue 18117012: [package:mdv] Support binding to HTMLSelectElement.selectedIndex (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix merge issue Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/mdv/lib/src/input_element.dart ('k') | pkg/mdv/test/element_bindings_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..34ab99ce11385d64c725956cacea22901e54ca53
--- /dev/null
+++ b/pkg/mdv/lib/src/select_element.dart
@@ -0,0 +1,85 @@
+// 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;
+
+ _SelectedIndexBinding _valueBinding;
+
+ void bind(String name, model, String path) {
+ if (name.toLowerCase() == 'selectedindex') {
+ unbind('selectedindex');
+ node.attributes.remove('selectedindex');
+ _valueBinding = new _SelectedIndexBinding(node, model, path);
+ return;
+ }
+ super.bind(name, model, path);
+ }
+
+ void unbind(String name) {
+ if (name.toLowerCase() == 'selectedindex' && _valueBinding != null) {
+ _valueBinding.unbind();
+ _valueBinding = null;
+ return;
+ }
+ super.unbind(name);
+ }
+
+ 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);
+ 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>.
+ 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;
+ }
+
+ // TODO(jmesserly,sigmund): I wonder how many bindings typically convert from
+ // one type to another (e.g. value-as-number) and whether it is useful to
+ // have something like a int/num binding converter (either as a base class or
+ // a wrapper).
+ static int _toInt(value) {
+ if (value is String) return int.parse(value, onError: (_) => null);
+ return value is int ? value : null;
+ }
+}
« no previous file with comments | « pkg/mdv/lib/src/input_element.dart ('k') | pkg/mdv/test/element_bindings_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698