loopback-component-explorer/public/lib/loadSwaggerUI.js

126 lines
3.8 KiB
JavaScript
Raw Normal View History

2016-05-06 00:10:55 +00:00
// Copyright IBM Corp. 2014,2016. All Rights Reserved.
// Node module: loopback-component-explorer
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
// Refactoring of inline script from index.html.
/*global SwaggerUi, log, ApiKeyAuthorization, hljs, window, $ */
$(function() {
// Pre load translate...
2016-04-22 21:09:48 +00:00
if (window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
var lsKey = 'swagger_accessToken';
$.getJSON('config.json', function(config) {
log(config);
loadSwaggerUi(config);
});
var accessToken;
function loadSwaggerUi(config) {
var methodOrder = ['get', 'head', 'options', 'put', 'post', 'delete'];
2016-04-22 21:14:57 +00:00
/* eslint-disable camelcase */
window.swaggerUi = new SwaggerUi({
2015-10-01 07:08:27 +00:00
validatorUrl: null,
url: config.url || '/swagger/resources',
apiKey: '',
dom_id: 'swagger-ui-container',
supportHeaderParams: true,
onComplete: function(swaggerApi, swaggerUi) {
log('Loaded SwaggerUI');
log(swaggerApi);
log(swaggerUi);
2016-04-22 21:09:48 +00:00
if (window.SwaggerTranslator) {
window.SwaggerTranslator.translate();
}
$('pre code').each(function(i, e) {
hljs.highlightBlock(e);
});
// Recover accessToken from localStorage if present.
if (window.localStorage) {
var key = window.localStorage.getItem(lsKey);
if (key) {
$('#input_accessToken').val(key).submit();
}
}
},
onFailure: function(data) {
log('Unable to Load SwaggerUI');
log(data);
},
docExpansion: 'none',
highlightSizeThreshold: 16384,
apisSorter: 'alpha',
operationsSorter: function(a, b) {
var pathCompare = a.path.localeCompare(b.path);
return pathCompare !== 0 ?
pathCompare :
methodOrder.indexOf(a.method) - methodOrder.indexOf(b.method);
2016-04-22 21:09:48 +00:00
},
});
2016-04-22 21:14:57 +00:00
/* eslint-disable camelcase */
$('#explore').click(setAccessToken);
$('#api_selector').submit(setAccessToken);
$('#input_accessToken').keyup(onInputChange);
window.swaggerUi.load();
}
function setAccessToken(e) {
e.stopPropagation(); // Don't let the default #explore handler fire
e.preventDefault();
var key = $('#input_accessToken')[0].value;
log('key: ' + key);
2016-04-22 21:09:48 +00:00
if (key && key.trim() !== '') {
log('added accessToken ' + key);
2016-04-22 21:14:57 +00:00
var apiKeyAuth =
new SwaggerClient.ApiKeyAuthorization('access_token', key, 'query');
window.swaggerUi.api.clientAuthorizations.add('key', apiKeyAuth);
accessToken = key;
$('.accessTokenDisplay').text('Token Set.').addClass('set');
$('.accessTokenDisplay').attr('data-tooltip', 'Current Token: ' + key);
// Save this token to localStorage if we can to make it persist on refresh.
if (window.localStorage) {
window.localStorage.setItem(lsKey, key);
}
2016-04-22 21:14:57 +00:00
} else {
// If submitted with an empty token, remove the current token. Can be
// useful to intentionally remove authorization.
log('removed accessToken.');
$('.accessTokenDisplay').text('Token Not Set.').removeClass('set');
$('.accessTokenDisplay').removeAttr('data-tooltip');
if (window.swaggerUi) {
window.swaggerUi.api.clientAuthorizations.remove('key');
}
if (window.localStorage) {
window.localStorage.removeItem(lsKey);
}
}
}
function onInputChange(e) {
var el = e.currentTarget;
var key = $(e.currentTarget)[0].value;
if (!key || key.trim === '') return;
if (accessToken !== key) {
$('.accessTokenDisplay').text('Token changed; submit to confirm.');
} else {
$('.accessTokenDisplay').text('Token Set.');
}
}
function log() {
if ('console' in window) {
console.log.apply(console, arguments);
}
}
});