-
Notifications
You must be signed in to change notification settings - Fork 55
/
pygsti_dashboard.js
188 lines (159 loc) · 5.38 KB
/
pygsti_dashboard.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
function render_katex(parentEl) {
$(parentEl).find(".math").each(function() {
console.log("Rendering KateX");
var texTxt = $(this).text();
el = $(this).get(0);
if(el.tagName == "DIV"){
addDisp = "\\displaystyle";
} else {
addDisp = "";
}
try {
katex.render(addDisp+texTxt, el);
}
catch(err) {
$(this).html("<span class=\'err\'>"+err);
}
});
}
function openTab(tabID) {
var targetTab = $(`#${tabID}`); // hacky
if(!targetTab.is(":visible")) {
// Fade out open tab
$('.tabcontent').hide().removeClass('active');
// Remove active style on any tab links
$('.tablink').removeClass('active');
//load the tab content on first use
load_contents_if_necessary(tabID);
// Fade in target tab
targetTab.show().addClass('active').trigger('tabchange');
// Add active style to event target link, presumably clicked to open this tab
//$(evt.target).addClass('active'); //(this doesn't work as evt is undefined)
}
}
function load_contents_if_necessary(itemID) {
var target = $(`#${itemID}`); // hacky
if(target.hasClass('notloaded')) {
target.trigger('load_loadable_item');
target.removeClass('notloaded');
}
}
/* Set the width of the side navigation to open width and the left margin of the page content to the same */
function openNav() {
document.getElementById("theSidenav").style.width = sidenav_width + "px";
document.getElementById("main").style.marginLeft = sidenav_width + "px"; // for push:
}
/* Set the width of the side navigation to 0 and the left margin of the page content to 0 */
function closeNav() {
document.getElementById("theSidenav").style.width = sidenav_closed_width + "px";
document.getElementById("main").style.marginLeft = sidenav_closed_width + "px"; // for push:
}
function tackNav() {
if(window.navtacked) {
window.navtacked = false;
document.getElementById("tackbtn").innerHTML = "⊙"
} else {
window.navtacked = true;
document.getElementById("tackbtn").innerHTML = "⊛"
}
}
function initNav() {
load_contents_if_necessary('theSidenav') // always active
window.navtacked = true;
openNav();
//Mousemove handler
$("body").on("mousemove",function(event) {
if(!window.navtacked) {
if (event.pageX < sidenav_mousetrigger_width) {
openNav();
}
else if (event.pageX > sidenav_width) {
closeNav();
}
}
});
}
function initBanner() {
//Top banner/header JS below here
var elSelector = '.header',
elClassHidden = 'header--hidden',
throttleTimeout = 500,
$element = $( elSelector );
if( !$element.length ) return true;
var $window = $( window ),
wHeight = 0,
wScrollCurrent = 0,
wScrollBefore = 0,
wScrollDiff = 0,
$document = $( document ),
dHeight = 0,
throttle = function( delay, fn ){
var last, deferTimer;
return function() {
var context = this, args = arguments, now = +new Date;
if( last && now < last + delay )
{
clearTimeout( deferTimer );
deferTimer = setTimeout( function(){ last = now; fn.apply( context, args ); }, delay );
}
else
{
last = now;
fn.apply( context, args );
}
};
};
$window.on( 'scroll',
throttle( throttleTimeout, function() {
dHeight = $document.height();
wHeight = $window.height();
wScrollCurrent = $window.scrollTop();
wScrollDiff = wScrollBefore - wScrollCurrent;
if( wScrollCurrent <= 0 ) // scrolled to the very top; element sticks to the top
$element.removeClass( elClassHidden );
else if( wScrollDiff > 0 && $element.hasClass( elClassHidden ) ) // scrolled up; element slides in
$element.removeClass( elClassHidden );
else if( wScrollDiff < 0 ) // scrolled down
{
if( wScrollCurrent + wHeight >= dHeight && $element.hasClass( elClassHidden ) ) // scrolled to the very bottom; element slides in
$element.removeClass( elClassHidden );
else // scrolled down; element slides out
$element.addClass( elClassHidden );
}
wScrollBefore = wScrollCurrent;
}));
}
sidenav_width = 200;
sidenav_closed_width = 10;
sidenav_mousetrigger_width = 50;
$(document).ready(function() {
// Create window plot manager
window.plotman = new PlotManager();
// Render KaTeX
render_katex('body');
// Enable figure caption toggling
$('figcaption').on('click', function() {
// captiondetails should be divs, not spans
$(this).children('.captiondetail').toggleClass('showcaption')
});
});
function testLocalAjax(url, onerror) {
var request = new XMLHttpRequest();
request.responseType = 'text';
request.withCredentials = true; //b/c jupyter notebooks use user authentication
request.open('GET', url, true);
request.onload = function() {
var is_safari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
var is_chrome = /Chrome/.test(navigator.userAgent) && /Google Inc/.test(navigator.vendor);
if (request.status == 200 || ((is_safari || is_chrome) && request.status == 0)) {
console.log('testLocalAjax success!');
}
else {
onerror(request.status);
}
};
request.onerror = function() {
onerror("connection error");
};
request.send();
}