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 todomvc.web.editable_label; | 5 library todomvc.web.editable_label; |
6 | 6 |
7 import 'dart:html'; | 7 import 'dart:html'; |
8 import 'package:polymer/polymer.dart'; | 8 import 'package:polymer/polymer.dart'; |
9 | 9 |
10 /** | 10 /** |
11 * Label whose [value] can be edited by double clicking. When editing, it | 11 * Label whose [value] can be edited by double clicking. When editing, it |
12 * displays a form and input element, otherwise it displays the label. | 12 * displays a form and input element, otherwise it displays the label. |
13 */ | 13 */ |
14 // For illustration purposes this type uses Polymer.register instead of | 14 // For illustration purposes this type uses Polymer.register instead of |
15 // CustomTag. We must mark it @reflectable to ensure its members | 15 // CustomTag. We must mark it @reflectable to ensure its members |
16 // (the event handlers) are preserved and can be referenced from HTML. | 16 // (the event handlers) are preserved and can be referenced from HTML. |
17 @reflectable | 17 @reflectable |
18 class EditableLabel extends PolymerElement { | 18 class EditableLabel extends PolymerElement { |
19 @observable bool editing = false; | 19 @observable bool editing = false; |
20 @published String value = ''; | 20 @published String value = ''; |
21 | 21 |
22 factory EditableLabel() => new Element.tag('editable-label'); | 22 factory EditableLabel() => new Element.tag('editable-label'); |
23 | 23 |
24 EditableLabel.created() : super.created(); | 24 EditableLabel.created() : super.created(); |
25 | 25 |
26 bool get applyAuthorStyles => true; | 26 bool get applyAuthorStyles => true; |
27 | 27 |
28 InputElement get _editBox => getShadowRoot("editable-label").query('#edit'); | 28 ShadowRoot get _shadowRoot => getShadowRoot('editable-label'); |
| 29 |
| 30 InputElement get _editBox => _shadowRoot.query('#edit'); |
29 | 31 |
30 void edit() { | 32 void edit() { |
31 editing = true; | 33 editing = true; |
32 | 34 |
33 // This causes _editBox to be inserted. | 35 // Wait for _editBox to be inserted. |
34 performMicrotaskCheckpoint(); | 36 onMutation(_shadowRoot, (_) { |
35 | 37 // For IE and Firefox: use .focus(), then reset the value to move the |
36 // For IE and Firefox: use .focus(), then reset the value to move the | 38 // cursor to the end. |
37 // cursor to the end. | 39 _editBox.focus(); |
38 _editBox.focus(); | 40 _editBox.value = ''; |
39 _editBox.value = ''; | 41 _editBox.value = value; |
40 _editBox.value = value; | 42 }); |
41 } | 43 } |
42 | 44 |
43 void update(Event e) { | 45 void update(Event e) { |
44 e.preventDefault(); // don't submit the form | 46 e.preventDefault(); // don't submit the form |
45 if (!editing) return; // bail if user canceled | 47 if (!editing) return; // bail if user canceled |
46 value = _editBox.value; | 48 value = _editBox.value; |
47 editing = false; | 49 editing = false; |
48 } | 50 } |
49 | 51 |
50 void maybeCancel(KeyboardEvent e) { | 52 void maybeCancel(KeyboardEvent e) { |
51 if (e.keyCode == KeyCode.ESC) { | 53 if (e.keyCode == KeyCode.ESC) { |
52 editing = false; | 54 editing = false; |
53 } | 55 } |
54 } | 56 } |
55 } | 57 } |
56 | 58 |
57 @initMethod | 59 @initMethod |
58 void _init() { | 60 void _init() { |
59 Polymer.register('editable-label', EditableLabel); | 61 Polymer.register('editable-label', EditableLabel); |
60 } | 62 } |
OLD | NEW |