2024-07-07 13:43:18 +02:00
(() => {
2024-07-09 15:15:17 +02:00
// node_modules/udn-frontend/index.ts
var UDNFrontend = class {
ws ;
// HANDLERS
connectionHandler = () => {
};
disconnectionHandler = () => {
};
messageHandler = ( data ) => {
};
2024-07-19 18:00:01 +02:00
mailboxHandler = ( mailboxId ) => {
2024-07-09 15:15:17 +02:00
};
2024-07-19 18:00:01 +02:00
mailboxConnectionHandler = ( mailboxId ) => {
2024-07-09 15:15:17 +02:00
};
2024-07-19 18:00:01 +02:00
mailboxDeleteHandler = ( mailboxId ) => {
2024-07-09 15:15:17 +02:00
};
// INIT
set onconnect ( handler ) {
this . connectionHandler = handler ;
}
set ondisconnect ( handler ) {
this . disconnectionHandler = handler ;
}
set onmessage ( handler ) {
this . messageHandler = handler ;
}
set onmailboxcreate ( handler ) {
this . mailboxHandler = handler ;
}
set onmailboxconnect ( handler ) {
this . mailboxConnectionHandler = handler ;
}
set onmailboxdelete ( handler ) {
this . mailboxDeleteHandler = handler ;
}
// UTILITY METHODS
send ( messageObject ) {
if ( this . ws == void 0 ) return false ;
const messageString = JSON . stringify ( messageObject );
this . ws . send ( messageString );
return true ;
}
// PUBLIC METHODS
// connection
2024-07-19 18:17:00 +02:00
connect ( address ) {
2024-07-09 15:15:17 +02:00
try {
this . disconnect ();
2024-07-19 18:17:00 +02:00
this . ws = new WebSocket ( address );
2024-07-09 15:15:17 +02:00
this . ws . addEventListener ( "open" , this . connectionHandler );
this . ws . addEventListener ( "close" , this . disconnectionHandler );
this . ws . addEventListener ( "message" , ( message ) => {
const dataString = message . data . toString ();
const data = JSON . parse ( dataString );
if ( data . assignedMailboxId ) {
return this . mailboxHandler ( data . assignedMailboxId );
} else if ( data . connectedMailboxId ) {
return this . mailboxConnectionHandler ( data . connectedMailboxId );
} else if ( data . deletedMailbox ) {
return this . mailboxDeleteHandler ( data . deletedMailbox );
} else {
this . messageHandler ( data );
}
});
} catch ( error ) {
console . error ( error );
}
}
disconnect () {
this . ws ? . close ();
}
// message
sendMessage ( channel , body ) {
const messageObject = {
messageChannel : channel ,
messageBody : body
};
return this . send ( messageObject );
}
// subscription
subscribe ( channel ) {
const messageObject = {
subscribeChannel : channel
};
return this . send ( messageObject );
}
unsubscribe ( channel ) {
const messageObject = {
unsubscribeChannel : channel
};
return this . send ( messageObject );
}
// mailbox
requestMailbox () {
const messageObject = {
requestingMailboxSetup : true
};
return this . send ( messageObject );
}
2024-07-19 18:00:01 +02:00
connectMailbox ( mailboxId ) {
2024-07-09 15:15:17 +02:00
const messageObject = {
2024-07-19 18:00:01 +02:00
requestedMailbox : mailboxId
2024-07-09 15:15:17 +02:00
};
return this . send ( messageObject );
}
2024-07-19 18:00:01 +02:00
deleteMailbox ( mailboxId ) {
2024-07-09 15:15:17 +02:00
const messageObject = {
2024-07-19 18:00:01 +02:00
deletingMailbox : mailboxId
2024-07-09 15:15:17 +02:00
};
return this . send ( messageObject );
}
};
2024-07-19 23:45:07 +02:00
// src/Model/Utility/utility.ts
2024-07-19 18:00:01 +02:00
function stringify ( data ) {
return JSON . stringify ( data );
2024-07-12 14:30:30 +02:00
}
2024-07-19 19:23:48 +02:00
function parse ( string ) {
return JSON . parse ( string );
}
2024-07-12 14:30:30 +02:00
2024-07-19 18:00:01 +02:00
// src/Model/connectionModel.ts
var ConnectionModel = class {
// basic
udn ;
// data
get isConnected () {
return this . udn . ws != void 0 && this . udn . ws . readyState == 1 ;
2024-07-13 17:13:59 +02:00
}
2024-07-19 18:02:23 +02:00
get address () {
2024-07-19 18:00:01 +02:00
return this . udn . ws ? . url ;
2024-07-13 17:13:59 +02:00
}
2024-07-19 18:00:01 +02:00
// connection
2024-07-19 18:17:00 +02:00
connect = ( address ) => {
this . udn . connect ( address );
2024-07-19 18:02:23 +02:00
};
disconnect = () => {
2024-07-19 18:00:01 +02:00
this . udn . disconnect ();
2024-07-19 18:02:23 +02:00
};
handleConnectionChange = () => {
console . log ( "connection status:" , this . isConnected , this . address );
};
2024-07-19 18:00:01 +02:00
// messaging
2024-07-19 18:02:23 +02:00
sendChatMessage = ( chatMessage ) => {
2024-07-19 18:00:01 +02:00
const stringifiedBody = stringify ( chatMessage );
return this . udn . sendMessage ( chatMessage . channel , stringifiedBody );
2024-07-19 18:02:23 +02:00
};
2024-07-19 18:00:01 +02:00
// setup
2024-07-19 18:17:00 +02:00
constructor ( configuration ) {
2024-07-19 18:00:01 +02:00
this . udn = new UDNFrontend ();
2024-07-19 18:17:00 +02:00
this . udn . onmessage = ( data ) => {
configuration . messageHandler ( data );
};
this . udn . onconnect = () => {
this . handleConnectionChange ();
configuration . connectionChangeHandler ();
};
this . udn . ondisconnect = () => {
this . handleConnectionChange ();
configuration . connectionChangeHandler ();
};
2024-07-09 17:23:29 +02:00
}
};
2024-07-09 17:27:15 +02:00
2024-07-19 23:45:07 +02:00
// node_modules/bloatless-react/index.ts
function createElement ( tagName , attributes = {}, ... children ) {
const element = document . createElement ( tagName );
if ( attributes != null )
Object . entries ( attributes ). forEach (( entry ) => {
const [ attributename , value ] = entry ;
const [ directiveKey , directiveValue ] = attributename . split ( ":" );
switch ( directiveKey ) {
case "on" : {
switch ( directiveValue ) {
case "enter" : {
element . addEventListener ( "keydown" , ( e ) => {
if ( e . key != "Enter" ) return ;
value ();
});
break ;
}
default : {
element . addEventListener ( directiveValue , value );
}
}
break ;
}
case "subscribe" : {
const state = value ;
state . subscribe (
( newValue ) => element [ directiveValue ] = newValue
);
break ;
}
case "bind" : {
const state = value ;
state . subscribe (
( newValue ) => element [ directiveValue ] = newValue
);
element . addEventListener (
"input" ,
() => state . value = element [ directiveValue ]
);
break ;
}
case "toggle" : {
if ( value . subscribe ) {
const state = value ;
state . subscribe (
( newValue ) => element . toggleAttribute ( directiveValue , newValue )
);
} else {
element . toggleAttribute ( directiveValue , value );
}
break ;
}
case "set" : {
const state = value ;
state . subscribe (
( newValue ) => element . setAttribute ( directiveValue , newValue )
);
break ;
}
case "children" : {
switch ( directiveValue ) {
case "set" : {
const state = value ;
state . subscribe (( newValue ) => {
element . innerHTML = "" ;
element . append (...[ newValue ]. flat ());
});
break ;
}
case "append" :
case "appendandscroll" :
case "prepend" : {
element . style . scrollBehavior = "smooth" ;
try {
const [ listState , toElement ] = value ;
listState . handleAddition (( newItem ) => {
const child = toElement ( newItem );
listState . handleRemoval (
newItem ,
() => child . remove ()
);
if ( directiveValue == "append" ) {
element . append ( child );
} else if ( directiveValue == "prepend" ) {
element . prepend ( child );
}
});
} catch {
throw `error: cannot process subscribe:children directive because StateItemConverter is not defined. Usage: "subscribe:children={[list, converter]}"; you can find a more detailed example in the documentation` ;
}
}
}
}
default :
element . setAttribute ( attributename , value );
}
});
children . filter (( x ) => x ). forEach (( child ) => element . append ( child ));
return element ;
}
// src/View/translations.ts
var englishTranslations = {
general : {
closeButton : "close"
2024-07-19 18:17:00 +02:00
},
2024-07-19 23:45:07 +02:00
homePage : {
appName : "Comms" ,
///
overviewHeadline : "Overview" ,
statusHeadline : "Status" ,
settingsButton : "Settings" ,
///
chatsHeadline : "Chats" ,
addChatAudioLabel : "name of new chat" ,
addChatPlaceholder : "Add chat" ,
addChatButton : "Add chat"
},
settings : {
settingsHeadline : "Settings"
///
2024-07-19 18:17:00 +02:00
}
2024-07-19 23:45:07 +02:00
};
var allTranslations = {
en : englishTranslations
};
var language = navigator . language . substring ( 0 , 2 );
var translations = allTranslations [ language ] || allTranslations . en ;
// src/View/homePage.tsx
function HomePage () {
return /* @__PURE__ */ createElement ( "article" , { id : "home-page" }, /* @__PURE__ */ createElement ( "header" , null , /* @__PURE__ */ createElement ( "span" , null , translations . homePage . appName )), /* @__PURE__ */ createElement ( "div" , null , /* @__PURE__ */ createElement ( "div" , { id : "overview-section" }, /* @__PURE__ */ createElement ( "h2" , null , translations . homePage . overviewHeadline ), /* @__PURE__ */ createElement ( "div" , { class : "tile flex-no" }, /* @__PURE__ */ createElement ( "span" , { class : "icon" }, "cell_tower" ), /* @__PURE__ */ createElement ( "div" , null , /* @__PURE__ */ createElement ( "h3" , null , translations . homePage . statusHeadline ))), /* @__PURE__ */ createElement ( "button" , { class : "tile flex-no" }, /* @__PURE__ */ createElement ( "span" , { class : "icon" }, "settings" ), /* @__PURE__ */ createElement ( "div" , null , translations . homePage . settingsButton ))), /* @__PURE__ */ createElement ( "div" , { id : "chat-section" }, /* @__PURE__ */ createElement ( "h2" , null , translations . homePage . chatsHeadline ), /* @__PURE__ */ createElement ( "div" , { class : "flex-row width-input" }, /* @__PURE__ */ createElement (
"input" ,
{
placeholder : translations . homePage . addChatPlaceholder ,
"aria-label" : translations . homePage . addChatAudioLabel
}
), /* @__PURE__ */ createElement (
"button" ,
{
class : "primary" ,
"aria-label" : translations . homePage . addChatButton
},
/* @__PURE__ */ createElement ( "span" , { class : "icon" }, "add" )
)))));
}
// src/View/mainPage.tsx
function MainPage () {
return /* @__PURE__ */ createElement ( "article" , { id : "home-page" }, /* @__PURE__ */ createElement ( "header" , null , /* @__PURE__ */ createElement ( "span" , null , translations . homePage . appName )), /* @__PURE__ */ createElement ( "div" , null ));
2024-07-12 21:11:33 +02:00
}
2024-07-19 23:45:07 +02:00
// src/View/setingsModal.tsx
function SetttingsModal () {
return /* @__PURE__ */ createElement ( "div" , { class : "modal" }, /* @__PURE__ */ createElement ( "div" , null , /* @__PURE__ */ createElement ( "main" , null , /* @__PURE__ */ createElement ( "h2" , null , translations . settings . settingsHeadline )), /* @__PURE__ */ createElement ( "button" , null , translations . general . closeButton , /* @__PURE__ */ createElement ( "span" , { class : "icon" }, "close" ))));
2024-07-09 17:23:29 +02:00
}
2024-07-19 19:23:48 +02:00
2024-07-19 23:45:07 +02:00
// src/Model/Utility/typeSafety.ts
2024-07-19 21:10:31 +02:00
var DATA_VERSION = "v2" ;
2024-07-19 19:23:48 +02:00
// src/Model/storageModel.ts
2024-07-19 21:10:31 +02:00
var StorageModel = class _StorageModel {
2024-07-19 19:23:48 +02:00
storageEntryTree = {};
// basic
2024-07-19 21:10:31 +02:00
store = ( key , value ) => {
localStorage . setItem ( key , value );
const pathComponents = _StorageModel . keyToPathComponents ( key );
this . updateTree (... pathComponents );
2024-07-19 19:23:48 +02:00
};
2024-07-19 21:10:31 +02:00
restore = ( key ) => {
return localStorage . getItem ( key );
2024-07-19 19:23:48 +02:00
};
2024-07-19 21:10:31 +02:00
remove = ( key ) => {
localStorage . removeItem ( key );
2024-07-19 19:23:48 +02:00
};
2024-07-19 21:10:31 +02:00
list = ( key ) => {
const pathComponents = _StorageModel . keyToPathComponents ( key );
2024-07-19 19:23:48 +02:00
let currentParent = this . storageEntryTree ;
2024-07-19 21:10:31 +02:00
for ( const component of pathComponents ) {
const nextParent = currentParent [ component ];
if ( nextParent == void 0 ) return [];
currentParent = nextParent ;
2024-07-19 19:23:48 +02:00
}
return [... Object . keys ( currentParent )];
};
2024-07-19 21:10:31 +02:00
// stringifiable
storeStringifiable = ( key , value ) => {
2024-07-19 19:23:48 +02:00
const valueString = stringify ( value );
2024-07-19 21:10:31 +02:00
this . store ( key , valueString );
2024-07-19 19:23:48 +02:00
};
2024-07-19 21:10:31 +02:00
restoreStringifiable = ( key ) => {
const valueString = this . restore ( key );
2024-07-19 19:23:48 +02:00
if ( ! valueString ) return null ;
2024-07-19 21:10:31 +02:00
return parse ( valueString );
2024-07-19 19:23:48 +02:00
};
// init
constructor () {
for ( const key of Object . keys ( localStorage )) {
2024-07-19 21:10:31 +02:00
const components = _StorageModel . keyToPathComponents ( key );
this . updateTree (... components );
2024-07-19 19:23:48 +02:00
}
}
// utility
2024-07-19 21:10:31 +02:00
updateTree = (... pathComponents ) => {
2024-07-19 19:23:48 +02:00
let currentParent = this . storageEntryTree ;
2024-07-19 21:10:31 +02:00
for ( const pathPart of pathComponents ) {
2024-07-19 19:23:48 +02:00
if ( ! currentParent [ pathPart ]) {
currentParent [ pathPart ] = {};
}
currentParent = currentParent [ pathPart ];
}
};
2024-07-19 21:10:31 +02:00
static pathComponentsToKey = (... pathComponents ) => {
return pathComponents . join ( "/" );
2024-07-19 19:23:48 +02:00
};
2024-07-19 21:10:31 +02:00
static keyToPathComponents = ( key ) => {
2024-07-19 19:23:48 +02:00
return key . split ( "/" );
};
2024-07-19 21:10:31 +02:00
static join = (... items ) => {
let allComponents = [];
for ( const item of items ) {
const parts = this . keyToPathComponents ( item );
allComponents . push (... parts );
}
return _StorageModel . pathComponentsToKey (... allComponents );
};
};
var storageKeys = {
// connection
socketAddress : ` ${ DATA_VERSION } /connection/socket-address` ,
// settings
userName : ` ${ DATA_VERSION } /settings/user-name` ,
firstDayOfWeek : ` ${ DATA_VERSION } /settings/first-day-of-week` ,
// history
previousAddresses : ` ${ DATA_VERSION } /history/previous-addresses` ,
previousObjectCategories : ` ${ DATA_VERSION } /history/object-categories` ,
previousObjectStatuses : ` ${ DATA_VERSION } /history/object-statuses` ,
previousObjectFilters : ` ${ DATA_VERSION } /history/object-filters` ,
// chat etc
chatInfo : ( id ) => ` ${ DATA_VERSION } /chat/ ${ id } /info` ,
chatMessages : ( id ) => ` ${ DATA_VERSION } /chat/ ${ id } /messages` ,
chatObjects : ( id ) => ` ${ DATA_VERSION } /chat/ ${ id } /objects` ,
chatOutbox : ( id ) => ` ${ DATA_VERSION } /chat/ ${ id } /outbox`
2024-07-19 19:23:48 +02:00
};
// src/index.tsx
var storageModel = new StorageModel ();
2024-07-19 23:45:07 +02:00
var connectionModel = new ConnectionModel ({
connectionChangeHandler () {
},
messageHandler ( data ) {
}
});
document . querySelector ( "main" ). append ( HomePage (), MainPage (), SetttingsModal ());
2024-07-07 13:43:18 +02:00
})();