|
4 | 4 | * Requirement: <https://github.com/iamkun/dayjs>
|
5 | 5 | */
|
6 | 6 |
|
| 7 | +/* A tool for locale datetime */ |
| 8 | +const LocaleHelper = (function () { |
| 9 | + const $preferLocale = $('meta[name="prefer-datetime-locale"]'); |
| 10 | + const locale = $preferLocale.length > 0 ? |
| 11 | + $preferLocale.attr('content').toLowerCase() : $('html').attr('lang').substr(0, 2); |
| 12 | + const attrTimestamp = 'data-ts'; |
| 13 | + const attrDateFormat = 'data-df'; |
| 14 | + |
| 15 | + return { |
| 16 | + locale: () => locale, |
| 17 | + attrTimestamp: () => attrTimestamp, |
| 18 | + attrDateFormat: () => attrDateFormat, |
| 19 | + getTimestamp: ($elem) => Number($elem.attr(attrTimestamp)), // unix timestamp |
| 20 | + getDateFormat: ($elem) => $elem.attr(attrDateFormat) |
| 21 | + }; |
| 22 | + |
| 23 | +}()); |
| 24 | + |
7 | 25 | $(function() {
|
8 | 26 | dayjs.locale(LocaleHelper.locale());
|
9 | 27 | dayjs.extend(window.dayjs_plugin_localizedFormat);
|
10 | 28 |
|
11 | 29 | $(`[${LocaleHelper.attrTimestamp()}]`).each(function () {
|
12 | 30 | const date = dayjs.unix(LocaleHelper.getTimestamp($(this)));
|
13 |
| - const df = LocaleHelper.getDateFormat($(this)); |
14 |
| - const text = date.format(df); |
15 |
| - |
| 31 | + const text = date.format(LocaleHelper.getDateFormat($(this))); |
16 | 32 | $(this).text(text);
|
17 | 33 | $(this).removeAttr(LocaleHelper.attrTimestamp());
|
18 | 34 | $(this).removeAttr(LocaleHelper.attrDateFormat());
|
| 35 | + |
| 36 | + // setup tooltips |
| 37 | + const tooltip = $(this).attr('data-toggle'); |
| 38 | + if (typeof tooltip === 'undefined' || tooltip !== 'tooltip') { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const tooltipText = date.format('llll'); // see: https://day.js.org/docs/en/display/format#list-of-localized-formats |
| 43 | + $(this).attr('data-original-title', tooltipText); |
19 | 44 | });
|
20 | 45 | });
|
0 commit comments