|
140 | 140 | <paper-textarea label="OpenAPI text content" id="ot"></paper-textarea> |
141 | 141 | <paper-button class="action-button" raised on-click="_oasText">Go</paper-button> |
142 | 142 | </section> |
| 143 | + |
143 | 144 | <section> |
144 | 145 | <div class="row"> |
145 | | - <paper-input label="AMF main file location or zip" type="url" id="au"></paper-input> |
| 146 | + <paper-input label="AMF model file location" type="url" id="au"></paper-input> |
146 | 147 | <paper-button class="action-button" raised on-click="_amfUrl">Go</paper-button> |
147 | 148 | </div> |
148 | 149 | or |
149 | 150 | <div class="row"> |
150 | | - <paper-input label="AMF zip file or single api file" type="file" id="af"></paper-input> |
| 151 | + <paper-input label="AMF model file" type="file" accept=".json,application/json" id="af"></paper-input> |
151 | 152 | <paper-button class="action-button" raised on-click="_amfFile">Go</paper-button> |
152 | 153 | </div> |
153 | 154 | or |
|
176 | 177 | * @memberof ApiElements |
177 | 178 | */ |
178 | 179 | class ParserElement extends Polymer.Element { |
179 | | - static get is() { return 'parser-element'; } |
| 180 | + static get is() { |
| 181 | + return 'parser-element'; |
| 182 | + } |
180 | 183 | static get properties() { |
181 | 184 | return { |
182 | | - selected:{ |
| 185 | + selected: { |
183 | 186 | type: Number, |
184 | 187 | value: 0 |
185 | 188 | }, |
|
387 | 390 | this._error('Please, provide a value for the data location'); |
388 | 391 | return; |
389 | 392 | } |
390 | | - const opts = { |
391 | | - dataType: 'amf', |
392 | | - dataFormat: 'url', |
393 | | - dataValue: value |
394 | | - }; |
395 | | - this._processText(opts); |
| 393 | + this._processDownloadModel(value); |
396 | 394 | } |
397 | 395 |
|
398 | 396 | _amfFile() { |
|
401 | 399 | this._error('Please, select file first'); |
402 | 400 | return; |
403 | 401 | } |
404 | | - const opts = { |
405 | | - dataType: 'amf', |
406 | | - dataFormat: 'file', |
407 | | - dataValue: value |
408 | | - }; |
409 | | - this._processFile(opts); |
| 402 | + this._processAmfFile(value); |
410 | 403 | } |
411 | 404 |
|
412 | 405 | _amfText() { |
|
431 | 424 | } |
432 | 425 |
|
433 | 426 | _loadFile(file) { |
434 | | - this.loading = true; |
435 | 427 | const url = '../models/' + file; |
436 | | - const xhr = new XMLHttpRequest(); |
437 | | - xhr.addEventListener('error', () => { |
438 | | - this._error('Request error. File not found.'); |
| 428 | + this._processDownloadModel(url); |
| 429 | + } |
| 430 | + |
| 431 | + _processDownloadModel(modelLocation) { |
| 432 | + this.loading = true; |
| 433 | + this._downloadFile(modelLocation) |
| 434 | + .then((data) => { |
| 435 | + this.loading = false; |
| 436 | + this._setAmfValue(data); |
| 437 | + }) |
| 438 | + .catch((cause) => { |
| 439 | + this.loading = false; |
| 440 | + this._error(cause.message); |
439 | 441 | }); |
440 | | - xhr.addEventListener('load', (e) => this._setAmfValue(e.target.response)); |
441 | | - xhr.open('GET', url, true); |
442 | | - try { |
443 | | - xhr.send(); |
444 | | - } catch (e) { |
445 | | - this._error(e.message); |
446 | | - } |
| 442 | + } |
| 443 | + |
| 444 | + _downloadFile(fileLocation) { |
| 445 | + return new Promise((resolve, reject) => { |
| 446 | + const xhr = new XMLHttpRequest(); |
| 447 | + xhr.addEventListener('load', (e) => resolve(e.target.response)); |
| 448 | + xhr.addEventListener('error', () => reject(new Error('Unable to download ' + fileLocation))); |
| 449 | + xhr.open('GET', fileLocation, true); |
| 450 | + try { |
| 451 | + xhr.send(); |
| 452 | + } catch (e) { |
| 453 | + reject(new Error(e.message)); |
| 454 | + } |
| 455 | + }); |
| 456 | + } |
| 457 | + |
| 458 | + _processAmfFile(file) { |
| 459 | + this.loading = true; |
| 460 | + const reader = new FileReader(); |
| 461 | + reader.onload = (e) => { |
| 462 | + this.loading = false; |
| 463 | + this._setAmfValue(e.target.result); |
| 464 | + }; |
| 465 | + reader.onerror = () => { |
| 466 | + this.loading = false; |
| 467 | + this._error('Unable to read the file'); |
| 468 | + }; |
| 469 | + reader.readAsText(file); |
447 | 470 | } |
448 | 471 | } |
449 | 472 | window.customElements.define(ParserElement.is, ParserElement); |
|
0 commit comments