OLD | NEW |
| (Empty) |
1 <!-- | |
2 // Copyright 2015 The Chromium Authors. All rights reserved. | |
3 // Use of this source code is governed by a BSD-style license that can be | |
4 // found in the LICENSE file. | |
5 --> | |
6 <import src="sky-element.sky" /> | |
7 <import src="sky-icon.sky" /> | |
8 | |
9 <sky-element attributes="selected:boolean, group:string"> | |
10 <template> | |
11 <style> | |
12 :host { | |
13 display: inline-block; | |
14 -webkit-user-select: none; | |
15 margin: 8px 16px; | |
16 } | |
17 </style> | |
18 <sky-icon size="18" /> | |
19 </template> | |
20 <script> | |
21 import "dart:sky"; | |
22 | |
23 final Map<Node, _RadioGroupController> _controllerMap = new Map(); | |
24 | |
25 class _RadioGroupController { | |
26 static _RadioGroupController forRadio(radio) { | |
27 Node owner = radio.owner; | |
28 return _controllerMap.putIfAbsent(owner, () => | |
29 new _RadioGroupController(owner)); | |
30 } | |
31 | |
32 final Node _scope; | |
33 final Set<SkyRadio> _radios = new Set<SkyRadio>(); | |
34 | |
35 _RadioGroupController(this._scope); | |
36 | |
37 void addRadio(SkyRadio radio) { | |
38 _radios.add(radio); | |
39 // If this new radio is default-selected, take selection from the group. | |
40 if (radio.selected) | |
41 takeSelectionFromGroup(radio); | |
42 } | |
43 | |
44 void removeRadio(SkyRadio radio) { | |
45 _radios.remove(radio); | |
46 if (_radios.isEmpty) | |
47 _controllerMap.remove(_scope); | |
48 } | |
49 | |
50 void takeSelectionFromGroup(SkyRadio selectedRadio) { | |
51 String group = selectedRadio.group; | |
52 if (group == null) | |
53 return; | |
54 _radios.forEach((SkyRadio radio) { | |
55 if (selectedRadio == radio) | |
56 return; | |
57 if (radio.group != group) | |
58 return; | |
59 radio.selected = false; | |
60 }); | |
61 } | |
62 } | |
63 | |
64 const String _kOnIcon = 'toggle/radio_button_on_black'; | |
65 const String _kOffIcon = 'toggle/radio_button_off_black'; | |
66 | |
67 @Tagname('sky-radio') | |
68 class SkyRadio extends SkyElement { | |
69 _RadioGroupController _controller; | |
70 SkyIcon _icon; | |
71 | |
72 SkyRadio() { | |
73 addEventListener('click', _handleClick); | |
74 } | |
75 | |
76 void shadowRootReady() { | |
77 _icon = shadowRoot.querySelector('sky-icon'); | |
78 _icon.type = selected ? _kOnIcon : _kOffIcon; | |
79 } | |
80 | |
81 void attached() { | |
82 super.attached(); | |
83 _controller = _RadioGroupController.forRadio(this); | |
84 _controller.addRadio(this); | |
85 } | |
86 | |
87 void detached() { | |
88 super.detached(); | |
89 _controller.removeRadio(this); | |
90 _controller = null; | |
91 } | |
92 | |
93 void selectedChanged(bool oldValue, bool newValue) { | |
94 if (_icon != null) | |
95 _icon.type = newValue ? _kOnIcon : _kOffIcon; | |
96 if (newValue && _controller != null) | |
97 _controller.takeSelectionFromGroup(this); | |
98 } | |
99 | |
100 void groupChanged(String oldValue, String newValue) { | |
101 if (selected && _controller != null) | |
102 _controller.takeSelectionFromGroup(this); | |
103 } | |
104 | |
105 _handleClick(_) { | |
106 this.selected = true; | |
107 } | |
108 } | |
109 | |
110 _init(script) => register(script, SkyRadio); | |
111 </script> | |
112 </sky-element> | |
OLD | NEW |