-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
function loadStyle(url) { | ||
return new Promise(function (acceptFn, rejectFn) { | ||
var link = document.createElement("link"); | ||
link.onload = acceptFn; | ||
link.onerror = rejectFn; | ||
document.querySelector("head").appendChild(link); | ||
link.rel = "stylesheet"; | ||
link.href = url; | ||
}); | ||
} | ||
function loadScript(url) { | ||
return new Promise(function (acceptFn, rejectFn) { | ||
var script = document.createElement("script"); | ||
script.onload = acceptFn; | ||
script.onerror = rejectFn; | ||
document.querySelector("head").appendChild(script); | ||
script.src = url; | ||
}); | ||
} | ||
const TOH_BASE_URL = "https://openwrt.github.io/toh/"; | ||
function initToH() { | ||
Promise.all([ | ||
loadStyle("https://cdn.datatables.net/1.13.7/css/jquery.dataTables.css"), | ||
loadStyle( | ||
"https://cdn.datatables.net/searchpanes/2.2.0/css/searchPanes.dataTables.min.css" | ||
), | ||
loadScript("https://code.jquery.com/jquery-3.7.0.js"), | ||
]) | ||
.then(function () { | ||
return loadScript( | ||
"https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js" | ||
); | ||
}) | ||
.then(function () { | ||
return Promise.all([ | ||
fetch(TOH_BASE_URL), | ||
loadScript( | ||
"https://cdn.datatables.net/1.13.7/js/jquery.dataTables.min.js" | ||
), | ||
]); | ||
}) | ||
.then(function (promises) { | ||
return promises[0].text(); | ||
}) | ||
.then(function (toh_table) { | ||
var parse = new DOMParser(); | ||
var html = parse.parseFromString(toh_table, "text/html"); | ||
document.querySelectorAll("div.wrap_toh").forEach(function (wrapper) { | ||
$(wrapper) | ||
.empty() | ||
.append(html.querySelector("#devices").cloneNode(true)); | ||
// Setup - add a text input to each footer cell | ||
$(wrapper) | ||
.find("thead tr") | ||
.clone(true) | ||
.addClass("filters") | ||
.appendTo("#devices thead"); | ||
// Init datatable | ||
var table = $(wrapper) | ||
.children("table") | ||
.DataTable({ | ||
pageLength: 50, | ||
orderCellsTop: true, | ||
fixedHeader: true, | ||
initComplete: function () { | ||
var api = this.api(); | ||
// For each column | ||
api | ||
.columns() | ||
.eq(0) | ||
.each(function (colIdx) { | ||
// Set the header cell to contain the input element | ||
var cell = $(".filters th").eq( | ||
$(api.column(colIdx).header()).index() | ||
); | ||
var title = $(cell).text(); | ||
$(cell).html( | ||
'<input type="text" placeholder="' + title + '" />' | ||
); | ||
// On every keypress in this input | ||
$( | ||
"input", | ||
$(".filters th").eq($(api.column(colIdx).header()).index()) | ||
) | ||
.off("keyup change") | ||
.on("change", function (e) { | ||
// Get the search value | ||
$(this).attr("title", $(this).val()); | ||
var regexr = "({search})"; //$(this).parents('th').find('select').val(); | ||
var cursorPosition = this.selectionStart; | ||
// Search the column for that value | ||
api | ||
.column(colIdx) | ||
.search( | ||
this.value != "" | ||
? regexr.replace( | ||
"{search}", | ||
"(((" + this.value + ")))" | ||
) | ||
: "", | ||
this.value != "", | ||
this.value == "" | ||
) | ||
.draw(); | ||
}) | ||
.on("keyup", function (e) { | ||
e.stopPropagation(); | ||
$(this).trigger("change"); | ||
$(this) | ||
.focus()[0] | ||
.setSelectionRange(cursorPosition, cursorPosition); | ||
}); | ||
}); | ||
}, | ||
}); | ||
wrapper.classList.forEach(function (className) { | ||
var m = className.match(/^wrap_toh_brand_(.+)$/); | ||
if (m) table.column(0).search(m[1]).draw(); | ||
}); | ||
}); | ||
}); | ||
} | ||
document.addEventListener("DOMContentLoaded", function () { | ||
if (document.querySelector("div.wrap_toh")) initToH(); | ||
}); |