-
-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make this code useful for more than just tables
- Loading branch information
1 parent
07f9c02
commit 8f7511e
Showing
4 changed files
with
35 additions
and
27 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
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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* @overview Randomizes the FIRST table on a page. | ||
* @overview Randomizes the order of an HTML element's children. | ||
* @author Jonah Aragon <[email protected]> | ||
* @license | ||
* Copyright (c) 2024 Jonah Aragon | ||
|
@@ -23,20 +23,35 @@ | |
* SOFTWARE. | ||
*/ | ||
|
||
// Create references to table and rows | ||
let table = document.querySelector("table:first-of-type tbody"); | ||
let rows = table.querySelectorAll("tr"); | ||
let globalData = document.querySelector("#global-data"); | ||
if (globalData.dataset.randomizeElement) { | ||
// Get elements to be randomized from meta tag in HTML | ||
let randomizeElement = globalData.dataset.randomizeElement; | ||
console.log("Randomizing child elements of " + randomizeElement) | ||
|
||
// Randomize rows (simple, probably not efficient) | ||
let randomRows = Array.prototype.slice.call(rows) | ||
.map(value => ({ value, sort: Math.random() })) | ||
.sort((a, b) => a.sort - b.sort) | ||
.map(({ value }) => value) | ||
// Get all elements to be randomized | ||
let randomizeChildren = document.querySelectorAll(randomizeElement); | ||
let randomizeArray = Array.from(randomizeChildren); | ||
|
||
// Clear the contents of the table | ||
table.innerHTML = ""; | ||
// Handle multiple elements to be randomized | ||
for (const element of randomizeArray) { | ||
// Select all direct children of element | ||
let children = element.querySelectorAll(randomizeElement + " > *"); | ||
|
||
// Randomize rows (simple, probably not efficient) | ||
let randomChildren = Array.from(children) | ||
.map(value => ({ value, sort: Math.random() })) | ||
.sort((a, b) => a.sort - b.sort) | ||
.map(({ value }) => value) | ||
|
||
// Clear the contents of the table | ||
element.innerHTML = ""; | ||
|
||
// Iterate over each row of randomRows | ||
for (const childElement of randomChildren) { | ||
element.appendChild(childElement); | ||
} | ||
|
||
} | ||
|
||
// Iterate over each row of randomRows | ||
for (const element of randomRows) { | ||
table.appendChild(element); | ||
} |
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