gazeplotter
Version:
Gazeplotter is a Svelte application for visualizing eye-tracking data.
112 lines (111 loc) • 4.03 kB
JavaScript
import { get, writable } from 'svelte/store';
const getUniqueScarfPlotId = () => {
return Date.now() + Math.floor(Math.random() * 1000);
};
const returnDefaultScarfPlotState = (isPrerendered = false) => {
return [
{
scarfPlotId: isPrerendered ? 0 : getUniqueScarfPlotId(),
stimulusId: 0,
groupId: -1,
zoomLevel: 0,
timeline: 'absolute',
absoluteGeneralLastVal: 0,
absoluteStimuliLastVal: [],
ordinalGeneralLastVal: 0,
ordinalStimuliLastVal: [],
},
];
};
export const scarfPlotStates = writable(returnDefaultScarfPlotState(true));
export const setDefaultScarfPlotState = () => {
scarfPlotStates.set(returnDefaultScarfPlotState());
};
export const removeScarfPlotState = (scarfPlotId) => {
scarfPlotStates.update(prev => prev.filter(state => state.scarfPlotId !== scarfPlotId));
};
export const getScarfPlotState = (scarfStates, scarfPlotId) => {
return scarfStates.find(state => state.scarfPlotId === scarfPlotId);
};
const getDefinedScarfPlotState = (scarfStates, scarfPlotId) => {
const scarfPlotState = getScarfPlotState(scarfStates, scarfPlotId);
if (scarfPlotState === undefined)
throw new Error(`Scarf plot state with id ${scarfPlotId} not found`);
return scarfPlotState;
};
export const getStimulusId = (scarfPlotId) => {
const scarfStates = get(scarfPlotStates);
const scarfPlotState = getScarfPlotState(scarfStates, scarfPlotId);
if (scarfPlotState === undefined)
throw new Error(`Scarf plot state with id ${scarfPlotId} not found`);
return scarfPlotState.stimulusId;
};
export const getStimulusLastValue = (scarfPlotId, stimulusId, type) => {
const scarfStates = get(scarfPlotStates);
const scarfPlotState = getDefinedScarfPlotState(scarfStates, scarfPlotId);
if (type === 'absolute') {
const val = scarfPlotState.absoluteStimuliLastVal[stimulusId];
if (!Number.isNaN(val))
return val;
return 0;
}
else {
const val = scarfPlotState.ordinalStimuliLastVal[stimulusId];
if (!Number.isNaN(val))
return val;
return 0;
}
};
export const updateTimeline = (scarfPlotId, timeline) => {
scarfPlotStates.update(prev => {
const newState = getDefinedScarfPlotState(prev, scarfPlotId);
newState.timeline = timeline;
return prev;
});
};
export const updateStimulusId = (scarfPlotId, stimulusId) => {
scarfPlotStates.update(prev => {
const newState = getDefinedScarfPlotState(prev, scarfPlotId);
newState.stimulusId = stimulusId;
return prev;
});
};
export const updateZoom = (scarfPlotId, zoom) => {
scarfPlotStates.update(prev => {
const newState = getDefinedScarfPlotState(prev, scarfPlotId);
newState.zoomLevel = zoom;
return prev;
});
};
export const updateStimulusLastValue = (scarfPlotId, stimulusId, lastValue, type) => {
scarfPlotStates.update(prev => {
const newState = getDefinedScarfPlotState(prev, scarfPlotId);
if (type === 'absolute') {
newState.absoluteStimuliLastVal[stimulusId] = lastValue;
}
if (type === 'ordinal') {
newState.ordinalStimuliLastVal[stimulusId] = lastValue;
}
return prev;
});
};
export const duplicateScarfPlotState = (scarfPlotId) => {
scarfPlotStates.update(prev => {
const scarfPlotState = getDefinedScarfPlotState(prev, scarfPlotId);
prev.push({
...scarfPlotState,
scarfPlotId: getUniqueScarfPlotId(),
});
return prev;
});
};
export const updateGroup = (scarfPlotId, groupId) => {
scarfPlotStates.update(prev => {
const newState = getDefinedScarfPlotState(prev, scarfPlotId);
newState.groupId = groupId;
return prev;
});
};
export const redrawScarfPlotStates = () => {
scarfPlotStates.update(currentStates => [...currentStates]);
};