ES6ify /gr-date-formatter/*
Bug: Issue 6179 Change-Id: I1cea560935a9dd57e1c388ac8b160d6744539135
This commit is contained in:
@@ -14,12 +14,12 @@
|
||||
(function() {
|
||||
'use strict';
|
||||
|
||||
var Duration = {
|
||||
const Duration = {
|
||||
HOUR: 1000 * 60 * 60,
|
||||
DAY: 1000 * 60 * 60 * 24,
|
||||
};
|
||||
|
||||
var TimeFormats = {
|
||||
const TimeFormats = {
|
||||
TIME_12: 'h:mm A', // 2:14 PM
|
||||
TIME_12_WITH_SEC: 'h:mm:ss A', // 2:14:00 PM
|
||||
TIME_24: 'HH:mm', // 14:14
|
||||
@@ -61,16 +61,16 @@
|
||||
Gerrit.TooltipBehavior,
|
||||
],
|
||||
|
||||
attached: function() {
|
||||
attached() {
|
||||
this._loadPreferences();
|
||||
},
|
||||
|
||||
_getUtcOffsetString: function() {
|
||||
_getUtcOffsetString() {
|
||||
return ' UTC' + moment().format('Z');
|
||||
},
|
||||
|
||||
_loadPreferences: function() {
|
||||
return this._getLoggedIn().then(function(loggedIn) {
|
||||
_loadPreferences() {
|
||||
return this._getLoggedIn().then(loggedIn => {
|
||||
if (!loggedIn) {
|
||||
this._timeFormat = TimeFormats.TIME_24;
|
||||
this._relative = false;
|
||||
@@ -80,12 +80,12 @@
|
||||
this._loadTimeFormat(),
|
||||
this._loadRelative(),
|
||||
]);
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
_loadTimeFormat: function() {
|
||||
return this._getPreferences().then(function(preferences) {
|
||||
var timeFormat = preferences && preferences.time_format;
|
||||
_loadTimeFormat() {
|
||||
return this._getPreferences().then(preferences => {
|
||||
const timeFormat = preferences && preferences.time_format;
|
||||
switch (timeFormat) {
|
||||
case 'HHMM_12':
|
||||
this._timeFormat = TimeFormats.TIME_12;
|
||||
@@ -96,55 +96,55 @@
|
||||
default:
|
||||
throw Error('Invalid time format: ' + timeFormat);
|
||||
}
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
_loadRelative: function() {
|
||||
return this._getPreferences().then(function(prefs) {
|
||||
_loadRelative() {
|
||||
return this._getPreferences().then(prefs => {
|
||||
// prefs.relative_date_in_change_table is not set when false.
|
||||
this._relative = !!(prefs && prefs.relative_date_in_change_table);
|
||||
}.bind(this));
|
||||
});
|
||||
},
|
||||
|
||||
_getLoggedIn: function() {
|
||||
_getLoggedIn() {
|
||||
return this.$.restAPI.getLoggedIn();
|
||||
},
|
||||
|
||||
_getPreferences: function() {
|
||||
_getPreferences() {
|
||||
return this.$.restAPI.getPreferences();
|
||||
},
|
||||
|
||||
/**
|
||||
* Return true if date is within 24 hours and on the same day.
|
||||
*/
|
||||
_isWithinDay: function(now, date) {
|
||||
var diff = -date.diff(now);
|
||||
_isWithinDay(now, date) {
|
||||
const diff = -date.diff(now);
|
||||
return diff < Duration.DAY && date.day() === now.getDay();
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns true if date is from one to six months.
|
||||
*/
|
||||
_isWithinHalfYear: function(now, date) {
|
||||
var diff = -date.diff(now);
|
||||
_isWithinHalfYear(now, date) {
|
||||
const diff = -date.diff(now);
|
||||
return (date.day() !== now.getDay() || diff >= Duration.DAY) &&
|
||||
diff < 180 * Duration.DAY;
|
||||
},
|
||||
|
||||
_computeDateStr: function(dateStr, timeFormat, relative) {
|
||||
_computeDateStr(dateStr, timeFormat, relative) {
|
||||
if (!dateStr) { return ''; }
|
||||
var date = moment(util.parseDate(dateStr));
|
||||
const date = moment(util.parseDate(dateStr));
|
||||
if (!date.isValid()) { return ''; }
|
||||
if (relative) {
|
||||
var dateFromNow = date.fromNow();
|
||||
const dateFromNow = date.fromNow();
|
||||
if (dateFromNow === 'a few seconds ago') {
|
||||
return 'just now';
|
||||
} else {
|
||||
return dateFromNow;
|
||||
}
|
||||
}
|
||||
var now = new Date();
|
||||
var format = TimeFormats.MONTH_DAY_YEAR;
|
||||
const now = new Date();
|
||||
let format = TimeFormats.MONTH_DAY_YEAR;
|
||||
if (this._isWithinDay(now, date)) {
|
||||
format = timeFormat;
|
||||
} else if (this._isWithinHalfYear(now, date)) {
|
||||
@@ -153,17 +153,17 @@
|
||||
return date.format(format);
|
||||
},
|
||||
|
||||
_timeToSecondsFormat: function(timeFormat) {
|
||||
_timeToSecondsFormat(timeFormat) {
|
||||
return timeFormat === TimeFormats.TIME_12 ?
|
||||
TimeFormats.TIME_12_WITH_SEC :
|
||||
TimeFormats.TIME_24_WITH_SEC;
|
||||
},
|
||||
|
||||
_computeFullDateStr: function(dateStr, timeFormat) {
|
||||
_computeFullDateStr(dateStr, timeFormat) {
|
||||
if (!dateStr) { return ''; }
|
||||
var date = moment(util.parseDate(dateStr));
|
||||
const date = moment(util.parseDate(dateStr));
|
||||
if (!date.isValid()) { return ''; }
|
||||
var format = TimeFormats.MONTH_DAY_YEAR + ', ';
|
||||
let format = TimeFormats.MONTH_DAY_YEAR + ', ';
|
||||
format += this._timeToSecondsFormat(timeFormat);
|
||||
return date.format(format) + this._getUtcOffsetString();
|
||||
},
|
||||
|
||||
@@ -33,15 +33,15 @@ limitations under the License.
|
||||
</test-fixture>
|
||||
|
||||
<script>
|
||||
suite('gr-date-formatter tests', function() {
|
||||
var element;
|
||||
var sandbox;
|
||||
suite('gr-date-formatter tests', () => {
|
||||
let element;
|
||||
let sandbox;
|
||||
|
||||
setup(function() {
|
||||
setup(() => {
|
||||
sandbox = sinon.sandbox.create();
|
||||
});
|
||||
|
||||
teardown(function() {
|
||||
teardown(() => {
|
||||
sandbox.restore();
|
||||
});
|
||||
|
||||
@@ -49,7 +49,7 @@ limitations under the License.
|
||||
* Parse server-formatter date and normalize into current timezone.
|
||||
*/
|
||||
function normalizedDate(dateStr) {
|
||||
var d = util.parseDate(dateStr);
|
||||
const d = util.parseDate(dateStr);
|
||||
d.setMinutes(d.getMinutes() + d.getTimezoneOffset());
|
||||
return d;
|
||||
}
|
||||
@@ -60,8 +60,8 @@ limitations under the License.
|
||||
.toJSON().replace('T', ' ').slice(0, -1);
|
||||
sandbox.useFakeTimers(normalizedDate(nowStr).getTime());
|
||||
element.dateStr = dateStr;
|
||||
flush(function() {
|
||||
var span = element.$$('span');
|
||||
flush(() => {
|
||||
const span = element.$$('span');
|
||||
assert.equal(span.textContent.trim(), expected);
|
||||
assert.equal(element.title, expectedTooltip);
|
||||
done();
|
||||
@@ -69,8 +69,8 @@ limitations under the License.
|
||||
}
|
||||
|
||||
function stubRestAPI(preferences) {
|
||||
var loggedInPromise = Promise.resolve(preferences !== null);
|
||||
var preferencesPromise = Promise.resolve(preferences);
|
||||
const loggedInPromise = Promise.resolve(preferences !== null);
|
||||
const preferencesPromise = Promise.resolve(preferences);
|
||||
stub('gr-rest-api-interface', {
|
||||
getLoggedIn: sinon.stub().returns(loggedInPromise),
|
||||
getPreferences: sinon.stub().returns(preferencesPromise),
|
||||
@@ -78,115 +78,115 @@ limitations under the License.
|
||||
return Promise.all([loggedInPromise, preferencesPromise]);
|
||||
}
|
||||
|
||||
suite('24 hours time format preference', function() {
|
||||
setup(function() {
|
||||
suite('24 hours time format preference', () => {
|
||||
setup(() => {
|
||||
return stubRestAPI(
|
||||
{time_format: 'HHMM_24', relative_date_in_change_table: false}
|
||||
).then(function() {
|
||||
).then(() => {
|
||||
element = fixture('basic');
|
||||
sandbox.stub(element, '_getUtcOffsetString').returns('');
|
||||
return element._loadPreferences();
|
||||
});
|
||||
});
|
||||
|
||||
test('invalid dates are quietly rejected', function() {
|
||||
test('invalid dates are quietly rejected', () => {
|
||||
assert.notOk((new Date('foo')).valueOf());
|
||||
assert.equal(element._computeDateStr('foo', 'h:mm A'), '');
|
||||
});
|
||||
|
||||
test('Within 24 hours on same day', function(done) {
|
||||
test('Within 24 hours on same day', done => {
|
||||
testDates('2015-07-29 20:34:14.985000000',
|
||||
'2015-07-29 15:34:14.985000000',
|
||||
'15:34', 'Jul 29, 2015, 15:34:14', done);
|
||||
});
|
||||
|
||||
test('Within 24 hours on different days', function(done) {
|
||||
test('Within 24 hours on different days', done => {
|
||||
testDates('2015-07-29 03:34:14.985000000',
|
||||
'2015-07-28 20:25:14.985000000',
|
||||
'Jul 28', 'Jul 28, 2015, 20:25:14', done);
|
||||
});
|
||||
|
||||
test('More than 24 hours but less than six months', function(done) {
|
||||
test('More than 24 hours but less than six months', done => {
|
||||
testDates('2015-07-29 20:34:14.985000000',
|
||||
'2015-06-15 03:25:14.985000000',
|
||||
'Jun 15', 'Jun 15, 2015, 03:25:14', done);
|
||||
});
|
||||
|
||||
test('More than six months', function(done) {
|
||||
test('More than six months', done => {
|
||||
testDates('2015-09-15 20:34:00.000000000',
|
||||
'2015-01-15 03:25:00.000000000',
|
||||
'Jan 15, 2015', 'Jan 15, 2015, 03:25:00', done);
|
||||
});
|
||||
});
|
||||
|
||||
suite('12 hours time format preference', function() {
|
||||
setup(function() {
|
||||
suite('12 hours time format preference', () => {
|
||||
setup(() => {
|
||||
// relative_date_in_change_table is not set when false.
|
||||
return stubRestAPI(
|
||||
{time_format: 'HHMM_12'}
|
||||
).then(function() {
|
||||
).then(() => {
|
||||
element = fixture('basic');
|
||||
sandbox.stub(element, '_getUtcOffsetString').returns('');
|
||||
return element._loadPreferences();
|
||||
});
|
||||
});
|
||||
|
||||
test('Within 24 hours on same day', function(done) {
|
||||
test('Within 24 hours on same day', done => {
|
||||
testDates('2015-07-29 20:34:14.985000000',
|
||||
'2015-07-29 15:34:14.985000000',
|
||||
'3:34 PM', 'Jul 29, 2015, 3:34:14 PM', done);
|
||||
});
|
||||
});
|
||||
|
||||
suite('relative date preference', function() {
|
||||
setup(function() {
|
||||
suite('relative date preference', () => {
|
||||
setup(() => {
|
||||
return stubRestAPI(
|
||||
{time_format: 'HHMM_12', relative_date_in_change_table: true}
|
||||
).then(function() {
|
||||
).then(() => {
|
||||
element = fixture('basic');
|
||||
sandbox.stub(element, '_getUtcOffsetString').returns('');
|
||||
return element._loadPreferences();
|
||||
});
|
||||
});
|
||||
|
||||
test('Within 24 hours on same day', function(done) {
|
||||
test('Within 24 hours on same day', done => {
|
||||
testDates('2015-07-29 20:34:14.985000000',
|
||||
'2015-07-29 15:34:14.985000000',
|
||||
'5 hours ago', 'Jul 29, 2015, 3:34:14 PM', done);
|
||||
});
|
||||
|
||||
test('More than six months', function(done) {
|
||||
test('More than six months', done => {
|
||||
testDates('2015-09-15 20:34:00.000000000',
|
||||
'2015-01-15 03:25:00.000000000',
|
||||
'8 months ago', 'Jan 15, 2015, 3:25:00 AM', done);
|
||||
});
|
||||
});
|
||||
|
||||
suite('logged in', function() {
|
||||
setup(function() {
|
||||
suite('logged in', () => {
|
||||
setup(() => {
|
||||
return stubRestAPI(
|
||||
{time_format: 'HHMM_12', relative_date_in_change_table: true}
|
||||
).then(function() {
|
||||
).then(() => {
|
||||
element = fixture('basic');
|
||||
return element._loadPreferences();
|
||||
});
|
||||
});
|
||||
|
||||
test('Preferences are respected', function() {
|
||||
test('Preferences are respected', () => {
|
||||
assert.equal(element._timeFormat, 'h:mm A');
|
||||
assert.isTrue(element._relative);
|
||||
});
|
||||
});
|
||||
|
||||
suite('logged out', function() {
|
||||
setup(function() {
|
||||
return stubRestAPI(null).then(function() {
|
||||
suite('logged out', () => {
|
||||
setup(() => {
|
||||
return stubRestAPI(null).then(() => {
|
||||
element = fixture('basic');
|
||||
return element._loadPreferences();
|
||||
});
|
||||
});
|
||||
|
||||
test('Default preferences are respected', function() {
|
||||
test('Default preferences are respected', () => {
|
||||
assert.equal(element._timeFormat, 'HH:mm');
|
||||
assert.isFalse(element._relative);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user