snappymail/dev/Common/Momentor.js

156 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-07-02 06:49:59 +08:00
import window from 'window';
import _ from '_';
import $ from '$';
import moment from 'moment';
2019-07-05 03:19:24 +08:00
import { i18n } from 'Common/Translator';
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
let _moment = null;
let _momentNow = 0;
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
const updateMomentNow = _.debounce(
() => {
_moment = moment();
},
500,
true
);
const updateMomentNowUnix = _.debounce(
() => {
_momentNow = moment().unix();
},
500,
true
);
2015-11-15 08:23:16 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {moment}
*/
2019-07-05 03:19:24 +08:00
export function momentNow() {
2016-06-16 07:36:44 +08:00
updateMomentNow();
return _moment || moment();
}
2015-11-15 08:23:16 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {number}
*/
2019-07-05 03:19:24 +08:00
export function momentNowUnix() {
2016-06-16 07:36:44 +08:00
updateMomentNowUnix();
return _momentNow || 0;
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {number} date
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function searchSubtractFormatDateHelper(date) {
return momentNow()
.clone()
.subtract(date, 'days')
.format('YYYY.MM.DD');
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {Object} m
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
function formatCustomShortDate(m) {
2016-06-16 07:36:44 +08:00
const now = momentNow();
2019-07-05 03:19:24 +08:00
if (m && now) {
switch (true) {
2016-06-16 07:36:44 +08:00
case 4 >= now.diff(m, 'hours'):
return m.fromNow();
case now.format('L') === m.format('L'):
2016-06-17 07:23:49 +08:00
return i18n('MESSAGE_LIST/TODAY_AT', {
2016-06-16 07:36:44 +08:00
TIME: m.format('LT')
});
2019-07-05 03:19:24 +08:00
case now
.clone()
.subtract(1, 'days')
.format('L') === m.format('L'):
2016-06-17 07:23:49 +08:00
return i18n('MESSAGE_LIST/YESTERDAY_AT', {
2016-06-16 07:36:44 +08:00
TIME: m.format('LT')
});
case now.year() === m.year():
return m.format('D MMM.');
2016-06-30 08:02:45 +08:00
// no default
2015-11-15 08:23:16 +08:00
}
}
2016-06-16 07:36:44 +08:00
return m ? m.format('LL') : '';
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {number} timeStampInUTC
* @param {string} formatStr
2016-06-30 08:02:45 +08:00
* @returns {string}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function format(timeStampInUTC, formatStr) {
let m = null,
2016-06-30 08:02:45 +08:00
result = '';
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
const now = momentNowUnix();
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
timeStampInUTC = 0 < timeStampInUTC ? timeStampInUTC : 0 === timeStampInUTC ? now : 0;
2016-06-16 07:36:44 +08:00
timeStampInUTC = now < timeStampInUTC ? now : timeStampInUTC;
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
m = 0 < timeStampInUTC ? moment.unix(timeStampInUTC) : null;
2019-07-05 03:19:24 +08:00
if (m && 1970 === m.year()) {
2016-06-16 07:36:44 +08:00
m = null;
}
2015-11-15 08:23:16 +08:00
2019-07-05 03:19:24 +08:00
if (m) {
switch (formatStr) {
2016-06-16 07:36:44 +08:00
case 'FROMNOW':
result = m.fromNow();
break;
case 'SHORT':
result = formatCustomShortDate(m);
break;
case 'FULL':
result = m.format('LLL');
break;
default:
result = m.format(formatStr);
break;
2015-11-15 08:23:16 +08:00
}
}
2016-06-16 07:36:44 +08:00
return result;
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
/**
* @param {Object} element
2016-06-30 08:02:45 +08:00
* @returns {void}
2016-06-16 07:36:44 +08:00
*/
2019-07-05 03:19:24 +08:00
export function momentToNode(element) {
let key = '',
time = 0;
2019-07-05 03:19:24 +08:00
const $el = $(element);
2016-06-16 07:36:44 +08:00
time = $el.data('moment-time');
2019-07-05 03:19:24 +08:00
if (time) {
2016-06-16 07:36:44 +08:00
key = $el.data('moment-format');
2019-07-05 03:19:24 +08:00
if (key) {
2016-06-16 07:36:44 +08:00
$el.text(format(time, key));
}
2015-11-15 08:23:16 +08:00
2016-06-16 07:36:44 +08:00
key = $el.data('moment-format-title');
2019-07-05 03:19:24 +08:00
if (key) {
2016-06-16 07:36:44 +08:00
$el.attr('title', format(time, key));
2015-11-15 08:23:16 +08:00
}
}
2016-06-16 07:36:44 +08:00
}
2015-11-15 08:23:16 +08:00
2016-06-30 08:02:45 +08:00
/**
* @returns {void}
*/
2019-07-05 03:19:24 +08:00
export function reload() {
2016-06-16 07:36:44 +08:00
_.defer(() => {
$('.moment', window.document).each((index, item) => {
momentToNode(item);
2015-11-15 08:23:16 +08:00
});
2016-06-16 07:36:44 +08:00
});
2015-11-15 08:23:16 +08:00
}