-
Notifications
You must be signed in to change notification settings - Fork 0
/
subj.js
133 lines (122 loc) · 4.63 KB
/
subj.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
class Subject {
constructor(options = {}) {
Object.assign(this, {
num: 'pre-post',
subjNumScript: '',
subjNumCallback: () => void 0,
savingScript: '',
subjNumFile: '',
visitFile: 'visit.txt',
attritionFile: 'attrition.txt',
subjFile: 'subj.txt',
savingDir: 'data/testing',
condition: false,
conditionList: [''],
titles: [''],
viewportMinW: 0,
viewportMinH: 0
}, options);
if (this.num == 'pre-post') {
this.obtainSubjNum();
}
this.data = array_to_formatted_string(this.titles);
this.dateObj = new Date();
this.date = format_date(this.dateObj, 'UTC', '-', true);
this.startTime = format_time(this.dateObj, 'UTC', ':', true);
this.hiddenCount = 0;
this.hiddenDurations = [];
}
obtainSubjNum() {
let that = this;
function subj_num_update_succeeded(number) {
that.num = number;
that.subjNumCallback();
if (that.condition == 'auto') {
that.assignCondition();
}
}
function subj_num_update_failed() {
that.num = -999;
that.subjNumCallback();
}
post_data(this.subjNumScript, { 'directory_path': this.savingDir, 'file_name': this.subjNumFile }, subj_num_update_succeeded, subj_num_update_failed);
}
assignCondition() {
let that = this;
const check_subj_num = function(){
if(that.num != 'pre-post'){
clearInterval(interval_id);
that.condition = that.conditionList[(that.num-1) % that.conditionList.length];
that.conditionAssigned = true;
}
};
let interval_id = setInterval(check_subj_num, 10);
}
saveVisit() {
let data = 'subjNum\tstartDate\tstartTime\tid\tinView\tviewportW\tviewportH\n';
this.viewport = this.viewportSize;
this.inView = this.viewport['inView'];
this.viewportW = this.viewport['w'];
this.viewportH = this.viewport['h'];
let dataList = [this.num, this.date, this.startTime, this.id, this.inView, this.viewportW, this.viewportH];
data += array_to_formatted_string(dataList);
post_data(this.savingScript, { 'directory_path': this.savingDir, 'file_name': this.visitFile, 'data': data });
}
getID(get_variable) {
let id = get_parameters(get_variable, null);
this.validID = this.checkID(id);
if (this.validID) {
return id;
} else {
return null;
}
}
checkID(id) {
let valid_id = (id !== null);
if (valid_id) {
id = id.replace(/\s+/g, '');
valid_id = (id !== '');
}
return valid_id;
}
get viewportSize() {
let w = $(window).width();
let h = $(window).height();
let inView = (w >= this.viewportMinW) && (h >= this.viewportMinH);
return { 'h': h, 'w': w, 'inView': inView };
}
saveAttrition() {
let data = 'subjNum\tstartDate\tstartTime\tid\tinView\tviewportW\tviewportH\n';
this.viewport = this.viewportSize;
this.inView = this.viewport['inView'];
this.viewportW = this.viewport['w'];
this.viewportH = this.viewport['h'];
let dataList = [this.num, this.date, this.startTime, this.id, this.inView, this.viewportW, this.viewportH];
data += array_to_formatted_string(dataList);
post_data(this.savingScript, { 'directory_path': this.savingDir, 'file_name': this.attritionFile, 'data': data });
}
submitAnswers() {
let endTimeObj = new Date();
this.endTime = format_time(endTimeObj, 'UTC', ':', true);
this.duration = (endTimeObj - this.dateObj) / 60000;
let dataList = array_from_attribute_names(this, this.titles);
this.data += array_to_formatted_string(dataList);
post_data(this.savingScript, { 'directory_path': this.savingDir, 'file_name': this.subjFile, 'data': this.data });
}
detectVisibilityStart() {
let that = this;
document.addEventListener('visibilitychange', that.handleVisibilityChange);
}
detectVisibilityEnd() {
let that = this;
document.removeEventListener('visibilitychange', that.handleVisibilityChange);
}
handleVisibilityChange = () => {
if (document.hidden) {
this.hiddenCount += 1;
this.hiddenStartTime = Date.now();
} else {
this.hiddenDurations.push((Date.now() - this.hiddenStartTime)/1000);
}
}
}