2015-11-15 08:23:16 +08:00
|
|
|
|
2016-07-02 06:49:59 +08:00
|
|
|
import window from 'window';
|
|
|
|
import _ from '_';
|
|
|
|
import $ from '$';
|
|
|
|
import moment from 'moment';
|
2016-06-17 07:23:49 +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
|
|
|
|
2016-06-16 07:36:44 +08:00
|
|
|
const updateMomentNow = _.debounce(() => {
|
|
|
|
_moment = moment();
|
|
|
|
}, 500, true);
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-06-16 07:36:44 +08:00
|
|
|
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}
|
|
|
|
*/
|
2016-06-16 07:36:44 +08:00
|
|
|
export function momentNow()
|
|
|
|
{
|
|
|
|
updateMomentNow();
|
|
|
|
return _moment || moment();
|
|
|
|
}
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-06-30 08:02:45 +08:00
|
|
|
/**
|
|
|
|
* @returns {number}
|
|
|
|
*/
|
2016-06-16 07:36:44 +08:00
|
|
|
export function momentNowUnix()
|
|
|
|
{
|
|
|
|
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
|
|
|
*/
|
|
|
|
export function searchSubtractFormatDateHelper(date)
|
|
|
|
{
|
2016-07-30 03:14:51 +08:00
|
|
|
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
|
|
|
*/
|
|
|
|
function formatCustomShortDate(m)
|
|
|
|
{
|
|
|
|
const now = momentNow();
|
|
|
|
if (m && now)
|
|
|
|
{
|
2016-06-30 08:02:45 +08:00
|
|
|
switch (true)
|
2015-11-15 08:23:16 +08:00
|
|
|
{
|
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')
|
|
|
|
});
|
2016-07-30 03:14:51 +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
|
|
|
*/
|
|
|
|
export function format(timeStampInUTC, formatStr)
|
|
|
|
{
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-06-16 07:36:44 +08:00
|
|
|
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
|
|
|
|
2016-06-16 07:36:44 +08:00
|
|
|
timeStampInUTC = 0 < timeStampInUTC ? timeStampInUTC : (0 === timeStampInUTC ? now : 0);
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (m && 1970 === m.year())
|
|
|
|
{
|
|
|
|
m = null;
|
|
|
|
}
|
2015-11-15 08:23:16 +08:00
|
|
|
|
2016-06-16 07:36:44 +08:00
|
|
|
if (m)
|
|
|
|
{
|
|
|
|
switch (formatStr)
|
2015-11-15 08:23:16 +08:00
|
|
|
{
|
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
|
|
|
*/
|
|
|
|
export function momentToNode(element)
|
|
|
|
{
|
|
|
|
var
|
|
|
|
key = '',
|
|
|
|
time = 0,
|
2016-06-30 08:02:45 +08:00
|
|
|
$el = $(element);
|
2016-06-16 07:36:44 +08:00
|
|
|
|
|
|
|
time = $el.data('moment-time');
|
|
|
|
if (time)
|
|
|
|
{
|
|
|
|
key = $el.data('moment-format');
|
|
|
|
if (key)
|
|
|
|
{
|
|
|
|
$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');
|
|
|
|
if (key)
|
2015-11-15 08:23:16 +08:00
|
|
|
{
|
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}
|
|
|
|
*/
|
2016-06-16 07:36:44 +08:00
|
|
|
export function reload()
|
|
|
|
{
|
|
|
|
_.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
|
|
|
}
|