OLD | NEW |
| (Empty) |
1 <!-- | |
2 // Copyright 2014 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/framework/sky-button/sky-button.sky" as="SkyButton" /> | |
7 | |
8 <sky-element | |
9 name="sky-radio" | |
10 attributes="selected:boolean, group:string" | |
11 on-click="handleClick"> | |
12 <template> | |
13 <style> | |
14 :host { | |
15 display: inline-block; | |
16 -webkit-user-select: none; | |
17 width: 14px; | |
18 height: 14px; | |
19 border-radius: 7px; | |
20 border: 1px solid blue; | |
21 margin: 0 5px; | |
22 } | |
23 :host([highlight=true]) box { | |
24 background-color: orange; | |
25 } | |
26 dot { | |
27 -webkit-user-select: none; | |
28 width: 10px; | |
29 height: 10px; | |
30 border-radius: 5px; | |
31 background-color: black; | |
32 margin: 2px; | |
33 } | |
34 </style> | |
35 <template if="{{ selected }}"> | |
36 <dot /> | |
37 </template> | |
38 </template> | |
39 <script> | |
40 const kControllerMap = new WeakMap(); | |
41 | |
42 class RadioGroupController { | |
43 static forRadio(radio) { | |
44 var scope = radio.ownerScope; | |
45 var controller = kControllerMap.get(scope); | |
46 if (!controller) | |
47 kControllerMap.set(scope, new RadioGroupController()); | |
48 return kControllerMap.get(scope); | |
49 } | |
50 constructor() { | |
51 this.radios = new Set(); | |
52 } | |
53 addRadio(radio) { | |
54 this.radios.add(radio); | |
55 // If this new radio is default-selected, take selection from the group. | |
56 if (radio.selected) | |
57 this.takeSelectionFromGroup(radio); | |
58 } | |
59 removeRadio(radio) { | |
60 this.radios.remove(radio); | |
61 } | |
62 takeSelectionFromGroup(selectedRadio) { | |
63 // Emtpy/null/undefined group means an isolated radio. | |
64 if (!selectedRadio.group) | |
65 return; | |
66 this.radios.forEach(function(radio) { | |
67 if (selectedRadio === radio) | |
68 return; | |
69 if (radio.group != selectedRadio.group) | |
70 return; | |
71 radio.selected = false; | |
72 }); | |
73 } | |
74 }; | |
75 | |
76 module.exports = class extends SkyButton { | |
77 created() { | |
78 super.created(); | |
79 | |
80 this.controller = null; | |
81 } | |
82 attached() { | |
83 super.attached(); | |
84 this.controller = RadioGroupController.forRadio(this); | |
85 this.controller.addRadio(this); | |
86 } | |
87 detached() { | |
88 super.detached(); | |
89 this.controller.removeRadio(this); | |
90 this.controller = null; | |
91 } | |
92 selectedChanged(oldValue, newValue) { | |
93 if (newValue && this.controller) | |
94 this.controller.takeSelectionFromGroup(this); | |
95 } | |
96 groupChanged(oldValue, newValue) { | |
97 if (this.selected && this.controller) | |
98 this.controller.takeSelectionFromGroup(this); | |
99 } | |
100 handleClick() { | |
101 this.selected = true; | |
102 } | |
103 }.register(); | |
104 </script> | |
105 </sky-element> | |
OLD | NEW |