Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(121)

Side by Side Diff: samples/third_party/todomvc/web/lib-elements/polymer_selector.dart

Issue 60353013: Update TodoMVC based on https://github.com/polymer/todomvc (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: fix pubspecs Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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_selector;
5
6 import 'dart:html';
7 import 'package:polymer/polymer.dart';
8 import 'package:template_binding/template_binding.dart' show
9 nodeBind, isSemanticTemplate;
10 import 'polymer_selection.dart';
11
12 @CustomTag('polymer-selector')
13 class PolymerSelector extends PolymerElement {
14 /**
15 * Gets or sets the selected element. Default is to use the index
16 * of the currently selected element.
17 *
18 * If you want a specific attribute value of the selected element to be
19 * used instead of index, set "valueattr" to that attribute name.
20 *
21 * Example:
22 *
23 * <polymer-selector valueattr="label" selected="foo">
24 * <div label="foo"></div>
25 * <div label="bar"></div>
26 * <div label="zot"></div>
27 * </polymer-selector>
28 */
29 @observable String selected;
30
31 /** If true, multiple selections are allowed. */
32 @observable bool multi = false;
33
34 /** Specifies the attribute to be used for "selected" attribute. */
35 @observable String valueattr = 'name';
36
37 /** Specifies the CSS class to be used to add to the selected element. */
38 @observable String selectedClass = 'polymer-selected';
39
40 /**
41 * Specifies the property to be used to set on the selected element
42 * to indicate its active state.
43 */
44 @observable String selectedProperty = 'active';
45
46 /** Returns the model associated with the selected element. */
47 @observable var selectedModel;
48
49 @observable var selectedItem;
50
51 @observable bool notap = false;
52
53 factory PolymerSelector() => new Element.tag('polymer-selector');
54 PolymerSelector.created() : super.created();
55
56 List<Node> get items => ($['items'] as ContentElement).getDistributedNodes()
57 .where((n) => !isSemanticTemplate(n)).toList();
58
59 get selection => ($['selection'] as PolymerSelection).getSelection();
60
61 void selectedChanged() {
62 valueToSelection(selected);
63 }
64
65 void valueToSelection(value) {
66 var item = items.firstWhere((i) => valueForNode(i) == value,
67 orElse: () => null);
68
69 selectedItem = item;
70 ($['selection'] as PolymerSelection).select(item);
71 updateSelectedModel();
72 }
73
74 void updateSelectedModel() {
75 if (selectedItem != null) {
76 var t = nodeBind(selectedItem).templateInstance;
77 selectedModel = t != null ? t.model : null;
78 } else {
79 selectedModel = null;
80 }
81 }
82
83 String valueForNode(node) {
84 // TODO(jmesserly): faster way to do this
85 var value = new PathObserver(node, valueattr).value;
86 return value != null ? value : node.attributes[valueattr];
87 }
88
89 // events fired from <polymer-selection> object
90 void selectionSelect(e, PolymerSelectEventDetail detail) {
91 if (detail.item != null) {
92 if (selectedClass != null) {
93 detail.item.classes.toggle(selectedClass, detail.isSelected);
94 }
95 if (selectedProperty != null) {
96 new PathObserver(detail.item, selectedProperty).value =
97 detail.isSelected;
98 }
99 }
100 }
101
102 // event fired from host
103 void activateHandler(e) {
104 if (!notap) {
105 var i = findDistributedTarget(e.target, items);
106 if (i >= 0) {
107 var selected = valueForNode(items[i]);
108 if (selected == null) selected = i;
109 if (multi) {
110 valueToSelection(selected);
111 } else {
112 this.selected = selected;
113 }
114 asyncFire('polymer-activate', detail:
115 new PolymerActivateEventDetail(items[i]));
116 }
117 }
118 }
119
120 int findDistributedTarget(Node target, List<Node> nodes) {
121 // find first ancestor of target (including itself) that
122 // is in inNodes, if any
123 while (target != null && target != this) {
124 var i = nodes.indexOf(target);
125 if (i >= 0) return i;
126 target = target.parentNode;
127 }
128 return -1;
129 }
130 }
131
132 class PolymerActivateEventDetail {
133 final item;
134
135 PolymerActivateEventDetail(this.item);
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698