diff --git a/src/index.mjs b/src/index.mjs index d652233ee..1c312fea2 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -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 @@ -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}) + } }; /** @@ -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 } /** @@ -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)) @@ -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 @@ -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 diff --git a/www/webgazer.js b/www/webgazer.js index b341a25bf..7b623e082 100644 --- a/www/webgazer.js +++ b/www/webgazer.js @@ -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 @@ -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}) + } }; /** @@ -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 } /** @@ -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)) @@ -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 @@ -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