From d59b5138e1c218efe77ce0793cf3acc7969f80cc Mon Sep 17 00:00:00 2001 From: Leon Gersen Date: Sat, 20 Jan 2018 19:34:06 +0100 Subject: [PATCH] Improve examples --- .../examples-content/colorpicker.php | 27 +++---------------- documentation/examples-content/dates.php | 4 +-- .../examples-content/huge-numbers.php | 10 ++++--- documentation/examples-content/keyboard.php | 8 +----- documentation/examples-content/keypress.php | 8 +++--- documentation/examples-content/non-linear.php | 2 +- documentation/examples.php | 8 +++--- .../examples/colorpicker-setcolor.js | 13 --------- documentation/examples/colorpicker-slider.js | 24 +++++++++++------ documentation/examples/keyboard-slider.js | 4 +-- documentation/examples/keyboard.css | 3 --- documentation/examples/keyboard.js | 21 ++++++--------- documentation/examples/locked-crossupdate.js | 5 +++- documentation/examples/locked-link.js | 4 --- documentation/examples/locked-setup.js | 22 +++++++-------- 15 files changed, 63 insertions(+), 100 deletions(-) delete mode 100644 documentation/examples/colorpicker-setcolor.js delete mode 100644 documentation/examples/keyboard.css diff --git a/documentation/examples-content/colorpicker.php b/documentation/examples-content/colorpicker.php index f588d624..2219ef62 100644 --- a/documentation/examples-content/colorpicker.php +++ b/documentation/examples-content/colorpicker.php @@ -5,7 +5,9 @@
-

We'll initialize all sliders with the same options, and use the slide callback to keep to color in sync with the slider values. This callback fires when the slider is moved by sliding, or when it is clicked or tapped.

+

We'll initialize all sliders with the same options, and use the update callback to keep to color in sync with the slider values.

+ +

This callback fires any time a slider value updates.

@@ -13,34 +15,13 @@
-
-
The HTML
- -
- -
<div id="colorpicker">
-	<div class="sliders" id="red"></div>
-	<div class="sliders" id="green"></div>
-	<div class="sliders" id="blue"></div>
-
-	<div class="result"></div>
-</div>
- -
- -
The setColor function
- -
- -
- -
Initializing the slider
+
Colorpicker
diff --git a/documentation/examples-content/dates.php b/documentation/examples-content/dates.php index 99841797..fbd47878 100644 --- a/documentation/examples-content/dates.php +++ b/documentation/examples-content/dates.php @@ -5,12 +5,10 @@
-

As all dates in JavaScript can be represented as time, noUiSlider can handle them, too. This example will show you how to convert dates to numerical ranges, and then use the update event to display them in a pretty format.

+

This example shows how to convert dates to numerical ranges, and then use the update event to display them in a pretty format.

We'll be creating timestamps from strings. In order to do this easily, we'll define a new helper function. This function accepts a string, creates a new Date and then returns it as a timestamp.

-

In in overview below you'll find the code used to run this example. For readability, all helper functions have been moved into their own tab.

-
diff --git a/documentation/examples-content/huge-numbers.php b/documentation/examples-content/huge-numbers.php index 86bb4caf..8934eb25 100644 --- a/documentation/examples-content/huge-numbers.php +++ b/documentation/examples-content/huge-numbers.php @@ -5,11 +5,15 @@
-

If you are working with arbitrarily large numbers, you should not use these directly in noUiSlider, as you'll run into some JavaScript limitations. Instead, you should map your values to an array.

+

If you are working with arbitrarily large numbers, you should not use these directly in noUiSlider as you'll run into some JavaScript limitations. Instead, you should map your values to an array of string values.

-

Numbers is JavaScript are Double Precision Floats, which can store numbers up to 2^53 (9007199254740992) precisely. For reference, see this StackOverflow question, or issue #427 filed on GitHub.

+

Numbers is JavaScript are "double precision floats", which can store numbers up to 2^53 (9007199254740992) precisely.

+ +

For reference, see this StackOverflow question, or issue #427 filed on GitHub.

-

As an example, see the 'range' option for a RAM selector offering 14 steps from 512MB to 8GB. The 'step' are ommited for clarity. The values are provided as bytes. A better solution would be to abstract the byte values away from the slider, looking up the byte values in an array. This keeps the slider configuration simple and prevents issues with floating point precision.

+

As an example, see the 'range' option for a RAM selector offering 14 steps from 512MB to 8GB. The 'step' are omitted for clarity. The values are provided as bytes.

+ +

