Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

method to set custom click listener / load data from an external source #215

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 50 additions & 3 deletions src/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var paused = false;
//registered callback for loop
var nopCallback = function(data, time) {};
var callback = nopCallback;
var clickHandler = null;

//Types that regression systems should handle
//Describes the source of data so that regression systems may ignore or handle differently the various generating events
Expand Down Expand Up @@ -376,6 +377,11 @@ var clickListener = async function(event) {
// // Debug line
// console.log('Model size: ' + JSON.stringify(await localforage.getItem(localstorageDataLabel)).length / 1000000 + 'MB');
}

// if defined, call user supplied handler passing in the event object, data and settings
if (typeof clickHandler === 'function') {
clickHandler(event, {data: getStoreData(), settings})
}
};

/**
Expand Down Expand Up @@ -440,18 +446,25 @@ async function loadGlobalData() {
console.log("loaded stored data into regression model");
}

/**
* Gets data for storage
*/
function getStoreData() {
return regs[0].getData() || data; // Array
//TODO data should probably be stored in webgazer object instead of each regression model
// -> requires duplication of data, but is likely easier on regression model implementors
}

/**
* Adds data to localforage
*/
async function setGlobalData() {
// Grab data from regression model
var storeData = regs[0].getData() || data; // Array
var storeData = getStoreData();

// Store data into localforage
localforage.setItem(localstorageSettingsLabel, settings) // [20200605 XK] is 'settings' ever being used?
localforage.setItem(localstorageDataLabel, storeData);
//TODO data should probably be stored in webgazer object instead of each regression model
// -> requires duplication of data, but is likely easier on regression model implementors
}

/**
Expand Down Expand Up @@ -1048,6 +1061,20 @@ webgazer.addRegression = function(name) {
return webgazer;
};

/**
* Load data from an external storage
* @param {String} data - Data to load into regression model(s)
* @return {webgazer} this
*/
webgazer.loadData = function(data) {
data = data;

// Load data into regression model(s)
for (var reg in regs) {
regs[reg].setData(data);
}
};

/**
* Sets a callback to be executed on every gaze event (currently all time steps)
* @param {function} listener - The callback function to call (it must be like function(data, elapsedTime))
Expand All @@ -1058,6 +1085,16 @@ webgazer.setGazeListener = function(listener) {
return webgazer;
};

/**
* Sets a callback to be executed on every click, in addition to the default handler
* @param {function} listener - The callback function to call (passed click event obj)
* @return {webgazer} this
*/
webgazer.setClickListener = function(listener) {
clickHandler = listener;
return webgazer;
};

/**
* Removes the callback set by setGazeListener
* @return {webgazer} this
Expand All @@ -1067,6 +1104,16 @@ webgazer.clearGazeListener = function() {
return webgazer;
};

/**
* Removes the callback set by setClickListener
* @param {function} listener - The callback function to call (passed click event obj)
* @return {webgazer} this
*/
webgazer.clearClickListener = function(listener) {
clickHandler = null;
return webgazer;
};

/**
* Set the video element canvas; useful if you want to run WebGazer on your own canvas (e.g., on any random image).
* @return The current video element canvas
Expand Down
53 changes: 50 additions & 3 deletions www/webgazer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138529,6 +138529,7 @@ var paused = false;
//registered callback for loop
var nopCallback = function(data, time) {};
var callback = nopCallback;
var clickHandler = null;

//Types that regression systems should handle
//Describes the source of data so that regression systems may ignore or handle differently the various generating events
Expand Down Expand Up @@ -138856,6 +138857,11 @@ var clickListener = async function(event) {
// // Debug line
// console.log('Model size: ' + JSON.stringify(await localforage.getItem(localstorageDataLabel)).length / 1000000 + 'MB');
}

// if defined, call user supplied handler passing in the event object, data and settings
if (typeof clickHandler === 'function') {
clickHandler(event, {data: getStoreData(), settings})
}
};

/**
Expand Down Expand Up @@ -138920,18 +138926,25 @@ async function loadGlobalData() {
console.log("loaded stored data into regression model");
}

/**
* Gets data for storage
*/
function getStoreData() {
return regs[0].getData() || data; // Array
//TODO data should probably be stored in webgazer object instead of each regression model
// -> requires duplication of data, but is likely easier on regression model implementors
}

/**
* Adds data to localforage
*/
async function setGlobalData() {
// Grab data from regression model
var storeData = regs[0].getData() || data; // Array
var storeData = getStoreData();

// Store data into localforage
localforage.setItem(localstorageSettingsLabel, settings) // [20200605 XK] is 'settings' ever being used?
localforage.setItem(localstorageDataLabel, storeData);
//TODO data should probably be stored in webgazer object instead of each regression model
// -> requires duplication of data, but is likely easier on regression model implementors
}

/**
Expand Down Expand Up @@ -139528,6 +139541,20 @@ src_webgazer.addRegression = function(name) {
return src_webgazer;
};

/**
* Load data from an external storage
* @param {String} data - Data to load into regression model(s)
* @return {webgazer} this
*/
src_webgazer.loadData = function(data) {
data = data;

// Load data into regression model(s)
for (var reg in regs) {
regs[reg].setData(data);
}
};

/**
* Sets a callback to be executed on every gaze event (currently all time steps)
* @param {function} listener - The callback function to call (it must be like function(data, elapsedTime))
Expand All @@ -139538,6 +139565,16 @@ src_webgazer.setGazeListener = function(listener) {
return src_webgazer;
};

/**
* Sets a callback to be executed on every click, in addition to the default handler
* @param {function} listener - The callback function to call (passed click event obj)
* @return {webgazer} this
*/
src_webgazer.setClickListener = function(listener) {
clickHandler = listener;
return src_webgazer;
};

/**
* Removes the callback set by setGazeListener
* @return {webgazer} this
Expand All @@ -139547,6 +139584,16 @@ src_webgazer.clearGazeListener = function() {
return src_webgazer;
};

/**
* Removes the callback set by setClickListener
* @param {function} listener - The callback function to call (passed click event obj)
* @return {webgazer} this
*/
src_webgazer.clearClickListener = function(listener) {
clickHandler = null;
return src_webgazer;
};

/**
* Set the video element canvas; useful if you want to run WebGazer on your own canvas (e.g., on any random image).
* @return The current video element canvas
Expand Down