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 name="sky-radio"> | |
9 <template> | |
10 <style> | |
11 :host { | |
12 display: inline-block; | |
13 -webkit-user-select: none; | |
14 width: 14px; | |
15 height: 14px; | |
16 border-radius: 7px; | |
17 border: 1px solid blue; | |
18 margin: 0 5px; | |
19 } | |
20 :host([highlight=true]) box { | |
21 background-color: orange; | |
22 } | |
23 dot { | |
24 -webkit-user-select: none; | |
25 width: 10px; | |
26 height: 10px; | |
27 border-radius: 5px; | |
28 background-color: black; | |
29 margin: 2px; | |
30 } | |
31 </style> | |
32 <template if="{{ selected }}"> | |
33 <dot /> | |
34 </template> | |
35 </template> | |
36 <script> | |
37 const kControllerMap = new WeakMap(); | |
38 | |
39 class RadioGroupController { | |
40 static forRadio(radio) { | |
41 var scope = radio; | |
esprehn
2014/12/18 00:54:28
this.ownerScope, I added it :)
| |
42 while (scope.parentNode) | |
43 scope = scope.parentNode; | |
44 | |
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 } | |
56 removeRadio(radio) { | |
57 this.radios.remove(radio); | |
58 } | |
59 takeSelectionFromGroup(selectedRadio) { | |
60 // Emtpy/null/undefined group means and isolated radio. | |
61 if (!selectedRadio.group) | |
62 return; | |
63 this.radios.forEach(function(radio) { | |
64 if (selectedRadio === radio) | |
65 return; | |
66 if (radio.group != selectedRadio.group) | |
67 return; | |
68 radio.setSelected(false); | |
69 }) | |
70 } | |
71 }; | |
72 | |
73 module.exports = class extends SkyButton { | |
74 created() { | |
75 super.created(); | |
76 this.setSelected(this.getAttribute('selected')); | |
esprehn
2014/12/18 00:54:28
this.cachedController = null;
You want to define
| |
77 this.group = this.getAttribute('group'); | |
78 | |
79 this.addEventListener("mouseup", function() { | |
80 this.setSelected(true); | |
81 }); | |
82 } | |
83 attached() { | |
84 super.attached(); | |
85 this.cachedController = RadioGroupController.forRadio(this); | |
86 this.cachedController.addRadio(this); | |
87 // If we're selected and just now attaching, take selection from the group. | |
88 if (this.selected) | |
89 this.cachedController.takeSelectionFromGroup(this); | |
esprehn
2014/12/18 00:54:28
It seems like addRadio should do this?
| |
90 } | |
91 detached() { | |
92 super.detached(); | |
93 this.cachedController.removeButton(this); | |
94 } | |
95 setSelected(selected) { | |
96 if (selected == this.selected) | |
97 return; | |
98 this.setAttribute('selected', selected); | |
99 this.selected = selected; | |
100 if (selected && this.cachedController) | |
101 this.cachedController.takeSelectionFromGroup(this); | |
102 } | |
103 }.register(); | |
104 </script> | |
105 </sky-element> | |
OLD | NEW |