Replace moment.js with Date.toLocaleString

Bug: T224626
Change-Id: Ib312821d2c72c2570cec29153557fd44e4db2bf7
This commit is contained in:
Simon Legner 2019-05-29 22:56:30 +02:00
parent a4e63f623c
commit 682873dd2d
4 changed files with 21 additions and 48 deletions

View file

@ -439,8 +439,7 @@
"mmv.ui.ondemandshareddependencies",
"mmv.ui.reuse.shareembed",
"mmv.ui.download.pane",
"mmv.ui.tipsyDialog",
"moment"
"mmv.ui.tipsyDialog"
]
},
"EventLoggingSchemas": {

View file

@ -1026,7 +1026,7 @@
* Preloads JS and CSS dependencies that aren't needed to display the first image, but could be needed later
*/
MMVP.preloadDependencies = function () {
mw.loader.load( [ 'mmv.ui.reuse.shareembed', 'moment' ] );
mw.loader.load( [ 'mmv.ui.reuse.shareembed' ] );
};
/**

View file

@ -757,19 +757,9 @@
mw.mmv.attributionLogger.logAttribution( imageData );
if ( imageData.creationDateTime ) {
// Use the raw date until moment can try to interpret it
panel.setDateTime( imageData.creationDateTime );
this.formatDate( imageData.creationDateTime ).then( function ( formattedDate ) {
panel.setDateTime( formattedDate, true );
} );
panel.setDateTime( this.formatDate( imageData.creationDateTime ), true );
} else if ( imageData.uploadDateTime ) {
// Use the raw date until moment can try to interpret it
panel.setDateTime( imageData.uploadDateTime );
this.formatDate( imageData.uploadDateTime ).then( function ( formattedDate ) {
panel.setDateTime( formattedDate );
} );
panel.setDateTime( this.formatDate( imageData.uploadDateTime ) );
}
this.buttons.set( imageData, repoData );
@ -814,29 +804,24 @@
* Unrecognized strings are returned unchanged.
*
* @param {string} dateString
* @return {jQuery.Deferred}
* @return {string} formatted date
*/
MPP.formatDate = function ( dateString ) {
var deferred = $.Deferred(),
date;
mw.loader.using( 'moment', function () {
/* global moment */
date = moment( dateString );
if ( date.isValid() ) {
deferred.resolve( date.format( 'LL' ) );
} else {
deferred.resolve( dateString );
var date,
lang = mw.config.get( 'wgUserLanguage' );
if ( lang === 'en' ) { lang = 'en-GB'; } // for D MMMM YYYY format
date = new Date( dateString );
try {
if ( date instanceof Date && !isNaN( date ) ) {
return date.toLocaleString( lang, {
day: 'numeric',
month: 'long',
year: 'numeric'
} );
}
}, function ( error ) {
deferred.reject( error );
if ( window.console && window.console.error ) {
window.console.error( 'mw.loader.using error when trying to load moment', error );
}
} );
return deferred.promise();
} catch ( ignore ) {}
// fallback to original date string
return dateString;
};
/**

View file

@ -113,16 +113,8 @@
getArticlePath: function () { return 'Foo'; },
isCommons: function () { return false; }
},
oldMoment = window.moment,
// custom clock will give MPP.formatDate some time to load moment.js
clock = this.sandbox.useFakeTimers();
/* window.moment = function ( date ) {
// This has no effect for now, since writing this test revealed that our moment.js
// doesn't have any language configuration
return oldMoment( date ).lang( 'fr' );
};*/
panel.setImageInfo( image, imageData, repoData );
assert.strictEqual( panel.$title.text(), title, 'Title is correctly set' );
@ -164,7 +156,6 @@
assert.ok( panel.$datetime.text().indexOf( '25 August 2013' ) > 0, 'Correct date is displayed' );
window.moment = oldMoment;
clock.restore();
} );
@ -181,11 +172,9 @@
var $qf = $( '#qunit-fixture' ),
panel = new mw.mmv.ui.MetadataPanel( $qf, $( '<div>' ).appendTo( $qf ), mw.storage, new mw.mmv.Config( {}, mw.config, mw.user, new mw.Api(), mw.storage ) ),
date1 = 'Garbage',
promise = panel.formatDate( date1 );
result = panel.formatDate( date1 );
return promise.then( function ( result ) {
assert.strictEqual( result, date1, 'Invalid date is correctly ignored' );
} );
assert.strictEqual( result, date1, 'Invalid date is correctly ignored' );
} );
QUnit.test( 'About links', function ( assert ) {