From 682873dd2d0a0d723618e25b055eec0509be801c Mon Sep 17 00:00:00 2001 From: Simon Legner Date: Wed, 29 May 2019 22:56:30 +0200 Subject: [PATCH] Replace moment.js with Date.toLocaleString Bug: T224626 Change-Id: Ib312821d2c72c2570cec29153557fd44e4db2bf7 --- extension.json | 3 +- resources/mmv/mmv.js | 2 +- resources/mmv/ui/mmv.ui.metadataPanel.js | 49 +++++++------------ .../qunit/mmv/ui/mmv.ui.metadataPanel.test.js | 15 +----- 4 files changed, 21 insertions(+), 48 deletions(-) diff --git a/extension.json b/extension.json index 4a7f5404a..ee93e52bc 100644 --- a/extension.json +++ b/extension.json @@ -439,8 +439,7 @@ "mmv.ui.ondemandshareddependencies", "mmv.ui.reuse.shareembed", "mmv.ui.download.pane", - "mmv.ui.tipsyDialog", - "moment" + "mmv.ui.tipsyDialog" ] }, "EventLoggingSchemas": { diff --git a/resources/mmv/mmv.js b/resources/mmv/mmv.js index 619291cfc..e51db715a 100644 --- a/resources/mmv/mmv.js +++ b/resources/mmv/mmv.js @@ -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' ] ); }; /** diff --git a/resources/mmv/ui/mmv.ui.metadataPanel.js b/resources/mmv/ui/mmv.ui.metadataPanel.js index 5ded26a9e..de1a8c9e7 100644 --- a/resources/mmv/ui/mmv.ui.metadataPanel.js +++ b/resources/mmv/ui/mmv.ui.metadataPanel.js @@ -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; }; /** diff --git a/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js b/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js index 0a6b88ab9..d804352c3 100644 --- a/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js +++ b/tests/qunit/mmv/ui/mmv.ui.metadataPanel.test.js @@ -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, $( '
' ).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 ) {