From ce59f924897fc8c57ad0b7c2d7c9fa2582549cf2 Mon Sep 17 00:00:00 2001 From: Prateek Saxena Date: Fri, 7 Feb 2014 13:05:34 +0530 Subject: [PATCH] Add time ago in words at the bottom of the popup Change-Id: I53f77a0c802ae3b9582c51a07fc56ad56e3fd53b --- Popups.i18n.php | 12 ++++++++- Popups.php | 11 +++++++- resources/ext.popups.core.js | 50 +++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Popups.i18n.php b/Popups.i18n.php index f676a119a..3283f4f8f 100644 --- a/Popups.i18n.php +++ b/Popups.i18n.php @@ -26,7 +26,12 @@ $messages = array(); */ $messages['en'] = array( 'popups-message' => 'Popups', - 'popups-desc' => 'Displays popups with summaries of page contents when the user hovers over an page link', + 'popups-desc' => 'Displays popups with summaries of article contents when the user hovers over an article link.', + 'popups-edited-seconds' => 'Last edited {{PLURAL:$1|1 second|$1 seconds}} ago.', + 'popups-edited-minutes' => 'Last edited {{PLURAL:$1|1 minute|$1 minutes}} ago.', + 'popups-edited-hours' => 'Last edited {{PLURAL:$1|1 hour|$1 hours}} ago.', + 'popups-edited-days' => 'Last edited {{PLURAL:$1|yesterday|$1 days ago}}.', + 'popups-edited-years' => 'Last edited {{PLURAL:$1|1 year|$1 years}} ago.', ); /** Message documentation (Message documentation) @@ -34,6 +39,11 @@ $messages['en'] = array( $messages['qqq'] = array( 'popups-message' => 'Name shown in user preference for this extension', 'popups-desc' => '{{desc|name=Popups|url=https://www.mediawiki.org/wiki/Extension:Popups}}', + 'popups-edited-seconds' => 'Message to show time span if page was edited less than a minute ago', + 'popups-edited-minutes' => 'Message to show time span if page was edited less than an hour ago', + 'popups-edited-hours' => 'Message to show time span if page was edited less than a day ago', + 'popups-edited-days' => 'Message to show time span if page was edited less than a year ago', + 'popups-edited-years' => 'Message to show time span if page was edited more than a year ago', ); /** Arabic (العربية) diff --git a/Popups.php b/Popups.php index 3faafaa58..890f44628 100644 --- a/Popups.php +++ b/Popups.php @@ -35,7 +35,9 @@ $remoteExtPath = 'Popups'; $wgResourceModules = array_merge( $wgResourceModules, array( "ext.popups" => array( - 'scripts' => 'resources/ext.popups.core.js', + 'scripts' => array( + 'resources/ext.popups.core.js', + ), 'styles' => array( 'resources/ext.popups.core.less', 'resources/ext.popups.animation.less', @@ -43,6 +45,13 @@ $wgResourceModules = array_merge( $wgResourceModules, array( 'dependencies' => array( 'mediawiki.api', ), + 'messages' => array( + 'popups-edited-seconds', + 'popups-edited-minutes', + 'popups-edited-hours', + 'popups-edited-days', + 'popups-edited-years', + ), 'remoteExtPath' => $remoteExtPath, 'localBasePath' => $localBasePath, ), diff --git a/resources/ext.popups.core.js b/resources/ext.popups.core.js index 5f4d098b9..5e02a4e35 100644 --- a/resources/ext.popups.core.js +++ b/resources/ext.popups.core.js @@ -57,7 +57,7 @@ $timestamp = $( '
' ) .addClass( timestampclass ) .append( - $( '' ).text( timestamp.toString() ) + $( '' ).text( timeAgo( timediff ).text() ) ); if ( thumbnail ){ @@ -237,5 +237,53 @@ } } ) .appendTo( document.body ); + + } ); + + // Util functions that should be separated out into their own files at some point + + /** + * @method timeAgo + * Formats a given time duration (in ms) into a relative string. + * + * @param {number} ms The time duration to convert to a relative string, in ms + * @return {Object} A mw.message object with the appropriate relative string. + */ + function timeAgo( ms ) { + var i, ts, timeSegments = [ + { + factor: 1000, + min: 60, + message: 'popups-edited-seconds' + }, + { + factor: 60, + min: 60, + message: 'popups-edited-minutes' + }, + { + factor: 60, + min: 24, + message: 'popups-edited-hours' + }, + { + factor: 24, + min: 365, + message: 'popups-edited-days' + }, + { + factor: 365, + message: 'popups-edited-years' + } + ], curDuration = ms; + + for ( i = 0; i <= timeSegments.length; i++ ) { + ts = timeSegments[ i ]; + curDuration = Math.floor( curDuration / ts.factor ); + if ( typeof ts.min === 'undefined' || curDuration < ts.min ) { + return mw.message( ts.message, curDuration ); + } + } + } } ) ( jQuery );