Skip to content

Commit 3441a09

Browse files
committed
Fixing demo page to accept AMF model
1 parent f6950f0 commit 3441a09

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

demo/parser-element/parser-element.html

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@
140140
<paper-textarea label="OpenAPI text content" id="ot"></paper-textarea>
141141
<paper-button class="action-button" raised on-click="_oasText">Go</paper-button>
142142
</section>
143+
143144
<section>
144145
<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>
146147
<paper-button class="action-button" raised on-click="_amfUrl">Go</paper-button>
147148
</div>
148149
or
149150
<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>
151152
<paper-button class="action-button" raised on-click="_amfFile">Go</paper-button>
152153
</div>
153154
or
@@ -176,10 +177,12 @@
176177
* @memberof ApiElements
177178
*/
178179
class ParserElement extends Polymer.Element {
179-
static get is() { return 'parser-element'; }
180+
static get is() {
181+
return 'parser-element';
182+
}
180183
static get properties() {
181184
return {
182-
selected:{
185+
selected: {
183186
type: Number,
184187
value: 0
185188
},
@@ -387,12 +390,7 @@
387390
this._error('Please, provide a value for the data location');
388391
return;
389392
}
390-
const opts = {
391-
dataType: 'amf',
392-
dataFormat: 'url',
393-
dataValue: value
394-
};
395-
this._processText(opts);
393+
this._processDownloadModel(value);
396394
}
397395

398396
_amfFile() {
@@ -401,12 +399,7 @@
401399
this._error('Please, select file first');
402400
return;
403401
}
404-
const opts = {
405-
dataType: 'amf',
406-
dataFormat: 'file',
407-
dataValue: value
408-
};
409-
this._processFile(opts);
402+
this._processAmfFile(value);
410403
}
411404

412405
_amfText() {
@@ -431,19 +424,49 @@
431424
}
432425

433426
_loadFile(file) {
434-
this.loading = true;
435427
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);
439441
});
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);
447470
}
448471
}
449472
window.customElements.define(ParserElement.is, ParserElement);

0 commit comments

Comments
 (0)