├── .gitignore ├── minimal.html ├── d3RangeSlider.css ├── README.md ├── index.html ├── LICENSE └── d3RangeSlider.js /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | -------------------------------------------------------------------------------- /minimal.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 19 | 20 | 21 | 22 |
23 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /d3RangeSlider.css: -------------------------------------------------------------------------------- 1 | .slider-container { 2 | background-color: #f2f2f9; 3 | } 4 | 5 | .slider { 6 | position: absolute; 7 | border: 1px solid #AAB; 8 | background: #BCE; 9 | height: 100%; 10 | width: 58px; 11 | top: 0px; 12 | cursor: move; 13 | /*margin:-0.5px;*/ 14 | } 15 | 16 | .slider .handle { 17 | position: absolute; 18 | height: 9px; 19 | width: 9px; 20 | border: 1px solid #AAB; 21 | background: #9AC; 22 | 23 | /* Support for bootstrap */ 24 | box-sizing: border-box; 25 | -moz-box-sizing: border-box; 26 | -webkit-box-sizing: border-box; 27 | } 28 | 29 | .slider .EE { 30 | right: -4px; 31 | cursor: e-resize; 32 | } 33 | 34 | .slider .WW { 35 | cursor: w-resize; 36 | left: -4px; 37 | } 38 | 39 | .slider .EE, .slider .WW { 40 | top: 50%; 41 | margin-top: -4px; 42 | } 43 | 44 | .play-container .button{ 45 | fill-opacity: 0.0; 46 | stroke: #AAB; 47 | stroke-width: 1.6; 48 | } 49 | 50 | .play-container .play, .play-container .stop { 51 | fill: #BCE; 52 | stroke: #AAB; 53 | stroke-width: 1; 54 | } 55 | 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Range slider 2 | 3 | A small widget that allows the user to select a contiguous range of whole numbers 4 | using a slider. Check out [this site](https://rasmusfonseca.github.io/d3RangeSlider/) for a demo. The page 5 | `minimal.html` constitutes a minimal working example: 6 | ```html 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 25 | 26 | 27 | 28 |
29 | 32 | 33 | 34 | 35 | ``` 36 | 37 | This creates a slider that spans the range from 0 - 100 (both inclusive) and adds it to the container-div. If you 38 | want diffent placements of the handles or background colors, the 39 | [supplied CSS](https://github.com/RasmusFonseca/d3RangeSlider/blob/master/d3RangeSlider.css) can easily be adapted. A 40 | couple of functions are defined on the `slider` object: 41 | 42 | `slider.range()` returns the currently selected range as an `{begin: number, end: number}`-object. 43 | 44 | `slider.range(s,b)` sets the range to span the interval from `s` to `b` (both included). If `s>b` the two numbers 45 | are swapped. If `s` or `b` are outside the range limits specified in the call to `createD3Rangeslider` a warning is 46 | printed in the console, and the values are clamped to the valid range limits. 47 | 48 | `slider.range(s)` moves the range without changing its width and so it starts at `s`. If the move causes the range to 49 | go outside the range limits a warning is printed in the console and the range moved back to the limit. 50 | 51 | `slider.onChange(callback)` adds a change-listener to the slider, so any UI modification or call to `range` triggers 52 | a call to `callback` with a single `{begin: number, end: number}`-argument that reflects the newly updated range. 53 | 54 | This example illustrates the use of these functions 55 | ```javascript 56 | // Create slider spanning the range from 0 to 10 57 | var slider = createD3RangeSlider(0, 10, "#slider-container"); 58 | 59 | // Range changes to 3-6 60 | slider.range(3,6); 61 | 62 | // Listener gets added 63 | slider.onChange(function(newRange){ 64 | console.log(newRange); 65 | }); 66 | 67 | // Range changes to 7-10 68 | // Warning is printed that you attempted to set a range (8-11) outside the limits (0-10) 69 | // "{begin: 7, end: 10}" is printed in the console because of the listener 70 | slider.range(8); 71 | 72 | // Access currently set range 73 | var curRange = slider.range(); 74 | 75 | // "7-10" is written to the current position in the document 76 | document.write(curRange.begin + "-" + curRange.end); 77 | ``` 78 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | D3 range slider 25 | 26 | 27 | 60 | 61 | 62 | 63 |
64 |

D3 range slider

65 | 66 | 67 |
68 | 69 |

Drag the bar to select a range

70 |
71 |
72 | 73 | 82 |
83 | 84 | 85 | 86 |
87 |

Embed the range slider

88 |
<div id="slider-container"></div>
 89 | <div id="range-label">0 - 10</div>
 90 | 
 91 | <script type="text/javascript">
 92 |     var slider = createD3RangeSlider(0, 100, "#slider-container");
 93 | 
 94 |     slider.onChange(function(newRange){
 95 |         d3.select("#range-label").text(newRange.begin + " - " + newRange.end);
 96 |     });
 97 | 
 98 |     slider.range(0,10);
 99 | </script>
100 | 
101 |
102 | 103 | 104 |
105 |

Fetch the code

106 |
107 | github.com/RasmusFonseca/d3RangeSlider 108 |
109 | 110 |
111 | 112 |
113 |

Technical details

114 | 120 |
121 | 122 | 123 |
124 | 131 | 132 | 133 | 134 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /d3RangeSlider.js: -------------------------------------------------------------------------------- 1 | /*jslint browser: true */ 2 | /*jslint this */ 3 | 4 | 5 | /** 6 | * Create a d3 range slider that selects ranges between `rangeMin` and `rangeMax`, and add it to the 7 | * `containerSelector`. The contents of the container is laid out as follows 8 | * 9 | *
10 | *
11 | *
12 | *
13 | *
14 | * The appearance can be changed with CSS, but the `position` must be `relative`, and the width of `.slider` should be 15 | * left unaltered. 16 | * 17 | * @param rangeMin Minimum value of the range 18 | * @param rangeMax Maximum value of the range 19 | * @param containerSelector A CSS selection indicating exactly one element in the document 20 | * @returns {{range: function(number, number), onChange: function(function)}} 21 | */ 22 | function createD3RangeSlider (rangeMin, rangeMax, containerSelector, playButton) { 23 | "use strict"; 24 | 25 | var minWidth = 10; 26 | 27 | var sliderRange = {begin: rangeMin, end: rangeMin}; 28 | var changeListeners = []; 29 | var touchEndListeners = []; 30 | var container = d3.select(containerSelector); 31 | var playing = false; 32 | var resumePlaying = false; // Used by drag-events to resume playing on release 33 | var playingRate = 100; 34 | var containerHeight = container.node().offsetHeight; 35 | 36 | // Set up play button if requested 37 | if (playButton) { 38 | // Wrap an additional container inside the main one, and set up a box-layout, see also 39 | // http://stackoverflow.com/questions/14319097/css-auto-resize-div-to-fit-container-width 40 | var box = container.append("div") 41 | .style("display", "box") 42 | .style("display", "-moz-box") 43 | .style("display", "-webkit-box") 44 | .style("box-orient", "horizontal") 45 | .style("-moz-box-orient", "horizontal") 46 | .style("-webkit-box-orient", "horizontal"); 47 | 48 | var playBox = box.append("div") 49 | .style("width", containerHeight + "px") 50 | .style("height", containerHeight + "px") 51 | .style("margin-right", "10px") 52 | .style("box-flex", "0") 53 | .style("-moz-box-flex", "0") 54 | .style("-webkit-box-flex", "0") 55 | .classed("play-container", true); 56 | 57 | var sliderBox = box.append("div") 58 | .style("position", "relative") 59 | .style("min-width", (minWidth*2) + "px") 60 | .style("height", containerHeight + "px") 61 | .style("box-flex", "1") 62 | .style("-moz-box-flex", "1") 63 | .style("-webkit-box-flex", "1") 64 | .classed("slider-container", true); 65 | 66 | var playSVG = playBox.append("svg") 67 | .attr("width", containerHeight + "px") 68 | .attr("height", containerHeight + "px") 69 | .style("overflow", "visible"); 70 | 71 | var circleSymbol = playSVG.append("circle") 72 | .attr("cx", containerHeight / 2) 73 | .attr("cy", containerHeight / 2) 74 | .attr("r", containerHeight / 2) 75 | .classed("button", true); 76 | 77 | var h = containerHeight; 78 | var stopSymbol = playSVG.append("rect") 79 | .attr("x", 0.3*h) 80 | .attr("y", 0.3*h) 81 | .attr("width", 0.4*h) 82 | .attr("height", 0.4*h) 83 | .style("visibility", "hidden") 84 | .classed("stop", true); 85 | 86 | var playSymbol = playSVG.append("polygon") 87 | .attr("points", (0.37*h) + "," + (0.2*h) + " " + (0.37*h) + "," + (0.8*h) + " " + (0.75*h) + "," + (0.5*h)) 88 | .classed("play", true); 89 | 90 | //Circle that captures mouse interactions 91 | playSVG.append("circle") 92 | .attr("cx", containerHeight / 2) 93 | .attr("cy", containerHeight / 2) 94 | .attr("r", containerHeight / 2) 95 | .style("fill-opacity", "0.0") 96 | .style("cursor", "pointer") 97 | .on("click", togglePlayButton) 98 | .on("mouseenter", function(){ 99 | circleSymbol 100 | .transition() 101 | .attr("r", 1.2 * containerHeight / 2) 102 | .transition() 103 | .attr("r", containerHeight / 2); 104 | }); 105 | 106 | 107 | } else { 108 | var sliderBox = container.append("div") 109 | .style("position", "relative") 110 | .style("height", containerHeight + "px") 111 | .style("min-width", (minWidth*2) + "px") 112 | .classed("slider-container", true); 113 | } 114 | 115 | //Create elements in container 116 | var slider = sliderBox 117 | .append("div") 118 | .attr("class", "slider"); 119 | var handleW = slider.append("div").attr("class", "handle WW"); 120 | var handleE = slider.append("div").attr("class", "handle EE"); 121 | 122 | /** Update the `left` and `width` attributes of `slider` based on `sliderRange` */ 123 | function updateUIFromRange () { 124 | var conW = sliderBox.node().clientWidth; 125 | var rangeW = sliderRange.end - sliderRange.begin; 126 | var slope = (conW - minWidth) / (rangeMax - rangeMin); 127 | var uirangeW = minWidth + rangeW * slope; 128 | var ratio = (sliderRange.begin - rangeMin) / (rangeMax - rangeMin - rangeW); 129 | if (isNaN(ratio)) { 130 | ratio = 0; 131 | } 132 | var uirangeL = ratio * (conW - uirangeW); 133 | 134 | slider 135 | .style("left", uirangeL + "px") 136 | .style("width", uirangeW + "px"); 137 | } 138 | 139 | /** Update the `sliderRange` based on the `left` and `width` attributes of `slider` */ 140 | function updateRangeFromUI () { 141 | var uirangeL = parseFloat(slider.style("left")); 142 | var uirangeW = parseFloat(slider.style("width")); 143 | var conW = sliderBox.node().clientWidth; //parseFloat(container.style("width")); 144 | var slope = (conW - minWidth) / (rangeMax - rangeMin); 145 | var rangeW = (uirangeW - minWidth) / slope; 146 | if (conW == uirangeW) { 147 | var uislope = 0; 148 | } else { 149 | var uislope = (rangeMax - rangeMin - rangeW) / (conW - uirangeW); 150 | } 151 | var rangeL = rangeMin + uislope * uirangeL; 152 | sliderRange.begin = Math.round(rangeL); 153 | sliderRange.end = Math.round(rangeL + rangeW); 154 | 155 | //Fire change listeners 156 | changeListeners.forEach(function (callback) { 157 | callback({begin: sliderRange.begin, end: sliderRange.end}); 158 | }); 159 | } 160 | 161 | // configure drag behavior for handles and slider 162 | var dragResizeE = d3.drag() 163 | .on("start", function () { 164 | d3.event.sourceEvent.stopPropagation(); 165 | resumePlaying = playing; 166 | playing = false; 167 | }) 168 | .on("end", function () { 169 | if (resumePlaying) { 170 | startPlaying(); 171 | } 172 | touchEndListeners.forEach(function (callback) { 173 | callback({begin: sliderRange.begin, end: sliderRange.end}); 174 | }); 175 | }) 176 | .on("drag", function () { 177 | var dx = d3.event.dx; 178 | if (dx == 0) return; 179 | var conWidth = sliderBox.node().clientWidth; //parseFloat(container.style("width")); 180 | var newLeft = parseInt(slider.style("left")); 181 | var newWidth = parseFloat(slider.style("width")) + dx; 182 | newWidth = Math.max(newWidth, minWidth); 183 | newWidth = Math.min(newWidth, conWidth - newLeft); 184 | slider.style("width", newWidth + "px"); 185 | updateRangeFromUI(); 186 | }); 187 | 188 | var dragResizeW = d3.drag() 189 | .on("start", function () { 190 | this.startX = d3.mouse(this)[0]; 191 | d3.event.sourceEvent.stopPropagation(); 192 | resumePlaying = playing; 193 | playing = false; 194 | }) 195 | .on("end", function () { 196 | if (resumePlaying) { 197 | startPlaying(); 198 | } 199 | touchEndListeners.forEach(function (callback) { 200 | callback({begin: sliderRange.begin, end: sliderRange.end}); 201 | }); 202 | }) 203 | .on("drag", function () { 204 | var dx = d3.mouse(this)[0] - this.startX; 205 | if (dx==0) return; 206 | var newLeft = parseFloat(slider.style("left")) + dx; 207 | var newWidth = parseFloat(slider.style("width")) - dx; 208 | 209 | if (newLeft < 0) { 210 | newWidth += newLeft; 211 | newLeft = 0; 212 | } 213 | if (newWidth < minWidth) { 214 | newLeft -= minWidth - newWidth; 215 | newWidth = minWidth; 216 | } 217 | 218 | slider.style("left", newLeft + "px"); 219 | slider.style("width", newWidth + "px"); 220 | 221 | updateRangeFromUI(); 222 | }); 223 | 224 | var dragMove = d3.drag() 225 | .on("start", function () { 226 | d3.event.sourceEvent.stopPropagation(); 227 | resumePlaying = playing; 228 | playing = false; 229 | }) 230 | .on("end", function () { 231 | if (resumePlaying) { 232 | startPlaying(); 233 | } 234 | touchEndListeners.forEach(function (callback) { 235 | callback({begin: sliderRange.begin, end: sliderRange.end}); 236 | }); 237 | }) 238 | .on("drag", function () { 239 | var dx = d3.event.dx; 240 | var conWidth = sliderBox.node().clientWidth; //parseInt(container.style("width")); 241 | var newLeft = parseInt(slider.style("left")) + dx; 242 | var newWidth = parseInt(slider.style("width")); 243 | 244 | newLeft = Math.max(newLeft, 0); 245 | newLeft = Math.min(newLeft, conWidth - newWidth); 246 | slider.style("left", newLeft + "px"); 247 | 248 | updateRangeFromUI(); 249 | }); 250 | 251 | handleE.call(dragResizeE); 252 | handleW.call(dragResizeW); 253 | slider.call(dragMove); 254 | 255 | //Click on bar 256 | sliderBox.on("mousedown", function (ev) { 257 | var x = d3.mouse(sliderBox.node())[0]; 258 | var props = {}; 259 | var sliderWidth = parseFloat(slider.style("width")); 260 | var conWidth = sliderBox.node().clientWidth; //parseFloat(container.style("width")); 261 | props.left = Math.min(conWidth - sliderWidth, Math.max(x - sliderWidth / 2, 0)); 262 | props.left = Math.round(props.left); 263 | props.width = Math.round(props.width); 264 | slider.style("left", props.left + "px") 265 | .style("width", props.width + "px"); 266 | updateRangeFromUI(); 267 | }); 268 | 269 | //Reposition slider on window resize 270 | window.addEventListener("resize", function () { 271 | updateUIFromRange(); 272 | }); 273 | 274 | function onChange(callback){ 275 | changeListeners.push(callback); 276 | return this; 277 | } 278 | 279 | function onTouchEnd(callback){ 280 | touchEndListeners.push(callback); 281 | return this; 282 | } 283 | 284 | function setRange (b, e) { 285 | sliderRange.begin = b; 286 | sliderRange.end = e; 287 | 288 | updateUIFromRange(); 289 | 290 | //Fire change listeners 291 | changeListeners.forEach(function (callback) { 292 | callback({begin: sliderRange.begin, end: sliderRange.end}); 293 | }); 294 | } 295 | 296 | 297 | /** 298 | * Returns or sets the range depending on arguments. 299 | * If `b` and `e` are both numbers then the range is set to span from `b` to `e`. 300 | * If `b` is a number and `e` is undefined the beginning of the slider is moved to `b`. 301 | * If both `b` and `e` are undefined the currently set range is returned as an object with `begin` and `end` 302 | * attributes. 303 | * If any arguments cause the range to be outside of the `rangeMin` and `rangeMax` specified on slider creation 304 | * then a warning is printed and the range correspondingly clamped. 305 | * @param b beginning of range 306 | * @param e end of range 307 | * @returns {{begin: number, end: number}} 308 | */ 309 | function range(b, e) { 310 | var rLower; 311 | var rUpper; 312 | 313 | if (typeof b === "number" && typeof e === "number") { 314 | 315 | rLower = Math.min(b, e); 316 | rUpper = Math.max(b, e); 317 | 318 | //Check that lower and upper range are within their bounds 319 | if (rLower < rangeMin || rUpper > rangeMax) { 320 | console.log("Warning: trying to set range (" + rLower + "," + rUpper + ") which is outside of bounds (" + rangeMin + "," + rangeMax + "). "); 321 | rLower = Math.max(rLower, rangeMin); 322 | rUpper = Math.min(rUpper, rangeMax); 323 | } 324 | 325 | //Set the range 326 | setRange(rLower, rUpper); 327 | } else if (typeof b === "number") { 328 | 329 | rLower = b; 330 | var dif = sliderRange.end - sliderRange.begin; 331 | rUpper = rLower + dif; 332 | 333 | if (rLower < rangeMin) { 334 | console.log("Warning: trying to set range (" + rLower + "," + rUpper + ") which is outside of bounds (" + rangeMin + "," + rangeMax + "). "); 335 | rLower = rangeMin; 336 | } 337 | if(rUpper > rangeMax){ 338 | console.log("Warning: trying to set range (" + rLower + "," + rUpper + ") which is outside of bounds (" + rangeMin + "," + rangeMax + "). "); 339 | rLower = rangeMax - dif; 340 | rUpper = rangeMax; 341 | } 342 | 343 | setRange(rLower, rUpper); 344 | } 345 | 346 | return {begin: sliderRange.begin, end: sliderRange.end}; 347 | } 348 | 349 | function togglePlayButton () { 350 | if (playing) { 351 | stopPlaying(); 352 | } else { 353 | startPlaying(); 354 | } 355 | } 356 | 357 | function frameTick() { 358 | if (!playing) { 359 | return; 360 | } 361 | 362 | var limitWidth = rangeMax - rangeMin + 1; 363 | var rangeWidth = sliderRange.end - sliderRange.begin + 1; 364 | var delta = Math.min(Math.ceil(rangeWidth / 10), Math.ceil(limitWidth / 100)); 365 | 366 | // Check if playback has reached the end 367 | if (sliderRange.end + delta > rangeMax) { 368 | delta = rangeMax - sliderRange.end; 369 | stopPlaying(); 370 | } 371 | 372 | setRange(sliderRange.begin + delta, sliderRange.end + delta); 373 | 374 | setTimeout(frameTick, playingRate); 375 | } 376 | 377 | function startPlaying(rate) { 378 | if (rate !== undefined) { 379 | playingRate = rate; 380 | } 381 | 382 | if (playing) { 383 | return; 384 | } 385 | 386 | playing = true; 387 | if (playButton) { 388 | playSymbol.style("visibility", "hidden"); 389 | stopSymbol.style("visibility", "visible"); 390 | } 391 | frameTick(); 392 | } 393 | 394 | function stopPlaying() { 395 | playing = false; 396 | if (playButton) { 397 | playSymbol.style("visibility", "visible"); 398 | stopSymbol.style("visibility", "hidden"); 399 | } 400 | } 401 | 402 | setRange(sliderRange.begin, sliderRange.end); 403 | 404 | return { 405 | range: range, 406 | startPlaying: startPlaying, 407 | stopPlaying: stopPlaying, 408 | onChange: onChange, 409 | onTouchEnd: onTouchEnd, 410 | updateUIFromRange: updateUIFromRange 411 | }; 412 | } 413 | --------------------------------------------------------------------------------