| 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 library editable_label; | 5 library editable_label; |
| 6 | 6 |
| 7 import 'dart:html'; | 7 import 'dart:html'; |
| 8 import 'package:observe/observe.dart'; | 8 import 'dart:async'; |
| 9 import 'package:polymer/polymer.dart'; | 9 import 'package:polymer/polymer.dart'; |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Label whose [value] can be edited by double clicking. When editing, it | 12 * Label whose [value] can be edited by double clicking. When editing, it |
| 13 * displays a form and input element, otherwise it displays the label. | 13 * displays a form and input element, otherwise it displays the label. |
| 14 */ | 14 */ |
| 15 class EditableLabel extends PolymerElement with ObservableMixin { | 15 class EditableLabel extends PolymerElement with ObservableMixin { |
| 16 @observable bool editing = false; | 16 @observable bool editing = false; |
| 17 @observable String value = ''; | 17 @observable String value = ''; |
| 18 bool get applyAuthorStyles => true; | 18 bool get applyAuthorStyles => true; |
| 19 | 19 |
| 20 // TODO(jmesserly): replace this with allowing not-operator in templates. | 20 // TODO(jmesserly): replace this with allowing not-operator in templates. |
| 21 bool get notEditing => !editing; | 21 bool get notEditing => !editing; |
| 22 | 22 |
| 23 InputElement get _editBox => getShadowRoot("editable-label").query('#edit'); | 23 InputElement get _editBox => getShadowRoot("editable-label").query('#edit'); |
| 24 | 24 |
| 25 void created() { | 25 void created() { |
| 26 super.created(); | 26 super.created(); |
| 27 | 27 |
| 28 bindProperty(this, const Symbol('editing'), | 28 bindProperty(this, const Symbol('editing'), |
| 29 () => notifyProperty(this, const Symbol('notEditing'))); | 29 () => notifyProperty(this, const Symbol('notEditing'))); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void edit() { | 32 void edit() { |
| 33 editing = true; | 33 editing = true; |
| 34 | 34 |
| 35 // This causes _editBox to be inserted. | 35 // This causes _editBox to be inserted. |
| 36 deliverChangeRecords(); | 36 Observable.dirtyCheck(); |
| 37 | 37 |
| 38 // For IE and Firefox: use .focus(), then reset the value to move the | 38 // TODO(sigmund): remove the 2 runAsync calls. To do so, we might want to |
| 39 // cursor to the end. | 39 // make dirtyCheck return a future or something to indicate that all |
| 40 _editBox.focus(); | 40 // change propagations are done. |
| 41 _editBox.value = ''; | 41 runAsync(() => runAsync(() { |
| 42 _editBox.value = value; | 42 // For IE and Firefox: use .focus(), then reset the value to move the |
| 43 // cursor to the end. |
| 44 _editBox.focus(); |
| 45 _editBox.value = ''; |
| 46 _editBox.value = value; |
| 47 })); |
| 43 } | 48 } |
| 44 | 49 |
| 45 void update(Event e) { | 50 void update(Event e) { |
| 46 e.preventDefault(); // don't submit the form | 51 e.preventDefault(); // don't submit the form |
| 47 if (!editing) return; // bail if user canceled | 52 if (!editing) return; // bail if user canceled |
| 48 value = _editBox.value; | 53 value = _editBox.value; |
| 49 editing = false; | 54 editing = false; |
| 50 } | 55 } |
| 51 | 56 |
| 52 void maybeCancel(KeyboardEvent e) { | 57 void maybeCancel(KeyboardEvent e) { |
| 53 if (e.keyCode == KeyCode.ESC) { | 58 if (e.keyCode == KeyCode.ESC) { |
| 54 editing = false; | 59 editing = false; |
| 55 } | 60 } |
| 56 } | 61 } |
| 57 } | 62 } |
| 63 |
| 64 @polymerInitMethod |
| 65 void _init() { |
| 66 registerPolymerElement('editable-label', () => new EditableLabel()); |
| 67 } |
| OLD | NEW |