Spaces:
Running
Running
File size: 3,817 Bytes
4445260 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
const $ = (...params) => document.querySelector(...params);
/**
* Take a string representing the location in the library
* and return an object containing the following properties:
* room: the room number
* wall: the wall number
* shelf: the shelf number
* book: the book number
* page: the page number
*/
function getLocationDetails(location) {
const parts = location.split(".");
return {
room: parseInt(parts[0], 36),
wall: parseInt(parts[1]),
shelf: parseInt(parts[2]),
book: parseInt(parts[3]),
page: parseInt(parts[4]),
};
}
/**
* Take an object containing the following properties:
* room: the room number
* wall: the wall number
* shelf: the shelf number
* book: the book number
* page: the page number
* and return a string representing the unique identifier for the book.
*/
function getUniqueBookId(location) {
const { room, wall, shelf, book, page } = location;
return (room * 1000 + wall * 100 + shelf * 32 + book * 410) + page * 161;
}
/**
* Given a book index (integer 1-100), return the page number (1-410)
*/
function getPageNumber(bookIndex) {
return (bookIndex - 1) % 410 + 1;
}
/**
* Given a book index (integer 1-100), return the book number (1-32)
*/
function getBookNumber(bookIndex) {
return (bookIndex - 1) % 64 + 1;
}
/**
* Given a book index (integer 1-100), return the shelf number (1-5)
*/
function getShelfNumber(bookIndex) {
return (bookIndex - 1) % 32 + 1;
}
/**
* Given a book index (integer 1-100), return the wall number (1-4)
*/
function getWallNumber(bookIndex) {
return (bookIndex - 1) % 8 + 1;
}
/**
* Given a book index (integer 1-100), return the room number (string containing a letter and an integer)
*/
function getRoomNumber(bookIndex) {
return "Room " + ((bookIndex - 1) % 26 + 1).toUpperCase();
}
$(document).addEventListener("alpine:init", () => {
const app = new Alpine.Component();
app.define("location-details", {
input: "",
bookIndex: 0,
uniqueId: "",
});
app.store({
index: 1,
inputs: [],
});
app.watch("Input", (value) => {
const location = getLocationDetails(value);
app.bookIndex = getUniqueBookId(location);
app.uniqueId = getRoomNumber(app.bookIndex) + "." + getWallNumber(app.bookIndex) + "." + getShelfNumber(app.bookIndex) + "." + getBookNumber(app.bookIndex) + "." + getPageNumber(app.bookIndex);
});
app.watch("bookIndex", (value) => {
app.uniqueId = getRoomNumber(value) + "." + getWallNumber(value) + "." + getShelfNumber(value) + "." + getBookNumber(value) + "." + getPageNumber(value);
});
app.mount("#app");
// Update the input value with the unique identifier
app.on("change:bookIndex", (e) => {
const pagination = app.bookIndex;
const otherBooks = app. books.map((book) => book.bookIndex);
if (!otherBooks.includes(pagination)) {
app.bookIndex = pagination;
app.uniqueId = getRoomNumber(pagination) + "." + getWallNumber(pagination) + "." + getShelfNumber(pagination) + "." + getBookNumber(pagination) + "." + getPageNumber(pagination);
}
});
// Update the button label with the unique identifier
app.on("click", (e) => {
const pagination = app.bookIndex;
const otherBooks = app. books.map((book) => book.bookIndex);
if (!otherBooks.includes(pagination)) {
app.uniqueId = getRoomNumber(pagination) + "." + getWallNumber(pagination) + "." + getShelfNumber(pagination) + "." + getBookNumber(pagination) + "." + getPageNumber(pagination);
}
});
});
$(document).addEventListener("DOMContentLoaded", () => {
const app = new Alpine.Component();
app.define("library", {
books: [],
addBook: (title) => {
return { bookIndex: app.books.length + 1, title };
},
});
app.store({
books: [],
});
app.mount("#app");
}); |