OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 <!-- |
| 7 /** |
| 8 * @module Polymer UI Elements |
| 9 */ |
| 10 /** |
| 11 * polymer-ui-toggle-button is a button that has one of two states (ON/OFF). |
| 12 * You can tap or drag the button to switch state. |
| 13 * |
| 14 * Example: |
| 15 * |
| 16 * <polymer-ui-toggle-button></polymer-ui-toggle-button> |
| 17 * |
| 18 * @class polymer-ui-toggle-button |
| 19 */ |
| 20 --> |
| 21 <link rel="import" href="../polymer/polymer.html"> |
| 22 |
| 23 <polymer-element name="polymer-ui-toggle-button" attributes="value noCaption onC
aption offCaption" |
| 24 on-tap="{{toggle}}" on-trackstart="{{trackStart}}" on-track="{{track}}" on-t
rackend="{{trackEnd}}" on-flick="{{flick}}"> |
| 25 <template> |
| 26 <link rel="stylesheet" href="polymer-ui-toggle-button.css" /> |
| 27 <link rel="stylesheet" href="../polymer-flex-layout/polymer-flex-layout.css"
/> |
| 28 <div id="toggle" class="toggle"> |
| 29 <span class="on-label flexbox align-center justify-center">{{noCaption ? '
' : onCaption}}</span> |
| 30 <span id="thumb" class="thumb"></span> |
| 31 <span class="off-label flexbox align-center justify-center">{{noCaption ?
'' : offCaption}}</span> |
| 32 </div> |
| 33 </template> |
| 34 <script> |
| 35 Polymer('polymer-ui-toggle-button', { |
| 36 /** |
| 37 * Gets or sets the state, true is ON and false is OFF. |
| 38 * |
| 39 * @attribute value |
| 40 * @type boolean |
| 41 * @default false |
| 42 */ |
| 43 value: false, |
| 44 /** |
| 45 * If true, don't display caption. |
| 46 * |
| 47 * @attribute noCaption |
| 48 * @type boolean |
| 49 * @default false |
| 50 */ |
| 51 noCaption: false, |
| 52 /** |
| 53 * Caption for on state. |
| 54 * |
| 55 * @attribute onCaption |
| 56 * @type string |
| 57 * @default 'ON' |
| 58 */ |
| 59 onCaption: 'ON', |
| 60 /** |
| 61 * Caption for off state. |
| 62 * |
| 63 * @attribute offCaption |
| 64 * @type string |
| 65 * @default 'OFF' |
| 66 */ |
| 67 offCaption: 'OFF', |
| 68 toggle: function() { |
| 69 this.value = !this.value; |
| 70 }, |
| 71 valueChanged: function() { |
| 72 this.classList.toggle('on', this.value); |
| 73 this.$.toggle.classList.toggle('on', this.value); |
| 74 }, |
| 75 trackStart: function(e) { |
| 76 e.preventTap(); |
| 77 this.w = this.$.toggle.offsetWidth - this.clientWidth; |
| 78 this.$.toggle.classList.add('dragging'); |
| 79 }, |
| 80 track: function(e) { |
| 81 this.x = Math.max(-this.w, Math.min(0, this.value ? e.dx : e.dx - this.w
)); |
| 82 this.$.toggle.style.left = this.x + 'px'; |
| 83 }, |
| 84 trackEnd: function() { |
| 85 this.$.toggle.style.left = null; |
| 86 this.$.toggle.classList.remove('dragging'); |
| 87 this.value = Math.abs(this.x) < this.w / 2; |
| 88 Platform.flush(); |
| 89 }, |
| 90 flick: function(e) { |
| 91 this.value = e.xVelocity > 0; |
| 92 Platform.flush(); |
| 93 } |
| 94 }); |
| 95 </script> |
| 96 </polymer-element> |
OLD | NEW |