|
|
|
class Hint {
|
|
constructor(hintIconId, hintContentId) {
|
|
this.hintIcon = document.querySelector(`#${hintIconId}`);
|
|
this.hintContent = document.querySelector(`#${hintContentId}`);
|
|
|
|
|
|
this.hintIcon.addEventListener("click", this.toggle.bind(this));
|
|
}
|
|
|
|
toggle(event) {
|
|
event.preventDefault();
|
|
this.hintContent.classList.toggle("show");
|
|
}
|
|
}
|
|
|
|
|
|
class Notification {
|
|
constructor() {
|
|
if (!Notification.instance) {
|
|
Notification.instance = this;
|
|
this.notifications = [];
|
|
}
|
|
return Notification.instance;
|
|
}
|
|
|
|
show(type, message) {
|
|
const notificationElement = document.createElement("div");
|
|
notificationElement.className = `notification ${type}`;
|
|
notificationElement.textContent = message;
|
|
|
|
|
|
const closeButton = document.createElement("span");
|
|
closeButton.className = "close-btn";
|
|
closeButton.innerHTML = "×";
|
|
closeButton.addEventListener("click", () =>
|
|
this.close(notificationElement)
|
|
);
|
|
notificationElement.appendChild(closeButton);
|
|
|
|
document.body.appendChild(notificationElement);
|
|
|
|
|
|
setTimeout(() => {
|
|
this.close(notificationElement);
|
|
}, 5000);
|
|
}
|
|
|
|
close(notificationElement) {
|
|
notificationElement.remove();
|
|
}
|
|
}
|
|
|
|
|
|
const notificationInstance = new Notification();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ImageHistory {
|
|
constructor(historyContainerId) {
|
|
this.history = [];
|
|
this.historyContainer = document.querySelector(`#${historyContainerId}`);
|
|
}
|
|
|
|
addToHistory(imageUrl) {
|
|
this.history.push(imageUrl);
|
|
this.renderHistory();
|
|
}
|
|
|
|
renderHistory() {
|
|
|
|
this.historyContainer.innerHTML = "";
|
|
|
|
if (this.history.length === 0) {
|
|
const historyItem = document.createElement("p");
|
|
historyItem.textContent = "No images generated yet.";
|
|
this.historyContainer.appendChild(historyItem);
|
|
return;
|
|
}
|
|
|
|
|
|
this.history.forEach((imageUrl, index) => {
|
|
const historyItem = document.createElement("div");
|
|
historyItem.classList.add("history-item");
|
|
|
|
const image = document.createElement("img");
|
|
image.src = imageUrl;
|
|
|
|
historyItem.appendChild(image);
|
|
|
|
|
|
const downloadButton = document.createElement("span");
|
|
const iconDownload = document.createElement("i");
|
|
iconDownload.classList.add("fas", "fa-arrow-down");
|
|
downloadButton.appendChild(iconDownload);
|
|
|
|
downloadButton.classList.add("downloadButton");
|
|
downloadButton.addEventListener("click", (e) => {
|
|
e.preventDefault();
|
|
this.downloadThisImage(index);
|
|
});
|
|
historyItem.appendChild(downloadButton);
|
|
|
|
|
|
const removeBtn = document.createElement("span");
|
|
const iconRemove = document.createElement("i");
|
|
iconRemove.classList.add("fas", "fa-xmark");
|
|
removeBtn.appendChild(iconRemove);
|
|
|
|
removeBtn.classList.add("removeButton");
|
|
|
|
removeBtn.addEventListener("click", (e) => this.removeFromHistory(index));
|
|
historyItem.appendChild(removeBtn);
|
|
|
|
this.historyContainer.appendChild(historyItem);
|
|
});
|
|
}
|
|
|
|
downloadThisImage(index) {
|
|
const imageUrl = this.history[index];
|
|
if (imageUrl) {
|
|
|
|
const link = document.createElement("a");
|
|
link.href = imageUrl;
|
|
link.download = `image_${index + 1}.png`;
|
|
link.click();
|
|
link.remove();
|
|
} else {
|
|
|
|
notificationInstance.show("error", "Image URL not found.");
|
|
}
|
|
}
|
|
|
|
removeFromHistory(index) {
|
|
if (index >= 0 && index < this.history.length) {
|
|
this.history.splice(index, 1);
|
|
this.renderHistory();
|
|
notificationInstance.show("success", "Image removed from history.");
|
|
} else {
|
|
|
|
notificationInstance.show("error", "Invalid index.");
|
|
}
|
|
}
|
|
|
|
clearHistory() {
|
|
this.history = [];
|
|
this.saveToLocalStorage();
|
|
this.renderHistory();
|
|
notificationInstance.show("success", "History cleared.");
|
|
}
|
|
|
|
saveToLocalStorage() {
|
|
localStorage.setItem("TextToImgHistory", JSON.stringify(this.history));
|
|
notificationInstance.show("success", "History saved.");
|
|
}
|
|
|
|
loadImageHistory() {
|
|
const savedHistory = localStorage.getItem("TextToImgHistory");
|
|
if (savedHistory) {
|
|
this.history = JSON.parse(savedHistory);
|
|
this.renderHistory();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
const imageHistoryInstance = new ImageHistory("history-container");
|
|
|
|
|
|
|
|
|