Skip to content

Commit

Permalink
add url compression
Browse files Browse the repository at this point in the history
add a url compression for the selecting generators
  • Loading branch information
HerrErde committed Apr 4, 2024
1 parent 5a971a2 commit ee61499
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 59 deletions.
71 changes: 71 additions & 0 deletions src/assets/js/generateUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// JavaScript function to compress numbers for URL
function compress(numbers) {
if (!numbers || numbers.length === 0) {
return [];
}

const compressed = [];
let start = (end = numbers[0]);

for (let i = 1; i < numbers.length; i++) {
const num = numbers[i];
if (num === end + 1) {
end += 1;
} else {
if (start === end) {
compressed.push(start.toString());
} else if (start === end - 1) {
compressed.push(start.toString());
compressed.push(end.toString());
} else {
compressed.push(`${start}-${end}`);
}
start = end = num;
}
}

// Append the last range or single number
if (start === end) {
compressed.push(start.toString());
} else if (start === end - 1) {
compressed.push(start.toString());
compressed.push(end.toString());
} else {
compressed.push(`${start}-${end}`);
}

return compressed;
}

// JavaScript function to generate URL with selected IDs and default selection
function generateUrl(url) {
const checkboxes = document.querySelectorAll('.select-checkbox:checked');
const defaultCheckbox = document.querySelector(
'.default-select-checkbox:checked'
);

const ids = [];
checkboxes.forEach(function (checkbox) {
ids.push(parseInt(checkbox.value)); // Parse value as integer
});

const defaultId = defaultCheckbox ? parseInt(defaultCheckbox.value) : 1;

// Compress the IDs for URL
const compressedIds = compress(ids);

// Include selected default ID and compressed IDs in URL
const generatedUrl =
url + defaultId + '&items=' + JSON.stringify(compressedIds);
window.location.href = generatedUrl;
}

// JavaScript function to retrieve URL from the script tag and call generateUrl() function
function generateUrlFunction() {
// Retrieve the URL from the script tag
var scriptTag = document.querySelector('script[src$="generateUrl.js"]');
var url = scriptTag.getAttribute('url');

// Call the generateUrl() function with the retrieved URL
generateUrl(url);
}
22 changes: 21 additions & 1 deletion src/generator/character.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
// Function to decompress the compressed data
function decompress($compressed)
{
$decompressed = [];

foreach ($compressed as $item) {
if (strpos($item, '-') !== false) {
list($start, $end) = array_map('intval', explode('-', $item));
$decompressed = array_merge($decompressed, range($start, $end));
} else {
$decompressed[] = intval($item);
}
}

return $decompressed;
}

// Function to get the ID from the json file based on item number
function getItemId($itemNumber, $jsonArray)
{
Expand All @@ -17,7 +34,10 @@ function getItemId($itemNumber, $jsonArray)

// Extract the "select" number and the items from the URL parameters
$selectNumber = isset($_GET['select']) ? $_GET['select'] : null;
$items = isset($_GET['items']) ? json_decode($_GET['items']) : [];
$compressedItems = isset($_GET['items']) ? json_decode($_GET['items']) : [];

// Decompress the items
$items = decompress($compressedItems);

// Initialize an array to store the IDs
$itemIds = [];
Expand Down
22 changes: 21 additions & 1 deletion src/generator/hoverboard.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
<?php
// Function to decompress the compressed data
function decompress($compressed)
{
$decompressed = [];

foreach ($compressed as $item) {
if (strpos($item, '-') !== false) {
list($start, $end) = array_map('intval', explode('-', $item));
$decompressed = array_merge($decompressed, range($start, $end));
} else {
$decompressed[] = intval($item);
}
}

return $decompressed;
}

// Function to get the ID from the json file based on item number
function getItemId($itemNumber, $jsonArray)
{
Expand All @@ -17,7 +34,10 @@ function getItemId($itemNumber, $jsonArray)

// Extract the "select" number and the items from the URL parameters
$selectNumber = isset($_GET['select']) ? $_GET['select'] : null;
$items = isset($_GET['items']) ? json_decode($_GET['items']) : [];
$compressedItems = isset($_GET['items']) ? json_decode($_GET['items']) : [];

// Decompress the items
$items = decompress($compressedItems);

// Initialize an array to store the IDs
$itemIds = [];
Expand Down
31 changes: 2 additions & 29 deletions src/page/character.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,40 +56,13 @@
?>
</fieldset>
<div id="filteredItems"></div>
<input type="button" class="btn btn-success" value="Generate URL" onclick="generateUrl()">
<input type="button" class="btn btn-success" value="Generate URL" onclick="generateUrlFunction()">
</form>

<script src="../assets/js/search.js"></script>
<script src="../assets/js/generate.js"></script>

<script>
// JavaScript function to generate URL with selected IDs and default selection
function generateUrl() {
var checkboxes = document.querySelectorAll('.select-checkbox:checked');
var defaultCheckbox = document.querySelector(
'.default-select-checkbox:checked'
);

var ids = [];
checkboxes.forEach(function (checkbox) {
ids.push(parseInt(checkbox.value)); // Parse value as integer
});

var defaultId = 1;
if (defaultCheckbox) {
defaultId = parseInt(defaultCheckbox.value);
}

// Include selected default ID in URL
var url =
'../generator/character.php?select=' +
defaultId +
'&items=' +
JSON.stringify(ids);
window.location.href = url;
}
</script>

<script src="../assets/js/generateUrl.js" url="../generator/character.php?select="></script>

<?php require "../require/footer.php"; ?>

Expand Down
30 changes: 2 additions & 28 deletions src/page/hoverboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,39 +56,13 @@
?>
</fieldset>
<div id="filteredItems"></div>
<input type="button" class="btn btn-success" value="Generate URL" onclick="generateUrl()">
<input type="button" class="btn btn-success" value="Generate URL" onclick="generateUrlFunction()">
</form>

<script src="../assets/js/search.js"></script>
<script src="../assets/js/generate.js"></script>

<script>
// JavaScript function to generate URL with selected IDs and default selection
function generateUrl() {
var checkboxes = document.querySelectorAll('.select-checkbox:checked');
var defaultCheckbox = document.querySelector(
'.default-select-checkbox:checked'
);

var ids = [];
checkboxes.forEach(function (checkbox) {
ids.push(parseInt(checkbox.value)); // Parse value as integer
});

var defaultId = 1;
if (defaultCheckbox) {
defaultId = parseInt(defaultCheckbox.value);
}

// Include selected default ID in URL
var url =
'../generator/hoverboard.php?select=' +
defaultId +
'&items=' +
JSON.stringify(ids);
window.location.href = url;
}
</script>
<script src="../assets/js/generateUrl.js" url="../generator/hoverboard.php?select="></script>

<?php require "../require/footer.php"; ?>

Expand Down

0 comments on commit ee61499

Please sign in to comment.