|
1 | 1 | /** |
2 | | - * svelte-range-slider-pips ~ 1.5.3 |
| 2 | + * svelte-range-slider-pips ~ 1.6.0 |
3 | 3 | * Multi-Thumb, Accessible, Beautiful Range Slider with Pips |
4 | | - * © MPL-2.0 ~ Simon Goellner <[email protected]> ~ 7/1/2021 |
| 4 | + * © MPL-2.0 ~ Simon Goellner <[email protected]> ~ 16/1/2021 |
5 | 5 | */ |
6 | 6 | (function (global, factory) { |
7 | 7 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
|
126 | 126 | function toggle_class(element, name, toggle) { |
127 | 127 | element.classList[toggle ? 'add' : 'remove'](name); |
128 | 128 | } |
| 129 | + function custom_event(type, detail) { |
| 130 | + const e = document.createEvent('CustomEvent'); |
| 131 | + e.initCustomEvent(type, false, false, detail); |
| 132 | + return e; |
| 133 | + } |
129 | 134 |
|
130 | 135 | let current_component; |
131 | 136 | function set_current_component(component) { |
132 | 137 | current_component = component; |
133 | 138 | } |
| 139 | + function get_current_component() { |
| 140 | + if (!current_component) |
| 141 | + throw new Error(`Function called outside component initialization`); |
| 142 | + return current_component; |
| 143 | + } |
| 144 | + function createEventDispatcher() { |
| 145 | + const component = get_current_component(); |
| 146 | + return (type, detail) => { |
| 147 | + const callbacks = component.$$.callbacks[type]; |
| 148 | + if (callbacks) { |
| 149 | + // TODO are there situations where events could be dispatched |
| 150 | + // in a server (non-DOM) environment? |
| 151 | + const event = custom_event(type, detail); |
| 152 | + callbacks.slice().forEach(fn => { |
| 153 | + fn.call(component, event); |
| 154 | + }); |
| 155 | + } |
| 156 | + }; |
| 157 | + } |
134 | 158 |
|
135 | 159 | const dirty_components = []; |
136 | 160 | const binding_callbacks = []; |
|
1104 | 1128 |
|
1105 | 1129 | function get_each_context$1(ctx, list, i) { |
1106 | 1130 | const child_ctx = ctx.slice(); |
1107 | | - child_ctx[52] = list[i]; |
1108 | | - child_ctx[54] = i; |
| 1131 | + child_ctx[58] = list[i]; |
| 1132 | + child_ctx[60] = i; |
1109 | 1133 | return child_ctx; |
1110 | 1134 | } |
1111 | 1135 |
|
1112 | | - // (671:6) {#if float} |
| 1136 | + // (724:6) {#if float} |
1113 | 1137 | function create_if_block_2$1(ctx) { |
1114 | 1138 | let span; |
1115 | 1139 | let t0; |
1116 | | - let t1_value = /*handleFormatter*/ ctx[18](/*value*/ ctx[52]) + ""; |
| 1140 | + let t1_value = /*handleFormatter*/ ctx[18](/*value*/ ctx[58]) + ""; |
1117 | 1141 | let t1; |
1118 | 1142 | let t2; |
1119 | 1143 |
|
|
1133 | 1157 | }, |
1134 | 1158 | p(ctx, dirty) { |
1135 | 1159 | if (dirty[0] & /*prefix*/ 32768) set_data(t0, /*prefix*/ ctx[15]); |
1136 | | - if (dirty[0] & /*handleFormatter, values*/ 262145 && t1_value !== (t1_value = /*handleFormatter*/ ctx[18](/*value*/ ctx[52]) + "")) set_data(t1, t1_value); |
| 1160 | + if (dirty[0] & /*handleFormatter, values*/ 262145 && t1_value !== (t1_value = /*handleFormatter*/ ctx[18](/*value*/ ctx[58]) + "")) set_data(t1, t1_value); |
1137 | 1161 | if (dirty[0] & /*suffix*/ 65536) set_data(t2, /*suffix*/ ctx[16]); |
1138 | 1162 | }, |
1139 | 1163 | d(detaching) { |
|
1142 | 1166 | }; |
1143 | 1167 | } |
1144 | 1168 |
|
1145 | | - // (653:2) {#each values as value, index} |
| 1169 | + // (706:2) {#each values as value, index} |
1146 | 1170 | function create_each_block$1(ctx) { |
1147 | 1171 | let span1; |
1148 | 1172 | let span0; |
|
1167 | 1191 | attr(span1, "role", "slider"); |
1168 | 1192 | attr(span1, "class", "rangeHandle"); |
1169 | 1193 | attr(span1, "tabindex", "0"); |
1170 | | - attr(span1, "style", span1_style_value = "" + ((/*vertical*/ ctx[5] ? "top" : "left") + ": " + /*$springPositions*/ ctx[24][/*index*/ ctx[54]] + "%; z-index: " + (/*activeHandle*/ ctx[22] === /*index*/ ctx[54] ? 3 : 2) + ";")); |
| 1194 | + attr(span1, "style", span1_style_value = "" + ((/*vertical*/ ctx[5] ? "top" : "left") + ": " + /*$springPositions*/ ctx[24][/*index*/ ctx[60]] + "%; z-index: " + (/*activeHandle*/ ctx[22] === /*index*/ ctx[60] ? 3 : 2) + ";")); |
1171 | 1195 |
|
1172 | | - attr(span1, "aria-valuemin", span1_aria_valuemin_value = /*range*/ ctx[1] === true && /*index*/ ctx[54] === 1 |
| 1196 | + attr(span1, "aria-valuemin", span1_aria_valuemin_value = /*range*/ ctx[1] === true && /*index*/ ctx[60] === 1 |
1173 | 1197 | ? /*values*/ ctx[0][0] |
1174 | 1198 | : /*min*/ ctx[2]); |
1175 | 1199 |
|
1176 | | - attr(span1, "aria-valuemax", span1_aria_valuemax_value = /*range*/ ctx[1] === true && /*index*/ ctx[54] === 0 |
| 1200 | + attr(span1, "aria-valuemax", span1_aria_valuemax_value = /*range*/ ctx[1] === true && /*index*/ ctx[60] === 0 |
1177 | 1201 | ? /*values*/ ctx[0][1] |
1178 | 1202 | : /*max*/ ctx[3]); |
1179 | 1203 |
|
1180 | | - attr(span1, "aria-valuenow", span1_aria_valuenow_value = /*value*/ ctx[52]); |
1181 | | - attr(span1, "aria-valuetext", span1_aria_valuetext_value = "" + (/*prefix*/ ctx[15] + /*handleFormatter*/ ctx[18](/*value*/ ctx[52]) + /*suffix*/ ctx[16])); |
| 1204 | + attr(span1, "aria-valuenow", span1_aria_valuenow_value = /*value*/ ctx[58]); |
| 1205 | + attr(span1, "aria-valuetext", span1_aria_valuetext_value = "" + (/*prefix*/ ctx[15] + /*handleFormatter*/ ctx[18](/*value*/ ctx[58]) + /*suffix*/ ctx[16])); |
1182 | 1206 | attr(span1, "aria-orientation", span1_aria_orientation_value = /*vertical*/ ctx[5] ? "vertical" : "horizontal"); |
1183 | 1207 | toggle_class(span1, "hoverable", /*hover*/ ctx[7]); |
1184 | | - toggle_class(span1, "active", /*focus*/ ctx[20] && /*activeHandle*/ ctx[22] === /*index*/ ctx[54]); |
1185 | | - toggle_class(span1, "press", /*handlePressed*/ ctx[21] && /*activeHandle*/ ctx[22] === /*index*/ ctx[54]); |
| 1208 | + toggle_class(span1, "active", /*focus*/ ctx[20] && /*activeHandle*/ ctx[22] === /*index*/ ctx[60]); |
| 1209 | + toggle_class(span1, "press", /*handlePressed*/ ctx[21] && /*activeHandle*/ ctx[22] === /*index*/ ctx[60]); |
1186 | 1210 | }, |
1187 | 1211 | m(target, anchor) { |
1188 | 1212 | insert(target, span1, anchor); |
|
1214 | 1238 | if_block = null; |
1215 | 1239 | } |
1216 | 1240 |
|
1217 | | - if (dirty[0] & /*vertical, $springPositions, activeHandle*/ 20971552 && span1_style_value !== (span1_style_value = "" + ((/*vertical*/ ctx[5] ? "top" : "left") + ": " + /*$springPositions*/ ctx[24][/*index*/ ctx[54]] + "%; z-index: " + (/*activeHandle*/ ctx[22] === /*index*/ ctx[54] ? 3 : 2) + ";"))) { |
| 1241 | + if (dirty[0] & /*vertical, $springPositions, activeHandle*/ 20971552 && span1_style_value !== (span1_style_value = "" + ((/*vertical*/ ctx[5] ? "top" : "left") + ": " + /*$springPositions*/ ctx[24][/*index*/ ctx[60]] + "%; z-index: " + (/*activeHandle*/ ctx[22] === /*index*/ ctx[60] ? 3 : 2) + ";"))) { |
1218 | 1242 | attr(span1, "style", span1_style_value); |
1219 | 1243 | } |
1220 | 1244 |
|
1221 | | - if (dirty[0] & /*range, values, min*/ 7 && span1_aria_valuemin_value !== (span1_aria_valuemin_value = /*range*/ ctx[1] === true && /*index*/ ctx[54] === 1 |
| 1245 | + if (dirty[0] & /*range, values, min*/ 7 && span1_aria_valuemin_value !== (span1_aria_valuemin_value = /*range*/ ctx[1] === true && /*index*/ ctx[60] === 1 |
1222 | 1246 | ? /*values*/ ctx[0][0] |
1223 | 1247 | : /*min*/ ctx[2])) { |
1224 | 1248 | attr(span1, "aria-valuemin", span1_aria_valuemin_value); |
1225 | 1249 | } |
1226 | 1250 |
|
1227 | | - if (dirty[0] & /*range, values, max*/ 11 && span1_aria_valuemax_value !== (span1_aria_valuemax_value = /*range*/ ctx[1] === true && /*index*/ ctx[54] === 0 |
| 1251 | + if (dirty[0] & /*range, values, max*/ 11 && span1_aria_valuemax_value !== (span1_aria_valuemax_value = /*range*/ ctx[1] === true && /*index*/ ctx[60] === 0 |
1228 | 1252 | ? /*values*/ ctx[0][1] |
1229 | 1253 | : /*max*/ ctx[3])) { |
1230 | 1254 | attr(span1, "aria-valuemax", span1_aria_valuemax_value); |
1231 | 1255 | } |
1232 | 1256 |
|
1233 | | - if (dirty[0] & /*values*/ 1 && span1_aria_valuenow_value !== (span1_aria_valuenow_value = /*value*/ ctx[52])) { |
| 1257 | + if (dirty[0] & /*values*/ 1 && span1_aria_valuenow_value !== (span1_aria_valuenow_value = /*value*/ ctx[58])) { |
1234 | 1258 | attr(span1, "aria-valuenow", span1_aria_valuenow_value); |
1235 | 1259 | } |
1236 | 1260 |
|
1237 | | - if (dirty[0] & /*prefix, handleFormatter, values, suffix*/ 360449 && span1_aria_valuetext_value !== (span1_aria_valuetext_value = "" + (/*prefix*/ ctx[15] + /*handleFormatter*/ ctx[18](/*value*/ ctx[52]) + /*suffix*/ ctx[16]))) { |
| 1261 | + if (dirty[0] & /*prefix, handleFormatter, values, suffix*/ 360449 && span1_aria_valuetext_value !== (span1_aria_valuetext_value = "" + (/*prefix*/ ctx[15] + /*handleFormatter*/ ctx[18](/*value*/ ctx[58]) + /*suffix*/ ctx[16]))) { |
1238 | 1262 | attr(span1, "aria-valuetext", span1_aria_valuetext_value); |
1239 | 1263 | } |
1240 | 1264 |
|
|
1247 | 1271 | } |
1248 | 1272 |
|
1249 | 1273 | if (dirty[0] & /*focus, activeHandle*/ 5242880) { |
1250 | | - toggle_class(span1, "active", /*focus*/ ctx[20] && /*activeHandle*/ ctx[22] === /*index*/ ctx[54]); |
| 1274 | + toggle_class(span1, "active", /*focus*/ ctx[20] && /*activeHandle*/ ctx[22] === /*index*/ ctx[60]); |
1251 | 1275 | } |
1252 | 1276 |
|
1253 | 1277 | if (dirty[0] & /*handlePressed, activeHandle*/ 6291456) { |
1254 | | - toggle_class(span1, "press", /*handlePressed*/ ctx[21] && /*activeHandle*/ ctx[22] === /*index*/ ctx[54]); |
| 1278 | + toggle_class(span1, "press", /*handlePressed*/ ctx[21] && /*activeHandle*/ ctx[22] === /*index*/ ctx[60]); |
1255 | 1279 | } |
1256 | 1280 | }, |
1257 | 1281 | d(detaching) { |
|
1263 | 1287 | }; |
1264 | 1288 | } |
1265 | 1289 |
|
1266 | | - // (676:2) {#if range} |
| 1290 | + // (729:2) {#if range} |
1267 | 1291 | function create_if_block_1$1(ctx) { |
1268 | 1292 | let span; |
1269 | 1293 | let span_style_value; |
|
1288 | 1312 | }; |
1289 | 1313 | } |
1290 | 1314 |
|
1291 | | - // (682:2) {#if pips} |
| 1315 | + // (735:2) {#if pips} |
1292 | 1316 | function create_if_block$1(ctx) { |
1293 | 1317 | let rangepips; |
1294 | 1318 | let current; |
|
1419 | 1443 | listen(window, "mouseup", /*bodyMouseUp*/ ctx[35]), |
1420 | 1444 | listen(window, "touchend", /*bodyTouchEnd*/ ctx[36]), |
1421 | 1445 | listen(window, "keydown", /*bodyKeyDown*/ ctx[37]), |
1422 | | - listen(div, "touchstart", prevent_default(/*sliderInteractStart*/ ctx[31])), |
1423 | 1446 | listen(div, "mousedown", /*sliderInteractStart*/ ctx[31]), |
1424 | | - listen(div, "touchend", prevent_default(/*sliderInteractEnd*/ ctx[32])), |
1425 | | - listen(div, "mouseup", /*sliderInteractEnd*/ ctx[32]) |
| 1447 | + listen(div, "mouseup", /*sliderInteractEnd*/ ctx[32]), |
| 1448 | + listen(div, "touchstart", prevent_default(/*sliderInteractStart*/ ctx[31])), |
| 1449 | + listen(div, "touchend", prevent_default(/*sliderInteractEnd*/ ctx[32])) |
1426 | 1450 | ]; |
1427 | 1451 |
|
1428 | 1452 | mounted = true; |
|
1590 | 1614 | let { handleFormatter = formatter } = $$props; |
1591 | 1615 | let { precision = 2 } = $$props; |
1592 | 1616 | let { springValues = { stiffness: 0.15, damping: 0.4 } } = $$props; |
| 1617 | + const dispatch = createEventDispatcher(); |
1593 | 1618 |
|
1594 | 1619 | // dom references |
1595 | 1620 | let slider; |
|
1601 | 1626 | let handlePressed = false; |
1602 | 1627 | let keyboardActive = false; |
1603 | 1628 | let activeHandle = values.length - 1; |
| 1629 | + let startValue; |
| 1630 | + let previousValue; |
1604 | 1631 |
|
1605 | 1632 | // save spring-tweened copies of the values for use |
1606 | 1633 | // when changing values and animating the handle/range nicely |
|
1750 | 1777 |
|
1751 | 1778 | // set the value for the handle, and align/clamp it |
1752 | 1779 | $$invalidate(0, values[index] = value, values); |
| 1780 | + |
| 1781 | + // fire the change event when the handle moves, |
| 1782 | + // and store the previous value for the next time |
| 1783 | + if (previousValue !== alignValueToStep(value)) { |
| 1784 | + eChange(); |
| 1785 | + previousValue = alignValueToStep(value); |
| 1786 | + } |
1753 | 1787 | } |
1754 | 1788 |
|
1755 | 1789 | /** |
|
1859 | 1893 | $$invalidate(21, handlePressed = true); |
1860 | 1894 | $$invalidate(22, activeHandle = getClosestHandle(clientPos)); |
1861 | 1895 |
|
| 1896 | + // fire the start event |
| 1897 | + startValue = values[activeHandle]; |
| 1898 | + |
| 1899 | + eStart(); |
| 1900 | + |
1862 | 1901 | // for touch devices we want the handle to instantly |
1863 | 1902 | // move to the position touched for more responsive feeling |
1864 | 1903 | if (e.type === "touchstart") { |
|
1872 | 1911 | * @param {event} e the event from browser |
1873 | 1912 | **/ |
1874 | 1913 | function sliderInteractEnd(e) { |
| 1914 | + // fire the stop event for touch devices |
| 1915 | + if (e.type === "touchend") { |
| 1916 | + eStop(); |
| 1917 | + } |
| 1918 | + |
1875 | 1919 | $$invalidate(21, handlePressed = false); |
1876 | 1920 | } |
1877 | 1921 |
|
|
1911 | 1955 | // this only works if a handle is active, which can |
1912 | 1956 | // only happen if there was sliderInteractStart triggered |
1913 | 1957 | // on the slider, already |
1914 | | - if (handleActivated && (el === slider || slider.contains(el))) { |
1915 | | - $$invalidate(20, focus = true); |
| 1958 | + if (handleActivated) { |
| 1959 | + if (el === slider || slider.contains(el)) { |
| 1960 | + $$invalidate(20, focus = true); |
1916 | 1961 |
|
1917 | | - if (!targetIsHandle(el)) { |
1918 | | - handleInteract(normalisedClient(e)); |
| 1962 | + if (!targetIsHandle(el)) { |
| 1963 | + handleInteract(normalisedClient(e)); |
| 1964 | + } |
1919 | 1965 | } |
| 1966 | + |
| 1967 | + // fire the stop event for mouse device |
| 1968 | + // when the body is triggered with an active handle |
| 1969 | + eStop(); |
1920 | 1970 | } |
1921 | 1971 |
|
1922 | 1972 | handleActivated = false; |
|
1939 | 1989 | } |
1940 | 1990 | } |
1941 | 1991 |
|
| 1992 | + function eStart() { |
| 1993 | + dispatch("start", { |
| 1994 | + activeHandle, |
| 1995 | + value: alignValueToStep(startValue), |
| 1996 | + values: values.map(v => alignValueToStep(v)) |
| 1997 | + }); |
| 1998 | + } |
| 1999 | + |
| 2000 | + function eStop() { |
| 2001 | + dispatch("stop", { |
| 2002 | + activeHandle, |
| 2003 | + startValue: alignValueToStep(startValue), |
| 2004 | + value: alignValueToStep(values[activeHandle]), |
| 2005 | + values: values.map(v => alignValueToStep(v)) |
| 2006 | + }); |
| 2007 | + } |
| 2008 | + |
| 2009 | + function eChange() { |
| 2010 | + dispatch("change", { |
| 2011 | + activeHandle, |
| 2012 | + previousValue: alignValueToStep(previousValue) || alignValueToStep(startValue) || alignValueToStep(values[activeHandle]), |
| 2013 | + value: alignValueToStep(values[activeHandle]), |
| 2014 | + values: values.map(v => alignValueToStep(v)) |
| 2015 | + }); |
| 2016 | + } |
| 2017 | + |
1942 | 2018 | function div_binding($$value) { |
1943 | 2019 | binding_callbacks[$$value ? "unshift" : "push"](() => { |
1944 | 2020 | slider = $$value; |
|
1983 | 2059 | * @param {number} val the value to clamp |
1984 | 2060 | * @return {number} the value after it's been clamped |
1985 | 2061 | **/ |
1986 | | - $$invalidate(45, clampValue = function (val) { |
| 2062 | + $$invalidate(47, clampValue = function (val) { |
1987 | 2063 | // return the min/max if outside of that range |
1988 | 2064 | return val <= min ? min : val >= max ? max : val; |
1989 | 2065 | }); |
1990 | 2066 | } |
1991 | 2067 |
|
1992 | | - if ($$self.$$.dirty[0] & /*min, max, step*/ 28 | $$self.$$.dirty[1] & /*clampValue, precision*/ 16640) { |
| 2068 | + if ($$self.$$.dirty[0] & /*min, max, step*/ 28 | $$self.$$.dirty[1] & /*clampValue, precision*/ 65792) { |
1993 | 2069 | /** |
1994 | 2070 | * align the value with the steps so that it |
1995 | 2071 | * always sits on the closest (above/below) step |
1996 | 2072 | * @param {number} val the value to align |
1997 | 2073 | * @return {number} the value after it's been aligned |
1998 | 2074 | **/ |
1999 | | - $$invalidate(44, alignValueToStep = function (val) { |
| 2075 | + $$invalidate(46, alignValueToStep = function (val) { |
2000 | 2076 | // sanity check for performance |
2001 | 2077 | if (val <= min) { |
2002 | 2078 | return min; |
|
2025 | 2101 | }); |
2026 | 2102 | } |
2027 | 2103 |
|
2028 | | - if ($$self.$$.dirty[0] & /*values*/ 1 | $$self.$$.dirty[1] & /*alignValueToStep*/ 8192) { |
| 2104 | + if ($$self.$$.dirty[0] & /*values*/ 1 | $$self.$$.dirty[1] & /*alignValueToStep*/ 32768) { |
2029 | 2105 | // watch the values array, and trim / clamp the values to the steps |
2030 | 2106 | // and boundaries set up in the slider on change |
2031 | 2107 | $$invalidate(0, values = trimRange(values).map(v => alignValueToStep(v))); |
|
0 commit comments