diff --git a/public/script.js b/public/script.js
index bbaf0c7..58b1af9 100644
--- a/public/script.js
+++ b/public/script.js
@@ -2,7 +2,18 @@ const form = document.getElementById('merge-form');
const calendars = document.getElementById('calendars');
const addCalendarButton = document.getElementById('add-calendar');
const result = document.getElementById('result');
+const resultInfo = document.querySelector('.result-info');
+// Search functionality elements
+const searchBtn = document.getElementById('search-btn');
+const calendarSearch = document.getElementById('calendar-search');
+const searchResult = document.getElementById('search-result');
+
+// Variable to track if we're editing an existing calendar
+let isEditing = false;
+let currentCalendarName = '';
+
+// Variable to track the index of the calendar being added
let calendarIndex = 1;
let mergedUrl = '';
@@ -12,78 +23,221 @@ function isValidUrl(url) {
return urlPattern.test(url);
}
- addCalendarButton.addEventListener('click', () => {
- const newCalendar = document.createElement('div');
- newCalendar.className = 'calendar-entry';
- newCalendar.innerHTML = `
-
-
-
-
-
-
-
- `;
- calendars.appendChild(newCalendar);
- calendarIndex++;
- });
-
- form.addEventListener('submit', (event) => {
- event.preventDefault();
- const linkGroupName = document.getElementById('link-group-name').value;
- const calendarsData = [];
- let valid = true; // Flag to track URL validity
+// Function to extract calendar name from URL or input
+function extractCalendarName(input) {
+ // If it's a URL, extract the last part of the path
+ if (input.startsWith('http')) {
+ try {
+ // Remove trailing slash if present
+ if (input.endsWith('/')) {
+ input = input.slice(0, -1);
+ }
- for (let i = 0; i < calendarIndex; i++) {
- const prefix = document.getElementById(`prefix-${i}`);
- const override = document.getElementById(`override-${i}`);
- const url = document.getElementById(`url-${i}`);
-
- if (prefix && override && url) {
- // Validate the URL
- if (!isValidUrl(url.value)) {
- valid = false; // Set flag to false if any URL is invalid
- alert(`Invalid URL format for calendar ${i + 1}: ${url.value}`);
- } else {
- calendarsData.push({
- prefix: prefix.value,
- override: override.checked,
- url: url.value
- });
- }
+ // Extract the last part of the path
+ const url = new URL(input);
+ const pathParts = url.pathname.split('/').filter(part => part.length > 0);
+
+ // If there's a path part, use the last one
+ if (pathParts.length > 0) {
+ let lastPart = pathParts[pathParts.length - 1];
+
+ // Remove .ics extension if present
+ if (lastPart.endsWith('.ics')) {
+ lastPart = lastPart.slice(0, -4);
}
+
+ return lastPart;
}
- if (valid) {
- fetch('/merge', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json'
- },
- body: JSON.stringify({ linkGroupName, calendars: calendarsData })
- })
- .then((response) => {
- if (!response.ok) {
- return response.json().then(err => { throw err; });
+ } catch (e) {
+ console.error('Error parsing URL:', e);
+ }
+ }
+
+ // If not a URL or URL parsing failed, just return the input as is
+ return input;
+}
+
+// Event listener for the search button
+if (searchBtn) {
+ searchBtn.addEventListener('click', searchCalendar);
+
+ // Also search when pressing Enter in the search field
+ if (calendarSearch) {
+ calendarSearch.addEventListener('keypress', function(e) {
+ if (e.key === 'Enter') {
+ searchCalendar();
+ }
+ });
+ }
+}
+
+// Function to search for a calendar
+function searchCalendar() {
+ let calendarName = calendarSearch.value.trim();
+ if (!calendarName) {
+ searchResult.innerHTML = '
Please enter a calendar name
';
+ return;
+ }
+
+ // Extract just the calendar name if a URL was entered
+ calendarName = extractCalendarName(calendarName);
+
+ searchResult.innerHTML = '