Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/docs/web/theme/js/base.js
169684 views
1
function getSearchTerm(){
2
let sPageURL = window.location.search.substring(1);
3
let sURLVariables = sPageURL.split('&');
4
for (let i = 0; i < sURLVariables.length; i++)
5
{
6
let sParameterName = sURLVariables[i].split('=');
7
if (sParameterName[0] == 'q')
8
{
9
return sParameterName[1];
10
}
11
}
12
}
13
14
$(document).ready(function() {
15
16
if (navigator.userAgent.match(/msie|trident/i)) {
17
$('#dark-mode-div').hide();
18
}
19
20
var search_term = getSearchTerm(),
21
$search_modal = $('#mkdocs_search_modal'),
22
$keyboard_modal = $('#mkdocs_keyboard_modal');
23
24
if(search_term){
25
$search_modal.modal();
26
}
27
28
// make sure search input gets autofocus everytime modal opens.
29
$search_modal.on('shown.bs.modal', function () {
30
$search_modal.find('#gsc-i-id1').focus();
31
});
32
33
// Close search modal when result is selected
34
// The links get added later so listen to parent
35
$('#mkdocs-search-results').click(function(e) {
36
if ($(e.target).is('a')) {
37
$search_modal.modal('hide');
38
}
39
});
40
41
// Populate keyboard modal with proper Keys
42
$keyboard_modal.find('.help.shortcut kbd')[0].innerHTML = keyCodes[shortcuts.help];
43
$keyboard_modal.find('.search.shortcut kbd')[0].innerHTML = keyCodes[shortcuts.search];
44
$keyboard_modal.find('.edit.shortcut kbd')[0].innerHTML = keyCodes[shortcuts.edit];
45
46
// Keyboard navigation
47
document.addEventListener("keydown", function(e) {
48
if ($(e.target).is(':input')) return true;
49
var key = e.which || e.keyCode || window.event && window.event.keyCode;
50
var page;
51
switch (key) {
52
case shortcuts.search:
53
e.preventDefault();
54
$keyboard_modal.modal('hide');
55
$search_modal.modal('show');
56
$search_modal.find('#gsc-i-id1').focus();
57
break;
58
case shortcuts.edit:
59
page = $('.navbar a[rel="edit"]:first').prop('href');
60
break;
61
case shortcuts.help:
62
$search_modal.modal('hide');
63
$keyboard_modal.modal('show');
64
break;
65
default: break;
66
}
67
if (page) {
68
$keyboard_modal.modal('hide');
69
window.location.href = page;
70
}
71
});
72
73
$('table').addClass('table table-striped table-hover');
74
75
// Improve the scrollspy behavior when users click on a TOC item.
76
$(".bs-sidenav a").on("click", function() {
77
var clicked = this;
78
setTimeout(function() {
79
var active = $('.nav li.active a');
80
active = active[active.length - 1];
81
if (clicked !== active) {
82
$(active).parent().removeClass("active");
83
$(clicked).parent().addClass("active");
84
}
85
}, 50);
86
});
87
88
});
89
90
$('body').scrollspy({
91
target: '.bs-sidebar',
92
offset: 100
93
});
94
95
/* Prevent disabled links from causing a page reload */
96
$("li.disabled a").click(function() {
97
event.preventDefault();
98
});
99
100
const keyCodes = {
101
8: 'backspace',
102
9: 'tab',
103
13: 'enter',
104
16: 'shift',
105
17: 'ctrl',
106
18: 'alt',
107
19: 'pause/break',
108
20: 'caps lock',
109
27: 'escape',
110
32: 'spacebar',
111
33: 'page up',
112
34: 'page down',
113
35: 'end',
114
36: 'home',
115
37: '&larr;',
116
38: '&uarr;',
117
39: '&rarr;',
118
40: '&darr;',
119
45: 'insert',
120
46: 'delete',
121
48: '0',
122
49: '1',
123
50: '2',
124
51: '3',
125
52: '4',
126
53: '5',
127
54: '6',
128
55: '7',
129
56: '8',
130
57: '9',
131
65: 'a',
132
66: 'b',
133
67: 'c',
134
68: 'd',
135
69: 'e',
136
70: 'f',
137
71: 'g',
138
72: 'h',
139
73: 'i',
140
74: 'j',
141
75: 'k',
142
76: 'l',
143
77: 'm',
144
78: 'n',
145
79: 'o',
146
80: 'p',
147
81: 'q',
148
82: 'r',
149
83: 's',
150
84: 't',
151
85: 'u',
152
86: 'v',
153
87: 'w',
154
88: 'x',
155
89: 'y',
156
90: 'z',
157
91: 'Left Windows Key / Left ⌘',
158
92: 'Right Windows Key',
159
93: 'Windows Menu / Right ⌘',
160
96: 'numpad 0',
161
97: 'numpad 1',
162
98: 'numpad 2',
163
99: 'numpad 3',
164
100: 'numpad 4',
165
101: 'numpad 5',
166
102: 'numpad 6',
167
103: 'numpad 7',
168
104: 'numpad 8',
169
105: 'numpad 9',
170
106: 'multiply',
171
107: 'add',
172
109: 'subtract',
173
110: 'decimal point',
174
111: 'divide',
175
112: 'f1',
176
113: 'f2',
177
114: 'f3',
178
115: 'f4',
179
116: 'f5',
180
117: 'f6',
181
118: 'f7',
182
119: 'f8',
183
120: 'f9',
184
121: 'f10',
185
122: 'f11',
186
123: 'f12',
187
124: 'f13',
188
125: 'f14',
189
126: 'f15',
190
127: 'f16',
191
128: 'f17',
192
129: 'f18',
193
130: 'f19',
194
131: 'f20',
195
132: 'f21',
196
133: 'f22',
197
134: 'f23',
198
135: 'f24',
199
144: 'num lock',
200
145: 'scroll lock',
201
186: '&semi;',
202
187: '&equals;',
203
188: '&comma;',
204
189: '&hyphen;',
205
190: '&period;',
206
191: '&quest;',
207
192: '&grave;',
208
219: '&lsqb;',
209
220: '&bsol;',
210
221: '&rsqb;',
211
222: '&apos;',
212
};
213
214
////////////////////////////////////////////////////
215
// Copy code - based on https://www.roboleary.net/2022/01/13/copy-code-to-clipboard-blog.html
216
////////////////////////////////////////////////////
217
218
const copyButtonLabel = "Copy";
219
220
// you can use a class selector instead if you, or the syntax highlighting library adds one to the 'pre'.
221
let blocks = document.querySelectorAll("pre");
222
223
blocks.forEach((block) => {
224
// only add button if browser supports Clipboard API
225
if (navigator.clipboard) {
226
let button = document.createElement("button");
227
button.classList.add("copyCodeButton");
228
button.innerText = copyButtonLabel;
229
button.addEventListener("click", copyCode);
230
block.appendChild(button);
231
}
232
});
233
234
async function copyCode(event) {
235
const button = event.srcElement;
236
const pre = button.parentElement;
237
let code = pre.querySelector("code");
238
let text = code.innerText;
239
await navigator.clipboard.writeText(text);
240
241
button.innerText = "Copied!";
242
button.style.color = "#338033";
243
244
setTimeout(()=> {
245
button.innerText = copyButtonLabel;
246
button.style.color = "#1e1e1e";
247
},1000)
248
}
249
250
////////////////////////////////////////////////////
251
// https://gomakethings.com/how-to-lazy-load-youtube-videos-with-vanilla-javascript/
252
////////////////////////////////////////////////////
253
254
// Get all of the videos
255
let videos = document.querySelectorAll('[data-youtube]');
256
257
for (let video of videos) {
258
// Get the video ID
259
let id = new URL(video.href).searchParams.get('v');
260
261
// Add the ID to the data-youtube attribute
262
video.setAttribute('data-youtube', id);
263
264
// Add a role of button
265
video.setAttribute('role', 'button');
266
267
// Add a thumbnail
268
video.innerHTML =
269
`${video.textContent}<br><div class="video-container">
270
<img alt="" class="tutorial-video" src="https://img.youtube.com/vi/${id}/maxresdefault.jpg">
271
<div class="centered video-text">Click to load external resources from YouTube.</div>
272
</div>`;
273
}
274
275
/**
276
* Handle click events on the video thumbnails
277
* @param {Event} event The event object
278
*/
279
function clickHandler (event) {
280
281
// Get the video link
282
let link = event.target.closest('[data-youtube]');
283
if (!link) return;
284
285
// Prevent the URL from redirecting users
286
event.preventDefault();
287
288
// Get the video ID
289
let id = link.getAttribute('data-youtube');
290
291
// Create the player
292
let player = document.createElement('div');
293
player.innerHTML = `<iframe width="560" height="315" src="https://www.youtube-nocookie.com/embed/${id}?autoplay=1" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>`;
294
295
// Inject the player into the UI
296
link.replaceWith(player);
297
298
}
299
300
// Detect clicks on the video thumbnails
301
document.addEventListener('click', clickHandler);
302
303