| OLD | NEW |
| 1 /** | 1 /** |
| 2 * `iron-range-behavior` provides the behavior for something with a minimum to m
aximum range. | 2 * `iron-range-behavior` provides the behavior for something with a minimum to m
aximum range. |
| 3 * | 3 * |
| 4 * @demo demo/index.html | 4 * @demo demo/index.html |
| 5 * @polymerBehavior | 5 * @polymerBehavior |
| 6 */ | 6 */ |
| 7 Polymer.IronRangeBehavior = { | 7 Polymer.IronRangeBehavior = { |
| 8 | 8 |
| 9 properties: { | 9 properties: { |
| 10 | 10 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 | 62 |
| 63 _calcRatio: function(value) { | 63 _calcRatio: function(value) { |
| 64 return (this._clampValue(value) - this.min) / (this.max - this.min); | 64 return (this._clampValue(value) - this.min) / (this.max - this.min); |
| 65 }, | 65 }, |
| 66 | 66 |
| 67 _clampValue: function(value) { | 67 _clampValue: function(value) { |
| 68 return Math.min(this.max, Math.max(this.min, this._calcStep(value))); | 68 return Math.min(this.max, Math.max(this.min, this._calcStep(value))); |
| 69 }, | 69 }, |
| 70 | 70 |
| 71 _calcStep: function(value) { | 71 _calcStep: function(value) { |
| 72 /** | |
| 73 * if we calculate the step using | |
| 74 * `Math.round(value / step) * step` we may hit a precision point issue | |
| 75 * eg. 0.1 * 0.2 = 0.020000000000000004 | |
| 76 * http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html | |
| 77 * | |
| 78 * as a work around we can divide by the reciprocal of `step` | |
| 79 */ | |
| 80 // polymer/issues/2493 | 72 // polymer/issues/2493 |
| 81 value = parseFloat(value); | 73 value = parseFloat(value); |
| 82 return this.step ? (Math.round((value + this.min) / this.step) - | 74 |
| 83 (this.min / this.step)) / (1 / this.step) : value; | 75 if (!this.step) { |
| 76 return value; |
| 77 } |
| 78 |
| 79 var numSteps = Math.round((value - this.min) / this.step); |
| 80 if (this.step < 1) { |
| 81 /** |
| 82 * For small values of this.step, if we calculate the step using |
| 83 * `Math.round(value / step) * step` we may hit a precision point issue |
| 84 * eg. 0.1 * 0.2 = 0.020000000000000004 |
| 85 * http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html |
| 86 * |
| 87 * as a work around we can divide by the reciprocal of `step` |
| 88 */ |
| 89 return numSteps / (1 / this.step) + this.min; |
| 90 } else { |
| 91 return numSteps * this.step + this.min; |
| 92 } |
| 84 }, | 93 }, |
| 85 | 94 |
| 86 _validateValue: function() { | 95 _validateValue: function() { |
| 87 var v = this._clampValue(this.value); | 96 var v = this._clampValue(this.value); |
| 88 this.value = this.oldValue = isNaN(v) ? this.oldValue : v; | 97 this.value = this.oldValue = isNaN(v) ? this.oldValue : v; |
| 89 return this.value !== v; | 98 return this.value !== v; |
| 90 }, | 99 }, |
| 91 | 100 |
| 92 _update: function() { | 101 _update: function() { |
| 93 this._validateValue(); | 102 this._validateValue(); |
| 94 this._setRatio(this._calcRatio(this.value) * 100); | 103 this._setRatio(this._calcRatio(this.value) * 100); |
| 95 } | 104 } |
| 96 | 105 |
| 97 }; | 106 }; |
| OLD | NEW |