A better solution would be to abstract the byte values away from the slider, looking up the byte values in an array. This keeps the slider configuration simple and prevents issues with floating point precision.

(These values fit within the limit just fine, but demonstrate the point really well!)

diff --git a/documentation/examples-content/keyboard.php b/documentation/examples-content/keyboard.php index c67c9208..dd568dec 100644 --- a/documentation/examples-content/keyboard.php +++ b/documentation/examples-content/keyboard.php @@ -5,7 +5,7 @@
-

Much like the keypress example, handles can be made keyboard-focusable.

+

Handles can be focused, but noUiSlider does not offer keyboard support by default. It can be added by adding a keypress listener on a handle.

@@ -29,11 +29,5 @@
-
CSS
- -
- -
-
diff --git a/documentation/examples-content/keypress.php b/documentation/examples-content/keypress.php index 9755f797..eb995607 100644 --- a/documentation/examples-content/keypress.php +++ b/documentation/examples-content/keypress.php @@ -1,15 +1,15 @@ -

Changing the slider by keypress

+

Changing the slider by key press

-

To keep the library small, features like keyboard interaction haven't been included. However, adding features to input fields linked to a slider is easy. noUiSlider provides API's to help you. In this example, pressing the keyboard arrow keys will increase/decrease the slider by one step.

+

noUiSlider provides a steps API to determine the previous and next steps for a handle. In this example, pressing the keyboard arrow keys will increase/decrease the slider by one step.

-

This example uses the 'step' API to determine by how much the slider should be changed. You don't need this function if your slider is linear. In that case, increase/decrease the value with the ammount of your step.

+

Use of this API is not necessary for linear sliders, as the step is constant in that case.

-

We'll listen to keydown on the '#input-with-keypress' element, and pass the event to a function so we can read the code that identifies the key.

+

We'll listen to keydown on the input element, and pass the event to a function so we can read the code that identifies the key.

Note that the slider value will be a string, so we'll need to parse it to an integer.

diff --git a/documentation/examples-content/non-linear.php b/documentation/examples-content/non-linear.php index d629ec94..eb4fc79f 100644 --- a/documentation/examples-content/non-linear.php +++ b/documentation/examples-content/non-linear.php @@ -5,7 +5,7 @@
-

One of noUiSlider's core features is the ability to divide the range in a non-linear fashion. Stepping can be applied. The example on the right shows where the handles are on the slider range in values and percentages.

+

One of noUiSlider's core features is the ability to divide the range in a non-linear fashion. Stepping can be applied. This example shows where the handles are on the slider range in values and percentages.

