pixelplanet/src/core/popUps.js

55 lines
1002 B
JavaScript
Raw Normal View History

/*
* keeping track of open popups
*/
import { unload } from '../store/actions';
class PopUps {
constructor() {
this.wins = [];
this.origin = window.location.origin;
window.addEventListener('beforeunload', () => {
this.dispatch(unload());
});
}
add(win) {
const pos = this.wins.indexOf(win);
if (pos === -1) {
this.wins.push(win);
}
}
remove(win) {
const pos = this.wins.indexOf(win);
if (~pos) this.wins.splice(pos, 1);
}
dispatch(msg) {
const { wins } = this;
try {
for (let i = 0; i < wins.length; i += 1) {
const win = wins[i];
if (win.closed) {
wins.splice(i, 1);
i -= 1;
continue;
}
win.postMessage(msg, this.origin);
}
} catch {
return false;
}
return true;
}
closeAll() {
while (this.wins.length) {
const win = this.wins.pop();
win.close();
}
}
}
const popUps = new PopUps();
export default popUps;