Skip to content

Commit

Permalink
Be a man, using Backbone, use routes! ;-)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofMadejski committed May 4, 2015
1 parent ce97046 commit 8ea3db1
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 100 deletions.
81 changes: 34 additions & 47 deletions dist/swagger-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,6 @@ window.SwaggerUi = Backbone.Router.extend({
this.uiRoot = '/';
}

//$(function() {
// ininitialize on dom-ready
Backbone.history.start({ pushState: true, root: that.uiRoot });
//});

// Globally capture clicks. If they are internal and not in the pass
// through list, route them through Backbone's navigate method.
$(document).on("click","a[href^='" + this.uiRoot + "']", function(event) {
Expand All @@ -97,13 +92,41 @@ window.SwaggerUi = Backbone.Router.extend({
var url = href.replace(/^\//,'').replace('\#\!\/','');

// Instruct Backbone to trigger routing events
that.navigate(url, { trigger: true });
that.navigate(url, { trigger: false });

return false
}
});
},

routes: {
":resource": "resource",
":resource/:operation": "operation"
},

// Expand all operations for the resource and scroll to it
resource: function(resource) {
Docs.expandEndpointListForResource(resource);

var dom_id = 'resource_' + resource;
$("#" + dom_id).slideto({highlight: false});
},

operation: function(resource, operation) {
// Expand Resource
Docs.expandEndpointListForResource(resource);

var dom_id = 'resource_' + resource;
$("#"+dom_id).slideto({highlight: false});

// Expand operation
var li_dom_id = resource + '_' + operation;
var li_content_dom_id = li_dom_id + "_content";

Docs.expandOperation($('#' + li_content_dom_id));
$('#' + li_dom_id).slideto({highlight: false});
},

// Set an option after initializing
setOption: function(option, value) {
this.options[option] = value;
Expand Down Expand Up @@ -173,11 +196,11 @@ window.SwaggerUi = Backbone.Router.extend({
}
this.renderGFM();

if (this.options.onComplete){
Backbone.history.start({ pushState: true, root: this.uiRoot });

if (this.options.onComplete) {
this.options.onComplete(this.api, this);
}

setTimeout(Docs.shebang.bind(this), 100);
},

buildUrl: function(base, url){
Expand Down Expand Up @@ -414,42 +437,6 @@ if (Function.prototype.bind && console && typeof console.log === "object") {
}

window.Docs = {

shebang: function() {
// TODO eliminate shebang!

// 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:
// 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 = 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')) {
Expand Down Expand Up @@ -20844,7 +20831,7 @@ SwaggerUi.Views.OperationView = Backbone.View.extend({
this.parentId = this.model.parentId;
this.nickname = this.model.nickname;
this.model.encodedParentId = encodeURIComponent(this.parentId);
this.model.uiRoot = Backbone.history.root;
this.model.uiRoot = this.router.uiRoot;
return this;
},

Expand Down Expand Up @@ -21624,7 +21611,7 @@ SwaggerUi.Views.ResourceView = Backbone.View.extend({
if (this.model.description) {
this.model.summary = this.model.description;
}
this.model.uiRoot = Backbone.history.root;
this.model.uiRoot = this.router.uiRoot;
},

render: function(){
Expand Down
16 changes: 8 additions & 8 deletions dist/swagger-ui.min.js

Large diffs are not rendered by default.

41 changes: 32 additions & 9 deletions src/main/javascript/SwaggerUi.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,6 @@ window.SwaggerUi = Backbone.Router.extend({
this.uiRoot = '/';
}

//$(function() {
// ininitialize on dom-ready
Backbone.history.start({ pushState: true, root: that.uiRoot });
//});

// Globally capture clicks. If they are internal and not in the pass
// through list, route them through Backbone's navigate method.
$(document).on("click","a[href^='" + this.uiRoot + "']", function(event) {
Expand All @@ -91,13 +86,41 @@ window.SwaggerUi = Backbone.Router.extend({
var url = href.replace(/^\//,'').replace('\#\!\/','');

// Instruct Backbone to trigger routing events
that.navigate(url, { trigger: true });
that.navigate(url, { trigger: false });

return false
}
});
},

routes: {
":resource": "resource",
":resource/:operation": "operation"
},

// Expand all operations for the resource and scroll to it
resource: function(resource) {
Docs.expandEndpointListForResource(resource);

var dom_id = 'resource_' + resource;
$("#" + dom_id).slideto({highlight: false});
},

operation: function(resource, operation) {
// Expand Resource
Docs.expandEndpointListForResource(resource);

var dom_id = 'resource_' + resource;
$("#"+dom_id).slideto({highlight: false});

// Expand operation
var li_dom_id = resource + '_' + operation;
var li_content_dom_id = li_dom_id + "_content";

Docs.expandOperation($('#' + li_content_dom_id));
$('#' + li_dom_id).slideto({highlight: false});
},

// Set an option after initializing
setOption: function(option, value) {
this.options[option] = value;
Expand Down Expand Up @@ -167,11 +190,11 @@ window.SwaggerUi = Backbone.Router.extend({
}
this.renderGFM();

if (this.options.onComplete){
Backbone.history.start({ pushState: true, root: this.uiRoot });

if (this.options.onComplete) {
this.options.onComplete(this.api, this);
}

setTimeout(Docs.shebang.bind(this), 100);
},

buildUrl: function(base, url){
Expand Down
36 changes: 0 additions & 36 deletions src/main/javascript/doc.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,42 +83,6 @@ if (Function.prototype.bind && console && typeof console.log === "object") {
}

window.Docs = {

shebang: function() {
// TODO eliminate shebang!

// 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:
// 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 = 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')) {
Expand Down

1 comment on commit 8ea3db1

@KrzysztofMadejski
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit and previous ones reference
#1236

Please sign in to comment.