@@ -177,7 +177,9 @@ class Element {
177177 this . _base = document . createElement ( object ) ;
178178 } else if ( Utils . isDOM ( object ) ) {
179179 this . _base = object ;
180- }
180+ } else {
181+ console . log ( object )
182+ }
181183 this . _i18n_key = null ;
182184 this . _i18n_params = { } ;
183185 this . _children = [ ]
@@ -210,12 +212,15 @@ class Element {
210212 }
211213 this . text ( $i18n . t ( this . _i18n_key ) )
212214 }
213- append ( element ) {
214- if ( Utils . isDOM ( element ) ) {
215- element = new Element ( element ) ;
216- }
217- this . _children . push ( element ) ;
218- this . _base . appendChild ( element . origin ) ;
215+ append ( ...elements ) {
216+ elements . forEach ( element => {
217+ if ( Utils . isDOM ( element ) ) {
218+ element = new Element ( element ) ;
219+ }
220+ this . _children . push ( element ) ;
221+ this . _base . appendChild ( element . origin ) ;
222+ } )
223+ return this
219224 }
220225 classes ( ...classes ) {
221226 this . _base . classList . add ( ...classes ) ;
@@ -250,24 +255,56 @@ class Element {
250255 } else {
251256 this . _children . splice ( this . _children . indexOf ( object ) , 1 ) ;
252257 }
258+ return this
253259 }
254260 get firstChild ( ) {
255261 return this . _children [ 0 ] ;
256262 }
257263 get lastChild ( ) {
258264 return this . _children [ this . _children . length - 1 ] ;
259265 }
266+ remove ( ) {
267+ this . _children . forEach ( child => child . remove ( ) ) ;
268+ this . _base . remove ( ) ;
269+ }
270+ appendBefore ( element ) {
271+ this . _children . unshift ( element ) ;
272+ this . _base . insertBefore ( element . origin , this . _base . firstChild ) ;
273+ return this
274+ }
275+ attributes ( attributes ) {
276+ Object . entries ( attributes ) . forEach ( ( [ key , value ] ) => {
277+ this . _base . setAttribute ( key , value ) ;
278+ } )
279+ return this ;
280+ }
281+ }
282+ class Configuration {
283+ constructor ( ) {
284+ // use local storage
285+ }
286+ get ( key , _def ) {
287+ console . log ( localStorage . getItem ( key ) )
288+ var item = JSON . parse ( localStorage . getItem ( key ) ) ;
289+ return item . value ;
290+ }
291+ set ( key , value ) {
292+ localStorage . setItem ( key , JSON . parse ( {
293+ "value" : value ,
294+ "timestamp" : new Date ( )
295+ } ) ) ;
296+ }
260297}
261298class Style {
262299 constructor ( ) {
263300 this . _styles = { }
264301 this . _style_dom = document . createElement ( "style" ) ;
302+ this . _themes = { }
303+ this . _current_theme = null ;
304+ this . applyTheme ( $configuration . get ( "theme" , window . matchMedia ( "(prefers-color-scheme: dark)" ) ? "dark" : "light" ) )
265305 document . getElementsByTagName ( "head" ) . item ( 0 ) . appendChild ( this . _style_dom ) ;
266306 }
267307 _parseToString ( object ) {
268- // if object is array, we foreach and parse again
269- // if obejct is table, we join ";" in it
270- // if object is string, we return it
271308 if ( Array . isArray ( object ) ) {
272309 return object . map ( this . _parseToString ) . join ( ";" ) ;
273310 } else if ( typeof object == "object" ) {
@@ -280,30 +317,149 @@ class Style {
280317 this . _styles [ name ] = this . _parseToString ( style ) ;
281318 this . render ( ) ;
282319 }
320+ addAll ( styles ) {
321+ Object . entries ( styles ) . forEach ( ( [ name , style ] ) => this . add ( name , style ) ) ;
322+ }
283323 render ( ) {
284- this . _style_dom . innerHTML = Object . entries ( this . _styles ) . map ( ( [ name , style ] ) => `${ name } {${ style } }` ) . join ( "" ) ;
324+ const theme = { } ;
325+ Object . entries ( this . _themes [ this . _current_theme ] || { } ) . forEach ( ( [ key , value ] ) => {
326+ theme [ `--${ key } ` ] = value ;
327+ } )
328+ this . _styles [ ":root" ] = this . _parseToString ( theme ) ;
329+ const styleRule = Object . entries ( this . _styles ) . map ( ( [ name , style ] ) => style == null ? "" : `${ name } {${ style } }` ) . join ( "" ) ;
330+ if ( ! this . _sheet_render ( styleRule ) ) {
331+ while ( this . _style_dom . childNodes . length > 0 ) {
332+ this . _style_dom . removeChild ( this . _style_dom . childNodes [ 0 ] ) ;
333+ }
334+ this . _style_dom . appendChild ( document . createTextNode ( styleRule ) ) ;
335+ }
336+ }
337+ _sheet_render ( styleRule ) {
338+ const styleSheet = this . _style_dom . sheet ;
339+ if ( styleSheet ) {
340+ while ( styleSheet . cssRules . length > 0 ) {
341+ styleSheet . deleteRule ( 0 ) ;
342+ }
343+ try {
344+ styleSheet . insertRule ( this . _sheet , styleSheet . cssRules . length ) ;
345+ return true ;
346+ } catch ( e ) {
347+ }
348+ return false ;
349+ }
350+ }
351+ applyTheme ( name ) {
352+ this . _current_theme = name || Object . keys ( this . _themes ) [ 0 ] ;
353+ this . render ( ) ;
354+ }
355+ setTheme ( name , style ) {
356+ this . _themes [ name ] = style ;
357+ }
358+ }
359+ class SVGContainers {
360+ static _parse ( element ) {
361+ return new Element ( document . createRange ( ) . createContextualFragment ( element ) . childNodes [ 0 ] ) ;
362+ }
363+ static get menu ( ) {
364+ return SVGContainers . _parse ( '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M4 6h16v2H4zm0 5h16v2H4zm0 5h16v2H4z"></path></svg>' )
365+ }
366+ static get moon ( ) {
367+ return SVGContainers . _parse ( '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path></svg>' )
368+ }
369+ static get sun ( ) {
370+ return SVGContainers . _parse ( '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path></svg>' )
285371 }
286372}
287373function createElement ( object ) {
288374 return new Element ( object ) ;
289375}
376+ const $configuration = new Configuration ( ) ;
290377const $ElementManager = new ElementManager ( ) ;
291378const $style = new Style ( ) ;
292379const $i18n = new I18NManager ( ) ;
293380const $socket = new Socket ( window . location . origin + "/api" ) ;
381+ $style . setTheme ( "light" , {
382+ "main-color" : "#ffffff" ,
383+ "color" : "#000000" ,
384+ "background" : "#F5F6F8"
385+ } )
386+ $style . setTheme ( "dark" , {
387+ "main-color" : "#000000" ,
388+ "color" : "#ffffff" ,
389+ "background" : "#181818"
390+ } )
391+ $style . addAll ( {
392+ "::-webkit-scrollbar, html ::-webkit-scrollbar" : {
393+ "width" : "5px" ,
394+ "height" : "5px" ,
395+ "border-radius" : "10px"
396+ } ,
397+ "::-webkit-scrollbar-thumb, html ::-webkit-scrollbar-thumb" : {
398+ "box-shadow" : "rgba(0, 0, 0, 0) 0px 0px 6px inset" ,
399+ "background-color" : "rgb(102, 102, 102)" ,
400+ "border-radius" : "10px" ,
401+ } ,
402+ "body" : {
403+ "overflow" : "hidden"
404+ } ,
405+ ".app" : {
406+ "height" : "100vh" ,
407+ "width" : "100vw" ,
408+ "background" : "var(--background)"
409+ } ,
410+ "header" : `
411+ background-color: var(--background);
412+ text-align: center;
413+ min-height: 56px;
414+ width: 100%;
415+ padding: 8px;
416+ position: fixed;
417+ z-index: 1;
418+ display: flex;
419+ align-items: center;
420+ flex-wrap: nowrap;
421+ justify-content: space-between
422+ ` ,
423+ "header svg" : {
424+ "width" : "48px" ,
425+ "height" : "48px" ,
426+ "padding" : "8px" ,
427+ "cursor" : "pointer"
428+ } ,
429+ "svg" : {
430+ "fill" : "var(--color)"
431+ }
432+ } )
433+ function load ( ) {
434+ const $dom_body = new Element ( document . body ) ;
435+
436+ const $app = createElement ( "div" ) . classes ( "app" )
437+ const $header = createElement ( "header" )
438+ const $header_content_left = createElement ( "div" ) . append (
439+ SVGContainers . menu ,
440+ SVGContainers . sun
441+ )
442+ const $header_content_right = createElement ( "div" ) ;
443+ $header . append ( $header_content_left , $header_content_right ) ;
444+ $app . append ( $header ) ;
294445
446+ $dom_body . appendBefore ( $app ) ;
447+ }
295448window . addEventListener ( "DOMContentLoaded" , ( ) => {
296- console . log ( "hello world" )
297- $style . add ( "body" , {
298- "background-color" : "black"
299- } )
449+ load ( )
300450 Array . from ( document . getElementsByClassName ( "preloader" ) ) . forEach ( e => {
301451 const element = new Element ( e ) ;
302452 requestAnimationFrame ( ( ) => {
303453 element . classes ( "hidden" ) ;
304454 setTimeout ( ( ) => {
305455 element . remove ( ) ;
306- } , 5000 )
456+ } , 1500 )
307457 } )
308458 } )
309- } )
459+ } )
460+
461+ globalThis . $configuration = $configuration ;
462+ globalThis . $ElementManager = $ElementManager ;
463+ globalThis . $style = $style ;
464+ globalThis . $i18n = $i18n ;
465+ globalThis . $socket = $socket ;
0 commit comments