-
Notifications
You must be signed in to change notification settings - Fork 0
/
otree-front-ext.js
111 lines (101 loc) · 2.89 KB
/
otree-front-ext.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
/*
// This file is originally a part of https://github.com/qwiglydee/otree-advanced-demos
// SPDX-FileCopyrightText: © 2024 Maxim Vasilyev <[email protected]>
// SPDX-License-Identifier: MIT
*/
/**
* Wrappers for lifecycle handlers.
*
* @example
* onLoad(startGame);
* onSubmit(doSomething);
*
* function startGame() { ... }
* function doSomething() { ... }
*/
function onLoad(handler) {
ot.onEvent('loaded', handler);
}
function onSubmit(handler) {
ot.onEvent('submitted', handler);
}
/**
* Warapper for input handler.
*
* @example
* onInput('inputname', inputSomething);
* onInputs(inputAll);
*
* function inputSomething(value) { ... }
* function inputAll(name, value) { ... }
*/
function onInputs(handler) {
ot.onEvent('input', (e) => handler(e.detail.name, e.detail.value));
}
function onInput(name, handler) {
ot.onEvent('input', name, (e) => handler(e.detail.value));
}
/**
* Warapper for timer handlers.
*
* @example
* onTimer(name, handleTimer);
* onTimers(handleTimers));
*
* function handleTimer(elapsed, counter) { ... }
* function handleTimers(name, elapsed, counter) { ... }
*/
function onTimers(handler) {
ot.onEvent('timer', (e) => handler(e.detail.name, e.detail.elapsed, e.detail.count));
}
function onTimer(name, handler) {
ot.onEvent('timer', name, (e) => handler(e.detail.elapsed, e.detail.count));
}
/**
* Wrapper for built-in oTree page timer.
*
* @example
* onCountdown(handleCountdown);
*
* function handleCountdown(remaining_seconds) { ... }
*/
function onCountdown(handler) {
ot.onEvent('countdown', (e) => handler(e.detail.remaining))
}
if (document.querySelector(".otree-timer")) {
$(".otree-timer__time-left").on("update.countdown", function (e) {
ot.triggerEvent('countdown', { remaining: e.offset.totalSeconds });
});
}
/**
* Get a property from actual stylesheet
*
* @example
* const FADEOUT_TIME = getStyleProp("--ot-fade-out-time");
*
* @param {string} propname property name to get from style
* @param {string} [selector] selector for an element, default is to ge global style
*/
function getStyleProp(propname, selector) {
let elem = selector ? document.querySelector(selector) : document.body;
return window.getComputedStyle(elem).getPropertyValue(propname);
}
/**
* Switch display subsections like <div id="secttion-subsection">
*
* It shows one specified subsection and hides all sibling subsections with the same prefix.
*
* @example
* <div id="feedback-success">...</div>
* <div id="feedback-failure">...</div>
* <div id="feedback-timeout">...</div>
*
* switchDisplays("feedback-failure");
*/
function switchDisplays(selector) {
let split = selector.lastIndexOf("-");
if(split == -1 || selector.endsWith('*')) throw Error("switchDisplays expects id like `prefix-subsection`");
let pattern = selector.slice(0, split+1) + "*";
ot.hideDisplays(pattern);
ot.showDisplays(selector);
}