OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <!-- | 2 <!-- |
3 Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 3 Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
4 for details. All rights reserved. Use of this source code is governed by a | 4 for details. All rights reserved. Use of this source code is governed by a |
5 BSD-style license that can be found in the LICENSE file. | 5 BSD-style license that can be found in the LICENSE file. |
6 --> | 6 --> |
7 <html> | 7 <html> |
8 <head> | 8 <head> |
9 <meta charset="utf-8"> | 9 <meta charset="utf-8"> |
10 </head> | 10 </head> |
11 <body> | 11 <body> |
12 <element name="editable-label" extends="div" apply-author-styles> | 12 <polymer-element name="editable-label" attributes="value"> |
13 <template> | 13 <template> |
14 <template if="!editing"> | 14 <template bind if="{{!editing}}"> |
15 <label class='edit-label' on-double-click="edit()">{{value}}</label> | 15 <label class='edit-label' on-double-click="edit">{{value}}</label> |
16 </template> | 16 </template> |
17 <template if="editing"> | 17 <template bind if="{{editing}}"> |
18 <form on-submit="update($event)"> | 18 <form on-submit="update"> |
19 <input id="edit" class="edit {{editing ? 'editing' : ''}}" | 19 <input id="edit" class="edit editing" |
20 on-blur="update($event)" | 20 on-blur="update" on-key-up="maybeCancel"> |
21 on-key-up="maybeCancel($event)"> | |
22 </form> | 21 </form> |
23 </template> | 22 </template> |
24 </template> | 23 </template> |
25 <script type="application/dart"> | 24 <script type="application/dart" src="editable_label.dart"></script> |
26 import 'dart:html'; | 25 </polymer-element> |
27 import 'package:web_ui/web_ui.dart'; | |
28 | |
29 /** | |
30 * Label whose [value] can be edited by double clicking. When editing, it | |
31 * displays a form and input element, otherwise it displays the label. You | |
32 * can enable two-way binding like this: | |
33 * | |
34 * <editable-label bind-value="dartAsignableValue"> | |
35 * </editable-label> | |
36 */ | |
37 @observable | |
38 class EditableLabel extends WebComponent { | |
39 bool editing; | |
40 String value; | |
41 | |
42 InputElement get _editBox => getShadowRoot("editable-label").query('#edit'); | |
43 | |
44 void created() { | |
45 super.created(); | |
46 editing = false; | |
47 value = ''; | |
48 } | |
49 | |
50 void edit() { | |
51 editing = true; | |
52 | |
53 // This causes _editBox to be inserted. | |
54 deliverChangesSync(); | |
55 | |
56 // For IE and Firefox: use .focus(), then reset the value to move the | |
57 // cursor to the end. | |
58 _editBox.focus(); | |
59 _editBox.value = ''; | |
60 _editBox.value = value; | |
61 } | |
62 | |
63 void update(Event e) { | |
64 e.preventDefault(); // don't submit the form | |
65 if (!editing) return; // bail if user canceled | |
66 value = _editBox.value; | |
67 editing = false; | |
68 } | |
69 | |
70 void maybeCancel(KeyEvent e) { | |
71 if (e.keyCode == KeyCode.ESC) { | |
72 editing = false; | |
73 } | |
74 } | |
75 } | |
76 </script> | |
77 </element> | |
78 </body> | 26 </body> |
79 </html> | 27 </html> |
OLD | NEW |