2021-06-06 22:18:40 +00:00
|
|
|
/* Imports */
|
|
|
|
import { Slider, Rail, Handles, Tracks, Ticks } from "react-compound-slider";
|
2022-01-30 15:29:45 +00:00
|
|
|
import React, { useState } from "react";
|
2021-06-06 22:18:40 +00:00
|
|
|
// https://sghall.github.io/react-compound-slider/#/getting-started/tutorial
|
|
|
|
|
|
|
|
/* Definitions */
|
|
|
|
|
2022-01-30 15:29:45 +00:00
|
|
|
const sliderStyle = {
|
|
|
|
// Give the slider some width
|
|
|
|
position: "relative",
|
|
|
|
width: "40em",
|
2021-06-06 22:18:40 +00:00
|
|
|
height: 40,
|
2022-01-30 15:29:45 +00:00
|
|
|
border: "5em",
|
|
|
|
};
|
2021-06-06 22:18:40 +00:00
|
|
|
|
|
|
|
const railStyle = {
|
2022-01-30 15:29:45 +00:00
|
|
|
position: "absolute",
|
|
|
|
width: "100%",
|
2021-06-06 22:18:40 +00:00
|
|
|
height: 15,
|
2021-10-06 08:54:40 +00:00
|
|
|
marginTop: 32.5,
|
2021-06-06 22:18:40 +00:00
|
|
|
borderRadius: 5,
|
2022-01-30 15:29:45 +00:00
|
|
|
backgroundColor: "lightgrey",
|
|
|
|
};
|
2021-06-06 22:18:40 +00:00
|
|
|
|
|
|
|
/* Support functions */
|
|
|
|
function Handle({
|
|
|
|
handle: { id, value, percent },
|
|
|
|
getHandleProps,
|
|
|
|
displayFunction,
|
2022-01-30 15:29:45 +00:00
|
|
|
handleWidth,
|
2021-06-06 22:18:40 +00:00
|
|
|
}) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="justify-center text-center text-gray-600 text-xs">
|
|
|
|
{displayFunction(value)}
|
|
|
|
</div>
|
|
|
|
<div
|
|
|
|
style={{
|
|
|
|
left: `${percent}%`,
|
2022-01-30 15:29:45 +00:00
|
|
|
position: "absolute",
|
2021-06-06 22:18:40 +00:00
|
|
|
marginLeft: -10,
|
2021-10-06 08:51:27 +00:00
|
|
|
marginTop: 2.5,
|
2021-06-06 22:18:40 +00:00
|
|
|
zIndex: 2,
|
|
|
|
width: 30,
|
|
|
|
height: 30,
|
2022-01-30 15:29:45 +00:00
|
|
|
cursor: "pointer",
|
|
|
|
borderRadius: "0%",
|
|
|
|
backgroundColor: "#374151",
|
|
|
|
color: "#374151",
|
2021-06-06 22:18:40 +00:00
|
|
|
}}
|
|
|
|
{...getHandleProps(id)}
|
2022-01-30 15:29:45 +00:00
|
|
|
></div>
|
2021-06-06 22:18:40 +00:00
|
|
|
</>
|
2022-01-30 15:29:45 +00:00
|
|
|
);
|
2021-06-06 22:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Track({ source, target, getTrackProps }) {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
style={{
|
2022-01-30 15:29:45 +00:00
|
|
|
position: "absolute",
|
2021-10-06 08:54:40 +00:00
|
|
|
height: 17.5,
|
2021-06-06 22:18:40 +00:00
|
|
|
zIndex: 1,
|
2021-10-06 08:54:40 +00:00
|
|
|
marginTop: 8.25,
|
2022-01-30 15:29:45 +00:00
|
|
|
backgroundColor: " #3B82F6",
|
2021-06-06 22:18:40 +00:00
|
|
|
borderRadius: 5,
|
2022-01-30 15:29:45 +00:00
|
|
|
cursor: "pointer",
|
2021-06-06 22:18:40 +00:00
|
|
|
left: `${source.percent}%`,
|
|
|
|
width: `${target.percent - source.percent}%`,
|
|
|
|
}}
|
2022-01-30 15:29:45 +00:00
|
|
|
{
|
|
|
|
...getTrackProps() /* this will set up events if you want it to be clickeable (optional) */
|
|
|
|
}
|
2021-06-06 22:18:40 +00:00
|
|
|
/>
|
2022-01-30 15:29:45 +00:00
|
|
|
);
|
2021-06-06 22:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Body */
|
2022-01-30 15:29:45 +00:00
|
|
|
// Two functions, essentially identical.
|
2021-10-06 11:00:09 +00:00
|
|
|
export function SliderElement({ onChange, value, displayFunction, domain }) {
|
2022-01-30 15:29:45 +00:00
|
|
|
let toLogDomain = (arr) => [
|
|
|
|
Math.log(arr[0]) / Math.log(10),
|
|
|
|
Math.log(arr[1]) / Math.log(10),
|
|
|
|
];
|
2021-06-06 22:18:40 +00:00
|
|
|
return (
|
|
|
|
<Slider
|
2022-01-30 15:29:45 +00:00
|
|
|
rootStyle={
|
|
|
|
sliderStyle /* inline styles for the outer div. Can also use className prop. */
|
|
|
|
}
|
2021-11-25 00:50:28 +00:00
|
|
|
domain={toLogDomain([1 / domain, domain])}
|
2021-06-06 22:18:40 +00:00
|
|
|
values={[value]}
|
2021-09-22 19:45:18 +00:00
|
|
|
step={0.0001}
|
2021-06-06 22:18:40 +00:00
|
|
|
onChange={onChange}
|
2021-06-07 17:09:30 +00:00
|
|
|
reversed={true}
|
2021-06-06 22:18:40 +00:00
|
|
|
>
|
|
|
|
<Rail>
|
2022-01-30 15:29:45 +00:00
|
|
|
{({ getRailProps }) => <div style={railStyle} {...getRailProps()} />}
|
2021-06-06 22:18:40 +00:00
|
|
|
</Rail>
|
|
|
|
<Handles>
|
|
|
|
{({ handles, getHandleProps }) => (
|
|
|
|
<div className="slider-handles">
|
2022-01-30 15:29:45 +00:00
|
|
|
{handles.map((handle) => (
|
2021-06-06 22:18:40 +00:00
|
|
|
<Handle
|
|
|
|
key={handle.id}
|
|
|
|
handle={handle}
|
|
|
|
getHandleProps={getHandleProps}
|
|
|
|
displayFunction={displayFunction}
|
|
|
|
handleWidth={"15em"}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Handles>
|
|
|
|
<Tracks right={false}>
|
|
|
|
{({ tracks, getTrackProps }) => (
|
|
|
|
<div className="slider-tracks">
|
|
|
|
{tracks.map(({ id, source, target }) => (
|
|
|
|
<Track
|
|
|
|
key={id}
|
|
|
|
source={source}
|
|
|
|
target={target}
|
|
|
|
getTrackProps={getTrackProps}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</Tracks>
|
|
|
|
</Slider>
|
2022-01-30 15:29:45 +00:00
|
|
|
);
|
2021-06-06 22:18:40 +00:00
|
|
|
}
|
2021-06-12 14:13:13 +00:00
|
|
|
|
2022-01-30 15:29:45 +00:00
|
|
|
export function SubmitSliderButton({
|
|
|
|
posList,
|
|
|
|
binaryComparisons,
|
|
|
|
sliderValue,
|
|
|
|
reasoning,
|
|
|
|
toComparePair,
|
|
|
|
nextStepSlider,
|
|
|
|
dontPushSubmitButtonAnyMore,
|
|
|
|
}) {
|
2021-06-12 14:13:13 +00:00
|
|
|
// This element didn't necessarily have to exist, but it made it easier for debugging purposes
|
|
|
|
let onClick = (event) => {
|
2022-01-30 15:29:45 +00:00
|
|
|
if (!dontPushSubmitButtonAnyMore) {
|
2021-12-08 15:46:17 +00:00
|
|
|
//event.preventDefault();
|
2022-01-30 15:29:45 +00:00
|
|
|
let obj = {
|
|
|
|
posList,
|
|
|
|
binaryComparisons,
|
|
|
|
sliderValue,
|
|
|
|
reasoning,
|
|
|
|
element1: toComparePair[1],
|
|
|
|
element2: toComparePair[0],
|
|
|
|
};
|
2021-12-08 15:46:17 +00:00
|
|
|
//
|
2022-01-30 15:29:45 +00:00
|
|
|
console.log("input@SubmitSliderButton");
|
|
|
|
console.log(obj);
|
2021-12-08 15:46:17 +00:00
|
|
|
if (!!Number(sliderValue) && sliderValue >= 0) {
|
2022-01-30 15:29:45 +00:00
|
|
|
nextStepSlider(obj);
|
2021-12-08 15:46:17 +00:00
|
|
|
} else if (!!Number(sliderValue) && sliderValue < 0) {
|
2022-01-30 15:29:45 +00:00
|
|
|
alert("Negative numbers not yet allowed");
|
2021-12-08 15:46:17 +00:00
|
|
|
} else {
|
2022-01-30 15:29:45 +00:00
|
|
|
alert("Your input is not a number");
|
2021-12-08 15:46:17 +00:00
|
|
|
}
|
2021-11-25 00:42:34 +00:00
|
|
|
}
|
2022-01-30 15:29:45 +00:00
|
|
|
};
|
2021-06-12 14:13:13 +00:00
|
|
|
|
2022-01-30 15:29:45 +00:00
|
|
|
return (
|
|
|
|
<button
|
|
|
|
className={
|
|
|
|
!dontPushSubmitButtonAnyMore
|
|
|
|
? "bg-transparent hover:bg-blue-500 text-blue-700 font-semibold hover:text-white py-2 px-4 border border-blue-500 hover:border-transparent rounded mt-5"
|
|
|
|
: "bg-transparent text-blue-700 font-semibold py-2 px-4 border border-blue-500 rounded mt-5"
|
|
|
|
}
|
|
|
|
onClick={onClick}
|
|
|
|
>
|
|
|
|
Submit
|
|
|
|
</button>
|
|
|
|
);
|
|
|
|
}
|
2021-06-12 14:13:13 +00:00
|
|
|
|