@@ -63,6 +63,11 @@ class I18NManager {
6363 return key ;
6464 }
6565 Object . entries ( params || { } ) . forEach ( ( [ key , v ] ) => {
66+ if ( v instanceof Element ) {
67+ var e = document . createElement ( "div" ) ;
68+ e . appendChild ( v . origin ) ;
69+ v = e . innerHTML ;
70+ }
6671 value = value . replaceAll ( `%${ key } %` , v ) ;
6772 } )
6873 return value ;
@@ -75,7 +80,6 @@ class I18NManager {
7580class ElementManager {
7681 constructor ( ) {
7782 this . _elements = [ ]
78- this . _elements_event = { }
7983 globalThis . $ElementManager = this ;
8084 window . addEventListener ( "langChange" , ( event ) => {
8185 this . _elements . forEach ( element => element . _render_i18n ( ) )
@@ -84,15 +88,6 @@ class ElementManager {
8488 add ( element ) {
8589 this . _elements . push ( element ) ;
8690 }
87- addEventListener ( event , element ) {
88- if ( ! ( event in this . _elements_event ) ) {
89- this . _elements_event [ event ] = [ ]
90- window . addEventListener ( event , ( event ) => {
91- this . _elements_event [ event ] . forEach ( element => element ( event ) )
92- } )
93- }
94- this . _elements_event [ event ] . push ( element )
95- }
9691}
9792class Element {
9893 constructor ( object ) {
@@ -134,7 +129,7 @@ class Element {
134129 if ( this . _i18n_key == null ) {
135130 return ;
136131 }
137- this . text ( $i18n . t ( this . _i18n_key , this . _i18n_params ) )
132+ this . html ( $i18n . t ( this . _i18n_key , this . _i18n_params ) )
138133 }
139134 append ( ...elements ) {
140135 elements . forEach ( element => {
@@ -154,7 +149,12 @@ class Element {
154149 this . _base . classList . remove ( ...classes ) ;
155150 return this ;
156151 }
152+ removeAllClasses ( ) {
153+ this . _base . classList = [ ] ;
154+ return this
155+ }
157156 hasClasses ( ...classes ) {
157+ console . log ( classes , this . _base . classList . contains ( ...classes ) , this . _base . classList )
158158 return this . _base . classList . contains ( ...classes ) ;
159159 }
160160 toggle ( ...classes ) {
@@ -273,14 +273,15 @@ class Style {
273273 if ( Array . isArray ( object ) ) {
274274 return object . map ( this . _parseToString ) . join ( ";" ) ;
275275 } else if ( typeof object == "object" ) {
276- return Object . entries ( object ) . map ( ( [ key , value ] ) => typeof value === "object" ? `${ key } {${ this . _parseToString ( value ) } }` : `${ key } :${ this . _parseToString ( value ) } ` ) . join ( "; " ) ;
276+ return Object . entries ( object ) . map ( ( [ key , value ] ) => typeof value === "object" ? `${ key } {${ this . _parseToString ( value ) } }` : `${ key } :${ this . _parseToString ( value ) } ; ` ) . join ( "" ) ;
277277 } else {
278278 return object . toString ( ) ;
279279 }
280280 }
281281 add ( name , style ) {
282282 if ( ! name . startsWith ( "@" ) ) {
283- this . _styles [ name ] = this . _parseToString ( style ) ;
283+
284+ this . _styles [ name ] = ( this . _styles [ name ] || '' ) + ";" + this . _parseToString ( style ) ;
284285 } else {
285286 if ( ! ( name in this . _medias ) ) this . _medias [ name ] = [ ]
286287 if ( this . _medias [ name ] . indexOf ( style ) == - 1 ) this . _medias [ name ] . push ( this . _parseToString ( style ) ) ;
@@ -370,6 +371,12 @@ class SVGContainers {
370371 static get close ( ) {
371372 return SVGContainers . _parse ( '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="m16.192 6.344-4.243 4.242-4.242-4.242-1.414 1.414L10.535 12l-4.242 4.242 1.414 1.414 4.242-4.242 4.243 4.242 1.414-1.414L13.364 12l4.242-4.242z"></path></svg>' )
372373 }
374+ static get exit ( ) {
375+ return SVGContainers . _parse ( '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M19.002 3h-14c-1.103 0-2 .897-2 2v4h2V5h14v14h-14v-4h-2v4c0 1.103.897 2 2 2h14c1.103 0 2-.897 2-2V5c0-1.103-.898-2-2-2z"></path><path d="m11 16 5-4-5-4v3.001H3v2h8z"></path></svg>' )
376+ }
377+ static get arrow_down ( ) {
378+ return SVGContainers . _parse ( '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M16.293 9.293 12 13.586 7.707 9.293l-1.414 1.414L12 16.414l5.707-5.707z"></path></svg>' )
379+ }
373380}
374381class Progressbar extends Element {
375382 constructor ( ) {
@@ -503,10 +510,7 @@ class Router {
503510 this . _popstate_handler ( )
504511 } )
505512 this . _popstate_handler ( )
506-
507- // 检测所有 a 标签的点击事件, 但捕获后来添加的 a 标签
508513 window . addEventListener ( "click" , ( e ) => {
509- console . log ( e . target . tagName )
510514 if ( e . target . tagName == "A" ) {
511515 const href = e . target . getAttribute ( "href" )
512516 const url = new URL ( href , window . location . origin ) ;
@@ -518,10 +522,13 @@ class Router {
518522 }
519523 } )
520524 }
525+ page ( path ) {
526+ this . _popstate_handler ( path )
527+ }
521528 _get_current_path ( ) {
522529 return window . location . pathname
523530 }
524- _popstate_handler ( path ) {
531+ async _popstate_handler ( path ) {
525532 const new_path = ( path ?? this . _get_current_path ( ) ) . replace ( this . _route_prefix , "" ) || "/"
526533 const old_path = this . _current_path
527534 if ( this . _handled == new_path ) return ;
@@ -534,17 +541,23 @@ class Router {
534541 }
535542 try {
536543 // get route
537- var route = this . _routes . find ( x => x . path . test ( new_path ) )
538- if ( route ) {
539- // route.path.match(route.path)
540- var params = route . path . exec ( new_path ) . slice ( 1 ) . reduce ( ( acc , cur , i ) => {
541- acc [ route . params [ i ] ] = cur
542- return acc
543- } , { } )
544- var handler = route ? route . handler : null
545- if ( handler ) {
546- handler ( new RouteEvent ( this , old_path , new_path , params ) )
547- }
544+ var routes = this . _routes . filter ( x => x . path . test ( new_path ) )
545+ if ( routes ) {
546+ routes . forEach ( route => {
547+ var params = route . path . exec ( new_path ) . slice ( 1 ) . reduce ( ( acc , cur , i ) => {
548+ acc [ route . params [ i ] ] = cur
549+ return acc
550+ } , { } )
551+ var handler = route ? route . handler : null
552+ if ( handler ) {
553+ try {
554+ handler ( new RouteEvent ( this , old_path , new_path , params ) )
555+ } catch ( e ) {
556+ console . log ( e )
557+ }
558+ }
559+ } )
560+
548561 }
549562 } catch ( e ) {
550563 console . log ( e )
@@ -558,10 +571,12 @@ class Router {
558571 on ( path , handler ) {
559572 // path {xxx}
560573 // replace to like python (?P<xxx>*.+)
574+ var params = ( path . match ( / \{ ( ( \w + ) | ( \: ( u r l ) \: ) ) \} / g) || [ ] ) . map ( x => x . replaceAll ( "{" , "" ) . replaceAll ( "}" , "" ) . replaceAll ( ":" , "" ) )
575+ var regexp_path = path . replace ( / \{ \: ( u r l ) \: \} / g, "(.*)" ) . replace ( / \{ ( \w + ) \} / g, "([^/]*)" )
561576 var config = {
562577 raw_path : path ,
563- path : new RegExp ( `^${ path . replace ( / \{ ( \w + ) \} / g , "([^/]*)" ) } $` ) ,
564- params : ( path . match ( / \{ ( \w + ) \} / g ) || [ ] ) . map ( x => x . replace ( "{" , "" ) . replace ( "}" , "" ) ) ,
578+ path : new RegExp ( `^${ regexp_path } $` ) ,
579+ params : params ,
565580 handler
566581 }
567582 this . _routes . push ( config )
@@ -578,48 +593,6 @@ class Router {
578593 return this
579594 }
580595}
581- class User {
582- constructor ( baseurl ) {
583- // 处理 baseurl 结尾的 /
584- this . _base = baseurl . replace ( / \/ + $ / , "" )
585- }
586- async getState ( ) {
587- if ( this . refreshtoken ) {
588- var payload = parseJWT ( this . refreshtoken )
589- var data = JSON . parse ( payload )
590- if ( data . exp * 1000 > Date . now ( ) ) {
591- return true
592- }
593- }
594- try {
595- await this . _refresh_token ( )
596- return true
597- } catch ( e ) {
598- return false
599- }
600- }
601- get refreshtoken ( ) {
602- return localStorage . getItem ( "auth_refresh_token" )
603- }
604- get accesstoken ( ) {
605- return localStorage . getItem ( "auth_access_token" )
606- }
607- async _refresh_token ( ) {
608- const refreshtoken = localStorage . getItem ( "auth_refresh_token" )
609- if ( ! refreshtoken ) throw new ServiceError ( 403 , "Unauthorized" ) ;
610- const resp = await fetch ( `${ this . _base } /refresh_token` , {
611- headers : {
612- "Authorization" : `Bearer ${ refreshtoken } `
613- }
614- } )
615- const data = await resp . json ( )
616- if ( ServiceError . isServiceError ( data ) ) {
617- throw ServiceError . fromJSON ( data )
618- }
619- localStorage . setItem ( "auth_access_token" , resp . headers . get ( "X-Access-Token" ) )
620- localStorage . setItem ( "auth_refresh_token" , resp . headers . get ( "X-Refresh-Token" ) )
621- }
622- }
623596class ServiceData {
624597 causedBy = null
625598 httpCode = null
@@ -804,7 +777,14 @@ class InputElement extends Element {
804777 this . classes ( "active" )
805778 } ) , createElement ( "fieldset" ) . append (
806779 createElement ( "legend" ) . append ( this . $label_span ) )
807- ) )
780+ ) ) . addEventListener ( "input" , ( ) => {
781+ this . classes ( "focus" )
782+ if ( Utils . isEmpty ( this . $input . val ) ) {
783+ this . removeClasses ( "active" )
784+ } else {
785+ this . classes ( "active" )
786+ }
787+ } )
808788 }
809789 label ( value ) {
810790 this . $label_span . text ( value )
@@ -859,21 +839,16 @@ function ref(object, params) {
859839 return new Proxy ( object , {
860840 set ( target , key , value ) {
861841 target [ key ] = value
862- var args = {
863- target,
864- key,
865- value
866- }
867842 if ( timeout > 0 ) {
868843 if ( task ) {
869844 clearTimeout ( task )
870845 }
871846 task = setTimeout ( ( ) => {
872- handler ( args )
847+ handler ( key , value )
873848 task = null
874849 } , timeout )
875850 } else {
876- handler ( args )
851+ handler ( key , value )
877852 }
878853 return true
879854 }
@@ -882,7 +857,8 @@ function ref(object, params) {
882857function calcElementHeight ( element ) {
883858 var origin = element . origin ;
884859 var rect = origin . getBoundingClientRect ( )
885- return rect . height
860+ return rect . height
861+
886862}
887863export {
888864 Element ,
@@ -894,7 +870,6 @@ export {
894870 ElementManager ,
895871 I18NManager ,
896872 Style ,
897- User ,
898873 ServiceData ,
899874 ServiceError ,
900875 Modal ,
0 commit comments