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
|
|
|
|
|
2014-07-04 19:28:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Refactoring of inline script from index.html.
|
2014-07-09 16:45:44 +00:00
|
|
|
/*global SwaggerUi, log, ApiKeyAuthorization, hljs, window, $ */
|
2014-07-04 19:28:47 +00:00
|
|
|
$(function() {
|
2015-08-11 16:52:56 +00:00
|
|
|
// Pre load translate...
|
2016-04-22 21:09:48 +00:00
|
|
|
if (window.SwaggerTranslator) {
|
2015-08-11 16:52:56 +00:00
|
|
|
window.SwaggerTranslator.translate();
|
|
|
|
}
|
|
|
|
|
2014-11-10 10:11:25 +00:00
|
|
|
var lsKey = 'swagger_accessToken';
|
2014-07-09 16:45:44 +00:00
|
|
|
$.getJSON('config.json', function(config) {
|
2014-11-10 10:11:25 +00:00
|
|
|
log(config);
|
|
|
|
loadSwaggerUi(config);
|
2014-07-09 16:45:44 +00:00
|
|
|
});
|
2014-07-04 19:28:47 +00:00
|
|
|
|
2014-07-09 16:45:44 +00:00
|
|
|
var accessToken;
|
|
|
|
function loadSwaggerUi(config) {
|
2015-09-08 02:55:53 +00:00
|
|
|
var methodOrder = ['get', 'head', 'options', 'put', 'post', 'delete'];
|
2016-04-22 21:14:57 +00:00
|
|
|
/* eslint-disable camelcase */
|
2014-07-04 19:28:47 +00:00
|
|
|
window.swaggerUi = new SwaggerUi({
|
2015-10-01 07:08:27 +00:00
|
|
|
validatorUrl: null,
|
2014-07-09 16:45:44 +00:00
|
|
|
url: config.url || '/swagger/resources',
|
|
|
|
apiKey: '',
|
|
|
|
dom_id: 'swagger-ui-container',
|
2014-07-04 19:28:47 +00:00
|
|
|
supportHeaderParams: true,
|
|
|
|
onComplete: function(swaggerApi, swaggerUi) {
|
2014-07-09 16:45:44 +00:00
|
|
|
log('Loaded SwaggerUI');
|
2014-07-04 19:28:47 +00:00
|
|
|
log(swaggerApi);
|
|
|
|
log(swaggerUi);
|
2015-08-11 16:52:56 +00:00
|
|
|
|
2016-04-22 21:09:48 +00:00
|
|
|
if (window.SwaggerTranslator) {
|
2015-08-11 16:52:56 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2014-07-04 19:28:47 +00:00
|
|
|
},
|
|
|
|
onFailure: function(data) {
|
2014-07-09 16:45:44 +00:00
|
|
|
log('Unable to Load SwaggerUI');
|
2014-07-04 19:28:47 +00:00
|
|
|
log(data);
|
|
|
|
},
|
2014-10-07 09:07:41 +00:00
|
|
|
docExpansion: 'none',
|
2014-10-07 09:12:47 +00:00
|
|
|
highlightSizeThreshold: 16384,
|
2015-09-08 02:55:53 +00:00
|
|
|
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
|
|
|
},
|
2014-07-04 19:28:47 +00:00
|
|
|
});
|
2016-04-22 21:14:57 +00:00
|
|
|
/* eslint-disable camelcase */
|
2014-07-04 19:28:47 +00:00
|
|
|
|
2014-07-09 16:45:44 +00:00
|
|
|
$('#explore').click(setAccessToken);
|
|
|
|
$('#api_selector').submit(setAccessToken);
|
|
|
|
$('#input_accessToken').keyup(onInputChange);
|
2014-07-04 19:28:47 +00:00
|
|
|
|
|
|
|
window.swaggerUi.load();
|
2014-07-09 16:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() !== '') {
|
2014-07-09 16:45:44 +00:00
|
|
|
log('added accessToken ' + key);
|
2016-04-22 21:14:57 +00:00
|
|
|
var apiKeyAuth =
|
|
|
|
new SwaggerClient.ApiKeyAuthorization('access_token', key, 'query');
|
2015-08-11 16:52:56 +00:00
|
|
|
window.swaggerUi.api.clientAuthorizations.add('key', apiKeyAuth);
|
2014-07-09 16:45:44 +00:00
|
|
|
accessToken = key;
|
2014-07-09 17:03:34 +00:00
|
|
|
$('.accessTokenDisplay').text('Token Set.').addClass('set');
|
2014-07-09 16:45:44 +00:00
|
|
|
$('.accessTokenDisplay').attr('data-tooltip', 'Current Token: ' + key);
|
2015-08-11 16:52:56 +00:00
|
|
|
|
|
|
|
// Save this token to localStorage if we can to make it persist on refresh.
|
2014-11-10 10:11:25 +00:00
|
|
|
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.
|
2015-03-06 05:32:45 +00:00
|
|
|
log('removed accessToken.');
|
|
|
|
$('.accessTokenDisplay').text('Token Not Set.').removeClass('set');
|
|
|
|
$('.accessTokenDisplay').removeAttr('data-tooltip');
|
2015-08-11 16:52:56 +00:00
|
|
|
if (window.swaggerUi) {
|
|
|
|
window.swaggerUi.api.clientAuthorizations.remove('key');
|
|
|
|
}
|
2015-03-06 05:32:45 +00:00
|
|
|
if (window.localStorage) {
|
|
|
|
window.localStorage.removeItem(lsKey);
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 16:45:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onInputChange(e) {
|
|
|
|
var el = e.currentTarget;
|
|
|
|
var key = $(e.currentTarget)[0].value;
|
|
|
|
if (!key || key.trim === '') return;
|
|
|
|
if (accessToken !== key) {
|
2014-07-09 17:03:34 +00:00
|
|
|
$('.accessTokenDisplay').text('Token changed; submit to confirm.');
|
2014-07-09 16:45:44 +00:00
|
|
|
} else {
|
2014-07-09 17:03:34 +00:00
|
|
|
$('.accessTokenDisplay').text('Token Set.');
|
2014-07-09 16:45:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-08-11 16:52:56 +00:00
|
|
|
|
|
|
|
function log() {
|
|
|
|
if ('console' in window) {
|
|
|
|
console.log.apply(console, arguments);
|
|
|
|
}
|
|
|
|
}
|
2014-07-09 16:45:44 +00:00
|
|
|
});
|