-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathscript.js
More file actions
240 lines (186 loc) · 8.09 KB
/
Copy pathscript.js
File metadata and controls
240 lines (186 loc) · 8.09 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
const API_URL = "https://api.shiraz-metro.workers.dev/api/v1";
let metroData = {};
let line = "line1";
async function fetchStations() {
try {
const response = await fetch(`${API_URL}/stations/stations`);
metroData = await response.json();
loadStations('line1');
} catch (error) {
console.error("Error fetching stations:", error);
}
}
function loadStations(line) {
const startSelect = document.getElementById('start');
const destinationSelect = document.getElementById('destination');
startSelect.innerHTML = '<option value="">انتخاب ایستگاه مبدا</option>';
destinationSelect.innerHTML = '<option value="">انتخاب ایستگاه مقصد</option>';
metroData[line].stations.forEach(station => {
const optionStart = document.createElement('option');
optionStart.value = station;
optionStart.textContent = station;
startSelect.appendChild(optionStart);
const optionDest = document.createElement('option');
optionDest.value = station;
optionDest.textContent = station;
destinationSelect.appendChild(optionDest);
});
}
function checkSelection() {
const startStation = document.getElementById('start').value;
const destinationStation = document.getElementById('destination').value;
const calculateButton = document.getElementById('calculate');
const scheduleContainer = document.getElementById('schedule');
scheduleContainer.classList.remove('show');
if (startStation === "" || destinationStation === "" || startStation === destinationStation) {
calculateButton.disabled = true;
} else {
calculateButton.disabled = false;
}
}
function displaySchedule(schedule) {
const now = new Date();
const nowTime = now.getHours() * 60 + now.getMinutes();
const scheduleContainer = document.getElementById('schedule');
scheduleContainer.innerHTML = '';
let nextTrainItem = null;
let hasResults = false;
let currentTimeFound = false;
if (schedule.length === 0) {
const noServiceItem = document.createElement('div');
noServiceItem.className = 'schedule-item no-service';
scheduleContainer.classList.add('show')
noServiceItem.textContent = "مترو در این روز خدمات رسانی ندارد";
scheduleContainer.appendChild(noServiceItem);
} else{
schedule.forEach(({ departure, arrival }) => {
const [depHours, depMinutes] = departure.split(':').map(Number);
const [arrHours, arrMinutes] = arrival.split(':').map(Number);
const departureTime = depHours * 60 + depMinutes;
const arrivalTime = arrHours * 60 + arrMinutes;
let status = 'missed';
let isCurrentTime = false;
let isNearTime = false;
if (departureTime > nowTime) {
status = 'future';
const timeDiff = departureTime - nowTime;
if (timeDiff <= 2 && timeDiff >= 0) {
isCurrentTime = true;
isNearTime = true;
currentTimeFound = true;
}
if (!nextTrainItem) {
nextTrainItem = true;
}
}
const scheduleItem = document.createElement('div');
scheduleItem.className = `schedule-item ${status}`;
if (isCurrentTime) {
scheduleItem.classList.add('current-time');
}
if (isNearTime) {
scheduleItem.classList.add('near-time');
}
if (status === 'future' && nextTrainItem === true) {
scheduleItem.id = 'next-train';
nextTrainItem = scheduleItem;
}
scheduleItem.innerHTML = `
<span class="departure-time">حرکتازمبدا: ${departure}</span>
<span class="arrival-time">رسیدنبهمقصد: ${arrival}</span>
`;
scheduleContainer.appendChild(scheduleItem);
hasResults = true;
});
if (hasResults) {
scheduleContainer.classList.add('show');
setTimeout(() => {
let targetElement = null;
const currentTimeElement = scheduleContainer.querySelector('.current-time');
if (currentTimeElement) {
targetElement = currentTimeElement;
} else if (nextTrainItem && nextTrainItem !== true) {
targetElement = nextTrainItem;
}
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'center'
});
}
}, 100);
} else {
scheduleContainer.classList.remove('show');
}
}
}
async function loadContributors() {
const owner = "MaxEdison";
const repo = "ShirazMetro";
const list = document.getElementById("contributors-list");
try {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/contributors?per_page=100`);
if (!response.ok) throw new Error("GitHub API error");
const contributors = await response.json();
contributors.sort((a, b) => b.contributions - a.contributions);
if (contributors.length === 0) {
list.innerHTML = "<p>هیچ مشارکت کننده ای یافت نشد</p>";
return;
}
list.innerHTML = "";
contributors.forEach(user => {
const div = document.createElement("a");
div.className = "contributor";
div.href = user.html_url;
div.target = "_blank";
div.title = `${user.login} – ${user.contributions} commit`;
div.innerHTML = `
<img src="${user.avatar_url}&s=112" alt="${user.login}" loading="lazy">
<span>${user.login}</span>
`;
list.appendChild(div);
});
} catch (err) {
console.error("Failed to load contributors:", err);
list.innerHTML = `<p style="color:#c70e30;">خطا در بارگذاری مشارکت کنندگان</p>`;
}
}
document.addEventListener('DOMContentLoaded', () => {
fetchStations();
const tabs = document.querySelectorAll('.switch-btn');
const slider = document.querySelector('.switch-slider');
tabs.forEach((tab, index) => {
tab.addEventListener('click', () => {
tabs.forEach(t => t.classList.remove('active'));
tab.classList.add('active');
slider.style.right = `${index * 50}%`;
line = tab.dataset.line;
if (line === "line1") {
slider.style.background = "#db0000";
// document.querySelector('.label-text').textContent = "روز تعطیل یا پنجشنبه ؟";
} else if (line === "line2") {
slider.style.background = "#047a00ff";
// document.querySelector('.label-text').textContent = "روز تعطیل؟";
}
loadStations(line);
});
});
loadContributors();
});
document.getElementById('calculate').addEventListener('click', async function() {
const startStation = document.getElementById('start').value;
const destinationStation = document.getElementById('destination').value;
const isHoliday = document.getElementById('holiday').checked;
try {
const response = await fetch(
`${API_URL}/schedules/calculate?startStation=${encodeURIComponent(startStation)}&destinationStation=${encodeURIComponent(destinationStation)}&holiday=${isHoliday ? 'yes' : 'no'}&line=${encodeURIComponent(line)}`
);
if (!response.ok) {
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
displaySchedule(data.schedule);
} catch (error) {
console.error("Error fetching schedule:", error);
}
});