DEV: removes jquery usage from time formatter (#14700)

This commit is contained in:
Joffrey JAFFEUX 2021-10-25 11:00:15 +02:00 committed by GitHub
parent c7099342ff
commit cbd7898d1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 38 deletions

View File

@ -6,7 +6,7 @@ export default {
initialize() { initialize() {
this._interval = setInterval(function () { this._interval = setInterval(function () {
updateRelativeAge($(".relative-date")); updateRelativeAge(document.querySelectorAll(".relative-date"));
}, 60 * 1000); }, 60 * 1000);
}, },

View File

@ -1,5 +1,6 @@
import { helperContext, makeArray } from "discourse-common/lib/helpers";
import deprecated from "discourse-common/lib/deprecated";
import I18n from "I18n"; import I18n from "I18n";
import { helperContext } from "discourse-common/lib/helpers";
export function shortDate(date) { export function shortDate(date) {
return moment(date).format(I18n.t("dates.medium.date_year")); return moment(date).format(I18n.t("dates.medium.date_year"));
@ -49,15 +50,22 @@ export function longDateNoYear(dt) {
} }
export function updateRelativeAge(elems) { export function updateRelativeAge(elems) {
// jQuery .each if (elems instanceof jQuery) {
elems.each(function () { elems = elems.toArray();
const $this = $(this); deprecated("updateRelativeAge now expects a DOM NodeList", {
$this.html( since: "2.8.0.beta7",
relativeAge(new Date($this.data("time")), { });
format: $this.data("format"), }
wrapInSpan: false,
}) if (!NodeList.prototype.isPrototypeOf(elems)) {
); elems = makeArray(elems);
}
elems.forEach((elem) => {
elem.innerHTML = relativeAge(new Date(parseInt(elem.dataset.time, 10)), {
format: elem.dataset.format,
wrapInSpan: false,
});
}); });
} }

View File

@ -10,6 +10,13 @@ import { discourseModule } from "discourse/tests/helpers/qunit-helpers";
import sinon from "sinon"; import sinon from "sinon";
import { test } from "qunit"; import { test } from "qunit";
function stringToDOMNode(string) {
let template = document.createElement("template");
string = string.trim();
template.innerHTML = string;
return template.content.firstChild;
}
function formatMins(mins, opts = {}) { function formatMins(mins, opts = {}) {
let dt = new Date(new Date() - mins * 60 * 1000); let dt = new Date(new Date() - mins * 60 * 1000);
return relativeAge(dt, { return relativeAge(dt, {
@ -37,7 +44,7 @@ function shortDateTester(format) {
} }
function strip(html) { function strip(html) {
return $(html).text(); return stringToDOMNode(html).innerText;
} }
discourseModule("Unit | Utility | formatter", function (hooks) { discourseModule("Unit | Utility | formatter", function (hooks) {
@ -108,10 +115,15 @@ discourseModule("Unit | Utility | formatter", function (hooks) {
); );
assert.equal( assert.equal(
$(formatDays(0, { format: "medium" })).attr("title"), stringToDOMNode(formatDays(0, { format: "medium" })).title,
longDate(new Date()) longDate(new Date())
); );
assert.equal($(formatDays(0, { format: "medium" })).attr("class"), "date");
assert.ok(
stringToDOMNode(formatDays(0, { format: "medium" })).classList.contains(
"date"
)
);
this.clock.restore(); this.clock.restore();
this.clock = sinon.useFakeTimers(new Date(2012, 0, 9, 12, 0).getTime()); // Jan 9, 2012 this.clock = sinon.useFakeTimers(new Date(2012, 0, 9, 12, 0).getTime()); // Jan 9, 2012
@ -205,49 +217,52 @@ discourseModule("Unit | Utility | formatter", function (hooks) {
test("autoUpdatingRelativeAge", function (assert) { test("autoUpdatingRelativeAge", function (assert) {
let d = moment().subtract(1, "day").toDate(); let d = moment().subtract(1, "day").toDate();
let $elem = $(autoUpdatingRelativeAge(d)); let elem = stringToDOMNode(autoUpdatingRelativeAge(d));
assert.equal($elem.data("format"), "tiny"); assert.equal(elem.dataset.format, "tiny");
assert.equal($elem.data("time"), d.getTime()); assert.equal(elem.dataset.time, d.getTime());
assert.equal($elem.attr("title"), undefined); assert.equal(elem.title, "");
$elem = $(autoUpdatingRelativeAge(d, { title: true })); elem = stringToDOMNode(autoUpdatingRelativeAge(d, { title: true }));
assert.equal($elem.attr("title"), longDate(d)); assert.equal(elem.title, longDate(d));
$elem = $( elem = stringToDOMNode(
autoUpdatingRelativeAge(d, { autoUpdatingRelativeAge(d, {
format: "medium", format: "medium",
title: true, title: true,
leaveAgo: true, leaveAgo: true,
}) })
); );
assert.equal($elem.data("format"), "medium-with-ago");
assert.equal($elem.data("time"), d.getTime());
assert.equal($elem.attr("title"), longDate(d));
assert.equal($elem.html(), "1 day ago");
$elem = $(autoUpdatingRelativeAge(d, { format: "medium" })); assert.equal(elem.dataset.format, "medium-with-ago");
assert.equal($elem.data("format"), "medium"); assert.equal(elem.dataset.time, d.getTime());
assert.equal($elem.data("time"), d.getTime()); assert.equal(elem.title, longDate(d));
assert.equal($elem.attr("title"), undefined); assert.equal(elem.innerHTML, "1 day ago");
assert.equal($elem.html(), "1 day");
elem = stringToDOMNode(autoUpdatingRelativeAge(d, { format: "medium" }));
assert.equal(elem.dataset.format, "medium");
assert.equal(elem.dataset.time, d.getTime());
assert.equal(elem.title, "");
assert.equal(elem.innerHTML, "1 day");
}); });
test("updateRelativeAge", function (assert) { test("updateRelativeAge", function (assert) {
let d = new Date(); let d = new Date();
let $elem = $(autoUpdatingRelativeAge(d)); let elem = stringToDOMNode(autoUpdatingRelativeAge(d));
$elem.data("time", d.getTime() - 2 * 60 * 1000); elem.dataset.time = d.getTime() - 2 * 60 * 1000;
updateRelativeAge($elem); updateRelativeAge(elem);
assert.equal($elem.html(), "2m"); assert.equal(elem.innerHTML, "2m");
d = new Date(); d = new Date();
$elem = $(autoUpdatingRelativeAge(d, { format: "medium", leaveAgo: true })); elem = stringToDOMNode(
$elem.data("time", d.getTime() - 2 * 60 * 1000); autoUpdatingRelativeAge(d, { format: "medium", leaveAgo: true })
);
elem.dataset.time = d.getTime() - 2 * 60 * 1000;
updateRelativeAge($elem); updateRelativeAge(elem);
assert.equal($elem.html(), "2 mins ago"); assert.equal(elem.innerHTML, "2 mins ago");
}); });
test("number", function (assert) { test("number", function (assert) {