|
146 | 146 | <script src="https://unpkg.com/moveable@latest/dist/moveable.min.js"></script> |
147 | 147 | <script> |
148 | 148 | let highestZIndex = 1000; |
| 149 | + let boxIndex = 0; |
149 | 150 |
|
150 | 151 | const jsonExample = "{\r\n \"data\": [\r\n {\r\n \"x\": [\r\n \"giraffes\",\r\n \"orangutans\",\r\n \"monkeys\"\r\n ],\r\n \"y\": [\r\n 20,\r\n 14,\r\n 23\r\n ],\r\n \"type\": \"bar\"\r\n }\r\n ]\r\n}"; |
151 | 152 |
|
|
155 | 156 | const box = document.createElement('div'); |
156 | 157 | box.className = 'box'; |
157 | 158 | box.style.zIndex = ++highestZIndex; |
| 159 | + let currentBoxIndex = ++boxIndex; |
158 | 160 | box.innerHTML = ` |
159 | 161 | <div class="box-header" data-drag-handle="true"> |
160 | 162 | <div class="header-buttons"> |
|
165 | 167 | </div> |
166 | 168 | <div class="box-body"> |
167 | 169 | <div class="view-input-type"> |
168 | | - <label for="input-type" class="input-label-required">Input Type:</label> |
169 | | - <select id="input-type" class="form-control"> |
| 170 | + <label for="form-input-type-${currentBoxIndex}" class="input-label-required">Input Type</label> |
| 171 | + <select id="form-input-type-${currentBoxIndex}" class="form-control input-type"> |
170 | 172 | <option value="json">JSON Text</option> |
171 | 173 | <option value="http">HTTP Request</option> |
172 | 174 | </select><br> |
173 | 175 | </div> |
174 | 176 | <div class="view-json"> |
175 | | - <label for="form-json-input" class="input-label-required">JSON Data:</label><br> |
176 | | - <textarea id="form-json-input" class="json-input form-control" rows="10"></textarea><br> |
| 177 | + <label for="form-json-input-${currentBoxIndex}" class="input-label-required">JSON Data</label><br> |
| 178 | + <textarea id="form-json-input-${currentBoxIndex}" class="json-input form-control" rows="10"></textarea><br> |
177 | 179 | <button class="btn btn-beautify">Beautify JSON</button><br><br> |
178 | | - <label for="form-json-path">JSON Path</label><br> |
179 | | - <input id="form-json-path" type="text" class="json-path-json form-control" placeholder="data.chart"><br> |
| 180 | + <label for="form-json-path-${currentBoxIndex}">JSON Path</label><br> |
| 181 | + <input id="form-json-path-${currentBoxIndex}" type="text" class="json-path-json form-control" placeholder="data.chart"><br> |
180 | 182 | </div> |
181 | 183 | <div class="view-http hidden"> |
182 | 184 | <form id="requestForm"> |
183 | | - <label for="form-http-url" class="input-label-required">URL:</label> |
184 | | - <input id="form-http-url" type="text" class="http-url form-control" required placeholder="http://localhost:8080/generate?key1=value1&key2=value2"><br> |
185 | | - <label for="form-http-method" class="input-label-required">HTTP Method:</label> |
186 | | - <select id="form-http-method" class="http-method form-control"> |
| 185 | + <label for="form-http-url-${currentBoxIndex}" class="input-label-required">URL</label> |
| 186 | + <input id="form-http-url-${currentBoxIndex}" type="text" class="http-url form-control" required placeholder="http://localhost:8080/generate?key1=value1&key2=value2"><br> |
| 187 | + <label for="form-http-method-${currentBoxIndex}" class="input-label-required">HTTP Method</label> |
| 188 | + <select id="form-http-method-${currentBoxIndex}" class="http-method form-control"> |
187 | 189 | <option value="GET">GET</option> |
188 | 190 | <option value="POST">POST</option> |
189 | 191 | </select><br> |
190 | 192 | <div class="headersContainer"> |
191 | | - <label for="form-http-headers">Headers</label><br> |
192 | | - <textarea id="form-http-headers" class="http-headers form-control" placeholder='Content-Type=application/json\nAccept=*/*'></textarea><br> |
| 193 | + <label for="form-http-headers-${currentBoxIndex}">Headers</label><br> |
| 194 | + <textarea id="form-http-headers-${currentBoxIndex}" class="http-headers form-control" placeholder='Content-Type=application/json\nAccept=*/*'></textarea><br> |
193 | 195 | </div> |
194 | 196 | <div class="bodyContainer"> |
195 | | - <label for="form-http-body">Body</label><br> |
196 | | - <textarea id="form-http-body" class="http-body form-control" placeholder='{"key1": "value1", "key2": "value2"}'></textarea><br> |
| 197 | + <label for="form-http-body-${currentBoxIndex}">Body</label><br> |
| 198 | + <textarea id="form-http-body-${currentBoxIndex}" class="http-body form-control" placeholder='{"key1": "value1", "key2": "value2"}'></textarea><br> |
197 | 199 | </div> |
198 | 200 | <div class="jsonPathContainer"> |
199 | | - <label for="form-http-json-path">JSON Path</label><br> |
200 | | - <input id="form-http-json-path" type="text" class="json-path-http form-control" placeholder="data.chart"><br> |
| 201 | + <label for="form-http-json-path-${currentBoxIndex}">JSON Path</label><br> |
| 202 | + <input id="form-http-json-path-${currentBoxIndex}" type="text" class="json-path-http form-control" placeholder="data.chart"><br> |
201 | 203 | </div> |
202 | 204 | </form> |
203 | 205 | </div> |
|
208 | 210 | `; |
209 | 211 | document.body.appendChild(box); |
210 | 212 |
|
211 | | - const headerEl = box.querySelector('[data-drag-handle]'); |
| 213 | + const dragHeader = box.querySelector('[data-drag-handle]'); |
212 | 214 |
|
213 | 215 | const moveable = new Moveable(document.body, { |
214 | 216 | target: box, |
| 217 | + dragTarget: dragHeader, |
215 | 218 | resizable: true, |
216 | 219 | keepRatio: false, |
217 | 220 | edge: true, |
218 | 221 | draggable: true, |
219 | 222 | checkInput: true, |
220 | | - checkSelect: true, |
221 | 223 | }); |
222 | 224 |
|
223 | | - box.querySelector('#input-type').addEventListener("mousedown", (e) => { |
| 225 | + box.querySelector('.input-type').addEventListener("mousedown", (e) => { |
224 | 226 | e.stopPropagation(); |
225 | 227 | }); |
226 | 228 |
|
227 | | - box.querySelector('#form-http-method').addEventListener("mousedown", (e) => { |
| 229 | + box.querySelector('.http-method').addEventListener("mousedown", (e) => { |
228 | 230 | e.stopPropagation(); |
229 | 231 | }); |
230 | 232 |
|
|
261 | 263 | }); |
262 | 264 | box.querySelector('.btn-beautify').addEventListener('click', () => beautifyJson(box)); |
263 | 265 |
|
264 | | - box.querySelector('#input-type').addEventListener('change', (event) => switchInputType(box, event.target.value)); |
| 266 | + box.querySelector('.input-type').addEventListener('change', (event) => switchInputType(box, event.target.value)); |
265 | 267 |
|
266 | 268 | box.querySelector('.json-input').value = jsonExample; |
267 | 269 | } |
|
295 | 297 | } |
296 | 298 |
|
297 | 299 | function switchToJson(box) { |
298 | | - const inputType = box.querySelector('#input-type').value; |
| 300 | + const inputType = box.querySelector('.input-type').value; |
299 | 301 | const viewJson = box.querySelector('.view-json'); |
300 | 302 | const viewHttp = box.querySelector('.view-http'); |
301 | 303 | const viewChart = box.querySelector('.view-chart'); |
|
316 | 318 | } |
317 | 319 |
|
318 | 320 | async function reloadPlot(box) { |
319 | | - const inputType = box.querySelector('#input-type').value; |
| 321 | + const inputType = box.querySelector('.input-type').value; |
320 | 322 | let plotData; |
321 | 323 |
|
322 | 324 | try { |
|
369 | 371 | switchToChart(box); // Switch to chart view immediately after generating the plot |
370 | 372 | } catch (error) { |
371 | 373 | console.error('Error:', error); |
372 | | - alert('Invalid JSON data or HTTP request'); |
| 374 | + const inputType = box.querySelector('.input-type').value; |
| 375 | + |
| 376 | + if (inputType === 'json') { |
| 377 | + alert('Invalid JSON data'); |
| 378 | + } else { |
| 379 | + alert('Invalid HTTP request'); |
| 380 | + } |
373 | 381 | } |
374 | 382 | } |
375 | 383 |
|
|
0 commit comments