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