forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show localized date/time on hover for articles/comments (forem#2722)
* Add article decorator published_timestamp * Use time HTML5 element and refactor date in partial * Add published_timestamp to Article Adding `published_timestamp` to the homepage we can then use JS to render the full timestamp localized for the user. We've also added the timestamp to the index and the API * Display article published timestamp on hover * Use time also in the article show page * Add timestamp to bottom articles as well * Remove published_timestamp from index because it is not used * Fix broken specs * Add more article dates specs * Refactor date initializers
- Loading branch information
1 parent
260fe90
commit 4e591fe
Showing
25 changed files
with
211 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
app/assets/javascripts/initializers/initializeArticleDate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* Show article date/time according to user's locale */ | ||
/* global addLocalizedDateTimeToElementsTitles */ | ||
|
||
function initializeArticleDate() { | ||
var articlesDates = document.querySelectorAll( | ||
'.single-article time, article time, .single-other-article time', | ||
); | ||
|
||
addLocalizedDateTimeToElementsTitles(articlesDates, 'datetime'); | ||
} |
48 changes: 3 additions & 45 deletions
48
app/assets/javascripts/initializers/initializeCommentDate.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,8 @@ | ||
/* Show comment date/time according to user's locale */ | ||
/* global timestampToLocalDateTime */ | ||
/* global addLocalizedDateTimeToElementsTitles */ | ||
|
||
function initializeCommentDate() { | ||
// example: "Wednesday, April 3, 2019, 2:55:14 PM" | ||
var hoverTimeOptions = { | ||
weekday: 'long', | ||
year: 'numeric', | ||
month: 'long', | ||
day: 'numeric', | ||
hour: 'numeric', | ||
minute: 'numeric', | ||
second: 'numeric', | ||
}; | ||
var commentsDates = document.querySelectorAll('.comment-date time'); | ||
|
||
// example: "Apr 3" | ||
var visibleDateOptions = { | ||
month: 'short', | ||
day: 'numeric', | ||
}; | ||
|
||
var commentDates = document.getElementsByClassName('comment-date'); | ||
for (var i = 0; i < commentDates.length; i += 1) { | ||
// get UTC timestamp set by the server | ||
var ts = commentDates[i].getAttribute('data-published-timestamp'); | ||
|
||
// add a full datetime to the comment date string, visible on hover | ||
// `navigator.language` is used for full date times to allow the hover date | ||
// to be localized according to the user's locale | ||
var hoverTime = timestampToLocalDateTime( | ||
ts, | ||
navigator.language, | ||
hoverTimeOptions, | ||
); | ||
commentDates[i].setAttribute('title', hoverTime); | ||
|
||
// replace the comment short visible date with the equivalent localized one | ||
var visibleDate = commentDates[i].querySelector('a'); | ||
if (visibleDate) { | ||
var localVisibleDate = timestampToLocalDateTime( | ||
ts, | ||
'en-US', // en-US because for now we want all users to see `Apr 3` | ||
visibleDateOptions, | ||
); | ||
if (localVisibleDate) { | ||
visibleDate.innerHTML = localVisibleDate; | ||
} | ||
} | ||
} | ||
addLocalizedDateTimeToElementsTitles(commentsDates, 'datetime'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<div class="comment-date"> | ||
<a href="<%= decorated_comment.path %>"> | ||
<time datetime="<%= decorated_comment.published_timestamp %>"> | ||
<%= decorated_comment.readable_publish_date %> | ||
</time> | ||
</a> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.