map_templ.go

  1// Code generated by templ - DO NOT EDIT.
  2
  3// templ: version: v0.3.1020
  4package components
  5
  6//lint:file-ignore SA4006 This context is only used if a nested component is present.
  7
  8import "github.com/a-h/templ"
  9import templruntime "github.com/a-h/templ/runtime"
 10
 11import "encoding/json"
 12
 13var mapHandle = templ.NewOnceHandle()
 14var routeHandle = templ.NewOnceHandle()
 15
 16func toJSON(bbox [4]float64) string {
 17	v, _ := json.Marshal(bbox)
 18	return string(v)
 19}
 20
 21func Map(style string, bbox [4]float64, class string) templ.Component {
 22	return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
 23		templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
 24		if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
 25			return templ_7745c5c3_CtxErr
 26		}
 27		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
 28		if !templ_7745c5c3_IsBuffer {
 29			defer func() {
 30				templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
 31				if templ_7745c5c3_Err == nil {
 32					templ_7745c5c3_Err = templ_7745c5c3_BufErr
 33				}
 34			}()
 35		}
 36		ctx = templ.InitializeContext(ctx)
 37		templ_7745c5c3_Var1 := templ.GetChildren(ctx)
 38		if templ_7745c5c3_Var1 == nil {
 39			templ_7745c5c3_Var1 = templ.NopComponent
 40		}
 41		ctx = templ.ClearChildren(ctx)
 42		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<script type=\"module\">\n\t\timport { Map } from 'https://esm.sh/maplibre-gl@5';\n\n\t\tfunction loadMapCSS() {\n          if (document.querySelector('link[href*=\"maplibre-gl.css\"]')) return;\n          const link = document.createElement('link');\n          link.rel = 'stylesheet';\n          link.href = 'https://esm.sh/maplibre-gl@5/dist/maplibre-gl.css';\n          document.head.appendChild(link);\n\t\t}\n\n\t\tclass RTWmap extends HTMLElement {\n\t\t  map;\n\n\t\t  constructor() {\n\t\t\t\tsuper();\n\t\t\t\tloadMapCSS();\n\t\t\t\tthis.addEventListener('map:context', (event) => {\n\t\t\t\t  console.log(\"received map:context\");\n\t\t\t\t  event.detail.cb(this.map);\n\t\t\t\t})\n\t\t  }\n\t\t  connectedCallback() {\n\t\t\tthis.style.cssText = 'display: block; width: 100%; height: 100%; overflow: hidden; border-radius: inherit;';\n\n\t\t\tconst elem = document.createElement('div');\n\t\t\telem.style.cssText = 'width:100%;height:100%;overflow:hidden;'\n\t\t\tthis.appendChild(elem);\n\n\t\t\tconst styleURL = this.getAttribute('map-style');\n\t\t\tthis.map = new Map({\n\t\t\t\tcontainer: elem,\n\t\t\t\tstyle: styleURL,\n\t\t\t\tcenter: [0, 0],\n\t\t\t\tzoom: 1,\n\t\t\t\tattributionControl: false\n\t\t\t});\n\t\t\tconst bbox = JSON.parse(this.getAttribute('bbox'));\n\t\t    this.map.fitBounds(bbox, {padding: {top: 50, bottom: 50, left: 75, right: 75}});\n\t\t  }\n\t\t  disconnectedCallback() {\n\t\t\tthis.map?.remove();\n\t\t  }\n\t\t}\n\t\tif (!customElements.get('rtw-map')) {\n\t\t  customElements.define('rtw-map', RTWmap);\n\t\t}\n\t</script>")
 43		if templ_7745c5c3_Err != nil {
 44			return templ_7745c5c3_Err
 45		}
 46		var templ_7745c5c3_Var2 = []any{class}
 47		templ_7745c5c3_Err = templ.RenderCSSItems(ctx, templ_7745c5c3_Buffer, templ_7745c5c3_Var2...)
 48		if templ_7745c5c3_Err != nil {
 49			return templ_7745c5c3_Err
 50		}
 51		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<rtw-map map-style=\"")
 52		if templ_7745c5c3_Err != nil {
 53			return templ_7745c5c3_Err
 54		}
 55		var templ_7745c5c3_Var3 string
 56		templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.ResolveAttributeValue(style)
 57		if templ_7745c5c3_Err != nil {
 58			return templ.Error{Err: templ_7745c5c3_Err, FileName: `pages/components/map.templ`, Line: 62, Col: 27}
 59		}
 60		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var3)
 61		if templ_7745c5c3_Err != nil {
 62			return templ_7745c5c3_Err
 63		}
 64		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "\" bbox=\"")
 65		if templ_7745c5c3_Err != nil {
 66			return templ_7745c5c3_Err
 67		}
 68		var templ_7745c5c3_Var4 string
 69		templ_7745c5c3_Var4, templ_7745c5c3_Err = templ.ResolveAttributeValue(toJSON(bbox))
 70		if templ_7745c5c3_Err != nil {
 71			return templ.Error{Err: templ_7745c5c3_Err, FileName: `pages/components/map.templ`, Line: 62, Col: 49}
 72		}
 73		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var4)
 74		if templ_7745c5c3_Err != nil {
 75			return templ_7745c5c3_Err
 76		}
 77		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "\" class=\"")
 78		if templ_7745c5c3_Err != nil {
 79			return templ_7745c5c3_Err
 80		}
 81		var templ_7745c5c3_Var5 string
 82		templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.ResolveAttributeValue(templ.CSSClasses(templ_7745c5c3_Var2).String())
 83		if templ_7745c5c3_Err != nil {
 84			return templ.Error{Err: templ_7745c5c3_Err, FileName: `pages/components/map.templ`, Line: 1, Col: 0}
 85		}
 86		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var5)
 87		if templ_7745c5c3_Err != nil {
 88			return templ_7745c5c3_Err
 89		}
 90		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 5, "\">")
 91		if templ_7745c5c3_Err != nil {
 92			return templ_7745c5c3_Err
 93		}
 94		templ_7745c5c3_Err = templ_7745c5c3_Var1.Render(ctx, templ_7745c5c3_Buffer)
 95		if templ_7745c5c3_Err != nil {
 96			return templ_7745c5c3_Err
 97		}
 98		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 6, "</rtw-map>")
 99		if templ_7745c5c3_Err != nil {
100			return templ_7745c5c3_Err
101		}
102		return nil
103	})
104}
105
106func Route(src string, color string) templ.Component {
107	return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
108		templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
109		if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
110			return templ_7745c5c3_CtxErr
111		}
112		templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W)
113		if !templ_7745c5c3_IsBuffer {
114			defer func() {
115				templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer)
116				if templ_7745c5c3_Err == nil {
117					templ_7745c5c3_Err = templ_7745c5c3_BufErr
118				}
119			}()
120		}
121		ctx = templ.InitializeContext(ctx)
122		templ_7745c5c3_Var6 := templ.GetChildren(ctx)
123		if templ_7745c5c3_Var6 == nil {
124			templ_7745c5c3_Var6 = templ.NopComponent
125		}
126		ctx = templ.ClearChildren(ctx)
127		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 7, "<script type=\"module\">\n\t\tclass RTWRoute extends HTMLElement {\n\t\tmap;\n\t\tconstructor() {\n\t\t  super();\n\t\t}\n\t\tconnectedCallback() {\n\t\t  console.log(\"route connected\");\n\t\t  if (!this.map) {\n\t\t\tthis.dispatchEvent(new CustomEvent('map:context', {\n\t\t\t  detail: {\n\t\t\t\tcb: (map) => {\n\t\t\t\t\tconsole.log(\"received map\", map);\n\t\t\t\t\tthis.map = map;\n\t\t\t\t    this.addRoute();\n\t\t\t\t}\n\t\t\t  },\n\t\t\t  bubbles: true,\n\t\t\t  composed: true\n\t\t\t}));\n\t\t  }\n\t\t}\n\t\taddRoute() {\n\t\t  if (!this.map) return;\n\t\t  const hash = this.getAttribute('src');\n\t\t  console.log(\"got hash\", hash);\n\t\t  if (!hash) return;\n\n\t\t  if (!this.map.isStyleLoaded()) {\n            this.map.once('load', () => this.addRoute());\n          }\n\n\t\t  this.map.addSource(`source-${hash}`, {type: 'geojson', data: `/route/${hash}.geojson`});\n\t\t  this.map.addLayer({\n\t\t\tid: `layer-${hash}`,\n\t\t\tsource: `source-${hash}`,\n\t\t\ttype: 'line',\n\t\t\tlayout: {\n              'line-join': 'round',\n              'line-cap': 'round'\n            },\n            paint: {\n              'line-color': this.getAttribute('color') || '#888',\n              'line-width': 4\n            }\n\t\t  });\n\t\t}\n\t\t}\n\t\tif (!customElements.get('rtw-route')) {\n\t\t  customElements.define('rtw-route', RTWRoute);\n\t\t}\n\t</script><rtw-route src=\"")
128		if templ_7745c5c3_Err != nil {
129			return templ_7745c5c3_Err
130		}
131		var templ_7745c5c3_Var7 string
132		templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.ResolveAttributeValue(src)
133		if templ_7745c5c3_Err != nil {
134			return templ.Error{Err: templ_7745c5c3_Err, FileName: `pages/components/map.templ`, Line: 120, Col: 21}
135		}
136		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var7)
137		if templ_7745c5c3_Err != nil {
138			return templ_7745c5c3_Err
139		}
140		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 8, "\" color=\"")
141		if templ_7745c5c3_Err != nil {
142			return templ_7745c5c3_Err
143		}
144		var templ_7745c5c3_Var8 string
145		templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.ResolveAttributeValue(color)
146		if templ_7745c5c3_Err != nil {
147			return templ.Error{Err: templ_7745c5c3_Err, FileName: `pages/components/map.templ`, Line: 120, Col: 37}
148		}
149		_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ_7745c5c3_Var8)
150		if templ_7745c5c3_Err != nil {
151			return templ_7745c5c3_Err
152		}
153		templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 9, "\"></rtw-route>")
154		if templ_7745c5c3_Err != nil {
155			return templ_7745c5c3_Err
156		}
157		return nil
158	})
159}
160
161var _ = templruntime.GeneratedTemplate