Content deleted Content added
No edit summary |
No edit summary |
||
Line 23:
/**
* Formats to display inline for each timestamp
*
* If a property
*
* `type` | `options`
* -----------|----------
* `relative` | `Intl.RelativeTimeFormat` options
* `absolute` | `Intl.AbsoluteTimeFormat` options
* `iso8601` | —
*
* If a property is a function, it is called to retrieve the formatted
* timestamp string. The function must accept one argument, a `Date` object.
*/
/**
* Relative dates are helpful if the user doesn’t remember today’s date.
* The tooltip provides a more specific timestamp to distinguish
* comments in rapid succession.
*/
relative: {
numeric: "auto",
},
/**
* Absolute dates are helpful for more distant dates, so that the user
*
*/
absolute: {
dateStyle: "full",
timeStyle: "short",
},
},
Line 62 ⟶ 59:
* Formats to display in each timestamp’s tooltip, one per line.
*
* If an element of this array is
*
* `type` | `options`
* -----------|----------
* `relative` | `Intl.RelativeTimeFormat` options
* `absolute` | `Intl.AbsoluteTimeFormat` options
* `iso8601` | —
*
* If an element of this array is a function, it is called to retrieve the
* formatted timestamp string.
* `Date` object.
*/
tooltipComponents: [
{
type: "
options: {
numeric: "auto",
},
},
{
type: "absolute",
options: {
dateStyle: "full",
timeStyle: "short",
},
},
{
type: "iso8601",
},
],
Line 222 ⟶ 236:
// abbr[title], guiding the user to the tooltip.
timeElt.addClass("localcomments explain");
timeElt.
}
$(dateNode).wrap(timeElt);
Line 229 ⟶ 243:
/**
* Returns
* elapsed between the given date and the current date.
*
* @param {Date} then The date object
* @returns {Object} An object indicating the date component’s value and
* unit compatible with `Intl.RelativeTimeFormat`.
*/
function
var now = new Date();
var value;
var unit;
Line 267 ⟶ 257:
value = seconds;
unit = "seconds";
var minutes = seconds / 60;
if (Math.abs(seconds) > 45) { // moment.relativeTimeThreshold("s")
Line 272 ⟶ 263:
unit = "minutes";
}
var hours = minutes / 60;
if (Math.abs(minutes) > 45) { // moment.relativeTimeThreshold("m")
Line 277 ⟶ 269:
unit = "hours";
}
var days = hours / 24;
if (Math.abs(hours) > 22) { // moment.relativeTimeThreshold("h")
Line 282 ⟶ 275:
unit = "days";
}
var weeks = days / 7;
if (Math.abs(days) > 7) {
Line 287 ⟶ 281:
unit = "weeks";
}
return {
value: Math.round(value),
unit: unit,
};
}
/**
* Returns a formatted string for the given date object.
*
* @param {Date} then The date object to format.
* @param {String} fmt A format string or function.
* @returns {String} A formatted string.
*/
function formatDate(then, fmt) {
if (fmt instanceof Function) {
return fmt(then);
}
var lang = mw.config.get("wgPageViewLanguage");
var format;
switch (fmt.type) {
case "absolute":
format = new Intl.DateTimeFormat(lang, fmt.options);
return format.format(then);
case "relative":
format = new Intl.RelativeTimeFormat(lang, fmt.options);
var component = relativeDateComponent(then);
return format.format(component.value, component.unit);
case "iso8601":
return then.toISOString();
}
}
/**
* Reformats a timestamp marked up with the <time> element.
*
* @param {Number} idx Unused.
* @param {Element} elt The <time> element.
*/
function formatTimestamp(idx, elt) {
var iso = $(elt).dateTime;
var then = new Date(Date.parse(iso));
// Add a tooltip with multiple formats.
elt.title = $.map(LocalComments.tooltipComponents, function (fmt, idx) {
return formatDate(then, fmt) || "";
}).join("\n");
// Replace the text.
var component = relativeDateComponent(then);
var text;
if (component.unit === "weeks") {
text = formatDate(then, {
type: "relative",
options: LocalComments.outputFormats.relative,
});
} else {
text = formatDate(then, {
type: "absolute",
options: LocalComments.outputFormats.absolute,
});
}
$(elt).text(text);
}
// Register for periodic updates.
$(elt).attr("data-localcomments-unit", component.unit);
}
|