github.com/qubitproducts/logspray@v0.2.14/server/swagger-ui/src/main/javascript/view/MainView.js (about)

     1  'use strict';
     2  
     3  SwaggerUi.Views.MainView = Backbone.View.extend({
     4    apisSorter : {
     5      alpha   : function(a,b){ return a.name.localeCompare(b.name); }
     6    },
     7    operationsSorters : {
     8      alpha   : function(a,b){ return a.path.localeCompare(b.path); },
     9      method  : function(a,b){ return a.method.localeCompare(b.method); }
    10    },
    11    initialize: function(opts){
    12      var sorterOption, sorterFn, key, value;
    13      opts = opts || {};
    14  
    15      this.router = opts.router;
    16  
    17      // Sort APIs
    18      if (opts.swaggerOptions.apisSorter) {
    19        sorterOption = opts.swaggerOptions.apisSorter;
    20        if (_.isFunction(sorterOption)) {
    21          sorterFn = sorterOption;
    22        } else {
    23          sorterFn = this.apisSorter[sorterOption];
    24        }
    25        if (_.isFunction(sorterFn)) {
    26          this.model.apisArray.sort(sorterFn);
    27        }
    28      }
    29      // Sort operations of each API
    30      if (opts.swaggerOptions.operationsSorter) {
    31        sorterOption = opts.swaggerOptions.operationsSorter;
    32        if (_.isFunction(sorterOption)) {
    33          sorterFn = sorterOption;
    34        } else {
    35          sorterFn = this.operationsSorters[sorterOption];
    36        }
    37        if (_.isFunction(sorterFn)) {
    38          for (key in this.model.apisArray) {
    39            this.model.apisArray[key].operationsArray.sort(sorterFn);
    40          }
    41        }
    42      }
    43  
    44      // set up the UI for input
    45      this.model.auths = [];
    46  
    47      for (key in this.model.securityDefinitions) {
    48        value = this.model.securityDefinitions[key];
    49  
    50        this.model.auths.push({
    51          name: key,
    52          type: value.type,
    53          value: value
    54        });
    55      }
    56  
    57      if ('validatorUrl' in opts.swaggerOptions) {
    58        // Validator URL specified explicitly
    59        this.model.validatorUrl = opts.swaggerOptions.validatorUrl;
    60      } else if (this.model.url.indexOf('localhost') > 0 || this.model.url.indexOf('127.0.0.1') > 0) {
    61        // Localhost override
    62        this.model.validatorUrl = null;
    63      } else {
    64        this.model.validatorUrl = '//online.swagger.io/validator';
    65      }
    66  
    67      // JSonEditor requires type='object' to be present on defined types, we add it if it's missing
    68      // is there any valid case were it should not be added ?
    69      var def;
    70      for(def in this.model.definitions){
    71        if (!this.model.definitions[def].type){
    72          this.model.definitions[def].type = 'object';
    73        }
    74      }
    75  
    76    },
    77  
    78    render: function () {
    79      $(this.el).html(Handlebars.templates.main(this.model));
    80      this.info = this.$('.info')[0];
    81  
    82      if (this.info) {
    83        this.info.addEventListener('click', this.onLinkClick, true);
    84      }
    85  
    86      this.model.securityDefinitions = this.model.securityDefinitions || {};
    87  
    88      // Render each resource
    89  
    90      var resources = {};
    91      var counter = 0;
    92      for (var i = 0; i < this.model.apisArray.length; i++) {
    93        var resource = this.model.apisArray[i];
    94        var id = resource.name;
    95        while (typeof resources[id] !== 'undefined') {
    96          id = id + '_' + counter;
    97          counter += 1;
    98        }
    99        resource.id = sanitizeHtml(id);
   100        resources[id] = resource;
   101        this.addResource(resource, this.model.auths);
   102      }
   103  
   104      $('.propWrap').hover(function onHover(){
   105        $('.optionsWrapper', $(this)).show();
   106      }, function offhover(){
   107        $('.optionsWrapper', $(this)).hide();
   108      });
   109      return this;
   110    },
   111  
   112    addResource: function(resource, auths){
   113      // Render a resource and add it to resources li
   114      resource.id = resource.id.replace(/\s/g, '_');
   115  
   116      // Make all definitions available at the root of the resource so that they can
   117      // be loaded by the JSonEditor
   118      resource.definitions = this.model.definitions;
   119  
   120      var resourceView = new SwaggerUi.Views.ResourceView({
   121        model: resource,
   122        router: this.router,
   123        tagName: 'li',
   124        id: 'resource_' + resource.id,
   125        className: 'resource',
   126        auths: auths,
   127        swaggerOptions: this.options.swaggerOptions
   128      });
   129      $('#resources', this.el).append(resourceView.render().el);
   130    },
   131  
   132    clear: function(){
   133      $(this.el).html('');
   134    },
   135  
   136    onLinkClick: function (e) {
   137      var el = e.target;
   138  
   139      if (el.tagName === 'A' && el.href && !el.target) {
   140          e.preventDefault();
   141          window.open(el.href, '_blank');
   142      }
   143    }
   144  });