-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
84 lines (75 loc) · 2.81 KB
/
index.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
/**
* introduces quicker image changes after interactions and slows down with time
* @param {AddonBase inherited} interface object to register and send events
*/
const functionFlexIntervals = (interface) => {
var config;
// show the image for at least three seconds and
// calculate the real value as soon as the config is available
var minChangeTime = 3000;
var counter = 1;
var isPaused = false;
var isVideo = false;
var changeTimeout = 0;
var nextChangeTime = 0;
/**
* Triggers the next image change if not paused
* @param {number} additionalChange millisecond
*/
const setChangeTimer = (additionalChange) => {
clearTimeout(changeTimeout);
if (!isPaused ) {
// to enable logging execute: `~/TeleFrame/tools/addon_control.sh config flexInterval logNextChangeInterval true`
if (interface.config.logNextChangeInterval) {
interface.logger.info("additional change in: " + additionalChange);
}
changeTimeout = setTimeout(() => {
// show next image only if there was no user interaction
if (!isPaused && new Date().getTime() >= nextChangeTime) {
// use the half of the configured fadeTime
interface.sendEvent('next', config.fadeTime / 2);
}
}, additionalChange);
}
};
interface.registerListener('teleFrame-ready', teleFrameObjects => {
config = teleFrameObjects.config;
minChangeTime = Math.max(minChangeTime, Math.round(config.interval/config.imageCount));
});
//interface.registerListener('newImage', (sender, type) => counter = 1);
interface.registerListener(['images-loaded', 'imageDeleted', 'paused', 'muted',
'recordStopped', 'unstarImage', 'starImage'],
() => {
counter = 1;
// trigger next change
nextChangeTime = new Date().getTime() + minChangeTime;
setChangeTimer(minChangeTime);
});
// additional handling for 'paused' event.
// if paused call clearTimeout
interface.registerListener('paused', (status) => {
isPaused = status;
if (isPaused) {
clearTimeout(changeTimeout);
}
});
interface.registerListener('changedActiveImage', index => {
clearTimeout(changeTimeout);
// detect manual image change
if ((new Date().getTime() < nextChangeTime) && (!isVideo)) {
counter = 1;
};
isVideo = (interface.images[index].src.match(/\.mp4$/i));
// introduce an additional picture change:
if (!isPaused && counter < config.imageCount) {
const additionalChange = Math.max(minChangeTime, Math.round(config.interval*counter/config.imageCount));
nextChangeTime = new Date().getTime() + additionalChange;
setChangeTimer(additionalChange);
counter += 1;
};
});
};
/*************** DO NOT EDIT THE LINE BELOW ***************/
if (typeof module !== "undefined") {
module.exports = functionFlexIntervals;
}