Skip to content

Commit

Permalink
Replace moment.js with dayjs
Browse files Browse the repository at this point in the history
  • Loading branch information
david-yz-liu committed Dec 14, 2024
1 parent 1f75199 commit 6c52afa
Show file tree
Hide file tree
Showing 11 changed files with 426 additions and 3,204 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### 🔧 Internal changes

- Reorganize Javascript files to separate Sprockets and Webpack compilation (#7345)
- Replace moment.js dependency with much smaller dayjs (#7346)

## [v2.6.1]

Expand Down
6 changes: 3 additions & 3 deletions app/assets/javascripts/PeriodDeltaChain/PeriodDeltaChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ PeriodDeltaChain.prototype.refresh = function () {
var from_time_node = this.querySelector(".PeriodDeltaChain_FromTime");
var to_time_node = this.querySelector(".PeriodDeltaChain_ToTime");
var hours_value = this.querySelector(".PeriodDeltaChain_Hours").value;
var from_time = moment(current_time, me.date_format);
var to_time = moment(current_time, me.date_format);
var from_time = dayjs(current_time, me.date_format);
var to_time = from_time.add(hours_value, "hours");

$(from_time_node).html(from_time.format(format));
$(to_time_node).html(to_time.add("hours", hours_value).format(format));
$(to_time_node).html(to_time.format(format));

current_time = to_time;
});
Expand Down
23 changes: 0 additions & 23 deletions app/assets/javascripts/localize_date.js
Original file line number Diff line number Diff line change
@@ -1,23 +0,0 @@
/** Convert a date/time to a nice Date object.
Example: 2014-08-21 14:38:00 UTC -> [Date object] */
function convert_date_time(date) {
var arr_date = date.split(/[ T]/);
var iso_date = arr_date[0] + " " + arr_date[1] + arr_date[2];
return moment(iso_date);
}

/** Localize the date with a specified locale format string. */
function localize_date(actual_date_div, date_div, format) {
if (actual_date_div.value !== "") {
var date = moment(actual_date_div.value);
date_div.value = date.format(format);
}
}

/** Localize the date/time with a specified locale format string. */
function localize_datetime(actual_date_div, date_div, format) {
if (actual_date_div.value.indexOf(" ") > -1) {
var date = convert_date_time($(actual_date_div).attr("value"));
date_div.value = date.format(format);
}
}
8 changes: 5 additions & 3 deletions app/javascript/application_webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ import "javascripts/jquery.easyModal";
import safe_marked from "./common/safe_marked";
window.safe_marked = safe_marked;

// moment (date/times manipulation)
import moment from "moment";
window.moment = moment;
// dayjs (date/times manipulation)
import dayjs from "dayjs";
import customParseFormat from "dayjs/plugin/customParseFormat";
dayjs.extend(customParseFormat);
window.dayjs = dayjs;

// mousetrap (keybindings)
import "mousetrap";
Expand Down
3 changes: 1 addition & 2 deletions app/views/assignments/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<% content_for :head do %>
<%= javascript_include_tag 'create_assignment',
'PeriodDeltaChain/PeriodDeltaChain',
'localize_date', nonce: true %>
'PeriodDeltaChain/PeriodDeltaChain', nonce: true %>
<%= render partial: 'boot', formats: [:js], handlers: [:erb] %>
<%= render partial: 'shared/navigation_warning',
formats: [:js],
Expand Down
3 changes: 1 addition & 2 deletions app/views/assignments/_peer_review_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<% content_for :head do %>
<%= javascript_include_tag 'create_assignment',
'PeriodDeltaChain/PeriodDeltaChain',
'localize_date', nonce: true %>
'PeriodDeltaChain/PeriodDeltaChain', nonce: true %>
<%= render partial: 'boot', formats: [:js], handlers: [:erb] %>
<%= render partial: 'shared/navigation_warning',
formats: [:js],
Expand Down
3 changes: 1 addition & 2 deletions app/views/automated_tests/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<% content_for :head do %>
<%= javascript_include_tag 'PeriodDeltaChain/PeriodDeltaChain',
'localize_date', nonce: true %>
<%= javascript_include_tag 'PeriodDeltaChain/PeriodDeltaChain', nonce: true %>

<%= javascript_tag nonce: true do %>
document.addEventListener('DOMContentLoaded', () => {
Expand Down
3 changes: 1 addition & 2 deletions app/views/groups/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<% content_for :head do %>
<%= javascript_include_tag 'Groups/index', nonce: true %>
<%= javascript_include_tag 'PeriodDeltaChain/PeriodDeltaChain',
'localize_date', nonce: true %>
<%= javascript_include_tag 'PeriodDeltaChain/PeriodDeltaChain', nonce: true %>

<%= javascript_tag nonce: true do %>
<% parts = @assignment.duration_parts
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_i18n_config.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= javascript_tag nonce: true do %>
if (I18n) {
I18n.locale = '<%= I18n.locale %>';
moment.locale(I18n.locale);
dayjs.locale(I18n.locale);
}
<% end %>
Loading

0 comments on commit 6c52afa

Please sign in to comment.