| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2013 The Polymer Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style |
| 3 // license that can be found in the LICENSE file. |
| 4 library todomvc.web.lib_elements.polymer_selection; |
| 5 |
| 6 import 'dart:html'; |
| 7 import 'package:polymer/polymer.dart'; |
| 8 |
| 9 @CustomTag('polymer-selection') |
| 10 class PolymerSelection extends PolymerElement { |
| 11 @observable bool multi = false; |
| 12 @observable final selection = new ObservableList(); |
| 13 |
| 14 factory PolymerSelection() => new Element.tag('polymer-selection'); |
| 15 PolymerSelection.created() : super.created(); |
| 16 |
| 17 void ready() { |
| 18 clear(); |
| 19 } |
| 20 |
| 21 void clear() { |
| 22 selection.clear(); |
| 23 } |
| 24 |
| 25 getSelection() { |
| 26 if (multi) return selection; |
| 27 if (selection.isNotEmpty) return selection[0]; |
| 28 return null; |
| 29 } |
| 30 |
| 31 bool isSelected(item) => selection.contains(item); |
| 32 |
| 33 void setItemSelected(item, bool isSelected) { |
| 34 if (item != null) { |
| 35 if (isSelected) { |
| 36 selection.add(item); |
| 37 } else { |
| 38 selection.remove(item); |
| 39 } |
| 40 // TODO(sjmiles): consider replacing with summary |
| 41 // notifications (asynchronous job) |
| 42 asyncFire("polymer-select", detail: |
| 43 new PolymerSelectEventDetail(isSelected, item)); |
| 44 } |
| 45 } |
| 46 |
| 47 select(item) { |
| 48 if (multi) { |
| 49 toggle(item); |
| 50 } else if (getSelection() != item) { |
| 51 setItemSelected(getSelection(), false); |
| 52 setItemSelected(item, true); |
| 53 } |
| 54 } |
| 55 |
| 56 toggle(item) { |
| 57 setItemSelected(item, !isSelected(item)); |
| 58 } |
| 59 } |
| 60 |
| 61 class PolymerSelectEventDetail { |
| 62 final bool isSelected; |
| 63 final item; |
| 64 |
| 65 PolymerSelectEventDetail(this.isSelected, this.item); |
| 66 } |
| OLD | NEW |