retro-terminaljs
Version:
Create retro terminals feeding live data.
169 lines (168 loc) • 4.09 kB
JavaScript
const electron = require('electron');
const url = require('url');
const path = require('path');
const { app, BrowserWindow } = electron;
const { ipcMain } = electron;
let win;
var activated = false;
var intro = true;
var termProperties = {
screen: {
appName: 'RETRO-TERMINALJS',
devTools: false
},
width: 450,
height: 450,
fullscreen: true
}
class EventEmitter {
constructor() {
this.events = {};
}
on(eventName, callback) {
if (this.events[eventName]) {
this.events[eventName].push(callback);
} else {
this.events[eventName] = [callback];
if (eventName === "ready") {
this.events[eventName].forEach(cb => {
activated = true;
cb.apply([]);
});
}
}
}
trigger(eventName, ...rest) {
if (this.events[eventName]) {
this.events[eventName].forEach(cb => {
cb.apply(rest);
});
}
}
shutdown() {
app.quit();
}
}
var events = require('events');
var myPinger = new events();
var listenPinger = new events();
module.exports.terminal = new EventEmitter();
module.exports.screen = function newScreen(_jsonArray) {
if (activated === true) {
if ('devTools' in _jsonArray.screen) {
termProperties.screen.devTools = _jsonArray.screen.devTools;
}
if ('width' in _jsonArray) {
termProperties.width = _jsonArray.width;
}
if ('height' in _jsonArray) {
termProperties.height = _jsonArray.height;
}
if ('fullscreen' in _jsonArray) {
termProperties.fullscreen = _jsonArray.fullscreen;
}
app.on('ready', function() {
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true
},
title: termProperties.screen.appName,
width: termProperties.width,
height: termProperties.height,
fullscreen: termProperties.fullscreen
});
win.setMenu(null);
win.on('closed', function(){
win = null;
app.quit();
});
win.loadURL(url.format ({
pathname: path.join(__dirname, './view.html'),
protocol: 'file:',
slashes: true
}));
setTimeout(function() {
intro = false;
}, 10000);
if (termProperties.screen.devTools === true) {
win.openDevTools();
}
win.webContents.once('did-finish-load', () => {
myPinger.on('pingDiplayTrigger', function sendDisplay(_jsonArray) {
win.webContents.send('pingDisplay', _jsonArray);
});
myPinger.on('pingDelDiplayTrigger', function sendDelDisplay() {
win.webContents.send('pingDelDisplay');
});
listenPinger.on('pingListenerTrigger', function(type, arg) {
win.webContents.send('pingListener', type, arg);
});
});
});
}
}
module.exports.display = function newDisplay(_jsonArray) {
if (activated === true) {
function checkIntro() {
if (intro === true) {
setTimeout(function() {
checkIntro();
}, 100);
} else {
myPinger.emit('pingDiplayTrigger', _jsonArray);
}
}
checkIntro();
}
}
module.exports.deleteDisplays = function delDisplay() {
if (activated === true) {
function checkIntro() {
if (intro === true) {
setTimeout(function() {
checkIntro();
}, 100);
} else {
myPinger.emit('pingDelDiplayTrigger');
}
}
checkIntro();
}
}
var EventListenerEvents = {};
class EventListener {
constructor() {
}
listen(eventName, arg, callback) {
if (EventListenerEvents[arg]) {
EventListenerEvents[arg].push(callback);
} else {
EventListenerEvents[arg] = [callback];
function checkIntro() {
if (intro === true) {
setTimeout(function() {
checkIntro();
}, 100);
} else {
listenPinger.emit('pingListenerTrigger', eventName, arg);
}
}
checkIntro();
}
}
trigger(eventName, arg, ...rest) {
if (this.events[arg]) {
this.events[arg].forEach(cb => {
cb.apply(rest);
});
}
}
}
ipcMain.on('replyListener', (event, _obj, _bool) => {
if (_bool === true) {
EventListenerEvents[_obj].forEach(cb => {
cb.apply([]);
});
}
});
module.exports.eventListener = new EventListener();