-
Notifications
You must be signed in to change notification settings - Fork 0
/
primate.js
261 lines (241 loc) · 8.69 KB
/
primate.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// primate.js - Not just Gorilla
// If running in Gorilla, primate.js provides verbose wrappers to the
// Gorilla API. Otherwise, it provides alternative functions that do
// the same things outside of gorilla. This means you can run the same
// code with and without Gorilla.
// See https://gorilla.sc/support/api/gorilla for documentation on the
// original API. Note that primate.finish() does not work in the same
// way as gorilla.finish(). See the code below for details.
const VERSION = '0.0.2';
const primate = (function(){
function is_gorilla(){
// To test whether we're running on Gorilla, we just test if
// window['gorillaClient'] exists. There's probably a better
// way to do this.
return window['gorillaClient'] != undefined;
}
window['mute_primate'] = false;
function mute() {window['mute_primate'] = true;};
function unmute() {window['mute_primate'] = false;};
function say(x, monkey=true){
if(window['mute_primate']==false){
// Cuter logging - 🦍 🙉 🐒
if(monkey){
// Speaker is a gorilla if running on Gorilla.
// Otherwise, it's a monkey. Fun, no?
let icon = is_gorilla() ? '🦍: ' : '🙉: ';
console.log(icon + x);
} else {
console.log(x);
}
}
}
// Start of core functions
function ready(cb){
say(`Setting '${cb.name}()' to run once page is ready.`);
if(is_gorilla()){
gorilla.ready(function(){
// If on Gorilla, load the 'body' template now.
primate.populate('#gorilla', 'body', {});
cb();
});
} else {
if (document.readyState === 'complete') {
// We're already ready
cb();
}
else {
$(document).ready(cb);
};
}
}
function manipulation(string, def){
// We can't retrieve anything, so just use the default.
say(`Getting value of manipulation '${string}'`);
let val;
if(is_gorilla()){
val = gorilla.manipulation(string, def);
say(`${string} = ${val}`);
if(val == def){
say('This is the default value. Coincidence, or bug?');
}
} else{
say(`Using default: ${string} = ${def}`);
val = def;
}
return val;
}
// For store and retrieve, use localStorage.
// Since localStorage saves everything as a string,
// save a supplementary entry indicating what the type should be,
// and use _to_type() to cooerce the value as appropriate.
function _to_type(value, type_string){
if(type_string=='object') { return JSON.parse(value); }
if(type_string=='number') { return Number(value); }
if(type_string=='string') { return value; }
if(type_string=='boolean') { return Boolean(value); }
}
function store(key, value, global=true){
say(`Storing ${key} = ${value} (global = ${global})`);
if(is_gorilla()){
gorilla.store(key, value, global);
} else {
say('...falling back to localStorage.');
localStorage[key] = value;
localStorage[key + '_type'] = typeof(value);
}
}
function storeMany(values, global){
for(k in values){
store(k, values[k]);
}
}
function retrieve(key, def=undefined, global=true){
say(`Retrieving value of ${key} (global = ${global})`);
let val;
if(is_gorilla()){
val = gorilla.retrieve(key, def, global);
} else {
say('...falling back to localStorage');
let type_string = localStorage[key + '_type'];
val = localStorage[key];
val = _to_type(val, type_string);
}
say(`${key} = ${val}`);
if(typeof val == 'undefined') {
say(`...Replacing with default value: ${def}`);
val = def;
}
return val;
}
function _deep_copy(object){
// (Not exported)
return JSON.parse(JSON.stringify(object));
}
function metric(results){
let copied_results = _deep_copy(results);
say('Sending these metrics to the server:');
say(results);
if(is_gorilla()){
gorilla.metric(copied_results);
} else {
// If page is a local file, do nothing
if(window.location.protocol == 'file:'){
say('Local file: Nowhere to send data');
} else {
$.ajax({
type: 'POST',
url: 'log.php',
data: JSON.stringify(copied_results),
success: function(res) { say(res); }
});
}
}
}
function stimuliURL(name){
say(`Getting stimulus url for '${name}'`);
if(is_gorilla()){
let url = gorilla.stimuliURL(name);
say('Found ' + url);
return url;
} else {
say(`...falling back to 'stimuli/${name}`);
return 'stimuli/' + name;
}
}
function fy_shuffle(array) {
// Fisher-Yates algorithm in pure JavaScript, from
// https://stackoverflow.com/a/2450976
// This code is covered by a Creative Commons license
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function shuffle(x){
// say('Shuffling...'); // Probably not necessary
if(is_gorilla()){
gorilla.shuffle(x);
} else {
return fy_shuffle(x);
}
}
function populate(element, template, content={}){
// Note that if you're running on Gorilla,
// the primate.ready() function already calls
// > gorilla.populate('#gorilla', 'body', {});
// when the page loads.
if(is_gorilla()){
say(`Populating ${element} with template '${template}`);
say(`(Contents: ${content})`);
return gorilla.populate(element, template, content);
} else {
say(`Skipping call to populate()`);
}
}
function populateAndLoad(element, template, content, cb){
if(is_gorilla()){
say(`Populating ${element} with template '${template}`);
say(`(Contents: ${content})`);
say(`Callback: ${cb.name}`);
return gorilla.populateAndLoad(element, template, content);
} else {
say(`Skipping call to populateAndLoad()`);
}
}
function finish(URL, redirect_gorilla=false){
// primate.finish() differs from gorilla.finish().
// Calling `primate.finish()` without any arguments will raise
// a pop-up window when running off Gorilla, and will redirect
// to the next node on Gorilla. This is the default behaviour
// of `gorilla.finish()`.
// Calling `primate.finish('example.com')` will redirect the
// participant to example.com if the experiment is running off
// Gorilla, but will send them to the next node in Gorilla.
// Calling `primate.finish('example.com', true)` will redirect
// to example.com even if running on Gorilla. You probably
// don't want to do this!
say('End of experiment!');
if(is_gorilla()){
if(redirect_gorilla){
say(`Redirecting to ${URL}`);
gorilla.finish(URL);
} else {
gorilla.finish();
}
} else {
if(typeof URL !== 'undefined') {
say(`Redirecting to ${URL}`);
window.location.href = URL;
} else {
alert('End of Experiment!');
}
}
}
let exports = {
mute: mute, unmute: unmute,
say: say,
is_gorilla: is_gorilla,
ready : ready,
manipulation : manipulation,
retrieve : retrieve,
store : store,
storeMany : storeMany,
metric : metric,
stimuliURL : stimuliURL,
shuffle : shuffle,
populate : populate,
populateAndLoad : populateAndLoad,
finish: finish,
version: VERSION
};
return exports;
})();