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.ownerScope; |
| 42 var controller = kControllerMap.get(scope); |
| 43 if (!controller) |
| 44 kControllerMap.set(scope, new RadioGroupController()); |
| 45 return kControllerMap.get(scope); |
| 46 } |
| 47 constructor() { |
| 48 this.radios = new Set(); |
| 49 } |
| 50 addRadio(radio) { |
| 51 this.radios.add(radio); |
| 52 // If this new radio is default-selected, take selection from the group. |
| 53 if (radio.selected) |
| 54 this.takeSelectionFromGroup(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')); |
| 77 this.group = this.getAttribute('group'); |
| 78 |
| 79 this.addEventListener("mouseup", function() { |
| 80 this.setSelected(true); |
| 81 }); |
| 82 this.cachedController = null; |
| 83 } |
| 84 attached() { |
| 85 super.attached(); |
| 86 this.cachedController = RadioGroupController.forRadio(this); |
| 87 this.cachedController.addRadio(this); |
| 88 } |
| 89 detached() { |
| 90 super.detached(); |
| 91 this.cachedController.removeRadio(this); |
| 92 } |
| 93 setSelected(selected) { |
| 94 if (selected == this.selected) |
| 95 return; |
| 96 this.setAttribute('selected', selected); |
| 97 this.selected = selected; |
| 98 if (selected && this.cachedController) |
| 99 this.cachedController.takeSelectionFromGroup(this); |
| 100 } |
| 101 }.register(); |
| 102 </script> |
| 103 </sky-element> |
OLD | NEW |