|
13 | 13 | <title>WorkAdventure build your map</title> |
14 | 14 | <link rel="icon" href="public/images/favicon.svg" type="image/svg+xml"> |
15 | 15 | <script type="module"> |
16 | | - document.addEventListener("DOMContentLoaded", (event) => { |
17 | | - // Load index.js to have access to getMapsList |
18 | | - import('/public/assets/js/index.js').then(() => { |
19 | | - loadTMJ(); |
| 16 | + document.addEventListener("DOMContentLoaded", () => { |
| 17 | + import('/public/assets/js/index.js').then((module) => { |
| 18 | + module.loadTMJ(); |
20 | 19 | }); |
21 | 20 | }); |
22 | | - |
23 | | - async function loadTMJ() { |
24 | | - try { |
25 | | - // Get the list of maps from the API |
26 | | - const maps = await window.getMapsList(); |
27 | | - |
28 | | - // Retrieve map images for background fade |
29 | | - const mapImages = maps |
30 | | - .map(map => { |
31 | | - if (map.mapImage) { |
32 | | - return map.mapImage.startsWith('http') ? map.mapImage : `/${map.mapImage}`; |
33 | | - } |
34 | | - return null; |
35 | | - }) |
36 | | - .filter(img => img !== null); |
37 | | - |
38 | | - // Create background image fade |
39 | | - if (mapImages.length > 0) { |
40 | | - await window.createBackgroundImageFade(mapImages); |
41 | | - } |
42 | | - |
43 | | - // Mustache template for a map card |
44 | | - const cardTemplate = ` |
45 | | - <div class="map-cover" style="background-image: url('{{mapImageUrl}}');"></div> |
46 | | - <div class="map-date"> |
47 | | - Last edit: {{lastModifiedFormatted}} |
48 | | - </div> |
49 | | - <div class="map-name"> |
50 | | - {{mapName}} |
51 | | - </div> |
52 | | - <div class="map-detail"> |
53 | | - <div class="map-file"> |
54 | | - <strong>{{filename}}</strong>.tmj |
55 | | - </div> |
56 | | - <div class="map-weight"> |
57 | | - <strong>{{size}}</strong> |
58 | | - <span style="opacity: .5">Mo</span> |
59 | | - </div> |
60 | | - </div> |
61 | | - <div class="map-desc"> |
62 | | - {{mapDescription}} |
63 | | - </div> |
64 | | - <div class="map-testurl"> |
65 | | - <a href="#" class="btn" data-map-path="{{path}}">Test my map</a> |
66 | | - </div> |
67 | | - `; |
68 | | - |
69 | | - // Prepare data for Mustache |
70 | | - const mapsData = maps.map(map => { |
71 | | - // Build the image URL - use the first available image or a default image |
72 | | - const mapImageUrl = map.mapImage |
73 | | - ? (map.mapImage.startsWith('http') ? map.mapImage : `/${map.mapImage}`) |
74 | | - : (mapImages.length > 0 ? mapImages[0] : 'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" width="1620" height="1024"><rect fill="%231b2a41" width="100%" height="100%"/></svg>'); |
75 | | - |
76 | | - return { |
77 | | - ...map, |
78 | | - mapImageUrl: mapImageUrl, |
79 | | - mapDescription: map.mapDescription || 'No description available' |
80 | | - }; |
81 | | - }); |
82 | | - |
83 | | - // Render each map with Mustache and inject them into the container |
84 | | - const mainElement = document.querySelector('main'); |
85 | | - if (mainElement) { |
86 | | - // Clear existing content |
87 | | - mainElement.innerHTML = ''; |
88 | | - |
89 | | - // Create a section for each map |
90 | | - mapsData.forEach(map => { |
91 | | - const section = document.createElement('section'); |
92 | | - section.className = 'card-map'; |
93 | | - section.innerHTML = Mustache.render(cardTemplate, map); |
94 | | - |
95 | | - // Add an event handler for the "Test my map" button |
96 | | - const testBtn = section.querySelector('.map-testurl a'); |
97 | | - if (testBtn) { |
98 | | - testBtn.addEventListener('click', (e) => { |
99 | | - e.preventDefault(); |
100 | | - const host = window.location.host; |
101 | | - let path = window.location.pathname; |
102 | | - if (path.endsWith('index.html')) { |
103 | | - path = path.substr(0, path.length - 'index.html'.length); |
104 | | - } |
105 | | - const instanceId = Math.random().toString(36).substring(2, 15); |
106 | | - const url = `https://play.workadventu.re/_/${instanceId}/${host}${path}${map.path}`; |
107 | | - window.open(url, '_blank'); |
108 | | - }); |
109 | | - } |
110 | | - |
111 | | - mainElement.appendChild(section); |
112 | | - }); |
113 | | - } |
114 | | - } catch (error) { |
115 | | - console.error('Error loading maps:', error); |
116 | | - } |
117 | | - } |
118 | 21 | </script> |
119 | 22 | </head> |
120 | 23 |
|
|
150 | 53 | </div> |
151 | 54 | </header> |
152 | 55 | <main> |
153 | | - <!-- Map cards will be injected here dynamically by Mustache --> |
| 56 | + <!-- Map cards are injected here by index.js --> |
154 | 57 | </main> |
155 | 58 | <div class="button-wrapper"> |
156 | 59 | <div style="flex-grow: 1;"> |
|
0 commit comments