diff --git a/lib/slider.js b/lib/slider.js
index 4d74700..4a4e33d 100644
--- a/lib/slider.js
+++ b/lib/slider.js
@@ -123,3 +123,22 @@ export function SliderElement({ onChange, value, displayFunction }) {
)
}
+
+export function SubmitSliderButton({posList, binaryComparisons, sliderValue, toComparePair, nextStepSlider}){
+ // This element didn't necessarily have to exist, but it made it easier for debugging purposes
+ let onClick = (event) => {
+ //event.preventDefault();
+ let obj = {posList, binaryComparisons, sliderValue, element1: toComparePair[1], element2: toComparePair[0]}
+ //
+ console.log("input@SubmitSliderButton")
+ console.log(obj)
+ nextStepSlider(obj)
+ }
+
+ return(
)
+
+}
\ No newline at end of file
diff --git a/lib/textAreaForJson.js b/lib/textAreaForJson.js
new file mode 100644
index 0000000..3e2be6d
--- /dev/null
+++ b/lib/textAreaForJson.js
@@ -0,0 +1,103 @@
+import React, { useState } from 'react';
+
+export function TextAreaForJson({handleSubmit}){
+ let [value, setValue] = useState(`[
+ {
+ "name": "Some element. The name field is necessary",
+ "url": "http://www.example.com",
+ "somethirdfield": "a"
+ },
+ {
+ "name": "Another element",
+ "url": "http://www.example1.com",
+ "somethirdfield": "b"
+ },
+ {
+ "name": "A third element",
+ "url": "http://www.example2.com",
+ "isReferenceValue": true,
+ "somethirdfield": "c"
+ }
+ ]`)
+ const [displayingDoneMessage, setDisplayingDoneMessage] = useState(false)
+ const [displayingDoneMessageTimer, setDisplayingDoneMessageTimer] = useState(null)
+
+ let handleChange = (event) => {
+ setValue(event.target.value)
+ }
+
+ let handleSubmitInner = (event) => {
+ clearTimeout(displayingDoneMessageTimer)
+ event.preventDefault();
+ //console.log(event)
+ console.log("value@handleSubmitInner@TextAreaForJson")
+ //console.log(typeof(value));
+ console.log(value);
+ try{
+ let newData = JSON.parse(value)
+ //console.log(typeof(newData))
+ //console.log(newData)
+ handleSubmit(newData)
+ if(!newData.length || newData.length < 2){
+ throw Error("Not enough objects")
+ }
+ setDisplayingDoneMessage(true)
+ let timer = setTimeout(() => setDisplayingDoneMessage(false), 3000);
+ setDisplayingDoneMessageTimer(timer)
+ }catch(error){
+ setDisplayingDoneMessage(false)
+ //alert(error)
+ //console.log(error)
+ let substituteText = `Error: ${error.message}
+
+Try something like:
+[
+ {
+ "name": "Some element. The name field is necessary",
+ "url": "http://www.example.com",
+ "somethirdfield": "a"
+ },
+ {
+ "name": "Another element",
+ "url": "http://www.example1.com",
+ "somethirdfield": "b"
+ },
+ {
+ "name": "Another element",
+ "url": "http://www.example2.com",
+ "isReferenceValue": true,
+ "somethirdfield": "c"
+ }
+]
+
+Your old input was: ${value}`
+ setValue(substituteText)
+ }
+
+
+ }
+ return (
+
+ );
+}
\ No newline at end of file
diff --git a/pages/index.js b/pages/index.js
index b5d965f..f3add9a 100644
--- a/pages/index.js
+++ b/pages/index.js
@@ -2,11 +2,12 @@ import Head from 'next/head'
import React, { useState } from "react";
import fs from 'fs';
import path from 'path';
-import {DrawGraph} from '../lib/labeledgraph';
-import { SliderElement } from "../lib/slider";
+import {DrawGraph, removeOldSvg} from '../lib/labeledgraph';
+import { SliderElement, SubmitSliderButton } from "../lib/slider";
import {DisplayElement} from '../lib/displayElement'
import {DisplayAsMarkdown} from '../lib/displayAsMarkdown'
import {CreateTableWithDistances} from '../lib/findPaths'
+import {TextAreaForJson} from "../lib/textAreaForJson"
// Utilities
@@ -45,7 +46,7 @@ let displayFunctionSlider = (value) => {
};
-let nicelyFormatLinks = (quantitativeComparisons , list) => quantitativeComparisons.map(([element1, element2, distance]) => ({source: list.indexOf(element1), target: list.indexOf(element2), distance: distance}))
+let nicelyFormatLinks = (quantitativeComparisons , listOfElements) => quantitativeComparisons.map(([element1, element2, distance]) => ({source: listOfElements[element1].name, target: listOfElements[element2].name, distance: distance}))
/* React components */
// fs can only be used here.
@@ -58,25 +59,66 @@ export async function getStaticProps() {
//console.log("metaforecasts", metaforecasts)
return {
props: {
- listOfPosts,
+ listOfElementsDefault: listOfPosts,
},
};
}
// Main react component
-export default function Home({listOfPosts}) {
+export default function Home({listOfElementsDefault}) {
// State
- let list = increasingList(listOfPosts.length) // [0,1,2,3,4]
- listOfPosts = listOfPosts.map((element, i) => ({...element, id: i}))
+ let initialListOfElements = listOfElementsDefault.map((element, i) => ({...element, id: i}))
+ let initialPosList = increasingList(listOfElementsDefault.length) // [0,1,2,3,4]
+ //let listOfElements = listOfElementsDefault.map((element, i) => ({...element, id: i}))
+ //let list = increasingList(listOfElementsDefault.length) // [0,1,2,3,4]
- const [toComparePair, setToComparePair] = useState([list[list.length-2], list[list.length-1]])
- const [sliderValue, setSliderValue] = useState(0)
+ //let initialComparePair = [list[list.length-2], list[list.length-1]]
+ let initialComparePair = [initialPosList[initialPosList.length-2], initialPosList[initialPosList .length-1]]
+ let initialSliderValue = 0
+ let initialBinaryComparisons = []
+ let initialQuantitativeComparisons = []
+ let initialIsListOdered = false
+ let initialOrderedList = []
+ let initialShowAdvancedOptions = false
+ let initialShowComparisons = false
+ let initialShowChangeDataSet = false
- const [binaryComparisons, setBinaryComparisons] = useState([])
- const [quantitativeComparisons, setQuantitativeComparisons] = useState([]) // More expressive, but more laborious to search through. For the ordering step, I only manipulate the binaryComparisons.
+ const [listOfElements, setListOfElements] = useState(initialListOfElements)
+ const [posList, setPosList] = useState(initialPosList)
+ //const posList = initialPosList
+ // let listOfElements = initialListOfElements
- const [isListOrdered, setIsListOrdered] = useState(false)
- const [orderedList, setOrderedList] = useState([])
+ const [toComparePair, setToComparePair] = useState(initialComparePair)
+ const [sliderValue, setSliderValue] = useState(initialSliderValue)
+ const [binaryComparisons, setBinaryComparisons] = useState(initialBinaryComparisons)
+ const [quantitativeComparisons, setQuantitativeComparisons] = useState(initialQuantitativeComparisons) // More expressive, but more laborious to search through. For the ordering step, I only manipulate the binaryComparisons.
+
+ const [isListOrdered, setIsListOrdered] = useState(initialIsListOdered)
+ const [orderedList, setOrderedList] = useState(initialOrderedList)
+
+ let [showAdvancedOptions, changeShowAdvanceOptions] = useState(initialShowAdvancedOptions);
+ let [showComparisons, changeShowComparisons] = useState(initialShowComparisons);
+ let [showChangeDataSet, changeshowChangeDataSet] = useState(initialShowChangeDataSet);
+
+ let restart = (posList) => {
+ setToComparePair([posList[posList.length-2], posList[posList.length-1]])
+ setSliderValue(initialSliderValue)
+ setBinaryComparisons(initialBinaryComparisons)
+ setQuantitativeComparisons(initialQuantitativeComparisons)
+ setIsListOrdered(initialIsListOdered)
+ setOrderedList(initialOrderedList)
+ removeOldSvg()
+ }
+
+ let changeDataSet = (listOfElementsNew) => {
+ listOfElementsNew =
+ listOfElementsNew.map((element, i) => ({...element, id: i}))
+ let newPosList = increasingList(listOfElementsNew.length)
+ setListOfElements(listOfElementsNew)
+ setPosList(increasingList(listOfElementsNew.length))
+ setToComparePair([newPosList[newPosList.length-2], newPosList[newPosList.length-1]])
+ restart(newPosList)
+ }
// Manipulations
let compareTwoElements = (newBinaryComparisons, element1, element2) => {
@@ -113,23 +155,23 @@ export default function Home({listOfPosts}) {
return [...sortedArr, ...left, ...right]; // if they don't have the same size, the remaining ones will be greater than the ones before
}
- function mergeSort(arr, newBinaryComparisons) {
- if(arr == "No comparison found; unable to proceed"){
+ function mergeSort({array, comparisons}) {
+ if(array == "No comparison found; unable to proceed"){
return "No comparison found; unable to proceed"
}
- const half = arr.length / 2;
+ const half = array.length / 2;
// the base case is array length <=1
- if (arr.length <= 1) {
- return arr;
+ if (array.length <= 1) {
+ return array;
}
- const left = arr.splice(0, half); // the first half of the array
- const right = arr;
- let orderedFirstHalf = mergeSort(left, newBinaryComparisons)
- let orderedSecondHalf = mergeSort(right, newBinaryComparisons)
+ const left = array.slice(0, half); // the first half of the array
+ const right = array.slice(half, array.length) // Note that splice is destructive.
+ let orderedFirstHalf = mergeSort({array:left, comparisons})
+ let orderedSecondHalf = mergeSort({array:right, comparisons})
if(orderedFirstHalf != "No comparison found; unable to proceed" && orderedSecondHalf != "No comparison found; unable to proceed"){
- let result = merge(newBinaryComparisons, orderedFirstHalf, orderedSecondHalf);
+ let result = merge(comparisons, orderedFirstHalf, orderedSecondHalf);
return result
}else{
return "No comparison found; unable to proceed"
@@ -137,7 +179,7 @@ export default function Home({listOfPosts}) {
}
- let nextStepSimple = (binaryComparisons, element1, element2) => {
+ let nextStepSimple = (posList, binaryComparisons, element1, element2) => {
//console.log("Binary comparisons: ")
//console.log(JSON.stringify(binaryComparisons, null, 4));
@@ -147,22 +189,28 @@ export default function Home({listOfPosts}) {
//console.log(JSON.stringify(newBinaryComparisons, null, 4));
setBinaryComparisons(newBinaryComparisons)
- let result = mergeSort(list, newBinaryComparisons)
+ let result = mergeSort({array: posList, comparisons: newBinaryComparisons})
//console.log(result)
if(result != "No comparison found; unable to proceed" && checkIfListIsOrdered(result, newBinaryComparisons)){
- //console.log(result)
+ // console.log(`isListOrdered: ${isListOrdered}`)
+ console.log("poslist@nextStepSimple")
+ console.log(posList)
+ console.log("result@nextStepSimple")
+ console.log(result)
+
setIsListOrdered(true)
setOrderedList(result)
}
}
- let nextStepSlider = (binaryComparisons, sliderValue, element1, element2) => {
+ let nextStepSlider = ({posList, binaryComparisons, sliderValue, element1, element2}) => {
if(sliderValue < 0){
sliderValue = -sliderValue;
[element1,element2] = [element2,element1]
}
-
- nextStepSimple(binaryComparisons, element1, element2)
+ console.log(`posList@nextStepSlider:`)
+ console.log(posList)
+ nextStepSimple(posList, binaryComparisons, element1, element2)
let newQuantitativeComparison = [element1, element2, simplifySliderValue(sliderValue)]
let newQuantitativeComparisons = [...quantitativeComparisons, newQuantitativeComparison]
@@ -173,25 +221,27 @@ export default function Home({listOfPosts}) {
// Html
return (
-
-
-
Utility Function Extractor
-
-
+
+
+
+
Utility Function Extractor
+
+
+
-
+
Utility Function Extractor
-
+
nextStep(binaryComparisons, toComparePair[0], toComparePair[1])}
>
-
+
@@ -200,7 +250,7 @@ export default function Home({listOfPosts}) {
//onClick={() => nextStep(binaryComparisons, toComparePair[1], toComparePair[0])}
>
-
+
@@ -213,38 +263,88 @@ export default function Home({listOfPosts}) {
displayFunction={displayFunctionSlider}
/>
-
+
+
listOfPosts[i])}
+ orderedList={orderedList}
+ listOfElements={listOfElements}
links={buildLinks(quantitativeComparisons)}>
-
+
listOfPosts[i])}
+ orderedList={orderedList}
+ listOfElements={listOfElements}
links={buildLinks(quantitativeComparisons)}
>
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {/*
+
listOfElements[i]), null, 4)}>
+ */}
+
Comparisons
+
+
+
+
-
-
listOfPosts[i]), null, 4)}>
-
{/*
{`Binary comparisons: ${JSON.stringify(binaryComparisons, null, 4)}`}
{`Quantitative comparisons: ${JSON.stringify(quantitativeComparisons, null, 4)}`}
*/}
-
+
+
+
)
}