-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-MagicMover.js
114 lines (104 loc) · 2.94 KB
/
MMM-MagicMover.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
/*
* MagicMirror²
* Module: MMM-MagicMover
*
* Prevent screen burn-in for MagicMirror².
*
* By Magnus Claesson https://github.com/Lavve
* MIT Licensed.
*/
Module.register('MMM-MagicMover', {
defaults: {
updateInterval: 20 * 60 * 1000,
ignoredRegions: [],
maxMove: 15,
moveWholescreen: false,
},
getStyles () {
return [`${this.name}.css`];
},
start () {
Log.info(`Starting module: ${this.name}`);
},
// Randomized coordinates
magicRandomizer () {
const min = ~(this.config.maxMove / 2) + 1;
const max = this.config.maxMove / 2;
return {
x: `${Math.ceil(Math.random() * (max - min) + min)}px`,
y: `${Math.ceil(Math.random() * (max - min) + min)}px`,
};
},
// Check if extra translate style is needed
magicTranslate (el) {
return el.matches('.region.top.center') || el.matches('.region.bottom.center')
? 'translateX(-50%)'
: el.matches('.region.upper.third') ||
el.matches('.region.middle.center') ||
el.matches('.region.lower.third')
? 'translateY(-50%)'
: '';
},
// Get all movable regions
magicRegions () {
if (this.config.moveWholescreen) return ['body'];
const ignores = [
...['.region.top.bar', '.region.bottom.bar'],
...this.config.ignoredRegions.map(
(ignore) => (ignore.slice(0, 1) === '.'
? ignore
: `.region.${ignore.split('_').join('.')}`)
)
];
return [...document.querySelectorAll('.region', '.ns-box', '.ns-alert')]
.map((r) => `.${r.className.replaceAll(' ', '.')}`)
.filter((r) => !ignores.includes(r));
},
// Move regions and start timer for each
magicMover () {
document.querySelectorAll(this.magicRegions().join(', ')).forEach((el) => {
el.classList.add('magic-mover');
this.timers.push(
setInterval(() => {
const c = this.magicRandomizer();
el.style.transform = `translate(${c.x}, ${
c.y
}) translate3d(0, 0, 0) ${this.magicTranslate(el)}`;
}, `${this.config.updateInterval + Math.ceil(Math.random() * (10000 - 1) + 1)}`)
);
});
this.isMoving = !0;
},
// Remove all movements and stop all timers
magicRemover () {
document.querySelectorAll('.magic-mover').forEach((el) => {
el.removeAttribute('style');
el.classList.remove('magic-mover');
});
this.timers.forEach((t) => {
clearInterval(t);
});
this.timers = [];
this.isMoving = !1;
},
// Remotely start or stop movements
notificationReceived (notification) {
switch (notification) {
case 'DOM_OBJECTS_CREATED':
this.timers = [];
this.magicMover();
break;
case 'MAGIC_MOVER_ON':
this.magicMover();
break;
case 'MAGIC_MOVER_OFF':
this.magicRemover();
break;
case 'MAGIC_MOVER_TOGGLE':
this[this.isMoving
? 'magicRemover'
: 'magicMover']();
break;
}
},
});