-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-ProxyImage.js
151 lines (137 loc) · 4.02 KB
/
MMM-ProxyImage.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
// eslint-disable-next-line no-undef
Module.register('MMM-ProxyImage', {
defaults: {
modalEnabled: false,
modalInterval: null,
modalTemplate: 'modal_template.njk',
updateInterval: 4000,
slideInterval: 10000,
port: 80,
height: 400,
width: 400,
auth: 'basic' // basic|digest
},
start() {
// Allows for multiple instances of the module
this.config.instanceName = (this.config.name || this.name).replace(
/\s+/g,
''
);
// send config to node helper
this.sendSocketNotification('INIT', this.config);
// Schedule update timer.
this.scheduleUpdate(this.config.updateInterval);
},
scheduleUpdate(delay) {
const self = this;
let nextLoad = self.config.updateInterval;
if (typeof delay !== 'undefined' && delay >= 0) {
nextLoad = delay;
}
setTimeout(() => {
self.updateDom();
self.scheduleUpdate();
}, nextLoad);
},
showModal(imageSrc) {
const self = this;
const { modalInterval, updateInterval } = self.config;
const refreshInterval = modalInterval || updateInterval;
this.sendNotification('OPEN_MODAL', {
template: self.config.modalTemplate,
data: {
imageSrc
},
options: {
callback(error) {
if (error) {
console.error('Modal rendering failed', error);
return false;
}
const modalTimer = setInterval(() => {
const modal = document.querySelector('.MMM-Modal');
if (modal.style.opacity === '1') {
const img = modal.querySelector('#proxy-image-modal-image');
img.src = imageSrc;
} else {
clearInterval(modalTimer);
}
}, refreshInterval);
}
}
});
},
getDom() {
const self = this;
const d = new Date();
const wrapperId = `proxyDiv${self.config.instanceName}`;
let wrapper = document.getElementById(wrapperId);
const imgId = `proxyImage${self.config.instanceName}`;
let img = document.getElementById(imgId);
const imgId2 = `proxyImage2${self.config.instanceName}`;
let img2 = document.getElementById(imgId2);
const imageSrc = `/${self.name}/${self.config.instanceName}?${d.getTime()}`;
if (!wrapper) {
wrapper = document.createElement('div');
wrapper.id = wrapperId;
wrapper.className = 'image-wrapper';
wrapper.style.width = `${self.config.width}px`;
wrapper.style.height = `${self.config.height}px`;
wrapper.style.border = 'none';
wrapper.style.display = 'block';
wrapper.style.overflow = 'hidden';
wrapper.style.backgroundColor = self.config.backgroundColor;
wrapper.scrolling = 'no';
}
if (self.config.modalEnabled) {
wrapper.onclick = () => {
self.showModal(imageSrc);
};
}
if (!img) {
img = document.createElement('img');
img.id = imgId;
img.width = self.config.width;
img.src = imageSrc;
wrapper.appendChild(img);
img2 = document.createElement('img');
img2.id = imgId2;
img2.width = self.config.width;
img2.style.display = 'none';
wrapper.appendChild(img2);
} else if (img && img.style.display !== 'none') {
// Give them a chance to load before swapping
img2.src = imageSrc;
setTimeout(() => {
img2.style.display = 'block';
img.style.display = 'none';
}, 2000);
} else if (img) {
img.src = imageSrc;
setTimeout(() => {
img.style.display = 'block';
img2.style.display = 'none';
}, 2000);
}
return wrapper;
},
suspend() {
const doms = document.getElementsByClassName('image-wrapper');
if (doms.length > 0) {
doms.forEach((dom) => {
dom.style.display = 'none';
});
}
},
resume() {
const doms = document.getElementsByClassName('image-wrapper');
if (doms.length > 0) {
doms.forEach((dom) => {
dom.style.display = 'block';
});
}
},
getStyles() {
return ['MMM-ProxyImage.css'];
}
});