This repository has been archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SidebarHeaderView.js
108 lines (90 loc) · 3.09 KB
/
SidebarHeaderView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
'use strict';
SwaggerUi.Views.SidebarHeaderView = Backbone.View.extend({
initialize: function (opts) {
this.options = opts || {};
this.router = this.options.router;
},
events: {
'click [data-endpoint]': 'clickSidebarItem'
},
render: function () {
$(this.el).html(Handlebars.templates.sidebar_header(this.model));
for (var i = 0; i < this.model.operationsArray.length; i++) {
var item = this.model.operationsArray[i].operation;
item.nickname = this.model.operationsArray[i].nickname;
item.parentId = this.model.operation.parentId;
// @theopak 2016-03-15
item.method = this.model.operationsArray[i].method;
item.deprecated = this.model.operationsArray[i].deprecated;
item.encodedParentId = encodeURIComponent(this.parentId);
item.path = this.model.operationsArray[i].path;
this.addSidebarItem(item, i);
}
return this;
},
addSidebarItem: function (item) {
var sidebarItemView = new SwaggerUi.Views.SidebarItemView({
model: item,
tagName: 'div',
className : 'item',
attributes: {
'data-endpoint': item.parentId + '_' + item.nickname
},
router: this.router,
swaggerOptions: this.options.swaggerOptions
});
$(this.el).append(sidebarItemView.render().el);
},
clickSidebarItem: function (e) {
/* @theopak 2016-05-11 NG-4302 */
// console.info(e);
var candidateElement = e.target;
while (candidateElement.parentNode && !candidateElement.hasAttribute('data-endpoint')) {
candidateElement = candidateElement.parentNode;
// console.info('.');
}
var elem = $(candidateElement);
var eln = $('#' + elem.attr('data-endpoint'));
if (elem.is('.item')) {
scroll(elem.attr('data-endpoint'));
setSelected(elem);
updateUrl(eln.find('.path a').first().attr('href'));
}
/* scroll */
function scroll(elem) {
var i = $('.sticky-nav').outerHeight();
var r = $('#' + elem).offset().top - i - 10;
if(matchMedia()) {
(r = $('#' + elem).offset().top - 10);
}
scrollT(r);
}
/* set selected value and select operation (class) */
function setSelected(element) {
var nav = $('.sticky-nav [data-navigator]');
{
$('#' + element.attr('data-endpoint'));
}
nav.find('[data-resource]').removeClass('active');
nav.find('[data-selected]').removeAttr('data-selected');
element.closest('[data-resource]').addClass('active');
element.attr('data-selected', '');
$('.sticky-nav').find('[data-selected-value]').html(element.text());
}
/* update navigation */
function updateUrl(/* element */) {
// HACK @theopak 2015-12-30 Commented out bc this is a horrible thing to do
// history.pushState && history.pushState(null, null, element)
}
function matchMedia() {
return window.matchMedia('(min-width: 992px)').matches;
}
function scrollT(e) {
if ('self' === e) {
var n = $(window).scrollTop();
return $(window).scrollTop(n);
}
return $(window).scrollTop(e);
}
}
});