[🔨] Mass refactor

This commit is contained in:
ThinLiquid 2024-01-16 05:49:49 +00:00
parent 36201f4af1
commit 3402faf74e
No known key found for this signature in database
GPG key ID: 17538DC3DF6A7387
15 changed files with 163 additions and 192 deletions

View file

@ -6,11 +6,7 @@ export default class HTML {
* @param elm The HTML element to be created or classified from.
*/
constructor (elm: string | HTMLElement) {
if (elm instanceof HTMLElement) {
this.elm = elm
} else {
this.elm = document.createElement(elm === '' ? 'div' : elm)
}
this.elm = elm instanceof HTMLElement ? elm : document.createElement(elm === '' ? 'div' : elm)
}
/**
@ -57,11 +53,7 @@ export default class HTML {
* @returns a new HTML
*/
qs (query: string): HTML | null {
if (this.elm.querySelector(query) != null) {
return HTML.from(this.elm.querySelector(query) as HTMLElement)
} else {
return null
}
return this.elm.querySelector(query) != null ? HTML.from(this.elm.querySelector(query) as HTMLElement) : null
}
/**
@ -70,13 +62,11 @@ export default class HTML {
* @returns a new HTML
*/
qsa (query: string): Array<HTML | null> | null {
if (this.elm.querySelector(query) != null) {
return Array.from(this.elm.querySelectorAll(query)).map((e) =>
return this.elm.querySelector(query) != null
? Array.from(this.elm.querySelectorAll(query)).map((e) =>
HTML.from(e as HTMLElement)
)
} else {
return null
}
: null
}
/**
@ -95,8 +85,8 @@ export default class HTML {
* @returns HTML
*/
class (...val: string[]): HTML {
for (let i = 0; i < val.length; i++) {
this.elm.classList.toggle(val[i])
for (const element of val) {
this.elm.classList.toggle(element)
}
return this
}
@ -107,8 +97,8 @@ export default class HTML {
* @returns HTML
*/
classOn (...val: string[]): HTML {
for (let i = 0; i < val.length; i++) {
this.elm.classList.add(val[i])
for (const element of val) {
this.elm.classList.add(element)
}
return this
}
@ -119,8 +109,8 @@ export default class HTML {
* @returns HTML
*/
classOff (...val: string[]): HTML {
for (let i = 0; i < val.length; i++) {
this.elm.classList.remove(val[i])
for (const element of val) {
this.elm.classList.remove(element)
}
return this
}
@ -234,10 +224,10 @@ export default class HTML {
*/
attr (obj: { [x: string]: any }): HTML {
for (const key in obj) {
if (obj[key] !== null && obj[key] !== undefined) {
this.elm.setAttribute(key, obj[key])
} else {
if (obj[key] == null) {
this.elm.removeAttribute(key)
} else {
this.elm.setAttribute(key, obj[key])
}
}
return this
@ -296,8 +286,7 @@ export default class HTML {
static from (elm: HTMLElement | string): HTML | null {
if (typeof elm === 'string') {
const element = HTML.qs(elm)
if (element === null) return null
else return element
return element === null ? null : element
} else {
return new HTML(elm)
}
@ -309,11 +298,7 @@ export default class HTML {
* @returns a new HTML
*/
static qs (query: string): HTML | null {
if (document.querySelector(query) != null) {
return HTML.from(document.querySelector(query) as HTMLElement)
} else {
return null
}
return document.querySelector(query) != null ? HTML.from(document.querySelector(query) as HTMLElement) : null
}
/**
@ -322,12 +307,10 @@ export default class HTML {
* @returns a new HTML
*/
static qsa (query: string): Array<HTML | null> | null {
if (document.querySelector(query) != null) {
return Array.from(document.querySelectorAll(query)).map((e) =>
return document.querySelector(query) != null
? Array.from(document.querySelectorAll(query)).map((e) =>
HTML.from(e as HTMLElement)
)
} else {
return null
}
: null
}
}

View file

@ -21,7 +21,7 @@ async function enableDebug (): Promise<void> {
return await Promise.resolve()
}
if (params.get('debug') !== null && params.get('debug') !== undefined) {
if (params.get('debug') != null) {
enableDebug().catch(e => console.error(e))
}
@ -69,7 +69,7 @@ export default class Kernel {
executable = importedExecutable.default
} catch {
if (this.fs === undefined) throw new Error('Filesystem hasn\'t been initiated.')
const dataURL = 'data:text/javascript;base64,' + Buffer.from(await this.fs.readFile('/opt/' + url + '.js')).toString('base64')
const dataURL = `data:text/javascript;base64,${Buffer.from(await this.fs.readFile(`/opt/${url}.js`)).toString('base64')}`
const importedExecutable = await import(dataURL)
executable = importedExecutable.default
}
@ -81,32 +81,26 @@ export default class Kernel {
else if (this.packageList[executable.config.name].url !== url) throw new Error(`Package name conflict: ${executable.config.name}`)
return await new Promise((resolve, reject) => {
switch (executable.config.type) {
case 'process': {
const executableProcess = executable as Process
console.group(`Starting ${url}`)
const pid = ProcLib.findEmptyPID(this)
const token = uuid()
const procLib = new ProcessLib(url, pid, token, permission, data, executableProcess, this)
this.processList.push({
pid,
token,
name: executableProcess.config.name
})
if (executable.config.type === 'process') {
const executableProcess = executable as Process
console.group(`Starting ${url}`)
const pid = ProcLib.findEmptyPID(this)
const token = uuid()
const procLib = new ProcessLib(url, pid, token, permission, data, executableProcess, this)
this.processList.push({
pid,
token,
name: executableProcess.config.name
})
document.dispatchEvent(new CustomEvent('update_process', {}))
executableProcess.run(procLib).then((value: any) => {
if (value !== undefined) procLib.kill().catch(e => console.error(e))
document.dispatchEvent(new CustomEvent('update_process', {}))
executableProcess.run(procLib).then((value: any) => {
if (value !== undefined) procLib.kill().catch(e => console.error(e))
document.dispatchEvent(new CustomEvent('update_process', {}))
resolve({ procLib, value })
}).catch(e => console.error(e))
break
}
default: {
reject(new Error(`Unknown executable type: ${executable.config.type as string}`))
break
}
resolve({ procLib, value })
}).catch(e => console.error(e))
return
}
reject(new Error(`Unknown executable type: ${executable.config.type as string}`))
})
}
@ -119,7 +113,7 @@ export default class Kernel {
executable = importedExecutable.default
} catch {
if (this.fs === undefined) throw new Error('Filesystem hasn\'t been initiated.')
const dataURL = 'data:text/javascript;base64,' + Buffer.from(await this.fs.readFile('/opt/' + url + '.js')).toString('base64')
const dataURL = `data:text/javascript;base64,${Buffer.from(await this.fs.readFile(`/opt/${url}.js`)).toString('base64')}`
const importedExecutable = await import(dataURL)
executable = importedExecutable.default
}

View file

@ -67,7 +67,7 @@ export default class ProcessLib {
executable = importedExecutable.default
} catch {
if (this._kernel.fs === undefined) throw new Error('Filesystem hasn\'t been initiated.')
const dataURL = 'data:text/javascript;base64,' + Buffer.from(await this._kernel.fs.readFile('/opt/' + url + '.js')).toString('base64')
const dataURL = `data:text/javascript;base64,${Buffer.from(await this._kernel.fs.readFile(`/opt/${url}.js`)).toString('base64')}`
const importedExecutable = await import(dataURL)
executable = importedExecutable.default
}

View file

@ -33,7 +33,11 @@ const BootLoader: Process = {
console.warn('Persistent storage is not supported.')
}
const fileSystem = await read()
fileSystem === undefined ? await write(defaultFS) : await setFileSystem(fileSystem)
if (fileSystem === undefined) {
await write(defaultFS)
} else {
await setFileSystem(fileSystem)
}
const config = Buffer.from(await fs.readFile('/etc/flow')).toString()
process.kernel.setFS(fs)
@ -46,7 +50,7 @@ const BootLoader: Process = {
}
try {
await navigator.serviceWorker.register('/uv-sw.js?url=' + encodeURIComponent(btoa(process.kernel.config.SERVER)) + '&e=' + uuid(), {
await navigator.serviceWorker.register(`/uv-sw.js?url=${encodeURIComponent(btoa(process.kernel.config.SERVER))}&e=${uuid()}`, {
scope: '/service/'
})
} catch (e) {
@ -81,7 +85,7 @@ const BootLoader: Process = {
}).appendTo(apps)
new HTML('img').attr({
src: executable.config.icon ?? nullIcon,
alt: executable.config.name + ' icon'
alt: `${executable.config.name} icon`
}).appendTo(appElement)
new HTML('div').text(executable.config.name).appendTo(appElement)
})
@ -102,7 +106,7 @@ const BootLoader: Process = {
statusBar.element.html(`
<div class="outlined" data-toolbar-id="start"><span class="material-symbols-rounded">space_dashboard</span></div>
${/* <div class="outlined" data-toolbar-id="widgets"><span class="material-symbols-rounded">widgets</span></div> */ ''}
<div data-toolbar-id="apps"></div>
<flex></flex>
<div class="outlined" data-toolbar-id="plugins"><span class="material-symbols-rounded">expand_less</span></div>
@ -111,9 +115,7 @@ const BootLoader: Process = {
<span class="material-symbols-rounded signal">signal_cellular_4_bar</span>
</div>
<div class="outlined" data-toolbar-id="calendar"></div>
${/* <div class="outlined" data-toolbar-id="notifications">
<span class="material-symbols-rounded">notifications</span>
</div> */ ''}
`)
setInterval((): any => {
@ -127,14 +129,14 @@ const BootLoader: Process = {
})
if ('getBattery' in navigator) {
(navigator as any).getBattery().then(function (battery: any) {
(navigator as any).getBattery().then((battery: any) => {
statusBar.updateBatteryIcon(battery)
battery.addEventListener('levelchange', function () {
battery.addEventListener('levelchange', () => {
statusBar.updateBatteryIcon(battery)
})
battery.addEventListener('chargingchange', function () {
battery.addEventListener('chargingchange', () => {
statusBar.updateBatteryIcon(battery)
})
})
@ -157,12 +159,12 @@ const BootLoader: Process = {
})
}
setInterval((): any => ping(performance.now()), 10000)
setInterval((): any => ping(performance.now()), 10_000)
document.addEventListener('app_opened', (e: AppOpenedEvent): void => {
new HTML('app').appendMany(
new HTML('img').attr({
alt: e.detail.proc.config.name + ' icon',
alt: `${e.detail.proc.config.name} icon`,
'data-id': e.detail.token,
src: e.detail.proc.config.icon ?? nullIcon
}).on('click', () => {

View file

@ -9,41 +9,42 @@ const UserAccessControl: Process = {
targetVer: '1.0.0-indev.0'
},
run: async (process) => {
if (Object.keys(process.data).length > 0) {
const initiator = process.data.process.process
const target = process.data.executable
return await new Promise((resolve) => {
process.loadLibrary('lib/WindowManager').then(async wm => {
let message
switch (process.data.type) {
case 'launch': {
message = `${initiator.config.name as string} wants to launch ${target.config.name as string}`
break
}
case 'kill': {
message = `${initiator.config.name as string} wants to kill ${process.data.name as string}`
break
}
case 'fs': {
message = `${initiator.config.name as string} wants to modify/access ${process.data.path as string}`
break
}
}
wm.createModal('User Account Control', message, process)
.then(async ({ value, win }: {
value: boolean
win: FlowWindow
}) => {
if (value) {
resolve(true)
} else {
resolve(false)
}
})
}).catch((e) => console.error(e))
})
if (Object.keys(process.data).length <= 0) {
return
}
const initiator = process.data.process.process
const target = process.data.executable
return await new Promise((resolve) => {
process.loadLibrary('lib/WindowManager').then(async wm => {
let message
switch (process.data.type) {
case 'launch': {
message = `${initiator.config.name as string} wants to launch ${target.config.name as string}`
break
}
case 'kill': {
message = `${initiator.config.name as string} wants to kill ${process.data.name as string}`
break
}
case 'fs': {
message = `${initiator.config.name as string} wants to modify/access ${process.data.path as string}`
break
}
}
wm.createModal('User Account Control', message, process)
.then(async ({ value, win }: {
value: boolean
win: FlowWindow
}) => {
if (value) {
resolve(true)
} else {
resolve(false)
}
})
}).catch((e) => console.error(e))
})
}
}

View file

@ -92,12 +92,12 @@ const BrowserApp: Process = {
}
(this.header.querySelector('.title') as HTMLElement).innerText = 'Tab'
this.iframe.src = (win.content.querySelector('input')?.value as string)
} else {
if (this === tabManager.activeTab) {
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_on'
}
this.iframe.src = `/service/${xor.encode(win.content.querySelector('input').value) as string}`
return
}
if (this === tabManager.activeTab) {
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_on'
}
this.iframe.src = `/service/${xor.encode(win.content.querySelector('input').value) as string}`
}
}
@ -137,20 +137,21 @@ const BrowserApp: Process = {
setActiveTab (tab: Tab): void {
this.tabs.forEach((tab) => {
if (tab.active) {
tab.active = false
tab.iframe.style.display = 'none'
tab.header.classList.remove('active')
if (!tab.active) {
return
}
tab.active = false
tab.iframe.style.display = 'none'
tab.header.classList.remove('active')
})
if (!tab.proxy) {
if (tab.proxy) {
try { (win.content.querySelector('.inp') as HTMLInputElement).value = xor.decode((tab.iframe.contentWindow as Window).location.href.split('/service/')[1]) } catch (e) { (win.content.querySelector('.inp') as HTMLInputElement).value = 'about:blank' }
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_on'
} else {
(tab.header.querySelector('.title') as HTMLElement).textContent = 'Tab'
try { (win.content.querySelector('.inp') as HTMLInputElement).value = (tab.iframe.contentWindow as Window).location.href } catch (e) { (win.content.querySelector('.inp') as HTMLInputElement).value = 'about:blank' }
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_off'
} else {
try { (win.content.querySelector('.inp') as HTMLInputElement).value = xor.decode((tab.iframe.contentWindow as Window).location.href.split('/service/')[1]) } catch (e) { (win.content.querySelector('.inp') as HTMLInputElement).value = 'about:blank' }
(win.content.querySelector('.toggle') as HTMLElement).innerHTML = 'toggle_on'
}
tab.active = true
@ -166,11 +167,7 @@ const BrowserApp: Process = {
win.content.querySelector('.inp')?.addEventListener('keydown', (event: KeyboardEvent) => {
if (event.key === 'Enter') {
if (tabManager.activeTab.proxy) {
tabManager.activeTab.iframe.src = `/service/${xor.encode((win.content.querySelector('.inp') as HTMLInputElement).value) as string}`
} else {
tabManager.activeTab.iframe.src = (win.content.querySelector('.inp') as HTMLInputElement).value
}
tabManager.activeTab.iframe.src = tabManager.activeTab.proxy ? `/service/${xor.encode((win.content.querySelector('.inp') as HTMLInputElement).value) as string}` : (win.content.querySelector('.inp') as HTMLInputElement).value
}
});
@ -195,18 +192,14 @@ const BrowserApp: Process = {
}
win.content.onfullscreenchange = () => {
if (document.fullscreenElement !== null) {
(win.content.querySelector('.fullscreen') as HTMLElement).innerHTML = 'fullscreen_exit'
} else {
(win.content.querySelector('.fullscreen') as HTMLElement).innerHTML = 'fullscreen'
}
(win.content.querySelector('.fullscreen') as HTMLElement).innerHTML = document.fullscreenElement !== null ? 'fullscreen_exit' : 'fullscreen'
}
(win.content.querySelector('.fullscreen') as HTMLElement).onclick = async () => {
if (document.fullscreenElement !== null) {
await document.exitFullscreen().catch(e => console.error)
} else {
if (document.fullscreenElement === null) {
await win.content.requestFullscreen().catch((e: any) => console.error)
} else {
await document.exitFullscreen().catch(e => console.error)
}
}

View file

@ -59,7 +59,12 @@ const Editor: Process = {
win.content.style.display = 'flex'
win.content.style.flexDirection = 'column'
if (data != null) {
if (data == null) {
await process.launch('lib/FileManager')
setTimeout(() => {
win.close()
}, 10)
} else {
const render = async (): Promise<void> => {
win.content.innerHTML = `
<div style="padding: 5px;display: flex;align-items: center;gap: 5px;">
@ -203,16 +208,11 @@ const Editor: Process = {
document.addEventListener('fs_update', () => {
render().catch(e => console.error(e))
})
} else {
await process.launch('lib/FileManager')
setTimeout(() => {
win.close()
}, 10)
}
} else {
await process.kill()
await process.launch('apps/Files')
return
}
await process.kill()
await process.launch('apps/Files')
}
}

View file

@ -48,14 +48,14 @@ const Files: Process = {
(win.content.querySelector('.file') as HTMLElement).onclick = async () => {
const title: string | null | undefined = prompt('Enter file name')
if (title !== null && title !== undefined) {
if (title != null) {
await fs.writeFile(`${dir}/${title}`, '')
}
}
(win.content.querySelector('.folder') as HTMLElement).onclick = async () => {
const title: string | null | undefined = prompt('Enter folder name')
if (title !== null && title !== undefined) {
if (title != null) {
await fs.mkdir(`${dir}/${title}`, '')
}
}

View file

@ -43,10 +43,10 @@ const ImageViewer: Process = {
document.addEventListener('fs_update', () => {
render().catch(e => console.error(e))
})
} else {
await process.kill()
await process.launch('apps/Files')
return
}
await process.kill()
await process.launch('apps/Files')
}
}

View file

@ -25,12 +25,12 @@ const Store: Process = {
win.content.style.background = 'var(--base)'
fetch(process.kernel.config.SERVER as string + '/apps/list/')
fetch(`${process.kernel.config.SERVER as string}/apps/list/`)
.then(async (res) => await res.json())
.then(handle)
.catch(e => console.error(e))
document.addEventListener('fs_update', () => {
fetch(process.kernel.config.SERVER as string + '/apps/list/')
fetch(`${process.kernel.config.SERVER as string}/apps/list/`)
.then(async (res) => await res.json())
.then(handle)
.catch(e => console.error(e))

View file

@ -49,7 +49,7 @@ const TaskManager: Process = {
}).appendTo(win.content)
const render = (): void => {
const processList = process.kernel.processList
const { processList } = process.kernel
table.html('')
new HTML('thead').appendTo(table)

View file

@ -4,7 +4,8 @@ import FlowLogo from '../../assets/flow.png'
import { Library } from '../../types'
import LibraryLib from '../../structures/LibraryLib'
let library: LibraryLib, kernel: Kernel
let library: LibraryLib
let kernel: Kernel
const SplashScreen: Library = {
config: {

View file

@ -29,24 +29,22 @@ const StatusBar: Library = {
} else if (battery.level >= 0) {
iconHTML = 'battery_charging_20'
}
} else {
if (battery.level === 1) {
iconHTML = 'battery_full'
} else if (battery.level >= 0.6) {
iconHTML = 'battery_6_bar'
} else if (battery.level >= 0.5) {
iconHTML = 'battery_5_bar'
} else if (battery.level >= 0.4) {
iconHTML = 'battery_4_bar'
} else if (battery.level >= 0.3) {
iconHTML = 'battery_3_bar'
} else if (battery.level >= 0.2) {
iconHTML = 'battery_2_bar'
} else if (battery.level >= 0.1) {
iconHTML = 'battery_1_bar'
} else if (battery.level >= 0) {
iconHTML = 'battery_0_bar'
}
} else if (battery.level === 1) {
iconHTML = 'battery_full'
} else if (battery.level >= 0.6) {
iconHTML = 'battery_6_bar'
} else if (battery.level >= 0.5) {
iconHTML = 'battery_5_bar'
} else if (battery.level >= 0.4) {
iconHTML = 'battery_4_bar'
} else if (battery.level >= 0.3) {
iconHTML = 'battery_3_bar'
} else if (battery.level >= 0.2) {
iconHTML = 'battery_2_bar'
} else if (battery.level >= 0.1) {
iconHTML = 'battery_1_bar'
} else if (battery.level >= 0) {
iconHTML = 'battery_0_bar'
}
const batteryDiv = document.querySelector('div[data-toolbar-id="controls"] > .battery')

View file

@ -253,7 +253,8 @@ export const setFileSystem = async (fileSystemObject: { root: Directory }): Prom
export let db: IDBDatabase
let fileSystem: { root: Directory }
let kernel: Kernel, process: ProcessLib
let kernel: Kernel
let process: ProcessLib
export const initializeDatabase = async (dbName: string): Promise<boolean> => {
return await new Promise((resolve, reject) => {
@ -315,9 +316,7 @@ const save = async (): Promise<void> => {
}
const handlePermissions = async (path: string): Promise<void> => {
let current
current = (await navigatePath(path)).current
let { current } = (await navigatePath(path))
if (current === undefined) current = (await navigatePathParent(path)).current
@ -374,7 +373,7 @@ const VirtualFS: Library = {
Reflect.deleteProperty(current.children, filename)
console.debug('unlink ' + path)
console.debug(`unlink ${path}`)
await save()
},
readFile: async (path: string): Promise<Buffer> => {
@ -384,7 +383,7 @@ const VirtualFS: Library = {
if (current.type !== 'file') throw new Error(Errors.EISDIR)
console.debug('read ' + path)
console.debug(`read ${path}`)
return current.content
},
writeFile: async (path: string, content: string | Buffer): Promise<void> => {
@ -392,11 +391,11 @@ const VirtualFS: Library = {
let permission
if (typeof current.children[filename] !== 'undefined') {
if (typeof current.children[filename] === 'undefined') {
permission = Permission.USER
} else {
await handlePermissions(path)
permission = current.children[filename].permission
} else {
permission = Permission.USER
}
current.children[filename] = {
@ -406,7 +405,7 @@ const VirtualFS: Library = {
content: Buffer.from(content)
}
console.debug('write ' + path)
console.debug(`write ${path}`)
await save()
},
mkdir: async (path: string): Promise<void> => {
@ -414,11 +413,11 @@ const VirtualFS: Library = {
let permission
if (typeof current.children[filename] !== 'undefined') {
if (typeof current.children[filename] === 'undefined') {
permission = Permission.USER
} else {
await handlePermissions(path)
permission = current.children[filename].permission
} else {
permission = Permission.USER
}
current.children[filename] = {
@ -428,7 +427,7 @@ const VirtualFS: Library = {
children: {}
}
console.debug('mkdir ' + path)
console.debug(`mkdir ${path}`)
await save()
},
rmdir: async (path: string): Promise<void> => {
@ -441,7 +440,7 @@ const VirtualFS: Library = {
Reflect.deleteProperty(current.children, filename)
console.debug('rmdir ' + path)
console.debug(`rmdir ${path}`)
await save()
},
readdir: async (path: string): Promise<string[]> => {
@ -450,13 +449,13 @@ const VirtualFS: Library = {
if (current.type === 'file') throw new Error(Errors.ENOTDIR)
const result = await Promise.all(Object.keys(current.children ?? {}))
console.debug('readdir ' + path)
console.debug(`readdir ${path}`)
return result
},
stat: async (path: string): Promise<{ isDirectory: () => boolean, isFile: () => boolean }> => {
const { current } = await navigatePath(path)
console.debug('stat ' + path)
console.debug(`stat ${path}`)
return {
isDirectory: () => current.type === 'directory',
isFile: () => current.type === 'file'
@ -475,11 +474,11 @@ const VirtualFS: Library = {
newCurrent.children[newFilename] = oldCurrent.children[oldFilename]
Reflect.deleteProperty(oldCurrent.children, oldFilename)
console.debug('rename ' + oldPath + ' -> ' + newPath)
console.debug(`rename ${oldPath} -> ${newPath}`)
await save()
},
exists: async (path: string): Promise<boolean> => {
console.debug('exists ' + path)
console.debug(`exists ${path}`)
try {
const { current } = await navigatePath(path)
return current !== undefined

View file

@ -37,7 +37,7 @@ const XOR: Library = {
return indCheck ? String.fromCharCode(char.charCodeAt(0) ^ 2) : char
})
.join('') + ((search.length > 0) ? '?' + search.join('?') : '')
.join('') + ((search.length > 0) ? `?${search.join('?')}` : '')
)
}
}