/**
* @Swagger拓展
* @Company Fantasy
*/
(function () {
'use strict';
//扩展,国际化
var locales = {
en: {
"explore": "explore",
"Show/Hide": "Show/Hide",
"List Operations": "List Operations",
"Expand Operations": "Expand Operations",
"base url": "base url",
"api version": "api version",
"Response Class": "Response Class",
"Status": "Status",
"Implementation Notes": "Implementation Notes",
"Model": "Model",
"Model Schema": "Model Schema",
"Response Content Type": "Response Content Type",
"Parameters": "Parameters",
"Parameter": "Parameter",
"Value": "Value",
"Description": "Description",
"Parameter Type": "Parameter Type",
"Data Type": "Data Type",
"Parameter content type": "Parameter content type",
"Click to set as parameter value": "Click to set as parameter value",
"(required)": "(required)",
"Response Messages": "Response Messages",
"HTTP Status Code": "HTTP Status Code",
"Reason": "Reason",
"Response Model": "Response Model",
"Headers": "Headers",
"Try it out": "Try it out!",
"Hide Response": "Hide Response",
"Request URL": "Request URL",
"Response Body": "Response Body",
"Response Code": "Response Code",
"Response Headers": "Response Headers",
"CORS Tip": "
Tip: Maybe it is caused by Ajax CORS.Click for help."
},
zh_CN: {
"explore": "浏览",
"Show/Hide": "显示/隐藏",
"List Operations": "收缩",
"Expand Operations": "展开",
"base url": "基路径",
"api version": "API版本",
"Response Class": "响应信息",
"Status": "状态码",
"Implementation Notes": "接口说明",
"Model": "对象",
"Model Schema": "结构",
"Response Content Type": "响应格式",
"Parameters": "请求参数",
"Parameter": "参数",
"Value": "值",
"Description": "说明",
"Parameter Type": "参数位置",
"Data Type": "数据类型",
"Parameter content type": "参数格式",
"Click to set as parameter value": "点击设置参数值",
"(required)": "必输项",
"Response Messages": "请求响应",
"HTTP Status Code": "状态码",
"Reason": "说明",
"Response Model": "响应信息",
"Headers": "头信息",
"Try it out": "测试",
"Hide Response": "隐藏响应",
"Request URL": "请求URL",
"Response Body": "响应内容",
"Response Code": "响应码",
"Response Headers": "响应头",
"CORS Tip": "
提示:可能是因为Ajax跨域请求导致。查看解决方式。"
}
};
//当前字符集
var currentLocale = locales.en;
window.SwaggerUi = Backbone.Router.extend({
dom_id: 'swagger_ui',
// Attributes
options: null,
api: null,
headerView: null,
mainView: null,
// SwaggerUi accepts all the same options as SwaggerApi
initialize: function (options) {
options = options || {};
//扩展,设置当前字符集
if (options.locale) {
this.setLocale(options.locale);
}
if (!options.highlightSizeThreshold) {
options.highlightSizeThreshold = 100000;
}
// Allow dom_id to be overridden
if (options.dom_id) {
this.dom_id = options.dom_id;
delete options.dom_id;
}
if (!options.supportedSubmitMethods) {
options.supportedSubmitMethods = [
'get',
'put',
'post',
'delete',
'head',
'options',
'patch'
];
}
if (typeof options.oauth2RedirectUrl === 'string') {
window.oAuthRedirectUrl = options.oauth2RedirectUrl;
}
// Create an empty div which contains the dom_id
if (!$('#' + this.dom_id).length) {
$('body').append('');
}
this.options = options;
// set marked options
marked.setOptions({ gfm: true });
// Set the callbacks
var that = this;
this.options.success = function () { return that.render(); };
this.options.progress = function (d) { return that.showMessage(d); };
this.options.failure = function (d) { return that.onLoadFailure(d); };
// Create view to handle the header inputs
this.headerView = new SwaggerUi.Views.HeaderView({ el: $('#header') });
// Event handler for when the baseUrl/apiKey is entered by user
this.headerView.on('update-swagger-ui', function (data) {
return that.updateSwaggerUi(data);
});
},
//扩展,设置语言
setLocale: function (locale) {
currentLocale = locales[locale];
$("#explore").text(currentLocale["explore"]);
},
// Set an option after initializing
setOption: function (option, value) {
this.options[option] = value;
},
// Get the value of a previously set option
getOption: function (option) {
return this.options[option];
},
// Event handler for when url/key is received from user
updateSwaggerUi: function (data) {
this.options.url = data.url;
this.load();
},
// Create an api and render
load: function () {
// Initialize the API object
if (this.mainView) {
this.mainView.clear();
}
var url = this.options.url;
if (url && url.indexOf('http') !== 0) {
url = this.buildUrl(window.location.href.toString(), url);
}
if (this.api) {
this.options.authorizations = this.api.clientAuthorizations.authz;
}
this.options.url = url;
this.headerView.update(url);
this.api = new SwaggerClient(this.options);
swaggerCustom.setControllerSummary();//加载Controller注解
//alert('hh');
setTimeout(function () {
swaggerStyle.init();
}, 360);
},
// collapse all sections
collapseAll: function () {
Docs.collapseEndpointListForResource('');
},
// list operations for all sections
listAll: function () {
Docs.collapseOperationsForResource('');
},
// expand operations for all sections
expandAll: function () {
Docs.expandOperationsForResource('');
},
// This is bound to success handler for SwaggerApi
// so it gets called when SwaggerApi completes loading
render: function () {
this.showMessage('Finished Loading Resource Information. Rendering Swagger UI...');
this.mainView = new SwaggerUi.Views.MainView({
model: this.api,
el: $('#' + this.dom_id),
swaggerOptions: this.options,
router: this
}).render();
this.showMessage();
switch (this.options.docExpansion) {
case 'full':
this.expandAll(); break;
case 'list':
this.listAll(); break;
default:
break;
}
this.renderGFM();
if (this.options.onComplete) {
this.options.onComplete(this.api, this);
}
setTimeout(Docs.shebang.bind(this), 100);
},
buildUrl: function (base, url) {
if (url.indexOf('/') === 0) {
var parts = base.split('/');
base = parts[0] + '//' + parts[2];
return base + url;
} else {
var endOfPath = base.length;
if (base.indexOf('?') > -1) {
endOfPath = Math.min(endOfPath, base.indexOf('?'));
}
if (base.indexOf('#') > -1) {
endOfPath = Math.min(endOfPath, base.indexOf('#'));
}
base = base.substring(0, endOfPath);
if (base.indexOf('/', base.length - 1) !== -1) {
return base + url;
}
return base + '/' + url;
}
},
// Shows message on topbar of the ui
showMessage: function (data) {
if (data === undefined) {
data = '';
}
var $msgbar = $('#message-bar');
$msgbar.removeClass('message-fail');
$msgbar.addClass('message-success');
$msgbar.html(data);
if (window.SwaggerTranslator) {
window.SwaggerTranslator.translate($msgbar);
}
},
// shows message in red
onLoadFailure: function (data) {
if (data === undefined) {
data = '';
}
$('#message-bar').removeClass('message-success');
$('#message-bar').addClass('message-fail');
var val = $('#message-bar').text(data);
if (this.options.onFailure) {
this.options.onFailure(data);
}
return val;
},
// Renders GFM for elements with 'markdown' class
renderGFM: function () {
$('.markdown').each(function () {
$(this).html(marked($(this).html()));
});
$('.propDesc', '.model-signature .description').each(function () {
$(this).html(marked($(this).html())).addClass('markdown');
});
}
});
window.SwaggerUi.Views = {};
// don't break backward compatibility with previous versions and warn users to upgrade their code
(function () {
window.authorizations = {
add: function () {
warn('Using window.authorizations is deprecated. Please use SwaggerUi.api.clientAuthorizations.add().');
if (typeof window.swaggerUi === 'undefined') {
throw new TypeError('window.swaggerUi is not defined');
}
if (window.swaggerUi instanceof SwaggerUi) {
window.swaggerUi.api.clientAuthorizations.add.apply(window.swaggerUi.api.clientAuthorizations, arguments);
}
}
};
window.ApiKeyAuthorization = function () {
warn('window.ApiKeyAuthorization is deprecated. Please use SwaggerClient.ApiKeyAuthorization.');
SwaggerClient.ApiKeyAuthorization.apply(window, arguments);
};
window.PasswordAuthorization = function () {
warn('window.PasswordAuthorization is deprecated. Please use SwaggerClient.PasswordAuthorization.');
SwaggerClient.PasswordAuthorization.apply(window, arguments);
};
function warn(message) {
if ('console' in window && typeof window.console.warn === 'function') {
console.warn(message);
}
}
})();
// UMD
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['b'], function (b) {
return (root.SwaggerUi = factory(b));
});
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('b'));
} else {
// Browser globals
root.SwaggerUi = factory(root.b);
}
}(this, function () {
return SwaggerUi;
}));
this["Handlebars"] = this["Handlebars"] || {};
this["Handlebars"]["templates"] = this["Handlebars"]["templates"] || {};
this["Handlebars"]["templates"]["apikey_button_view"] = Handlebars.template({
"compiler": [6, ">= 2.0.0-beta.1"], "main": function (depth0, helpers, partials, data) {
var helper, functionType = "function", helperMissing = helpers.helperMissing, escapeExpression = this.escapeExpression;
return "\n\n";
}, "useData": true
});
this["Handlebars"]["templates"]["basic_auth_button_view"] = Handlebars.template({
"compiler": [6, ">= 2.0.0-beta.1"], "main": function (depth0, helpers, partials, data) {
return "\n\n\n";
}, "useData": true
});
this["Handlebars"]["templates"]["content_type"] = Handlebars.template({
"1": function (depth0, helpers, partials, data) {
var stack1, buffer = "";
stack1 = helpers.each.call(depth0, (depth0 != null ? depth0.produces : depth0), { "name": "each", "hash": {}, "fn": this.program(2, data), "inverse": this.noop, "data": data });
if (stack1 != null) { buffer += stack1; }
return buffer;
}, "2": function (depth0, helpers, partials, data) {
var stack1, lambda = this.lambda, buffer = " \n";
}, "4": function (depth0, helpers, partials, data) {
return " \n";
}, "compiler": [6, ">= 2.0.0-beta.1"], "main": function (depth0, helpers, partials, data) {
var stack1, helper, functionType = "function", helperMissing = helpers.helperMissing, escapeExpression = this.escapeExpression, buffer = "\n\n";
}, "useData": true
});
'use strict';
$(function () {
// Helper function for vertically aligning DOM elements
// http://www.seodenver.com/simple-vertical-align-plugin-for-jquery/
$.fn.vAlign = function () {
return this.each(function () {
var ah = $(this).height();
var ph = $(this).parent().height();
var mh = (ph - ah) / 2;
$(this).css('margin-top', mh);
});
};
$.fn.stretchFormtasticInputWidthToParent = function () {
return this.each(function () {
var p_width = $(this).closest("form").innerWidth();
var p_padding = parseInt($(this).closest("form").css('padding-left'), 10) + parseInt($(this).closest('form').css('padding-right'), 10);
var this_padding = parseInt($(this).css('padding-left'), 10) + parseInt($(this).css('padding-right'), 10);
$(this).css('width', p_width - p_padding - this_padding);
});
};
$('form.formtastic li.string input, form.formtastic textarea').stretchFormtasticInputWidthToParent();
// Vertically center these paragraphs
// Parent may need a min-height for this to work..
$('ul.downplayed li div.content p').vAlign();
// When a sandbox form is submitted..
$("form.sandbox").submit(function () {
var error_free = true;
// Cycle through the forms required inputs
$(this).find("input.required").each(function () {
// Remove any existing error styles from the input
$(this).removeClass('error');
// Tack the error style on if the input is empty..
if ($(this).val() === '') {
$(this).addClass('error');
$(this).wiggle();
error_free = false;
}
});
return error_free;
});
});
function clippyCopiedCallback() {
$('#api_key_copied').fadeIn().delay(1000).fadeOut();
// var b = $("#clippy_tooltip_" + a);
// b.length != 0 && (b.attr("title", "copied!").trigger("tipsy.reload"), setTimeout(function() {
// b.attr("title", "copy to clipboard")
// },
// 500))
}
// Logging function that accounts for browsers that don't have window.console
function log() {
log.history = log.history || [];
log.history.push(arguments);
if (this.console) {
console.log(Array.prototype.slice.call(arguments)[0]);
}
}
// Handle browsers that do console incorrectly (IE9 and below, see http://stackoverflow.com/a/5539378/7913)
if (Function.prototype.bind && console && typeof console.log === "object") {
[
"log", "info", "warn", "error", "assert", "dir", "clear", "profile", "profileEnd"
].forEach(function (method) {
console[method] = this.bind(console[method], console);
}, Function.prototype.call);
}
window.Docs = {
shebang: function () {
// If shebang has an operation nickname in it..
// e.g. /docs/#!/words/get_search
var fragments = $.param.fragment().split('/');
fragments.shift(); // get rid of the bang
switch (fragments.length) {
case 1:
if (fragments[0].length > 0) { // prevent matching "#/"
// Expand all operations for the resource and scroll to it
var dom_id = 'resource_' + fragments[0];
Docs.expandEndpointListForResource(fragments[0]);
$("#" + dom_id).slideto({ highlight: false });
}
break;
case 2:
// Refer to the endpoint DOM element, e.g. #words_get_search
// Expand Resource
Docs.expandEndpointListForResource(fragments[0]);
$("#" + dom_id).slideto({ highlight: false });
// Expand operation
var li_dom_id = decodeURIComponent(fragments.join('_'));
var li_content_dom_id = li_dom_id + "_content";
Docs.expandOperation($('#' + li_content_dom_id));
$('#' + li_dom_id).slideto({ highlight: false });
break;
}
},
toggleEndpointListForResource: function (resource) {
var elem = $('li#resource_' + Docs.escapeResourceName(resource) + ' ul.endpoints');
if (elem.is(':visible')) {
Docs.collapseEndpointListForResource(resource);
} else {
Docs.expandEndpointListForResource(resource);
}
},
// Expand resource
expandEndpointListForResource: function (resource) {
var resource = Docs.escapeResourceName(resource);
if (resource == '') {
$('.resource ul.endpoints').slideDown();
return;
}
$('li#resource_' + resource).addClass('active');
var elem = $('li#resource_' + resource + ' ul.endpoints');
elem.slideDown();
},
// Collapse resource and mark as explicitly closed
collapseEndpointListForResource: function (resource) {
var resource = Docs.escapeResourceName(resource);
if (resource == '') {
$('.resource ul.endpoints').slideUp();
return;
}
$('li#resource_' + resource).removeClass('active');
var elem = $('li#resource_' + resource + ' ul.endpoints');
elem.slideUp();
},
expandOperationsForResource: function (resource) {
// Make sure the resource container is open..
Docs.expandEndpointListForResource(resource);
if (resource == '') {
$('.resource ul.endpoints li.operation div.content').slideDown();
return;
}
$('li#resource_' + Docs.escapeResourceName(resource) + ' li.operation div.content').each(function () {
Docs.expandOperation($(this));
});
},
collapseOperationsForResource: function (resource) {
// Make sure the resource container is open..
Docs.expandEndpointListForResource(resource);
if (resource == '') {
$('.resource ul.endpoints li.operation div.content').slideUp();
return;
}
$('li#resource_' + Docs.escapeResourceName(resource) + ' li.operation div.content').each(function () {
Docs.collapseOperation($(this));
});
},
escapeResourceName: function (resource) {
return resource.replace(/[!"#$%&'()*+,.\/:;<=>?@\[\\\]\^`{|}~]/g, "\\$&");
},
expandOperation: function (elem) {
elem.slideDown();
},
collapseOperation: function (elem) {
elem.slideUp();
}
};
'use strict';
Handlebars.registerHelper('sanitize', function (html) {
// Strip the script tags from the html, and return it as a Handlebars.SafeString
html = html.replace(/