Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/doc/tutorial-utils.js
16334 views
1
function getLabelName(innerHTML) {
2
var str = innerHTML.toLowerCase();
3
// Replace all '+' with 'p'
4
str = str.split('+').join('p');
5
// Replace all ' ' with '_'
6
str = str.split(' ').join('_');
7
// Replace all '#' with 'sharp'
8
str = str.split('#').join('sharp');
9
// Replace other special characters with 'ascii' + code
10
for (var i = 0; i < str.length; i++) {
11
var charCode = str.charCodeAt(i);
12
if (!(charCode == 95 || (charCode > 96 && charCode < 123) || (charCode > 47 && charCode < 58)))
13
str = str.substr(0, i) + 'ascii' + charCode + str.substr(i + 1);
14
}
15
return str;
16
}
17
18
function addToggle() {
19
var $getDiv = $('div.newInnerHTML').last();
20
var buttonName = $getDiv.html();
21
var label = getLabelName(buttonName.trim());
22
$getDiv.attr("title", label);
23
$getDiv.hide();
24
$getDiv = $getDiv.next();
25
$getDiv.attr("class", "toggleable_div label_" + label);
26
$getDiv.hide();
27
}
28
29
function addButton(label, buttonName) {
30
var b = document.createElement("BUTTON");
31
b.innerHTML = buttonName;
32
b.setAttribute('class', 'toggleable_button label_' + label);
33
b.onclick = function() {
34
$('.toggleable_button').css({
35
border: '2px outset',
36
'border-radius': '4px'
37
});
38
$('.toggleable_button.label_' + label).css({
39
border: '2px inset',
40
'border-radius': '4px'
41
});
42
$('.toggleable_div').css('display', 'none');
43
$('.toggleable_div.label_' + label).css('display', 'block');
44
};
45
b.style.border = '2px outset';
46
b.style.borderRadius = '4px';
47
b.style.margin = '2px';
48
return b;
49
}
50
51
function buttonsToAdd($elements, $heading, $type) {
52
if ($elements.length === 0) {
53
$elements = $("" + $type + ":contains(" + $heading.html() + ")").parent().prev("div.newInnerHTML");
54
}
55
var arr = jQuery.makeArray($elements);
56
var seen = {};
57
arr.forEach(function(e) {
58
var txt = e.innerHTML;
59
if (!seen[txt]) {
60
$button = addButton(e.title, txt);
61
if (Object.keys(seen).length == 0) {
62
var linebreak1 = document.createElement("br");
63
var linebreak2 = document.createElement("br");
64
($heading).append(linebreak1);
65
($heading).append(linebreak2);
66
}
67
($heading).append($button);
68
seen[txt] = true;
69
}
70
});
71
return;
72
}
73
74
function addTutorialsButtons() {
75
$("h2").each(function() {
76
$heading = $(this);
77
$smallerHeadings = $(this).nextUntil("h2").filter("h3").add($(this).nextUntil("h2").find("h3"));
78
if ($smallerHeadings.length) {
79
$smallerHeadings.each(function() {
80
var $elements = $(this).nextUntil("h2,h3").filter("div.newInnerHTML");
81
buttonsToAdd($elements, $(this), "h3");
82
});
83
} else {
84
var $elements = $(this).nextUntil("h2").filter("div.newInnerHTML");
85
buttonsToAdd($elements, $heading, "h2");
86
}
87
});
88
$(".toggleable_button").first().click();
89
var $clickDefault = $('.toggleable_button.label_python').first();
90
if ($clickDefault.length) {
91
$clickDefault.click();
92
}
93
$clickDefault = $('.toggleable_button.label_cpp').first();
94
if ($clickDefault.length) {
95
$clickDefault.click();
96
}
97
return;
98
}
99
100