Code refactoring

This commit is contained in:
RainLoop Team 2016-06-16 02:36:44 +03:00
parent 60bac7192f
commit 90d95d3ca4
28 changed files with 977 additions and 924 deletions

View file

@ -2,159 +2,147 @@
import {window, $, _, moment} from 'common';
import Translator from 'Common/Translator';
class Momentor
let _moment = null;
let _momentNow = 0;
const updateMomentNow = _.debounce(() => {
_moment = moment();
}, 500, true);
const updateMomentNowUnix = _.debounce(() => {
_momentNow = moment().unix();
}, 500, true);
export function momentNow()
{
_moment = null;
_momentNow = 0;
updateMomentNow();
return _moment || moment();
}
constructor()
export function momentNowUnix()
{
updateMomentNowUnix();
return _momentNow || 0;
}
/**
* @param {number} date
* @return {string}
*/
export function searchSubtractFormatDateHelper(date)
{
momentNow().clone().subtract('days', date).format('YYYY.MM.DD');
}
/**
* @param {Object} m
* @return {string}
*/
function formatCustomShortDate(m)
{
const now = momentNow();
if (m && now)
{
this.updateMomentNow = _.debounce(() => {
this._moment = moment();
}, 500, true);
this.updateMomentNowUnix = _.debounce(() => {
this._momentNow = moment().unix();
}, 500, true);
this.format = _.bind(this.format, this);
}
momentNow() {
this.updateMomentNow();
return this._moment || moment();
}
momentNowUnix() {
this.updateMomentNowUnix();
return this._momentNow || 0;
}
/**
* @param {number} date
* @return {string}
*/
searchSubtractFormatDateHelper(date) {
return this.momentNow().clone().subtract('days', date).format('YYYY.MM.DD');
}
/**
* @param {Object} m
* @return {string}
*/
formatCustomShortDate(m) {
const now = this.momentNow();
if (m && now)
switch(true)
{
switch(true)
{
case 4 >= now.diff(m, 'hours'):
return m.fromNow();
case now.format('L') === m.format('L'):
return Translator.i18n('MESSAGE_LIST/TODAY_AT', {
TIME: m.format('LT')
});
case now.clone().subtract('days', 1).format('L') === m.format('L'):
return Translator.i18n('MESSAGE_LIST/YESTERDAY_AT', {
TIME: m.format('LT')
});
case now.year() === m.year():
return m.format('D MMM.');
}
}
return m ? m.format('LL') : '';
}
/**
* @param {number} timeStampInUTC
* @param {string} format
* @return {string}
*/
format(timeStampInUTC, format) {
let
m = null,
result = ''
;
const now = this.momentNowUnix();
timeStampInUTC = 0 < timeStampInUTC ? timeStampInUTC : (0 === timeStampInUTC ? now : 0);
timeStampInUTC = now < timeStampInUTC ? now : timeStampInUTC;
m = 0 < timeStampInUTC ? moment.unix(timeStampInUTC) : null;
if (m && 1970 === m.year())
{
m = null;
}
if (m)
{
switch (format)
{
case 'FROMNOW':
result = m.fromNow();
break;
case 'SHORT':
result = this.formatCustomShortDate(m);
break;
case 'FULL':
result = m.format('LLL');
break;
default:
result = m.format(format);
break;
}
}
return result;
}
/**
* @param {Object} element
*/
momentToNode(element) {
var
key = '',
time = 0,
$el = $(element)
;
time = $el.data('moment-time');
if (time)
{
key = $el.data('moment-format');
if (key)
{
$el.text(this.format(time, key));
}
key = $el.data('moment-format-title');
if (key)
{
$el.attr('title', this.format(time, key));
}
case 4 >= now.diff(m, 'hours'):
return m.fromNow();
case now.format('L') === m.format('L'):
return Translator.i18n('MESSAGE_LIST/TODAY_AT', {
TIME: m.format('LT')
});
case now.clone().subtract('days', 1).format('L') === m.format('L'):
return Translator.i18n('MESSAGE_LIST/YESTERDAY_AT', {
TIME: m.format('LT')
});
case now.year() === m.year():
return m.format('D MMM.');
}
}
/**
* @param {Object} elements
*/
momentToNodes(elements) {
_.defer(() => {
$('.moment', elements).each((index, item) => {
this.momentToNode(item);
});
});
return m ? m.format('LL') : '';
}
/**
* @param {number} timeStampInUTC
* @param {string} formatStr
* @return {string}
*/
export function format(timeStampInUTC, formatStr)
{
let
m = null,
result = ''
;
const now = momentNowUnix();
timeStampInUTC = 0 < timeStampInUTC ? timeStampInUTC : (0 === timeStampInUTC ? now : 0);
timeStampInUTC = now < timeStampInUTC ? now : timeStampInUTC;
m = 0 < timeStampInUTC ? moment.unix(timeStampInUTC) : null;
if (m && 1970 === m.year())
{
m = null;
}
reload() {
this.momentToNodes(window.document);
if (m)
{
switch (formatStr)
{
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;
}
}
return result;
}
/**
* @param {Object} element
*/
export function momentToNode(element)
{
var
key = '',
time = 0,
$el = $(element)
;
time = $el.data('moment-time');
if (time)
{
key = $el.data('moment-format');
if (key)
{
$el.text(format(time, key));
}
key = $el.data('moment-format-title');
if (key)
{
$el.attr('title', format(time, key));
}
}
}
module.exports = new Momentor();
export function reload()
{
_.defer(() => {
$('.moment', window.document).each((index, item) => {
momentToNode(item);
});
});
}