mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-08-30 12:39:20 +03:00
Momentor (moment optimization)
This commit is contained in:
parent
7750587be1
commit
147dce6e4a
20 changed files with 331 additions and 173 deletions
|
|
@ -27,11 +27,6 @@
|
|||
*/
|
||||
Globals.now = (new window.Date()).getTime();
|
||||
|
||||
/**
|
||||
* @type {?}
|
||||
*/
|
||||
Globals.momentTrigger = ko.observable(true);
|
||||
|
||||
/**
|
||||
* @type {?}
|
||||
*/
|
||||
|
|
|
|||
188
dev/Common/Momentor.js
Normal file
188
dev/Common/Momentor.js
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
|
||||
(function () {
|
||||
|
||||
'use strict';
|
||||
|
||||
var
|
||||
window = require('window'),
|
||||
$ = require('$'),
|
||||
_ = require('_'),
|
||||
moment = require('moment'),
|
||||
|
||||
Translator = require('Common/Translator')
|
||||
;
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
function Momentor()
|
||||
{
|
||||
this.format = _.bind(this.format, this);
|
||||
|
||||
this.updateMomentNow = _.debounce(_.bind(function () {
|
||||
this._moment = moment();
|
||||
}, this), 500, true);
|
||||
|
||||
this.updateMomentNowUnix = _.debounce(_.bind(function () {
|
||||
this._momentNow = moment().unix();
|
||||
}, this), 500, true);
|
||||
}
|
||||
|
||||
Momentor.prototype._moment = null;
|
||||
Momentor.prototype._momentNow = 0;
|
||||
|
||||
Momentor.prototype.momentNow = function ()
|
||||
{
|
||||
this.updateMomentNow();
|
||||
return this._moment || moment();
|
||||
};
|
||||
|
||||
Momentor.prototype.momentNowUnix = function ()
|
||||
{
|
||||
this.updateMomentNowUnix();
|
||||
return this._momentNow || 0;
|
||||
};
|
||||
|
||||
Momentor.prototype.searchSubtractFormatDateHelper = function (iDate)
|
||||
{
|
||||
var oM = this.momentNow();
|
||||
return oM.clone().subtract('days', iDate).format('YYYY.MM.DD');
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} oMoment
|
||||
* @return {string}
|
||||
*/
|
||||
Momentor.prototype.formatCustomShortDate = function (oMoment)
|
||||
{
|
||||
var
|
||||
sResult = '',
|
||||
oMomentNow = this.momentNow()
|
||||
;
|
||||
|
||||
if (oMoment && oMomentNow)
|
||||
{
|
||||
if (4 >= oMomentNow.diff(oMoment, 'hours'))
|
||||
{
|
||||
sResult = oMoment.fromNow();
|
||||
}
|
||||
else if (oMomentNow.format('L') === oMoment.format('L'))
|
||||
{
|
||||
sResult = Translator.i18n('MESSAGE_LIST/TODAY_AT', {
|
||||
'TIME': oMoment.format('LT')
|
||||
});
|
||||
}
|
||||
else if (oMomentNow.clone().subtract('days', 1).format('L') === oMoment.format('L'))
|
||||
{
|
||||
sResult = Translator.i18n('MESSAGE_LIST/YESTERDAY_AT', {
|
||||
'TIME': oMoment.format('LT')
|
||||
});
|
||||
}
|
||||
else if (oMomentNow.year() === oMoment.year())
|
||||
{
|
||||
sResult = oMoment.format('D MMM.');
|
||||
}
|
||||
else
|
||||
{
|
||||
sResult = oMoment.format('LL');
|
||||
}
|
||||
}
|
||||
|
||||
return sResult;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {number} iTimeStampInUTC
|
||||
* @param {string} sFormat
|
||||
* @return {string}
|
||||
*/
|
||||
Momentor.prototype.format = function (iTimeStampInUTC, sFormat)
|
||||
{
|
||||
var
|
||||
oM = null,
|
||||
sResult = '',
|
||||
iNow = this.momentNowUnix()
|
||||
;
|
||||
|
||||
iTimeStampInUTC = 0 < iTimeStampInUTC ? iTimeStampInUTC : (0 === iTimeStampInUTC ? iNow : 0);
|
||||
iTimeStampInUTC = iNow < iTimeStampInUTC ? iNow : iTimeStampInUTC;
|
||||
|
||||
oM = 0 < iTimeStampInUTC ? moment.unix(iTimeStampInUTC) : null;
|
||||
|
||||
if (oM && 1970 === oM.year())
|
||||
{
|
||||
oM = null;
|
||||
}
|
||||
|
||||
if (oM)
|
||||
{
|
||||
switch (sFormat)
|
||||
{
|
||||
case 'FROMNOW':
|
||||
sResult = oM.fromNow();
|
||||
break;
|
||||
case 'SHORT':
|
||||
sResult = this.formatCustomShortDate(oM);
|
||||
break;
|
||||
case 'FULL':
|
||||
sResult = oM.format('LLL');
|
||||
break;
|
||||
default:
|
||||
sResult = oM.format(sFormat);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return sResult;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} oElement
|
||||
*/
|
||||
Momentor.prototype.momentToNode = function (oElement)
|
||||
{
|
||||
var
|
||||
sKey = '',
|
||||
iTime = 0,
|
||||
$oEl = $(oElement)
|
||||
;
|
||||
|
||||
iTime = $oEl.data('moment-time');
|
||||
if (iTime)
|
||||
{
|
||||
sKey = $oEl.data('moment-format');
|
||||
|
||||
if (sKey)
|
||||
{
|
||||
$oEl.text(this.format(iTime, sKey));
|
||||
}
|
||||
|
||||
sKey = $oEl.data('moment-format-title');
|
||||
if (sKey)
|
||||
{
|
||||
$oEl.attr('title', this.format(iTime, sKey));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} oElements
|
||||
*/
|
||||
Momentor.prototype.momentToNodes = function (oElements)
|
||||
{
|
||||
var self = this;
|
||||
_.defer(function () {
|
||||
$('.moment', oElements).each(function () {
|
||||
self.momentToNode(this);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
Momentor.prototype.reload = function ()
|
||||
{
|
||||
this.momentToNodes(window.document);
|
||||
};
|
||||
|
||||
module.exports = new Momentor();
|
||||
|
||||
}());
|
||||
|
|
@ -58,48 +58,57 @@
|
|||
|
||||
/**
|
||||
* @param {Object} oElement
|
||||
*/
|
||||
Translator.prototype.i18nToNode = function (oElement)
|
||||
{
|
||||
var
|
||||
sKey = '',
|
||||
$oEl = $(oElement)
|
||||
;
|
||||
|
||||
sKey = $oEl.data('i18n-text');
|
||||
if (sKey)
|
||||
{
|
||||
$oEl.text(this.i18n(sKey));
|
||||
}
|
||||
else
|
||||
{
|
||||
sKey = $oEl.data('i18n-html');
|
||||
if (sKey)
|
||||
{
|
||||
$oEl.html(this.i18n(sKey));
|
||||
}
|
||||
|
||||
sKey = $oEl.data('i18n-placeholder');
|
||||
if (sKey)
|
||||
{
|
||||
$oEl.attr('placeholder', this.i18n(sKey));
|
||||
}
|
||||
|
||||
sKey = $oEl.data('i18n-title');
|
||||
if (sKey)
|
||||
{
|
||||
$oEl.attr('title', this.i18n(sKey));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} oElements
|
||||
* @param {boolean=} bAnimate = false
|
||||
*/
|
||||
Translator.prototype.i18nToNode = function (oElement, bAnimate)
|
||||
Translator.prototype.i18nToNodes = function (oElements, bAnimate)
|
||||
{
|
||||
var self = this;
|
||||
_.defer(function () {
|
||||
$('.i18n', oElement).each(function () {
|
||||
var
|
||||
jqThis = $(this),
|
||||
sKey = ''
|
||||
;
|
||||
|
||||
sKey = jqThis.data('i18n-text');
|
||||
if (sKey)
|
||||
{
|
||||
jqThis.text(self.i18n(sKey));
|
||||
}
|
||||
else
|
||||
{
|
||||
sKey = jqThis.data('i18n-html');
|
||||
if (sKey)
|
||||
{
|
||||
jqThis.html(self.i18n(sKey));
|
||||
}
|
||||
|
||||
sKey = jqThis.data('i18n-placeholder');
|
||||
if (sKey)
|
||||
{
|
||||
jqThis.attr('placeholder', self.i18n(sKey));
|
||||
}
|
||||
|
||||
sKey = jqThis.data('i18n-title');
|
||||
if (sKey)
|
||||
{
|
||||
jqThis.attr('title', self.i18n(sKey));
|
||||
}
|
||||
}
|
||||
$('.i18n', oElements).each(function () {
|
||||
self.i18nToNode(this);
|
||||
});
|
||||
|
||||
if (bAnimate && Globals.bAnimationSupported)
|
||||
{
|
||||
$('.i18n-animation.i18n', oElement).letterfx({
|
||||
$('.i18n-animation.i18n', oElements).letterfx({
|
||||
'fx': 'fall fade', 'backwards': false, 'timing': 50, 'fx_duration': '50ms', 'letter_end': 'restore', 'element_end': 'restore'
|
||||
});
|
||||
}
|
||||
|
|
@ -112,7 +121,9 @@
|
|||
{
|
||||
this.data = window['rainloopI18N'] || {};
|
||||
|
||||
this.i18nToNode($(window.document), true);
|
||||
this.i18nToNodes(window.document, true);
|
||||
|
||||
require('Common/Momentor').reload();
|
||||
this.trigger(!this.trigger());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -585,71 +585,6 @@
|
|||
return fResult;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {{moment:Function}} oObject
|
||||
*/
|
||||
Utils.createMomentDate = function (oObject)
|
||||
{
|
||||
if (Utils.isUnd(oObject.moment))
|
||||
{
|
||||
oObject.moment = ko.observable(moment());
|
||||
}
|
||||
|
||||
return ko.computed(function () {
|
||||
Globals.momentTrigger();
|
||||
var oMoment = this.moment();
|
||||
return 1970 === oMoment.year() ? '' : oMoment.fromNow();
|
||||
}, oObject);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {{moment:Function, momentDate:Function}} oObject
|
||||
*/
|
||||
Utils.createMomentShortDate = function (oObject)
|
||||
{
|
||||
return ko.computed(function () {
|
||||
|
||||
var
|
||||
sResult = '',
|
||||
oMomentNow = moment(),
|
||||
oMoment = this.moment(),
|
||||
sMomentDate = this.momentDate()
|
||||
;
|
||||
|
||||
if (1970 === oMoment.year())
|
||||
{
|
||||
sResult = '';
|
||||
}
|
||||
else if (4 >= oMomentNow.diff(oMoment, 'hours'))
|
||||
{
|
||||
sResult = sMomentDate;
|
||||
}
|
||||
else if (oMomentNow.format('L') === oMoment.format('L'))
|
||||
{
|
||||
sResult = require('Common/Translator').i18n('MESSAGE_LIST/TODAY_AT', {
|
||||
'TIME': oMoment.format('LT')
|
||||
});
|
||||
}
|
||||
else if (oMomentNow.clone().subtract('days', 1).format('L') === oMoment.format('L'))
|
||||
{
|
||||
sResult = require('Common/Translator').i18n('MESSAGE_LIST/YESTERDAY_AT', {
|
||||
'TIME': oMoment.format('LT')
|
||||
});
|
||||
}
|
||||
else if (oMomentNow.year() === oMoment.year())
|
||||
{
|
||||
sResult = oMoment.format('D MMM.');
|
||||
}
|
||||
else
|
||||
{
|
||||
sResult = oMoment.format('LL');
|
||||
}
|
||||
|
||||
return sResult;
|
||||
|
||||
}, oObject);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} sTheme
|
||||
* @return {string}
|
||||
|
|
@ -764,7 +699,7 @@
|
|||
$('#rl-content', oBody).html(oTemplate.html());
|
||||
$('html', oWin.document).addClass('external ' + $('html').attr('class'));
|
||||
|
||||
require('Common/Translator').i18nToNode(oBody);
|
||||
require('Common/Translator').i18nToNodes(oBody);
|
||||
|
||||
if (oViewModel && $('#rl-content', oBody)[0])
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue