ed-safari/src/renderer.js

151 lines
4.7 KiB
JavaScript
Raw Normal View History

2023-05-08 12:25:13 -07:00
/**
* This file will automatically be loaded by vite and run in the "renderer" context.
* To learn more about the differences between the "main" and the "renderer" context in
* Electron, visit:
*
* https://electronjs.org/docs/tutorial/application-architecture#main-and-renderer-processes
*
* By default, Node.js integration in this file is disabled. When enabling Node.js integration
* in a renderer process, please be aware of potential security implications. You can read
* more about security risks here:
*
* https://electronjs.org/docs/tutorial/security
*
* To enable Node.js integration in this file, open up `main.js` and enable the `nodeIntegration`
* flag:
*
* ```
* // Create the browser window.
* mainWindow = new BrowserWindow({
* width: 800,
* height: 600,
* webPreferences: {
* nodeIntegration: true
* }
* });
* ```
*/
import 'bootstrap/dist/css/bootstrap.css'
import './assets/index.css'
2023-05-08 17:42:09 -07:00
import './icons/flaticon.css'
2023-05-08 12:25:13 -07:00
import './assets/ldom.min'
2023-05-12 22:48:31 -07:00
2023-05-13 12:50:39 -07:00
const { app, ipcRenderer } = require('electron')
import { Safari } from './models/Safari'
2023-05-10 21:54:32 -07:00
import { UI } from './models/UI'
import { Body } from './models/Body'
2023-05-11 01:54:03 -07:00
import { sep } from 'path'
2023-05-12 03:00:15 -07:00
import { EDSM } from './models/EDSM'
2023-05-08 12:25:13 -07:00
2023-05-08 17:42:09 -07:00
// Grab app.isPackaged from main process
let isPackaged = false
window.process.argv.forEach((item) => {
if (item.includes('EDS-ENV')) {
isPackaged = (item.split('=').pop() === 'true')
}
})
/* ------------------------------------------------------------------------------- app setup ---- */
2023-05-08 12:25:13 -07:00
2023-05-12 00:05:34 -07:00
const safari = Safari.start(isPackaged)
const journal = safari.journal
2023-05-12 03:00:15 -07:00
const edsm = EDSM.connect()
2023-05-08 13:10:38 -07:00
if (!journal) {
// handle error
}
2023-05-08 12:25:13 -07:00
safari.watchJournalDir()
journal.watch()
2023-05-13 12:50:39 -07:00
/* -------------------------------------------------------------------- close window handler ---- */
$('#closeBtn').on('click', () => {
ipcRenderer.send('CLOSE_WINDOW')
})
2023-05-11 21:02:23 -07:00
/* ------------------------------------------------------------------------- build body list ---- */
2023-05-11 21:02:23 -07:00
journal.once('BUILD_BODY_LIST', () => {
if (journal.location?.bodies?.length > 0) {
journal.location.sortBodies()
journal.location.bodies.forEach((body) => {
2023-05-10 21:54:32 -07:00
const row = UI.createBodyRow(body)
$('#scans').appendChild(row)
})
}
2023-05-09 13:40:32 -07:00
})
2023-05-11 21:02:23 -07:00
/* ----------------------------------------------------------------- started hyperspace jump ---- */
journal.on('ENTERING_WITCH_SPACE', () => UI.enterWitchSpace())
2023-05-09 13:40:32 -07:00
/* ---------------------------------------------------------------------- entered new system ---- */
journal.on('ENTERED_NEW_SYSTEM', () => {
2023-05-11 21:02:23 -07:00
UI.setCurrentSystem(journal.location)
2023-05-12 03:00:15 -07:00
$('#navRoute').children().filter(`#${CSS.escape(journal.location.SystemAddress)}`).remove()
2023-05-09 13:40:32 -07:00
2023-05-11 21:02:23 -07:00
// verify that the internal navRoute matches the UI navRoute, and rebuild it if not
if ($('#navRoute').children().length !== journal.navRoute.length) {
journal.emit('SET_NAV_ROUTE')
}
})
/* ---------------------------------------------------------------------- body scan detected ---- */
journal.on('BODY_SCANNED', (body, DSS) => {
journal.location.sortBodies()
// If this is a DSS scan, it's very likely that the body already exists in our list so we just
// need to remove the highlighting if applicable
if (DSS) {
const bodyRow = $(`#${body.BodyID}`)
if (bodyRow.length > 0) { // check just in case body was missed in earlier scans
bodyRow.removeClass('highlighted uncharted').addClass('charted')
} else {
2023-05-10 21:54:32 -07:00
const row = UI.createBodyRow(body)
$('#scans').appendChild(row)
}
} else { // else it's an FSS/auto scan and won't be in the list yet
2023-05-10 21:54:32 -07:00
const row = UI.createBodyRow(body)
$('#scans').appendChild(row)
}
2023-05-11 01:54:03 -07:00
})
/* --------------------------------------------------------------------------- nav route set ---- */
journal.on('SET_NAV_ROUTE', () => {
// clear previous nav route, if any
2023-05-11 02:45:02 -07:00
$('#navRoute').children().remove()
2023-05-11 21:02:23 -07:00
if (journal.navRoute.length > 0) {
2023-05-11 02:45:02 -07:00
journal.navRoute.forEach((system) => {
// duplicate check
// CSS.escape is needed since CSS technically doesn't allow numeric IDs
const systemRow = $(`#${CSS.escape(system.SystemAddress)}`)
if (systemRow.length === 0) {
const row = UI.createSystemRow(system)
$('#navRoute').appendChild(row)
}
})
}
2023-05-12 03:00:15 -07:00
})
/* ------------------------------------------------------------------------ system value set ---- */
edsm.on('SYSTEM_APPRAISED', (system) => {
const systemRow = $(`#${CSS.escape(system.SystemAddress)}`)
if (systemRow.length > 0) {
UI.setValue(systemRow, system.estimatedValueMapped)
}
})