| OLD | NEW |
| 1 | 1 |
| 2 /** | 2 /** |
| 3 * Fired when the slider's value changes. | 3 * Fired when the slider's value changes. |
| 4 * | 4 * |
| 5 * @event value-change | 5 * @event value-change |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 /** | 8 /** |
| 9 * Fired when the slider's immediateValue changes. | 9 * Fired when the slider's immediateValue changes. |
| 10 * | 10 * |
| 11 * @event immediate-value-change | 11 * @event immediate-value-change |
| 12 */ | 12 */ |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * Fired when the slider's value changes due to user interaction. | 15 * Fired when the slider's value changes due to user interaction. |
| 16 * | 16 * |
| 17 * Changes to the slider's value due to changes in an underlying | 17 * Changes to the slider's value due to changes in an underlying |
| 18 * bound variable will not trigger this event. | 18 * bound variable will not trigger this event. |
| 19 * | 19 * |
| 20 * @event change | 20 * @event change |
| 21 */ | 21 */ |
| 22 | 22 |
| 23 Polymer({ | 23 Polymer({ |
| 24 is: 'paper-slider', | 24 is: 'paper-slider', |
| 25 | 25 |
| 26 behaviors: [ | 26 behaviors: [ |
| 27 Polymer.IronRangeBehavior, | 27 Polymer.IronRangeBehavior, |
| 28 Polymer.IronControlState | 28 Polymer.IronA11yKeysBehavior, |
| 29 Polymer.IronFormElementBehavior, |
| 30 Polymer.PaperInkyFocusBehavior |
| 29 ], | 31 ], |
| 30 | 32 |
| 31 properties: { | 33 properties: { |
| 32 | |
| 33 /** | 34 /** |
| 34 * If true, the slider thumb snaps to tick marks evenly spaced based | 35 * If true, the slider thumb snaps to tick marks evenly spaced based |
| 35 * on the `step` property value. | 36 * on the `step` property value. |
| 36 */ | 37 */ |
| 37 snaps: { | 38 snaps: { |
| 38 type: Boolean, | 39 type: Boolean, |
| 39 value: false, | 40 value: false, |
| 40 notify: true | 41 notify: true |
| 41 }, | 42 }, |
| 42 | 43 |
| 43 /** | 44 /** |
| 44 * If true, a pin with numeric value label is shown when the slider thumb | 45 * If true, a pin with numeric value label is shown when the slider thumb |
| 45 * is pressed. Use for settings for which users need to know the exact | 46 * is pressed. Use for settings for which users need to know the exact |
| 46 * value of the setting. | 47 * value of the setting. |
| 47 */ | 48 */ |
| 48 pin: { | 49 pin: { |
| 49 type: Boolean, | 50 type: Boolean, |
| 50 value: false, | 51 value: false, |
| 51 notify: true | 52 notify: true |
| 52 }, | 53 }, |
| 53 | 54 |
| 54 /** | 55 /** |
| 55 * The number that represents the current secondary progress. | 56 * The number that represents the current secondary progress. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 readOnly: true | 108 readOnly: true |
| 108 }, | 109 }, |
| 109 | 110 |
| 110 transiting: { | 111 transiting: { |
| 111 type: Boolean, | 112 type: Boolean, |
| 112 value: false, | 113 value: false, |
| 113 readOnly: true | 114 readOnly: true |
| 114 }, | 115 }, |
| 115 | 116 |
| 116 markers: { | 117 markers: { |
| 118 type: Array, |
| 117 readOnly: true, | 119 readOnly: true, |
| 118 value: [] | 120 value: [] |
| 119 }, | 121 }, |
| 120 }, | 122 }, |
| 121 | 123 |
| 122 observers: [ | 124 observers: [ |
| 123 '_updateKnob(value, min, max, snaps, step)', | 125 '_updateKnob(value, min, max, snaps, step)', |
| 124 '_minChanged(min)', | 126 '_minChanged(min)', |
| 125 '_maxChanged(max)', | 127 '_maxChanged(max)', |
| 126 '_valueChanged(value)', | 128 '_valueChanged(value)', |
| 127 '_immediateValueChanged(immediateValue)' | 129 '_immediateValueChanged(immediateValue)' |
| 128 ], | 130 ], |
| 129 | 131 |
| 132 hostAttributes: { |
| 133 role: 'slider', |
| 134 tabindex: 0 |
| 135 }, |
| 136 |
| 137 keyBindings: { |
| 138 'left down pagedown home': '_decrementKey', |
| 139 'right up pageup end': '_incrementKey' |
| 140 }, |
| 141 |
| 130 ready: function() { | 142 ready: function() { |
| 131 // issue polymer/polymer#1305 | 143 // issue polymer/polymer#1305 |
| 144 |
| 132 this.async(function() { | 145 this.async(function() { |
| 133 this._updateKnob(this.value); | 146 this._updateKnob(this.value); |
| 134 this._updateInputValue(); | 147 this._updateInputValue(); |
| 135 }, 1); | 148 }, 1); |
| 136 }, | 149 }, |
| 137 | 150 |
| 138 /** | 151 /** |
| 139 * Increases value by `step` but not above `max`. | 152 * Increases value by `step` but not above `max`. |
| 140 * @method increment | 153 * @method increment |
| 141 */ | 154 */ |
| (...skipping 20 matching lines...) Expand all Loading... |
| 162 _maxChanged: function() { | 175 _maxChanged: function() { |
| 163 this.setAttribute('aria-valuemax', this.max); | 176 this.setAttribute('aria-valuemax', this.max); |
| 164 }, | 177 }, |
| 165 | 178 |
| 166 _valueChanged: function() { | 179 _valueChanged: function() { |
| 167 this.setAttribute('aria-valuenow', this.value); | 180 this.setAttribute('aria-valuenow', this.value); |
| 168 this.fire('value-change'); | 181 this.fire('value-change'); |
| 169 }, | 182 }, |
| 170 | 183 |
| 171 _immediateValueChanged: function() { | 184 _immediateValueChanged: function() { |
| 172 if (!this.dragging) { | 185 if (this.dragging) { |
| 186 this.fire('immediate-value-change'); |
| 187 } else { |
| 173 this.value = this.immediateValue; | 188 this.value = this.immediateValue; |
| 174 } | 189 } |
| 175 this._updateInputValue(); | 190 this._updateInputValue(); |
| 176 this.fire('immediate-value-change'); | |
| 177 }, | 191 }, |
| 178 | 192 |
| 179 _secondaryProgressChanged: function() { | 193 _secondaryProgressChanged: function() { |
| 180 this.secondaryProgress = this._clampValue(this.secondaryProgress); | 194 this.secondaryProgress = this._clampValue(this.secondaryProgress); |
| 181 }, | 195 }, |
| 182 | 196 |
| 183 _updateInputValue: function() { | 197 _updateInputValue: function() { |
| 184 if (this.editable) { | 198 if (this.editable) { |
| 185 this.$$('#input').value = this.immediateValue; | 199 this.$$('#input').value = this.immediateValue.toString(); |
| 186 } | 200 } |
| 187 }, | 201 }, |
| 188 | 202 |
| 189 _expandKnob: function() { | 203 _expandKnob: function() { |
| 204 this.$.ink.holdDown = false; |
| 190 this._setExpand(true); | 205 this._setExpand(true); |
| 191 }, | 206 }, |
| 192 | 207 |
| 193 _resetKnob: function() { | 208 _resetKnob: function() { |
| 194 this._expandJob && this._expandJob.stop(); | 209 this.cancelDebouncer('expandKnob'); |
| 195 this._setExpand(false); | 210 this._setExpand(false); |
| 211 this.$.ink.hidden = true; |
| 196 }, | 212 }, |
| 197 | 213 |
| 198 _positionKnob: function(ratio) { | 214 _positionKnob: function(ratio) { |
| 199 this._setImmediateValue(this._calcStep(this._calcKnobPosition(ratio)) || 0
); | 215 this._setImmediateValue(this._calcStep(this._calcKnobPosition(ratio))); |
| 200 this._setRatio(this.snaps ? this._calcRatio(this.immediateValue) : ratio); | 216 this._setRatio(this._calcRatio(this.immediateValue)); |
| 201 this.$.sliderKnob.style.left = this.ratio * 100 + '%'; | 217 |
| 218 this.$.sliderKnob.style.left = (this.ratio * 100) + '%'; |
| 202 }, | 219 }, |
| 203 | 220 |
| 204 _inputChange: function() { | 221 _inputChange: function() { |
| 205 this.value = this.$$('#input').value; | 222 this.value = this.$$('#input').value; |
| 206 this.fire('change'); | 223 this.fire('change'); |
| 207 }, | 224 }, |
| 208 | 225 |
| 209 _calcKnobPosition: function(ratio) { | 226 _calcKnobPosition: function(ratio) { |
| 210 return (this.max - this.min) * ratio + this.min; | 227 return (this.max - this.min) * ratio + this.min; |
| 211 }, | 228 }, |
| 212 | 229 |
| 213 _onTrack: function(e) { | 230 _onTrack: function(event) { |
| 214 switch (event.detail.state) { | 231 switch (event.detail.state) { |
| 215 case 'end': | 232 case 'start': |
| 216 this._trackEnd(event); | 233 this._trackStart(event); |
| 217 break; | 234 break; |
| 218 case 'track': | 235 case 'track': |
| 219 this._trackX(event); | 236 this._trackX(event); |
| 220 break; | 237 break; |
| 221 case 'start': | 238 case 'end': |
| 222 this._trackStart(event); | 239 this._trackEnd(); |
| 223 break; | 240 break; |
| 224 } | 241 } |
| 225 }, | 242 }, |
| 226 | 243 |
| 227 _trackStart: function(e) { | 244 _trackStart: function(event) { |
| 228 this._w = this.$.sliderBar.offsetWidth; | 245 this._w = this.$.sliderBar.offsetWidth; |
| 229 this._x = this.ratio * this._w; | 246 this._x = this.ratio * this._w; |
| 230 this._startx = this._x || 0; | 247 this._startx = this._x || 0; |
| 231 this._minx = - this._startx; | 248 this._minx = - this._startx; |
| 232 this._maxx = this._w - this._startx; | 249 this._maxx = this._w - this._startx; |
| 233 this.$.sliderKnob.classList.add('dragging'); | 250 this.$.sliderKnob.classList.add('dragging'); |
| 251 |
| 234 this._setDragging(true); | 252 this._setDragging(true); |
| 235 e.preventDefault(); | |
| 236 }, | 253 }, |
| 237 | 254 |
| 238 _trackX: function(e) { | 255 _trackX: function(e) { |
| 239 if (!this.dragging) { | 256 if (!this.dragging) { |
| 240 this._trackStart(e); | 257 this._trackStart(e); |
| 241 } | 258 } |
| 242 var x = Math.min(this._maxx, Math.max(this._minx, e.detail.dx)); | 259 |
| 243 this._x = this._startx + x; | 260 var dx = Math.min(this._maxx, Math.max(this._minx, e.detail.dx)); |
| 244 this._setImmediateValue(this._calcStep( | 261 this._x = this._startx + dx; |
| 245 this._calcKnobPosition(this._x / this._w)) || 0); | 262 |
| 246 var s = this.$.sliderKnob.style; | 263 var immediateValue = this._calcStep(this._calcKnobPosition(this._x / this.
_w)); |
| 247 s.transform = s.webkitTransform = 'translate3d(' + (this.snaps ? | 264 this._setImmediateValue(immediateValue); |
| 248 (this._calcRatio(this.immediateValue) * this._w) - this._startx : x) +
'px, 0, 0)'; | 265 |
| 266 // update knob's position |
| 267 var translateX = ((this._calcRatio(immediateValue) * this._w) - this._star
tx); |
| 268 this.translate3d(translateX + 'px', 0, 0, this.$.sliderKnob); |
| 249 }, | 269 }, |
| 250 | 270 |
| 251 _trackEnd: function() { | 271 _trackEnd: function() { |
| 252 var s = this.$.sliderKnob.style; | 272 var s = this.$.sliderKnob.style; |
| 253 s.transform = s.webkitTransform = ''; | 273 |
| 254 this.$.sliderKnob.classList.remove('dragging'); | 274 this.$.sliderKnob.classList.remove('dragging'); |
| 255 this._setDragging(false); | 275 this._setDragging(false); |
| 256 this._resetKnob(); | 276 this._resetKnob(); |
| 257 this.value = this.immediateValue; | 277 this.value = this.immediateValue; |
| 278 |
| 279 s.transform = s.webkitTransform = ''; |
| 280 |
| 258 this.fire('change'); | 281 this.fire('change'); |
| 259 }, | 282 }, |
| 260 | 283 |
| 261 _knobdown: function(e) { | 284 _knobdown: function(event) { |
| 262 e.preventDefault(); | |
| 263 this._expandKnob(); | 285 this._expandKnob(); |
| 286 |
| 287 // cancel selection |
| 288 event.detail.sourceEvent.preventDefault(); |
| 289 |
| 290 // set the focus manually because we will called prevent default |
| 291 this.focus(); |
| 264 }, | 292 }, |
| 265 | 293 |
| 266 _bardown: function(e) { | 294 _bardown: function(event) { |
| 267 e.preventDefault(); | 295 this.$.ink.hidden = true; |
| 268 this._setTransiting(true); | 296 |
| 297 event.preventDefault(); |
| 298 |
| 269 this._w = this.$.sliderBar.offsetWidth; | 299 this._w = this.$.sliderBar.offsetWidth; |
| 270 var rect = this.$.sliderBar.getBoundingClientRect(); | 300 var rect = this.$.sliderBar.getBoundingClientRect(); |
| 271 var ratio = (e.detail.x - rect.left) / this._w; | 301 var ratio = (event.detail.x - rect.left) / this._w; |
| 302 var prevRatio = this.ratio; |
| 303 |
| 304 this._setTransiting(true); |
| 305 |
| 272 this._positionKnob(ratio); | 306 this._positionKnob(ratio); |
| 273 this._expandJob = this.debounce(this._expandJob, this._expandKnob, 60); | 307 |
| 308 this.debounce('expandKnob', this._expandKnob, 60); |
| 309 |
| 310 // if the ratio doesn't change, sliderKnob's animation won't start |
| 311 // and `_knobTransitionEnd` won't be called |
| 312 // Therefore, we need to manually update the `transiting` state |
| 313 |
| 314 if (prevRatio === this.ratio) { |
| 315 this._setTransiting(false); |
| 316 } |
| 274 | 317 |
| 275 this.async(function() { | 318 this.async(function() { |
| 276 this.fire('change'); | 319 this.fire('change'); |
| 277 }); | 320 }); |
| 321 |
| 322 // cancel selection |
| 323 event.detail.sourceEvent.preventDefault(); |
| 278 }, | 324 }, |
| 279 | 325 |
| 280 _knobTransitionEnd: function(e) { | 326 _knobTransitionEnd: function(event) { |
| 281 if (e.target === this.$.sliderKnob) { | 327 if (event.target === this.$.sliderKnob) { |
| 282 this._setTransiting(false); | 328 this._setTransiting(false); |
| 283 } | 329 } |
| 284 }, | 330 }, |
| 285 | 331 |
| 286 _maxMarkersChanged: function(maxMarkers) { | 332 _maxMarkersChanged: function(maxMarkers) { |
| 287 var l = (this.max - this.min) / this.step; | 333 var l = (this.max - this.min) / this.step; |
| 288 if (!this.snaps && l > maxMarkers) { | 334 if (!this.snaps && l > maxMarkers) { |
| 289 this._setMarkers([]); | 335 this._setMarkers([]); |
| 290 } else { | 336 } else { |
| 291 this._setMarkers(new Array(l)); | 337 this._setMarkers(new Array(l)); |
| 292 } | 338 } |
| 293 }, | 339 }, |
| 294 | 340 |
| 295 _getClassNames: function() { | 341 _getClassNames: function() { |
| 296 var classes = {}; | 342 var classes = {}; |
| 297 | 343 |
| 298 classes['disabled'] = this.disabled; | 344 classes.disabled = this.disabled; |
| 299 classes['pin'] = this.pin; | 345 classes.pin = this.pin; |
| 300 classes['snaps'] = this.snaps; | 346 classes.snaps = this.snaps; |
| 301 classes['ring'] = this.immediateValue <= this.min; | 347 classes.ring = this.immediateValue <= this.min; |
| 302 classes['expand'] = this.expand; | 348 classes.expand = this.expand; |
| 303 classes['dragging'] = this.dragging; | 349 classes.dragging = this.dragging; |
| 304 classes['transiting'] = this.transiting; | 350 classes.transiting = this.transiting; |
| 305 classes['editable'] = this.editable; | 351 classes.editable = this.editable; |
| 306 | 352 |
| 307 return Object.keys(classes).filter( | 353 return Object.keys(classes).filter( |
| 308 function(className) { | 354 function(className) { |
| 309 return classes[className]; | 355 return classes[className]; |
| 310 }).join(' '); | 356 }).join(' '); |
| 311 }, | 357 }, |
| 312 | 358 |
| 313 _incrementKey: function(ev, keys) { | 359 _incrementKey: function(event) { |
| 314 if (keys.key === 'end') { | 360 if (event.detail.key === 'end') { |
| 315 this.value = this.max; | 361 this.value = this.max; |
| 316 } else { | 362 } else { |
| 317 this.increment(); | 363 this.increment(); |
| 318 } | 364 } |
| 319 this.fire('change'); | 365 this.fire('change'); |
| 320 }, | 366 }, |
| 321 | 367 |
| 322 _decrementKey: function(ev, keys) { | 368 _decrementKey: function(event) { |
| 323 if (keys.key === 'home') { | 369 if (event.detail.key === 'home') { |
| 324 this.value = this.min; | 370 this.value = this.min; |
| 325 } else { | 371 } else { |
| 326 this.decrement(); | 372 this.decrement(); |
| 327 } | 373 } |
| 328 this.fire('change'); | 374 this.fire('change'); |
| 329 } | 375 } |
| 330 }) | 376 }) |
| OLD | NEW |