Skip to content

Commit

Permalink
Merge pull request #54 from rugk/jqueryless
Browse files Browse the repository at this point in the history
Remove jQuery
  • Loading branch information
ar2rsawseen authored Mar 30, 2019
2 parents 071aae4 + e39bbc2 commit 73e2ce2
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 53 deletions.
13 changes: 8 additions & 5 deletions static/scripts/collapse.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
function hideAllButCurrent(){
//by default all submenut items are hidden
$("nav > ul > li > ul li").hide();
document.querySelectorAll("nav > ul > li > ul li").style.display = "none";

//only current page (if it exists) should be opened
var file = window.location.pathname.split("/").pop();
$("nav > ul > li > a[href^='"+file+"']").parent().find("> ul li").show();
document.querySelectorAll("nav > ul > li > a[href^='"+file+"']").forEach(function(parent) {
parent.parentNode.querySelectorAll("ul li").forEach(function(elem) {
elem.style.display = "block";
});
});
}
$( document ).ready(function() {
hideAllButCurrent();
});

hideAllButCurrent();
4 changes: 0 additions & 4 deletions static/scripts/jquery-3.1.1.min.js

This file was deleted.

4 changes: 4 additions & 0 deletions static/scripts/polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//IE Fix, src: https://www.reddit.com/r/programminghorror/comments/6abmcr/nodelist_lacks_foreach_in_internet_explorer/
if (typeof(NodeList.prototype.forEach)!==typeof(alert)){
NodeList.prototype.forEach=Array.prototype.forEach;
}
114 changes: 75 additions & 39 deletions static/scripts/search.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,83 @@
$( document ).ready(function() {
var searchAttr = 'data-search-mode';
jQuery.expr[':'].Contains = function(a,i,m){
return (a.textContent || a.innerText || "").toUpperCase().indexOf(m[3].toUpperCase())>=0;
};
//on search
$("#nav-search").on("keyup", function(event) {
var search = $(this).val();

if (!search) {
//no search, show all results
document.documentElement.removeAttribute(searchAttr);
$("nav > ul > li").not('.level-hide').show();
var searchAttr = 'data-search-mode';
function contains(a,m){
return (a.textContent || a.innerText || "").toUpperCase().includes(m)
};

if(typeof hideAllButCurrent === "function"){
//let's do what ever collapse wants to do
hideAllButCurrent();
}
else{
//menu by default should be opened
$("nav > ul > li > ul li").show();
}
//on search
document.getElementById("nav-search").addEventListener("keyup", function(event) {
var search = this.value.toUpperCase();

if (!search) {
//no search, show all results
document.documentElement.removeAttribute(searchAttr);

document.querySelectorAll("nav > ul > li:not(.level-hide)").forEach(function(elem) {
elem.style.display = "block";
});

if (typeof hideAllButCurrent === "function"){
//let's do what ever collapse wants to do
hideAllButCurrent();
} else {
//menu by default should be opened
document.querySelectorAll("nav > ul > li > ul li").forEach(function(elem) {
elem.style.display = "block";
});
}
else{
//we are searching
document.documentElement.setAttribute(searchAttr, '');
} else {
//we are searching
document.documentElement.setAttribute(searchAttr, '');

//show all parents
$("nav > ul > li").show();
//hide all results
$("nav > ul > li > ul li").hide();
//show results matching filter
$("nav > ul > li > ul").find("a:Contains("+search+")").parent().show();
//hide parents without children
$("nav > ul > li").each(function(){
if($(this).find("a:Contains("+search+")").length == 0 && $(this).children("ul").length === 0){
//has no child at all and does not contain text
$(this).hide();
//show all parents
document.querySelectorAll("nav > ul > li").forEach(function(elem) {
elem.style.display = "block";
});
//hide all results
document.querySelectorAll("nav > ul > li > ul li").forEach(function(elem) {
elem.style.display = "none";
});
//show results matching filter
document.querySelectorAll("nav > ul > li > ul a").forEach(function(elem) {
if (!contains(elem.parentNode, search)) {
return;
}
elem.parentNode.style.display = "block";
});
//hide parents without children
document.querySelectorAll("nav > ul > li").forEach(function(parent) {
var countSearchA = 0;
parent.querySelectorAll("a").forEach(function(elem) {
if (contains(elem, search)) {
countSearchA++;
}
});

var countUl = 0;
var countUlVisible = 0;
parent.querySelectorAll("ul").forEach(function(ulP) {
// count all elements that match the search
if (contains(ulP, search)) {
countUl++;
}
else if($(this).find("a:Contains("+search+")").length == 0 && $(this).find("ul").children(':visible').length == 0){
//has no visible child and does not contain text
$(this).hide();

// count all visible elements
var children = ulP.children
for (i=0; i<children.length; i++) {
var elem = children[i];
if (elem.style.display != "none") {
countUlVisible++;
}
}
});
}
});

if (countSearchA == 0 && countUl === 0){
//has no child at all and does not contain text
parent.style.display = "none";
} else if(countSearchA == 0 && countUlVisible == 0){
//has no visible child and does not contain text
parent.style.display = "none";
}
});
}
});
8 changes: 3 additions & 5 deletions tmpl/layout.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,13 @@
</footer>

<script>prettyPrint();</script>
<script src="scripts/polyfill.js"></script>
<script src="scripts/linenumber.js"></script>
<?js if (env.conf.docdash.search || env.conf.docdash.collapse) { ?>
<script src="scripts/jquery-3.1.1.min.js"></script>
<?js if (env.conf.docdash.search) { ?>
<script src="scripts/search.js"></script>
<script src="scripts/search.js" defer></script>
<?js } ?>
<?js if (env.conf.docdash.collapse) { ?>
<script src="scripts/collapse.js"></script>
<?js } ?>
<script src="scripts/collapse.js" defer></script>
<?js } ?>
<?js if (env.conf.docdash.scripts && env.conf.docdash.scripts.length) {
for(var i = 0; i < env.conf.docdash.scripts.length; i++){
Expand Down

0 comments on commit 73e2ce2

Please sign in to comment.