@@ -152,10 +152,10 @@ setup(context)
152152function setSelected ( region : string ) {
153153 const el = regionEls . get ( region )
154154 if ( ! el ) return
155- ( el as HTMLInputElement ) . checked = true
156- showSelected ( el . parentNode as any )
155+ showSelected ( el . parentNode as any , region )
157156}
158- function showSelected ( el ?: HTMLElement ) {
157+ function showSelected ( el : HTMLElement | undefined , region : string ) {
158+ history . pushState ( { } , '' , '/' + region . toLowerCase ( ) )
159159 for ( const other of document . querySelectorAll ( '.region-selected' ) ) {
160160 other . classList . remove ( 'region-selected' )
161161 }
@@ -164,23 +164,24 @@ function showSelected(el?: HTMLElement) {
164164
165165let regionEls : Map < string , HTMLElement > = new Map ( )
166166
167- const inputEls = [ ...document . querySelectorAll ( '#regions > label > input[name="region"]' ) ]
168- for ( const el0 of inputEls ) {
169- const el = el0 as HTMLInputElement
170- const parent = el . parentNode as HTMLElement
167+ const regionElsArr = [ ...document . querySelectorAll ( '#regions > .region' ) ]
168+ console . log ( regionElsArr . length )
169+ for ( const el0 of regionElsArr ) {
170+ const el = el0 as HTMLElement
171+ const regK = el . getAttribute ( 'data-region' ) as string
171172
172- const reg = regions [ el . value ]
173+ const reg = regions [ regK ]
173174 if ( ! reg ) {
174- parent . style . display = 'none'
175+ el . style . display = 'none'
175176 continue
176177 }
177178
178- el . onchange = ( ) => {
179- if ( ! el . checked ) return
180- showSelected ( parent )
181- showRegion ( el . value , reg )
179+ el . onclick = ( ev ) => {
180+ ev . preventDefault ( )
181+ showSelected ( el , regK )
182+ showRegion ( regK , reg )
182183 }
183- regionEls . set ( el . value , el )
184+ regionEls . set ( regK , el )
184185}
185186
186187// null if show all
@@ -191,14 +192,15 @@ function fillRegions(filter: string | null) {
191192 regs . push ( { key : regK as any , region : regions [ regK ] } )
192193 }
193194
194- for ( const el0 of inputEls ) {
195- const el = el0 as HTMLInputElement
196- const reg = regions [ el . value ]
197- if ( ( filter && ! el . value . includes ( filter ) ) || ! reg ) {
198- ( el . parentNode as HTMLElement ) . style . display = 'none'
195+ for ( const el0 of regionElsArr ) {
196+ const el = el0 as HTMLElement
197+ const regK = el . getAttribute ( 'data-region' ) as string
198+ const reg = regions [ regK ]
199+ if ( ( filter && ! regK . includes ( filter ) ) || ! reg ) {
200+ el . style . display = 'none'
199201 }
200202 else {
201- ( el . parentNode as HTMLElement ) . style . display = ''
203+ el . style . display = ''
202204 }
203205 }
204206}
@@ -527,6 +529,44 @@ function showRegion(regionName: RegionKey, region: Region, pos?: [number, number
527529 }
528530}
529531
530- fillRegions ( null )
531532
532- showRegion ( 'HI' , regions [ 'HI' ] , [ - 0 , 150 ] , 1 )
533+ {
534+ const defaultReg = ( window as any ) . defaultRegion as string
535+
536+ let pos : Pos
537+ if ( new URL ( window . location . toString ( ) ) . pathname === '/' ) {
538+ pos = [ - 0 , 150 ]
539+ }
540+ else {
541+ const reg = regions [ defaultReg ]
542+
543+ let totalX = 0
544+ let totalY = 0
545+ let count = 0
546+ for ( const roomK in reg . rooms ) {
547+ const room = reg . rooms [ roomK ]
548+ if ( ! room . data . mapPos ) continue
549+ totalX += room . data . mapPos [ 0 ] / 3 - room . data . size [ 0 ] * 0.5
550+ totalY += room . data . mapPos [ 1 ] / 3 - room . data . size [ 1 ] * 0.5
551+ count ++
552+ }
553+
554+ const avgX = totalX / count + halfWidth
555+ const avgY = totalY / count + halfHeight
556+
557+ pos = [ avgX , avgY ]
558+ }
559+
560+ fillRegions ( null )
561+ showRegion ( defaultReg , regions [ defaultReg ] , pos , 1 )
562+
563+ const showRegionsEl = ( window as any ) . show_regions as HTMLInputElement
564+ function showRegions ( ) {
565+ ( window as any ) . regions . classList . remove ( 'regions-hidden' )
566+ ( document . querySelector ( '.unhide' ) as HTMLElement ) . style . display = 'none'
567+ }
568+ if ( showRegionsEl . checked ) showRegions ( )
569+ showRegionsEl . onchange = ( ) => {
570+ if ( showRegionsEl . checked ) showRegions ( )
571+ }
572+ }
0 commit comments