nouislider-rails 7.0.1 → 7.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,269 +0,0 @@
1
- /*jslint browser: true */
2
- /*jslint white: true */
3
-
4
- /* Every input option is tested and parsed. This'll prevent
5
- endless validation in internal methods. These tests are
6
- structured with an item for every option available. An
7
- option can be marked as required by setting the 'r' flag.
8
- The testing function is provided with three arguments:
9
- - The provided value for the option;
10
- - A reference to the options object;
11
- - The name for the option;
12
-
13
- The testing function returns false when an error is detected,
14
- or true when everything is OK. It can also modify the option
15
- object, to make sure all values can be correctly looped elsewhere. */
16
-
17
- (function( $ ){
18
-
19
- 'use strict';
20
-
21
- // Wraps a variable as an array, if it isn't one yet.
22
- function asArray ( a ) {
23
- return $.isArray(a) ? a : [a];
24
- }
25
-
26
- // Checks whether a value is numerical.
27
- function isNumeric ( a ) {
28
- return typeof a === 'number' && !isNaN( a ) && isFinite( a );
29
- }
30
-
31
- /** @const */
32
- var defaultFormatter = { 'to': function( value ){
33
- return value.toFixed(2);
34
- }, 'from': Number };
35
-
36
- function testStep ( parsed, entry ) {
37
-
38
- if ( !isNumeric( entry ) ) {
39
- throw new Error("noUiSlider: 'step' is not numeric.");
40
- }
41
-
42
- // The step option can still be used to set stepping
43
- // for linear sliders. Overwritten if set in 'range'.
44
- parsed.singleStep = entry;
45
- }
46
-
47
- function testRange ( parsed, entry ) {
48
-
49
- // Filter incorrect input.
50
- if ( typeof entry !== 'object' || $.isArray(entry) ) {
51
- throw new Error("noUiSlider: 'range' is not an object.");
52
- }
53
-
54
- // Catch missing start or end.
55
- if ( entry.min === undefined || entry.max === undefined ) {
56
- throw new Error("noUiSlider: Missing 'min' or 'max' in 'range'.");
57
- }
58
-
59
- parsed.spectrum = new $.noUiSlider.Spectrum(entry, parsed.snap, parsed.dir, parsed.singleStep);
60
- }
61
-
62
- function testStart ( parsed, entry ) {
63
-
64
- entry = asArray(entry);
65
-
66
- // Validate input. Values aren't tested, as the public .val method
67
- // will always provide a valid location.
68
- if ( !$.isArray( entry ) || !entry.length || entry.length > 2 ) {
69
- throw new Error("noUiSlider: 'start' option is incorrect.");
70
- }
71
-
72
- // Store the number of handles.
73
- parsed.handles = entry.length;
74
-
75
- // When the slider is initialized, the .val method will
76
- // be called with the start options.
77
- parsed.start = entry;
78
- }
79
-
80
- function testSnap ( parsed, entry ) {
81
-
82
- // Enforce 100% stepping within subranges.
83
- parsed.snap = entry;
84
-
85
- if ( typeof entry !== 'boolean' ){
86
- throw new Error("noUiSlider: 'snap' option must be a boolean.");
87
- }
88
- }
89
-
90
- function testAnimate ( parsed, entry ) {
91
-
92
- // Enforce 100% stepping within subranges.
93
- parsed.animate = entry;
94
-
95
- if ( typeof entry !== 'boolean' ){
96
- throw new Error("noUiSlider: 'animate' option must be a boolean.");
97
- }
98
- }
99
-
100
- function testConnect ( parsed, entry ) {
101
-
102
- if ( entry === 'lower' && parsed.handles === 1 ) {
103
- parsed.connect = 1;
104
- } else if ( entry === 'upper' && parsed.handles === 1 ) {
105
- parsed.connect = 2;
106
- } else if ( entry === true && parsed.handles === 2 ) {
107
- parsed.connect = 3;
108
- } else if ( entry === false ) {
109
- parsed.connect = 0;
110
- } else {
111
- throw new Error("noUiSlider: 'connect' option doesn't match handle count.");
112
- }
113
- }
114
-
115
- function testOrientation ( parsed, entry ) {
116
-
117
- // Set orientation to an a numerical value for easy
118
- // array selection.
119
- switch ( entry ){
120
- case 'horizontal':
121
- parsed.ort = 0;
122
- break;
123
- case 'vertical':
124
- parsed.ort = 1;
125
- break;
126
- default:
127
- throw new Error("noUiSlider: 'orientation' option is invalid.");
128
- }
129
- }
130
-
131
- function testMargin ( parsed, entry ) {
132
-
133
- if ( !isNumeric(entry) ){
134
- throw new Error("noUiSlider: 'margin' option must be numeric.");
135
- }
136
-
137
- parsed.margin = parsed.spectrum.getMargin(entry);
138
-
139
- if ( !parsed.margin ) {
140
- throw new Error("noUiSlider: 'margin' option is only supported on linear sliders.");
141
- }
142
- }
143
-
144
- function testLimit ( parsed, entry ) {
145
-
146
- if ( !isNumeric(entry) ){
147
- throw new Error("noUiSlider: 'limit' option must be numeric.");
148
- }
149
-
150
- parsed.limit = parsed.spectrum.getMargin(entry);
151
-
152
- if ( !parsed.limit ) {
153
- throw new Error("noUiSlider: 'limit' option is only supported on linear sliders.");
154
- }
155
- }
156
-
157
- function testDirection ( parsed, entry ) {
158
-
159
- // Set direction as a numerical value for easy parsing.
160
- // Invert connection for RTL sliders, so that the proper
161
- // handles get the connect/background classes.
162
- switch ( entry ) {
163
- case 'ltr':
164
- parsed.dir = 0;
165
- break;
166
- case 'rtl':
167
- parsed.dir = 1;
168
- parsed.connect = [0,2,1,3][parsed.connect];
169
- break;
170
- default:
171
- throw new Error("noUiSlider: 'direction' option was not recognized.");
172
- }
173
- }
174
-
175
- function testBehaviour ( parsed, entry ) {
176
-
177
- // Make sure the input is a string.
178
- if ( typeof entry !== 'string' ) {
179
- throw new Error("noUiSlider: 'behaviour' must be a string containing options.");
180
- }
181
-
182
- // Check if the string contains any keywords.
183
- // None are required.
184
- var tap = entry.indexOf('tap') >= 0,
185
- drag = entry.indexOf('drag') >= 0,
186
- fixed = entry.indexOf('fixed') >= 0,
187
- snap = entry.indexOf('snap') >= 0;
188
-
189
- parsed.events = {
190
- tap: tap || snap,
191
- drag: drag,
192
- fixed: fixed,
193
- snap: snap
194
- };
195
- }
196
-
197
- function testFormat ( parsed, entry ) {
198
-
199
- parsed.format = entry;
200
-
201
- // Any object with a to and from method is supported.
202
- if ( typeof entry.to === 'function' && typeof entry.from === 'function' ) {
203
- return true;
204
- }
205
-
206
- throw new Error( "noUiSlider: 'format' requires 'to' and 'from' methods.");
207
- }
208
-
209
- function testOptions ( options ) {
210
-
211
- var parsed = {
212
- margin: 0,
213
- limit: 0,
214
- animate: true,
215
- format: defaultFormatter
216
- }, tests;
217
-
218
- // Tests are executed in the order they are presented here.
219
- tests = {
220
- 'step': { r: false, t: testStep },
221
- 'start': { r: true, t: testStart },
222
- 'connect': { r: true, t: testConnect },
223
- 'direction': { r: true, t: testDirection },
224
- 'snap': { r: false, t: testSnap },
225
- 'animate': { r: false, t: testAnimate },
226
- 'range': { r: true, t: testRange },
227
- 'orientation': { r: false, t: testOrientation },
228
- 'margin': { r: false, t: testMargin },
229
- 'limit': { r: false, t: testLimit },
230
- 'behaviour': { r: true, t: testBehaviour },
231
- 'format': { r: false, t: testFormat }
232
- };
233
-
234
- // Set defaults where applicable.
235
- options = $.extend({
236
- 'connect': false,
237
- 'direction': 'ltr',
238
- 'behaviour': 'tap',
239
- 'orientation': 'horizontal'
240
- }, options);
241
-
242
- // Run all options through a testing mechanism to ensure correct
243
- // input. It should be noted that options might get modified to
244
- // be handled properly. E.g. wrapping integers in arrays.
245
- $.each( tests, function( name, test ){
246
-
247
- // If the option isn't set, but it is required, throw an error.
248
- if ( options[name] === undefined ) {
249
-
250
- if ( test.r ) {
251
- throw new Error("noUiSlider: '" + name + "' is required.");
252
- }
253
-
254
- return true;
255
- }
256
-
257
- test.t( parsed, options[name] );
258
- });
259
-
260
- // Pre-define the styles.
261
- parsed.style = parsed.ort ? 'top' : 'left';
262
-
263
- return parsed;
264
- }
265
-
266
- // Test all developer settings and parse to assumption-safe values.
267
- $.noUiSlider.testOptions = testOptions;
268
-
269
- }( window.jQuery || window.Zepto ));
@@ -1,290 +0,0 @@
1
- /*jslint browser: true */
2
- /*jslint white: true */
3
-
4
- (function( $ ){
5
-
6
- 'use strict';
7
-
8
- // Helpers
9
-
10
- // Determine the size of a sub-range in relation to a full range.
11
- function subRangeRatio ( pa, pb ) {
12
- return (100 / (pb - pa));
13
- }
14
-
15
- // Round a value to the closest 'to'.
16
- function closest ( value, to ) {
17
- return Math.round(value / to) * to;
18
- }
19
-
20
- // Checks whether a value is numerical.
21
- function isNumeric ( a ) {
22
- return typeof a === 'number' && !isNaN( a ) && isFinite( a );
23
- }
24
-
25
- // Rounds a number to 7 supported decimals.
26
- function accurateNumber( number ) {
27
- var p = Math.pow(10, 7);
28
- return Number((Math.round(number*p)/p).toFixed(7));
29
- }
30
-
31
-
32
- // Value calculation
33
-
34
- // (percentage) How many percent is this value of this range?
35
- function fromPercentage ( range, value ) {
36
- return (value * 100) / ( range[1] - range[0] );
37
- }
38
-
39
- // (percentage) Where is this value on this range?
40
- function toPercentage ( range, value ) {
41
- return fromPercentage( range, range[0] < 0 ?
42
- value + Math.abs(range[0]) :
43
- value - range[0] );
44
- }
45
-
46
- // (value) How much is this percentage on this range?
47
- function isPercentage ( range, value ) {
48
- return ((value * ( range[1] - range[0] )) / 100) + range[0];
49
- }
50
-
51
-
52
- // Range conversion
53
-
54
- function getJ ( value, arr ) {
55
-
56
- var j = 1;
57
-
58
- while ( value >= arr[j] ){
59
- j += 1;
60
- }
61
-
62
- return j;
63
- }
64
-
65
- // (percentage) Input a value, find where, on a scale of 0-100, it applies.
66
- function toStepping ( xVal, xPct, value ) {
67
-
68
- if ( value >= xVal.slice(-1)[0] ){
69
- return 100;
70
- }
71
-
72
- var j = getJ( value, xVal ), va, vb, pa, pb;
73
-
74
- va = xVal[j-1];
75
- vb = xVal[j];
76
- pa = xPct[j-1];
77
- pb = xPct[j];
78
-
79
- return pa + (toPercentage([va, vb], value) / subRangeRatio (pa, pb));
80
- }
81
-
82
- // (value) Input a percentage, find where it is on the specified range.
83
- function fromStepping ( xVal, xPct, value ) {
84
-
85
- // There is no range group that fits 100
86
- if ( value >= 100 ){
87
- return xVal.slice(-1)[0];
88
- }
89
-
90
- var j = getJ( value, xPct ), va, vb, pa, pb;
91
-
92
- va = xVal[j-1];
93
- vb = xVal[j];
94
- pa = xPct[j-1];
95
- pb = xPct[j];
96
-
97
- return isPercentage([va, vb], (value - pa) * subRangeRatio (pa, pb));
98
- }
99
-
100
- // (percentage) Get the step that applies at a certain value.
101
- function getStep ( xPct, xSteps, snap, value ) {
102
-
103
- if ( value === 100 ) {
104
- return value;
105
- }
106
-
107
- var j = getJ( value, xPct ), a, b;
108
-
109
- // If 'snap' is set, steps are used as fixed points on the slider.
110
- if ( snap ) {
111
-
112
- a = xPct[j-1];
113
- b = xPct[j];
114
-
115
- // Find the closest position, a or b.
116
- if ((value - a) > ((b-a)/2)){
117
- return b;
118
- }
119
-
120
- return a;
121
- }
122
-
123
- if ( !xSteps[j-1] ){
124
- return value;
125
- }
126
-
127
- return xPct[j-1] + closest(
128
- value - xPct[j-1],
129
- xSteps[j-1]
130
- );
131
- }
132
-
133
-
134
- // Entry parsing
135
-
136
- function handleEntryPoint ( index, value, that ) {
137
-
138
- var percentage;
139
-
140
- // Wrap numerical input in an array.
141
- if ( typeof value === "number" ) {
142
- value = [value];
143
- }
144
-
145
- // Reject any invalid input, by testing whether value is an array.
146
- if ( Object.prototype.toString.call( value ) !== '[object Array]' ){
147
- throw new Error("noUiSlider: 'range' contains invalid value.");
148
- }
149
-
150
- // Covert min/max syntax to 0 and 100.
151
- if ( index === 'min' ) {
152
- percentage = 0;
153
- } else if ( index === 'max' ) {
154
- percentage = 100;
155
- } else {
156
- percentage = parseFloat( index );
157
- }
158
-
159
- // Check for correct input.
160
- if ( !isNumeric( percentage ) || !isNumeric( value[0] ) ) {
161
- throw new Error("noUiSlider: 'range' value isn't numeric.");
162
- }
163
-
164
- // Store values.
165
- that.xPct.push( percentage );
166
- that.xVal.push( value[0] );
167
-
168
- // NaN will evaluate to false too, but to keep
169
- // logging clear, set step explicitly. Make sure
170
- // not to override the 'step' setting with false.
171
- if ( !percentage ) {
172
- if ( !isNaN( value[1] ) ) {
173
- that.xSteps[0] = value[1];
174
- }
175
- } else {
176
- that.xSteps.push( isNaN(value[1]) ? false : value[1] );
177
- }
178
- }
179
-
180
- function handleStepPoint ( i, n, that ) {
181
-
182
- // Ignore 'false' stepping.
183
- if ( !n ) {
184
- return true;
185
- }
186
-
187
- // Factor to range ratio
188
- that.xSteps[i] = fromPercentage([
189
- that.xVal[i]
190
- ,that.xVal[i+1]
191
- ], n) / subRangeRatio (
192
- that.xPct[i],
193
- that.xPct[i+1] );
194
- }
195
-
196
-
197
- // Interface
198
-
199
- // The interface to Spectrum handles all direction-based
200
- // conversions, so the above values are unaware.
201
-
202
- function Spectrum ( entry, snap, direction, singleStep ) {
203
-
204
- this.xPct = [];
205
- this.xVal = [];
206
- this.xSteps = [ singleStep || false ];
207
- this.xNumSteps = [ false ];
208
-
209
- this.snap = snap;
210
- this.direction = direction;
211
-
212
- var that = this, index;
213
-
214
- // Loop all entries.
215
- for ( index in entry ) {
216
- if ( entry.hasOwnProperty(index) ) {
217
- handleEntryPoint(index, entry[index], that);
218
- }
219
- }
220
-
221
- // Store the actual step values.
222
- that.xNumSteps = that.xSteps.slice(0);
223
-
224
- for ( index in that.xNumSteps ) {
225
- if ( that.xNumSteps.hasOwnProperty(index) ) {
226
- handleStepPoint(Number(index), that.xNumSteps[index], that);
227
- }
228
- }
229
- }
230
-
231
- Spectrum.prototype.getMargin = function ( value ) {
232
- return this.xPct.length === 2 ? fromPercentage(this.xVal, value) : false;
233
- };
234
-
235
- Spectrum.prototype.toStepping = function ( value ) {
236
-
237
- value = toStepping( this.xVal, this.xPct, value );
238
-
239
- // Invert the value if this is a right-to-left slider.
240
- if ( this.direction ) {
241
- value = 100 - value;
242
- }
243
-
244
- return value;
245
- };
246
-
247
- Spectrum.prototype.fromStepping = function ( value ) {
248
-
249
- // Invert the value if this is a right-to-left slider.
250
- if ( this.direction ) {
251
- value = 100 - value;
252
- }
253
-
254
- return accurateNumber(fromStepping( this.xVal, this.xPct, value ));
255
- };
256
-
257
- Spectrum.prototype.getStep = function ( value ) {
258
-
259
- // Find the proper step for rtl sliders by search in inverse direction.
260
- // Fixes issue #262.
261
- if ( this.direction ) {
262
- value = 100 - value;
263
- }
264
-
265
- value = getStep(this.xPct, this.xSteps, this.snap, value );
266
-
267
- if ( this.direction ) {
268
- value = 100 - value;
269
- }
270
-
271
- return value;
272
- };
273
-
274
- Spectrum.prototype.getApplicableStep = function ( value ) {
275
-
276
- // If the value is 100%, return the negative step twice.
277
- var j = getJ(value, this.xPct), offset = value === 100 ? 2 : 1;
278
- return [this.xNumSteps[j-2], this.xVal[j-offset], this.xNumSteps[j-offset]];
279
- };
280
-
281
- // Outside testing
282
- Spectrum.prototype.convert = function ( value ) {
283
- return this.getStep(this.toStepping(value));
284
- };
285
-
286
- $.noUiSlider = {
287
- Spectrum: Spectrum
288
- };
289
-
290
- }( window.jQuery || window.Zepto ));