// Mock Data - Student Accommodations
const listingsData = [
{
id: 1,
title: "Vita Student Circle Square",
location: "Manchester, UK",
university: "University of Manchester",
price: 245,
originalPrice: 280,
rating: 4.9,
reviews: 128,
image: "https://images.unsplash.com/photo-1555854877-bab0e564b8d5?w=800&h=600&fit=crop",
badges: ["Bestseller", "Verified"],
amenities: ["Gym", "Cinema", "Study Room", "Roof Terrace"],
roomType: "Studio",
available: "Sep 2024",
featured: true,
coordinates: { lat: 53.4808, lng: -2.2426 }
},
{
id: 2,
title: "Student Castle Cambridge",
location: "Cambridge, UK",
university: "University of Cambridge",
price: 320,
originalPrice: 350,
rating: 4.8,
reviews: 96,
image: "https://images.unsplash.com/photo-1596276026589-276f90a9772d?w=800&h=600&fit=crop",
badges: ["Premium", "New"],
amenities: ["Pool", "Gym", "Game Room", "Laundry"],
roomType: "Ensuite",
available: "Oct 2024",
featured: true,
coordinates: { lat: 52.2053, lng: 0.1218 }
},
{
id: 3,
title: "IQ Shoreditch London",
location: "London, UK",
university: "UCL / LSE",
price: 425,
originalPrice: 475,
rating: 4.7,
reviews: 203,
image: "https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=800&h=600&fit=crop",
badges: ["Trending", "Verified"],
amenities: ["Sky Lounge", "Gym", "Cinema", "BBQ Area"],
roomType: "Studio",
available: "Sep 2024",
featured: true,
coordinates: { lat: 51.5074, lng: -0.1278 }
},
{
id: 4,
title: "Canvas Wembley",
location: "London, UK",
university: "University of Westminster",
price: 295,
originalPrice: 320,
rating: 4.6,
reviews: 87,
image: "https://images.unsplash.com/photo-1502672260266-1c1ef2d93688?w=800&h=600&fit=crop",
badges: ["Great Value"],
amenities: ["Gym", "Study Spaces", "Rooftop", "Events"],
roomType: "Ensuite",
available: "Sep 2024",
featured: false,
coordinates: { lat: 51.556, lng: -0.279 }
},
{
id: 5,
title: "Yugo Explore Glasgow",
location: "Glasgow, UK",
university: "University of Glasgow",
price: 185,
originalPrice: 210,
rating: 4.8,
reviews: 156,
image: "https://images.unsplash.com/photo-1493809842364-78817add7ffb?w=800&h=600&fit=crop",
badges: ["Popular", "Verified"],
amenities: ["Gym", "Cinema", "Game Room", "Laundry"],
roomType: "Studio",
available: "Aug 2024",
featured: true,
coordinates: { lat: 55.8609, lng: -4.2514 }
},
{
id: 6,
title: "Unilife Highfield",
location: "Southampton, UK",
university: "University of Southampton",
price: 165,
originalPrice: 185,
rating: 4.5,
reviews: 72,
image: "https://images.unsplash.com/photo-1501183638710-841dd1904471?w=800&h=600&fit=crop",
badges: ["Affordable"],
amenities: ["Study Room", "Common Area", "Bike Storage", "WiFi"],
roomType: "Ensuite",
available: "Sep 2024",
featured: false,
coordinates: { lat: 50.9097, lng: -1.4044 }
},
{
id: 7,
title: "Collegiate Nova",
location: "Nottingham, UK",
university: "University of Nottingham",
price: 195,
originalPrice: 225,
rating: 4.7,
reviews: 98,
image: "https://images.unsplash.com/photo-1484154218962-a1c002085d2f?w=800&h=600&fit=crop",
badges: ["New", "Verified"],
amenities: ["Gym", "Spa", "Cinema", "Rooftop Terrace"],
roomType: "Studio",
available: "Oct 2024",
featured: true,
coordinates: { lat: 52.9548, lng: -1.1581 }
},
{
id: 8,
title: "The Hive Birmingham",
location: "Birmingham, UK",
university: "University of Birmingham",
price: 175,
originalPrice: 200,
rating: 4.6,
reviews: 115,
image: "https://images.unsplash.com/photo-1502672023488-70e25813eb80?w=800&h=600&fit=crop",
badges: ["Great Value"],
amenities: ["Gym", "Study Spaces", "Social Hub", "Laundry"],
roomType: "Ensuite",
available: "Sep 2024",
featured: false,
coordinates: { lat: 52.4862, lng: -1.8904 }
},
{
id: 9,
title: "Scape Shoreditch",
location: "London, UK",
university: "Queen Mary University",
price: 385,
originalPrice: 420,
rating: 4.8,
reviews: 178,
image: "https://images.unsplash.com/photo-1522708323590-d24dbb6b0267?w=800&h=600&fit=crop",
badges: ["Premium", "Verified"],
amenities: ["Gym", "Cinema", "Rooftop Pool", "Events Program"],
roomType: "Studio",
available: "Sep 2024",
featured: true,
coordinates: { lat: 51.5203, lng: -0.0297 }
},
{
id: 10,
title: "Student Roost The Railyard",
location: "Liverpool, UK",
university: "University of Liverpool",
price: 155,
originalPrice: 175,
rating: 4.5,
reviews: 89,
image: "https://images.unsplash.com/photo-1493663284031-b7e3aefcae8e?w=800&h=600&fit=crop",
badges: ["Affordable"],
amenities: ["Common Room", "Study Area", "Bike Storage", "WiFi"],
roomType: "Ensuite",
available: "Sep 2024",
featured: false,
coordinates: { lat: 53.4084, lng: -2.9916 }
},
{
id: 11,
title: "Downing Students The Lyra",
location: "London, UK",
university: "Imperial College London",
price: 445,
originalPrice: 495,
rating: 4.9,
reviews: 234,
image: "https://images.unsplash.com/photo-1545324418-cc1a3fa10c00?w=800&h=600&fit=crop",
badges: ["Luxury", "Premium"],
amenities: ["Gym", "Spa", "Private Cinema", "Concierge", "Rooftop Terrace"],
roomType: "Studio",
available: "Sep 2024",
featured: true,
coordinates: { lat: 51.4988, lng: -0.1749 }
},
{
id: 12,
title: "iQ Leeds",
location: "Leeds, UK",
university: "University of Leeds",
price: 165,
originalPrice: 185,
rating: 4.6,
reviews: 134,
image: "https://images.unsplash.com/photo-1505691938895-1758d7feb511?w=800&h=600&fit=crop",
badges: ["Great Value", "Verified"],
amenities: ["Gym", "Study Rooms", "Social Spaces", "Laundry"],
roomType: "Ensuite",
available: "Sep 2024",
featured: false,
coordinates: { lat: 53.808, lng: -1.553 }
}
];
// State Management
let currentListings = [...listingsData];
let filteredListings = [...listingsData];
let currentView = 'grid';
let currentSort = 'recommended';
let favorites = new Set();
let isDarkMode = false;
// DOM Elements
const listingsGrid = document.getElementById('listingsGrid');
const listingsScrollContainer = document.getElementById('listingsScrollContainer');
const searchInput = document.getElementById('searchInput');
const priceFilter = document.getElementById('priceFilter');
const searchBtn = document.getElementById('searchBtn');
const sortSelect = document.getElementById('sortSelect');
const gridViewBtn = document.getElementById('gridViewBtn');
const listViewBtn = document.getElementById('listViewBtn');
const scrollLeftBtn = document.getElementById('scrollLeftBtn');
const scrollRightBtn = document.getElementById('scrollRightBtn');
const listingCount = document.getElementById('listingCount');
const emptyState = document.getElementById('emptyState');
const clearFiltersBtn = document.getElementById('clearFiltersBtn');
const loadMoreBtn = document.getElementById('loadMoreBtn');
const quickFilters = document.querySelectorAll('.quick-filter');
// Initialize
document.addEventListener('DOMContentLoaded', () => {
loadFavorites();
checkDarkMode();
renderListings();
setupEventListeners();
updateListingCount();
feather.replace();
});
// Event Listeners
function setupEventListeners() {
// Search
searchInput.addEventListener('input', debounce(handleSearch, 300));
searchBtn.addEventListener('click', handleSearch);
// Filters
priceFilter.addEventListener('change', handleSearch);
sortSelect.addEventListener('change', handleSort);
// View Toggle
gridViewBtn.addEventListener('click', () => setView('grid'));
listViewBtn.addEventListener('click', () => setView('list'));
// Scroll Navigation
scrollLeftBtn.addEventListener('click', () => scrollListings('left'));
scrollRightBtn.addEventListener('click', () => scrollListings('right'));
// Clear Filters
clearFiltersBtn.addEventListener('click', clearAllFilters);
// Load More
loadMoreBtn.addEventListener('click', loadMoreListings);
// Quick Filters
quickFilters.forEach(btn => {
btn.addEventListener('click', () => {
searchInput.value = btn.dataset.filter;
handleSearch();
});
});
// Scroll detection for navigation buttons
listingsScrollContainer.addEventListener('scroll', updateScrollButtons);
}
// Search & Filter Logic
function handleSearch() {
const query = searchInput.value.toLowerCase().trim();
const priceRange = priceFilter.value;
filteredListings = listingsData.filter(listing => {
// Text search
const matchesSearch = !query ||
listing.title.toLowerCase().includes(query) ||
listing.location.toLowerCase().includes(query) ||
listing.university.toLowerCase().includes(query) ||
listing.roomType.toLowerCase().includes(query) ||
listing.amenities.some(a => a.toLowerCase().includes(query));
// Price filter
let matchesPrice = true;
if (priceRange) {
const [min, max] = priceRange.split('-').map(v => v === '1200+' ? Infinity : parseInt(v));
matchesPrice = listing.price >= min && (max === Infinity ? listing.price >= 1200 : listing.price <= max);
}
return matchesSearch && matchesPrice;
});
// Apply current sort
applySort();
// Update UI
renderListings();
updateListingCount();
updateEmptyState();
}
function handleSort() {
currentSort = sortSelect.value;
applySort();
renderListings();
}
function applySort() {
const sortFunctions = {
'recommended': (a, b) => (b.featured ? 1 : 0) - (a.featured ? 1 : 0) || b.rating - a.rating,
'price-low': (a, b) => a.price - b.price,
'price-high': (a, b) => b.price - a.price,
'rating': (a, b) => b.rating - a.rating,
'newest': (a, b) => b.id - a.id
};
filteredListings.sort(sortFunctions[currentSort] || sortFunctions['recommended']);
}
// View Management
function setView(view) {
currentView = view;
// Update button states
if (view === 'grid') {
gridViewBtn.classList.add('bg-white', 'dark:bg-slate-700', 'text-primary-600', 'dark:text-primary-400', 'shadow-sm');
gridViewBtn.classList.remove('text-slate-500', 'dark:text-slate-400');
listViewBtn.classList.remove('bg-white', 'dark:bg-slate-700', 'text-primary-600', 'dark:text-primary-400', 'shadow-sm');
listViewBtn.classList.add('text-slate-500', 'dark:text-slate-400');
listingsGrid.classList.remove('flex-col');
listingsGrid.classList.add('flex');
} else {
listViewBtn.classList.add('bg-white', 'dark:bg-slate-700', 'text-primary-600', 'dark:text-primary-400', 'shadow-sm');
listViewBtn.classList.remove('text-slate-500', 'dark:text-slate-400');
gridViewBtn.classList.remove('bg-white', 'dark:bg-slate-700', 'text-primary-600', 'dark:text-primary-400', 'shadow-sm');
gridViewBtn.classList.add('text-slate-500', 'dark:text-slate-400');
}
renderListings();
}
// Scroll Navigation
function scrollListings(direction) {
const scrollAmount = 400;
const currentScroll = listingsScrollContainer.scrollLeft;
const targetScroll = direction === 'left'
? currentScroll - scrollAmount
: currentScroll + scrollAmount;
listingsScrollContainer.scrollTo({
left: targetScroll,
behavior: 'smooth'
});
}
function updateScrollButtons() {
const { scrollLeft, scrollWidth, clientWidth } = listingsScrollContainer;
const canScrollLeft = scrollLeft > 0;
const canScrollRight = scrollLeft < scrollWidth - clientWidth - 10;
scrollLeftBtn.style.opacity = canScrollLeft ? '1' : '0';
scrollLeftBtn.style.pointerEvents = canScrollLeft ? 'auto' : 'none';
scrollRightBtn.style.opacity = canScrollRight ? '1' : '0';
scrollRightBtn.style.pointerEvents = canScrollRight ? 'auto' : 'none';
}
// Render Listings
function renderListings() {
if (filteredListings.length === 0) {
listingsGrid.innerHTML = '';
return;
}
const cardsHTML = filteredListings.map(listing => createListingCard(listing)).join('');
listingsGrid.innerHTML = cardsHTML;
// Re-initialize feather icons
feather.replace();
// Add event listeners to new elements
document.querySelectorAll('.favorite-btn').forEach(btn => {
btn.addEventListener('click', (e) => toggleFavorite(e, parseInt(btn.dataset.id)));
});
document.querySelectorAll('.view-details-btn').forEach(btn => {
btn.addEventListener('click', () => showListingDetails(parseInt(btn.dataset.id)));
});
}
function createListingCard(listing) {
const isFavorited = favorites.has(listing.id);
const discount = listing.originalPrice ? Math.round((1 - listing.price / listing.originalPrice) * 100) : 0;
return `
${listing.title}