|
84 | 84 | <script> |
85 | 85 |
|
86 | 86 | const roomId = '{{chatgroup.id}}' |
| 87 | + var tempDaysWeekdays = []; |
87 | 88 |
|
88 | 89 | const chatSocket = new WebSocket( |
89 | 90 | `ws://${window.location.host}/ws/chat/${roomId}/` |
|
181 | 182 | </div> |
182 | 183 | </div>` |
183 | 184 | } |
| 185 | + |
| 186 | + if (msg_time) { |
| 187 | + showDatesWeekDays(msg_time) |
| 188 | + } else { |
| 189 | + showDatesWeekDays(new Date()) |
| 190 | + } |
184 | 191 |
|
185 | 192 | newDiv.innerHTML = msg1; |
186 | 193 |
|
|
240 | 247 | })); |
241 | 248 | } |
242 | 249 |
|
| 250 | + |
| 251 | + function showDatesWeekDays(date_created) { |
| 252 | + // add the newly created element and its content into the DOM |
| 253 | + |
| 254 | + dt = new Date(date_created) |
| 255 | + |
| 256 | + if (!tempDaysWeekdays.includes(dt.toLocaleDateString())) { |
| 257 | + let newDiv = document.createElement("div"); |
| 258 | + let currentDiv = document.getElementById("new-message-chat"); |
| 259 | + let parentDiv = currentDiv.parentNode; |
| 260 | + let days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']; |
| 261 | + |
| 262 | + if (dt.toDateString() == new Date().toDateString()) { |
| 263 | + // display TODAY in message |
| 264 | + date_weekday = 'TODAY'; |
| 265 | + } else if(dt > getDateBefore()) { |
| 266 | + // display week day in message |
| 267 | + date_weekday = days[dt.getDay()].toUpperCase() |
| 268 | + } else { |
| 269 | + // display date in message |
| 270 | + date_weekday = dt.toLocaleDateString(); |
| 271 | + } |
| 272 | + |
| 273 | + newDiv.style.display = "grid"; |
| 274 | + newDiv.innerHTML = `<div class="date_weekday">${date_weekday}</div>` |
| 275 | + parentDiv.insertBefore(newDiv, currentDiv); |
| 276 | + |
| 277 | + tempDaysWeekdays.push(dt.toLocaleDateString()) |
| 278 | + } |
| 279 | + |
| 280 | + } |
| 281 | + |
| 282 | + function getDateBefore(days=7) { |
| 283 | + // calculate the last 7 days date |
| 284 | + // 7 (days) * 24 (hours) * 60 (minutes) * 60 (seconds) * 1000 (milliseconds ) = 604800000 or 7 days in milliseconds. |
| 285 | + daysInMs= days * 24 * 60 * 60 * 1000 |
| 286 | + return new Date(Date.now() - daysInMs) |
| 287 | + } |
| 288 | + |
243 | 289 | function loadMessage() { |
244 | 290 | fetch("{% url 'chat:history' chatgroup.id %}") |
245 | 291 | .then( response => response.json() ) |
|
0 commit comments