import { JsPsych } from "../../JsPsych" ; import { DataCollection } from "./DataCollection" ; import { getQueryString } from "./utils" ; export class JsPsychData { private allData : DataCollection ; private interactionData : DataCollection ; private dataProperties = { } ; private query_string ; constructor ( private jsPsych ) { this . reset ( ) ; } reset ( ) { this . allData = new DataCollection ( ) ; this . interactionData = new DataCollection ( ) ; } get ( ) { return this . allData ; } getInteractionData ( ) { return this . interactionData ; } write ( data_object ) { const progress = this . jsPsych . getProgress ( ) ; const trial = this . jsPsych . getCurrentTrial ( ) ; const default_data = { trial_type : trial . type . info . name , trial_index : progress . current_trial_global , time_elapsed : this . jsPsych . getTotalTime ( ) , internal_node_id : this . jsPsych . getCurrentTimelineNodeID ( ) , } ; this . allData . push ( { ... data_object , ... trial . data , ... default_data , ... this . dataProperties , } ) ; } addProperties ( properties ) { this . allData . addToAll ( properties ) ; this . dataProperties = Object . assign ( { } , this . dataProperties , properties ) ; } addDataToLastTrial ( data ) { this . allData . addToLast ( data ) ; } getDataByTimelineNode ( node_id ) { return this . allData . filterCustom ( ( x ) => x . internal_node_id . slice ( 0 , node_id . length ) === node_id ) ; } getLastTrialData ( ) { return this . allData . top ( ) ; } getLastTimelineData ( ) { const lasttrial = this . getLastTrialData ( ) ; const node_id = lasttrial . select ( "internal_node_id" ) . values [ 0 ] ; if ( typeof node_id === "undefined" ) { return new DataCollection ( ) ; } else { const parent_node_id = node_id . substr ( 0 , node_id . lastIndexOf ( "-" ) ) ; const lastnodedata = this . getDataByTimelineNode ( parent_node_id ) ; return lastnodedata ; } } displayData ( format = "json" ) { format = format . toLowerCase ( ) ; if ( format != "json" && format != "csv" ) { console . log ( "Invalid format declared for displayData function. Using json as default." ) ; format = "json" ; } const data_string = format === "json" ? this . allData . json ( true ) : this . allData . csv ( ) ; const display_element = this . jsPsych . getDisplayElement ( ) ; display_element . innerHTML = '<pre id="jspsych-data-display"></pre>' ; document . getElementById ( "jspsych-data-display" ) . textContent = data_string ; } urlVariables ( ) { if ( typeof this . query_string == "undefined" ) { this . query_string = getQueryString ( ) ; } return this . query_string ; } getURLVariable ( whichvar ) { return this . urlVariables ( ) [ whichvar ] ; } createInteractionListeners ( ) { window . addEventListener ( "blur" , ( ) => { const data = { event : "blur" , trial : this . jsPsych . getProgress ( ) . current_trial_global , time : this . jsPsych . getTotalTime ( ) , } ; this . interactionData . push ( data ) ; this . jsPsych . getInitSettings ( ) . on_interaction_data_update ( data ) ; } ) ; window . addEventListener ( "focus" , ( ) => { const data = { event : "focus" , trial : this . jsPsych . getProgress ( ) . current_trial_global , time : this . jsPsych . getTotalTime ( ) , } ; this . interactionData . push ( data ) ; this . jsPsych . getInitSettings ( ) . on_interaction_data_update ( data ) ; } ) ; const fullscreenchange = ( ) => { const data = { event : document . isFullScreen || document . webkitIsFullScreen || document . mozIsFullScreen || document . fullscreenElement ? "fullscreenenter" : "fullscreenexit" , trial : this . jsPsych . getProgress ( ) . current_trial_global , time : this . jsPsych . getTotalTime ( ) , } ; this . interactionData . push ( data ) ; this . jsPsych . getInitSettings ( ) . on_interaction_data_update ( data ) ; } ; document . addEventListener ( "fullscreenchange" , fullscreenchange ) ; document . addEventListener ( "mozfullscreenchange" , fullscreenchange ) ; document . addEventListener ( "webkitfullscreenchange" , fullscreenchange ) ; } _customInsert ( data ) { this . allData = new DataCollection ( data ) ; } _fullreset ( ) { this . reset ( ) ; this . dataProperties = { } ; } }