github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/SwaggerUi.js (about) 1 /*global JSONEditor*/ 2 'use strict'; 3 4 window.SwaggerUi = Backbone.Router.extend({ 5 6 dom_id: 'swagger_ui', 7 8 // Attributes 9 options: null, 10 api: null, 11 headerView: null, 12 mainView: null, 13 14 // SwaggerUi accepts all the same options as SwaggerApi 15 initialize: function(options) { 16 options = options || {}; 17 18 if (options.defaultModelRendering !== 'model') { 19 options.defaultModelRendering = 'schema'; 20 } 21 22 if (!options.highlightSizeThreshold) { 23 options.highlightSizeThreshold = 100000; 24 } 25 26 // Allow dom_id to be overridden 27 if (options.dom_id) { 28 this.dom_id = options.dom_id; 29 delete options.dom_id; 30 } 31 32 if (!options.supportedSubmitMethods){ 33 options.supportedSubmitMethods = [ 34 'get', 35 'put', 36 'post', 37 'delete', 38 'head', 39 'options', 40 'patch' 41 ]; 42 } 43 44 if (typeof options.oauth2RedirectUrl === 'string') { 45 window.oAuthRedirectUrl = options.oauth2RedirectUrl; 46 } 47 48 // Create an empty div which contains the dom_id 49 if (! $('#' + this.dom_id).length){ 50 $('body').append('<div id="' + this.dom_id + '"></div>') ; 51 } 52 53 this.options = options; 54 55 // set marked options 56 marked.setOptions({gfm: true}); 57 58 // Set the callbacks 59 var that = this; 60 this.options.success = function() { return that.render(); }; 61 this.options.progress = function(d) { return that.showMessage(d); }; 62 this.options.failure = function(d) { return that.onLoadFailure(d); }; 63 64 // Create view to handle the header inputs 65 this.headerView = new SwaggerUi.Views.HeaderView({el: $('#header')}); 66 67 // Event handler for when the baseUrl/apiKey is entered by user 68 this.headerView.on('update-swagger-ui', function(data) { 69 return that.updateSwaggerUi(data); 70 }); 71 72 // JSon Editor custom theming 73 JSONEditor.defaults.iconlibs.swagger = JSONEditor.AbstractIconLib.extend({ 74 mapping: { 75 collapse: 'collapse', 76 expand: 'expand' 77 }, 78 icon_prefix: 'swagger-' 79 }); 80 81 }, 82 83 // Set an option after initializing 84 setOption: function(option, value) { 85 this.options[option] = value; 86 }, 87 88 // Get the value of a previously set option 89 getOption: function(option) { 90 return this.options[option]; 91 }, 92 93 // Event handler for when url/key is received from user 94 updateSwaggerUi: function(data){ 95 this.options.url = data.url; 96 this.load(); 97 }, 98 99 // Create an api and render 100 load: function(){ 101 // Initialize the API object 102 if (this.mainView) { 103 this.mainView.clear(); 104 } 105 106 if (this.authView) { 107 this.authView.remove(); 108 } 109 var url = this.options.url; 110 if (url && url.indexOf('http') !== 0) { 111 url = this.buildUrl(window.location.href.toString(), url); 112 } 113 if(this.api) { 114 this.options.authorizations = this.api.clientAuthorizations.authz; 115 } 116 this.options.url = url; 117 this.headerView.update(url); 118 119 this.api = new SwaggerClient(this.options); 120 }, 121 122 // collapse all sections 123 collapseAll: function(){ 124 Docs.collapseEndpointListForResource(''); 125 }, 126 127 // list operations for all sections 128 listAll: function(){ 129 Docs.collapseOperationsForResource(''); 130 }, 131 132 // expand operations for all sections 133 expandAll: function(){ 134 Docs.expandOperationsForResource(''); 135 }, 136 137 // This is bound to success handler for SwaggerApi 138 // so it gets called when SwaggerApi completes loading 139 render: function(){ 140 var authsModel; 141 this.showMessage('Finished Loading Resource Information. Rendering Swagger UI...'); 142 this.mainView = new SwaggerUi.Views.MainView({ 143 model: this.api, 144 el: $('#' + this.dom_id), 145 swaggerOptions: this.options, 146 router: this 147 }).render(); 148 if (!_.isEmpty(this.api.securityDefinitions)){ 149 authsModel = _.map(this.api.securityDefinitions, function (auth, name) { 150 var result = {}; 151 result[name] = auth; 152 return result; 153 }); 154 this.authView = new SwaggerUi.Views.AuthButtonView({ 155 data: SwaggerUi.utils.parseSecurityDefinitions(authsModel), 156 router: this 157 }); 158 $('#auth_container').append(this.authView.render().el); 159 } 160 this.showMessage(); 161 switch (this.options.docExpansion) { 162 case 'full': 163 this.expandAll(); break; 164 case 'list': 165 this.listAll(); break; 166 default: 167 break; 168 } 169 this.renderGFM(); 170 171 if (this.options.onComplete){ 172 this.options.onComplete(this.api, this); 173 } 174 175 setTimeout(Docs.shebang.bind(this), 100); 176 }, 177 178 buildUrl: function(base, url){ 179 if (url.indexOf('/') === 0) { 180 var parts = base.split('/'); 181 base = parts[0] + '//' + parts[2]; 182 return base + url; 183 } else { 184 var endOfPath = base.length; 185 186 if (base.indexOf('?') > -1){ 187 endOfPath = Math.min(endOfPath, base.indexOf('?')); 188 } 189 190 if (base.indexOf('#') > -1){ 191 endOfPath = Math.min(endOfPath, base.indexOf('#')); 192 } 193 194 base = base.substring(0, endOfPath); 195 196 if (base.indexOf('/', base.length - 1 ) !== -1){ 197 return base + url; 198 } 199 200 return base + '/' + url; 201 } 202 }, 203 204 // Shows message on topbar of the ui 205 showMessage: function(data){ 206 if (data === undefined) { 207 data = ''; 208 } 209 var $msgbar = $('#message-bar'); 210 $msgbar.removeClass('message-fail'); 211 $msgbar.addClass('message-success'); 212 $msgbar.text(data); 213 if(window.SwaggerTranslator) { 214 window.SwaggerTranslator.translate($msgbar); 215 } 216 }, 217 218 // shows message in red 219 onLoadFailure: function(data){ 220 if (data === undefined) { 221 data = ''; 222 } 223 $('#message-bar').removeClass('message-success'); 224 $('#message-bar').addClass('message-fail'); 225 226 var val = $('#message-bar').text(data); 227 228 if (this.options.onFailure) { 229 this.options.onFailure(data); 230 } 231 232 return val; 233 }, 234 235 // Renders GFM for elements with 'markdown' class 236 renderGFM: function(){ 237 $('.markdown').each(function(){ 238 $(this).html(marked($(this).html())); 239 }); 240 241 $('.propDesc', '.model-signature .description').each(function () { 242 $(this).html(marked($(this).html())).addClass('markdown'); 243 }); 244 } 245 246 }); 247 248 window.SwaggerUi.Views = {}; 249 window.SwaggerUi.Models = {}; 250 window.SwaggerUi.Collections = {}; 251 window.SwaggerUi.partials = {}; 252 window.SwaggerUi.utils = {}; 253 254 // don't break backward compatibility with previous versions and warn users to upgrade their code 255 (function(){ 256 window.authorizations = { 257 add: function() { 258 warn('Using window.authorizations is deprecated. Please use SwaggerUi.api.clientAuthorizations.add().'); 259 260 if (typeof window.swaggerUi === 'undefined') { 261 throw new TypeError('window.swaggerUi is not defined'); 262 } 263 264 if (window.swaggerUi instanceof SwaggerUi) { 265 window.swaggerUi.api.clientAuthorizations.add.apply(window.swaggerUi.api.clientAuthorizations, arguments); 266 } 267 } 268 }; 269 270 window.ApiKeyAuthorization = function() { 271 warn('window.ApiKeyAuthorization is deprecated. Please use SwaggerClient.ApiKeyAuthorization.'); 272 SwaggerClient.ApiKeyAuthorization.apply(window, arguments); 273 }; 274 275 window.PasswordAuthorization = function() { 276 warn('window.PasswordAuthorization is deprecated. Please use SwaggerClient.PasswordAuthorization.'); 277 SwaggerClient.PasswordAuthorization.apply(window, arguments); 278 }; 279 280 function warn(message) { 281 if ('console' in window && typeof window.console.warn === 'function') { 282 console.warn(message); 283 } 284 } 285 })(); 286 287 288 // UMD 289 (function (root, factory) { 290 if (typeof define === 'function' && define.amd) { 291 // AMD. Register as an anonymous module. 292 define(['b'], function (b) { 293 return (root.SwaggerUi = factory(b)); 294 }); 295 } else if (typeof exports === 'object') { 296 // Node. Does not work with strict CommonJS, but 297 // only CommonJS-like environments that support module.exports, 298 // like Node. 299 module.exports = factory(require('b')); 300 } else { 301 // Browser globals 302 root.SwaggerUi = factory(root.b); 303 } 304 }(this, function () { 305 return SwaggerUi; 306 }));