-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
458 lines (416 loc) · 12.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import {inject, bindable} from 'aurelia-framework';
import {I18N} from 'aurelia-i18n';
import {EventAggregator} from 'aurelia-event-aggregator';
import {parseJSON} from './resources/jsonparser';
import {Field} from './resources/form/abstract/field';
import {schema, fieldsToShow} from './schemas/index';
import {Validation} from './validation';
import * as space from './space';
import YAML from 'yamljs';
import $ from 'jquery';
window.$ = $;
window.jQuery = $;
import SwaggerUIBundle from 'swagger-ui';
import SwaggerUIStandalonePreset from 'swagger-ui/swagger-ui-standalone-preset';
// I have no idea why this works.
// PNotify has a very interesting module system that I couldn't get to work work
// when importing in any other way than by using the monstrosity below.
// Aurelia is probably not completely guilt-free either.
//
// To add a new pnotify module, you must add it to both of the arrays below in
// the same format as the existing extra modules.
require(
[
'pnotify',
'pnotify/pnotify.animate',
'pnotify/pnotify.confirm'
],
() => require(
[
'pnotify',
'pnotify.animate',
'pnotify.confirm'
],
pnfTemp => window.PNotify = pnfTemp));
window.stack_bottomright = {
dir1: 'up',
dir2: 'left',
push: 'up'
};
const DEFAULT_SPLIT = 'editor';
@inject(I18N, EventAggregator)
export class App {
@bindable
language = window.localStorage.language || 'en';
enableBranding = true;
spaceLoginApinf = false;
ieWarningDismissed = window.localStorage.ieWarningDismissed === 'true';
cookieWarningDismissed = window.localStorage.cookieWarningDismissed === 'true';
toggleSpaceLoginType() {
this.spaceLoginApinf = !this.spaceLoginApinf;
}
dismissIEWarning() {
this.ieWarningDismissed = true;
window.localStorage.ieWarningDismissed = true;
}
dismissCookieWarning() {
this.cookieWarningDismissed = true;
window.localStorage.cookieWarningDismissed = true;
}
constructor(i18n, ea) {
this.i18n = i18n;
Field.internationalizer = i18n;
Field.eventAggregator = ea;
Field.validationFunctions = new Validation(i18n);
// Allow access from browser console
window.$oai = this;
try {
const pointerlessSchema = $.extend(true, {}, schema);
this.forms = parseJSON('form', pointerlessSchema);
if (window.localStorage['openapi-v2-design']) {
const savedData = JSON.parse(window.localStorage['openapi-v2-design']);
this.forms.setValue(savedData);
}
} catch (exception) {
console.error(exception);
this.exception = exception;
return;
}
this.activeForm = 'about';
window.onhashchange = () => {
const formID = window.location.hash.substr(2) || 'about';
if (typeof this.activeForm === 'object') {
this.activeForm.show = false;
}
if (formID === 'about') {
this.activeForm = 'about';
} else if (this.forms.hasChild(formID)) {
this.activeForm = this.forms.getChild(formID);
this.activeForm.show = true;
} else {
return;
}
$('.nav > .editor-nav > .nav-link.open').removeClass('open');
$(`#nav-${formID}`).addClass('open');
};
this.forms.addChangeListener(() => this.saveFormLocal());
window.addEventListener('message', ({data}) => {
if (data.swagger) {
if (!data.noDelete) {
delete(true);
}
this.forms.setValue(data);
}
}, false);
}
bind() {
this.split(window.localStorage.split || DEFAULT_SPLIT, true);
}
languageChanged() {
window.localStorage.language = this.language;
location.reload();
}
spaceLogin() {
space.login(this, this.spaceUsername.value, this.spacePassword.value, this.spaceLoginApinf ? 'apinf' : 'space');
}
spaceRegister() {
space.register(this, this.spaceEmail.value, this.spaceRegisterUsername.value, this.spaceRegisterPassword.value);
}
logout() {
space.logout(this);
}
showRichPreview() {
if (!this.richPreviewObj) {
// The DOM isn't ready yet, but swagger-ui requires it to be ready.
// Let's try again a bit later.
setTimeout(() => this.showRichPreview(), 500);
return;
}
setTimeout(() => {
this.richPreview = new SwaggerUIBundle({
spec: this.getFormData(),
dom_id: '#rich-preview',
// Disable Swagger.io online validation (AKA spyware)
validatorUrl: null,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: 'StandaloneLayout'
});
}, 0);
}
hideRichPreview() {
this.richPreview = undefined;
$(this.richPreviewObj).empty();
}
split(type, force = false) {
if (type === 'preview') {
if (!force) {
let errors = {};
this.forms.revalidate(errors);
this.richPreviewErrors = Object.entries(errors);
if (this.richPreviewErrors.length > 0) {
this.richPreviewErrorModal.open();
return;
}
} else if (this.richPreviewErrorModal) {
this.richPreviewErrorModal.close();
}
}
this.previousSplit = window.localStorage.split || DEFAULT_SPLIT;
this.showEditor = type === 'editor' || type === 'split';
this.showOutput = type === 'output';
this.splitView = type === 'split';
this.showPreview = type === 'preview';
if (this.showPreview) {
this.showRichPreview();
} else if (this.richPreview) {
this.hideRichPreview();
}
window.localStorage.split = type;
}
notify(title, text = '', type = 'info', url = '', onclick = undefined) {
/*eslint no-new: 0*/
const notif = new PNotify({
title,
text,
type,
stack: stack_bottomright,
addclass: 'stack-bottomright',
animate: {
animate: true,
in_class: 'slideInUp',
out_class: 'slideOutDown'
}
});
notif.get().click(() => {
notif.remove();
if (onclick) {
onclick();
}
if (url) {
window.open(url, '_blank');
}
});
}
confirm(title, text, type = 'info') {
return new Promise((resolve, reject) => {
const notif = new PNotify({
title,
text,
type,
stack: stack_bottomright,
addclass: 'stack-bottomright',
animate: {
animate: true,
in_class: 'bounceInUp',
out_class: 'bounceOutDown'
},
confirm: {
confirm: true
}
});
notif.get()
.on('pnotify.confirm', resolve)
.on('pnotify.cancel', reject)
.click(() => notif.remove());
});
}
attached() {
if (window.onhashchange) {
window.onhashchange();
}
}
setLanguage(lang) {
window.localStorage.language = lang;
location.reload();
}
importOutputEditor() {
const rawData = $(this.outputEditor).val();
let data;
try {
data = YAML.parse(rawData);
} catch (_) {
try {
data = JSON.parse(rawData);
} catch (ex) {
this.notify(
this.i18n.tr('notify.import-failed.title'),
this.i18n.tr('notify.import-failed.body', {error: ex}),
'error');
return;
}
}
delete(true);
this.forms.setValue(data);
this.notify(
this.i18n.tr('notify.editor-import-complete.title'),
this.i18n.tr('notify.editor-import-complete.body', {
title: this.forms.resolveRef('#/header/info/title').getValue(),
version: this.forms.resolveRef('#/header/info/version').getValue()
}),
'success'
);
}
importFile() {
delete(true);
const fileInput = $('<input/>', { type: 'file' });
fileInput.css({display: 'none'});
fileInput.appendTo('body');
fileInput.trigger('click');
// IE/Edge don't want to trigger change events, so I have to do it for them...
//
// Check that the browser is IE/Edge
if (!!window.StyleMedia) {
// Check if there is a file every 0.5 seconds
const interval = setInterval(() => {
if (fileInput[0].files[0]) {
// The file was found, so trigger a change event and stop the interval.
fileInput.trigger('change');
clearInterval(interval);
}
}, 500);
}
fileInput.change(() => {
const file = fileInput[0].files[0];
const reader = new FileReader();
reader.addEventListener('load', () => {
let data;
try {
if (file.name.endsWith('.yaml') || file.name.endsWith('.yml')) {
data = YAML.parse(reader.result);
} else {
data = JSON.parse(reader.result);
}
} catch (ex) {
this.notify(
this.i18n.tr('notify.import-failed.title'),
this.i18n.tr('notify.import-failed.body', {error: ex}),
'error');
return;
}
this.forms.setValue(data);
this.notify(
this.i18n.tr('notify.import-complete.title'),
this.i18n.tr('notify.import-complete.body', {
title: this.forms.resolveRef('#/header/info/title').getValue(),
version: this.forms.resolveRef('#/header/info/version').getValue()
}),
'success'
);
}, false);
reader.readAsText(file);
});
}
download(type, force) {
if (!force) {
let errors = {};
this.forms.revalidate(errors);
this.downloadErrors = Object.entries(errors);
if (this.downloadErrors.length > 0) {
$(this.downloadErrorBox).attr('data-dl-type', type);
this.downloadErrorModal.open();
return;
}
} else {
this.downloadErrorModal.close();
}
this.downloadErrors = [];
if (!type) {
type = $(this.downloadErrorBox).attr('data-dl-type');
}
let data;
if (type === 'json') {
data = this.json;
} else if (type === 'yaml') {
data = this.yaml;
} else {
return;
}
// Add an anchor element that has the data as the href attribute, then click
// the element to download the data.
const blob = new Blob([data], {type: 'text/json'});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, `swagger.${type}`);
} else {
const downloadLink = document.createElement('a');
downloadLink.href = window.URL.createObjectURL(blob);
downloadLink.download = `swagger.${type}`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
}
}
uploadSpace() {
space.upload.call(space, this.getFormData(), this);
}
publish() {
space.publish.call(space, this.getFormData(), this);
}
delete(force = false, notify = false) {
if (!force) {
this.confirm(
this.i18n.tr('confirm.delete.title'),
this.i18n.tr('confirm.delete.body')
).then(() => this.delete(true, notify))
.catch(() => void (0));
return;
}
let oldData;
if (notify) {
oldData = this.forms.getValue();
}
const pointerlessSchema = $.extend(true, {}, schema);
this.forms = parseJSON('form', pointerlessSchema);
delete localStorage['openapi-v2-design'];
window.onhashchange();
if (notify) {
this.notify(
this.i18n.tr('notify.delete.title'),
this.i18n.tr('notify.delete.body'),
'info',
'',
() => {
this.delete(true, false);
this.forms.setValue(oldData);
});
}
}
getFormData() {
const data = this.forms.getValue();
data.swagger = '2.0';
return data;
}
get yaml() {
return YAML.stringify(this.getFormData(), 10, 2);
}
get json() {
const data = JSON.stringify(this.getFormData(), '', ' ');
return data;
}
saveFormLocal() {
window.localStorage['openapi-v2-design'] = JSON.stringify(this.getFormData());
}
get currentFormJSON() {
if (fieldsToShow.hasOwnProperty(this.activeForm.id)) {
const rawData = this.getFormData();
let output = '';
for (const field of fieldsToShow[this.activeForm.id]) {
if (rawData[field]) {
output += `"${field}": ${JSON.stringify(rawData[field], '', ' ')}\n`;
}
}
return output;
}
const data = this.activeForm.getValue();
if (!data || (typeof data === 'object' && Object.entries(data).length === 0)) {
return '';
}
const stringData = JSON.stringify(data, '', ' ');
return `"${this.activeForm.id}": ${stringData}`;
}
}