diff --git a/documentation/examples.php b/documentation/examples.php index eb9b7f9d..0fae95c2 100644 --- a/documentation/examples.php +++ b/documentation/examples.php @@ -6,6 +6,8 @@
+ + @@ -32,7 +34,5 @@ - - diff --git a/documentation/examples/colorpicker-setcolor.js b/documentation/examples/colorpicker-setcolor.js deleted file mode 100644 index 98dac3d1..00000000 --- a/documentation/examples/colorpicker-setcolor.js +++ /dev/null @@ -1,13 +0,0 @@ -function setColor(){ - - // Get the slider values, - // stick them together. - var color = 'rgb(' + - sliders[0].noUiSlider.get() + ',' + - sliders[1].noUiSlider.get() + ',' + - sliders[2].noUiSlider.get() + ')'; - - // Fill the color box. - resultElement.style.background = color; - resultElement.style.color = color; -} diff --git a/documentation/examples/colorpicker-slider.js b/documentation/examples/colorpicker-slider.js index 259fde70..9a160036 100644 --- a/documentation/examples/colorpicker-slider.js +++ b/documentation/examples/colorpicker-slider.js @@ -1,9 +1,10 @@ -var resultElement = document.getElementById('result'), - sliders = document.getElementsByClassName('sliders'); +var resultElement = document.getElementById('result'); +var sliders = document.getElementsByClassName('sliders'); +var colors = [0,0,0]; -for ( var i = 0; i < sliders.length; i++ ) { +[].slice.call(sliders).forEach(function( slider, index ){ - noUiSlider.create(sliders[i], { + noUiSlider.create(slider, { start: 127, connect: [true, false], orientation: "vertical", @@ -15,8 +16,15 @@ for ( var i = 0; i < sliders.length; i++ ) { decimals: 0 }) }); + + // Bind the color changing function to the update event. + slider.noUiSlider.on('update', function ( ) { + + colors[index] = slider.noUiSlider.get(); + + var color = 'rgb(' + colors.join(',') + ')'; - // Bind the color changing function - // to the slide event. - sliders[i].noUiSlider.on('slide', setColor); -} + resultElement.style.background = color; + resultElement.style.color = color; + }); +}); diff --git a/documentation/examples/keyboard-slider.js b/documentation/examples/keyboard-slider.js index 1cdaf7a2..34fab45d 100644 --- a/documentation/examples/keyboard-slider.js +++ b/documentation/examples/keyboard-slider.js @@ -1,6 +1,6 @@ -var slider = document.getElementById('keyboard'); +var keyboardSlider = document.getElementById('keyboard'); -noUiSlider.create(slider, { +noUiSlider.create(keyboardSlider, { start: 10, step: 10, range: { diff --git a/documentation/examples/keyboard.css b/documentation/examples/keyboard.css deleted file mode 100644 index 66c7695a..00000000 --- a/documentation/examples/keyboard.css +++ /dev/null @@ -1,3 +0,0 @@ -.noUi-handle:focus { - box-shadow: 0 0 5px orange; -} diff --git a/documentation/examples/keyboard.js b/documentation/examples/keyboard.js index 8d870528..3ae41965 100644 --- a/documentation/examples/keyboard.js +++ b/documentation/examples/keyboard.js @@ -1,19 +1,14 @@ -var handle = slider.querySelector('.noUi-handle'); - -handle.setAttribute('tabindex', 0); - -handle.addEventListener('click', function(){ - this.focus(); -}); +var handle = keyboardSlider.querySelector('.noUi-handle'); handle.addEventListener('keydown', function( e ) { - var value = Number( slider.noUiSlider.get() ); + var value = Number( keyboardSlider.noUiSlider.get() ); + + if ( e.which === 37 ) { + keyboardSlider.noUiSlider.set( value - 10 ); + } - switch ( e.which ) { - case 37: slider.noUiSlider.set( value - 10 ); - break; - case 39: slider.noUiSlider.set( value + 10 ); - break; + if ( e.which === 39 ) { + keyboardSlider.noUiSlider.set( value + 10 ); } }); diff --git a/documentation/examples/locked-crossupdate.js b/documentation/examples/locked-crossupdate.js index d3f4987b..5fdba7e5 100644 --- a/documentation/examples/locked-crossupdate.js +++ b/documentation/examples/locked-crossupdate.js @@ -6,7 +6,10 @@ function crossUpdate ( value, slider ) { // Select whether to increase or decrease // the other slider value. - var a = slider1 === slider ? 0 : 1, b = a ? 0 : 1; + var a = slider1 === slider ? 0 : 1; + + // Invert a + var b = a ? 0 : 1; // Offset the slider value. value -= lockedValues[b] - lockedValues[a]; diff --git a/documentation/examples/locked-link.js b/documentation/examples/locked-link.js index 9e8760d9..d48e2ecf 100644 --- a/documentation/examples/locked-link.js +++ b/documentation/examples/locked-link.js @@ -8,10 +8,6 @@ function setLockedValues ( ) { slider1.noUiSlider.on('change', setLockedValues); slider2.noUiSlider.on('change', setLockedValues); -// The value will be send to the other slider, -// using a custom function as the serialization -// method. The function uses the global 'lockedState' -// variable to decide whether the other slider is updated. slider1.noUiSlider.on('slide', function( values, handle ){ crossUpdate(values[handle], slider2); }); diff --git a/documentation/examples/locked-setup.js b/documentation/examples/locked-setup.js index 0520fd04..0637db74 100644 --- a/documentation/examples/locked-setup.js +++ b/documentation/examples/locked-setup.js @@ -1,15 +1,15 @@ -// Store the locked state and slider values. -var lockedState = false, - lockedSlider = false, - lockedValues = [60, 80], - slider1 = document.getElementById('slider1'), - slider2 = document.getElementById('slider2'), - lockButton = document.getElementById('lockbutton'), - slider1Value = document.getElementById('slider1-span'), - slider2Value = document.getElementById('slider2-span'); +var lockedState = false; +var lockedSlider = false; +var lockedValues = [60, 80]; -// When the button is clicked, the locked -// state is inverted. +var slider1 = document.getElementById('slider1'); +var slider2 = document.getElementById('slider2'); + +var lockButton = document.getElementById('lockbutton'); +var slider1Value = document.getElementById('slider1-span'); +var slider2Value = document.getElementById('slider2-span'); + +// When the button is clicked, the locked state is inverted. lockButton.addEventListener('click', function(){ lockedState = !lockedState; this.textContent = lockedState ? 'unlock' : 'lock';