MediaWiki:Gadget-UTCLiveClock.js: Różnice pomiędzy wersjami

Z Almanach
mNie podano opisu zmian
mNie podano opisu zmian
 
Linia 1: Linia 1:
// Warning! Global gadget file!
/**
( function ( mw, $ ) {
* Warning! Global gadget file!
*
* This gadget adds a clock in the personal toolbar that shows the current time
* in UTC (or a different timezone of your choosing), and also provides a link
* to purge the current page.
*
* Revision: July 2020
* Source: https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js
*
* Installation:
*
* 1. Copy the JS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js
* to the page [[MediaWiki:Gadget-UTCLiveClock.js]] on your wiki.
*
* 2. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.css
* to the page [[MediaWiki:Gadget-UTCLiveClock.css]] on your wiki.
*
* 3. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock-pagestyles.css
* to the page [[MediaWiki:Gadget-UTCLiveClock-pagestyles.css]] on your wiki.
*
* 4. Add a description of the gadget to the page [[MediaWiki:Gadget-UTCLiveClock]]
* on your wiki. You can use https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock
* as a template.
*
* 5. Add the following code to your wiki's [[MediaWiki:Gadgets-definition]]:
*
*    * UTCLiveClock[ResourceLoader|type=general|dependencies=mediawiki.util,mediawiki.api|peers=UTCLiveClock-pagestyles]|UTCLiveClock.js|UTCLiveClock.css
*    * UTCLiveClock-pagestyles[hidden|skins=vector,monobook]|UTCLiveClock-pagestyles.css
*
*
* To set the timezone used to one other than UTC, set window.LiveClockTimeZone to
* the desired timezone. For example, adding the following to your common.js
*      window.LiveClockTimeZone = 'America/Los_Angeles';
* would result in the local time in Los Angeles being shown. See
* [[:w:List of tz database time zones]] for valid options (use the TZ database name).
*/
/*global mw, $ */
mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {


var $target;
function padWithZeroes( num ) {
// Pad a number with zeroes. The number must be an integer where
// 0 <= num < 100.
return num < 10 ? '0' + num.toString() : num.toString();  
}


function showTime( $target ) {
function showTime( $target ) {
var dateNode = UTCLiveClockConfig.node;
var now = new Date();
if ( !dateNode ) {
return;
var timezone = window.LiveClockTimeZone || 'UTC';
}


var now = new Date();
// Set the time.
var hh = now.getUTCHours();
var hh, mm, ss;
var mm = now.getUTCMinutes();
if ( timezone === "UTC" ) {
var ss = now.getUTCSeconds();
hh = now.getUTCHours();
if ( $target === undefined ) {
mm = now.getUTCMinutes();
$target = $( dateNode ).find( 'a:first' );
ss = now.getUTCSeconds();
} else if ( timezone === "local" ) {
hh = now.getHours();
mm = now.getMinutes();
ss = now.getSeconds();
} else {
var newNow;
try {
newNow = new Date(
now.toLocaleString(
"en-US",
{ timeZone: timezone }
)
);
hh = newNow.getHours();
mm = newNow.getMinutes();
ss = newNow.getSeconds();
} catch ( err ) {
console.log( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name);
timezone = "UTC";
newNow = now;
hh = now.getUTCHours();
mm = now.getUTCMinutes();
ss = now.getUTCSeconds();
}
}
}
var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss );
var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss );
$target.text( time );
$target.text( time );


        var ms = now.getUTCMilliseconds();
// Schedule the next time change.
 
//
// We schedule the change for 100 ms _after_ the next clock tick. The delay
// from setTimeout is not precise, and if we aim exactly for the tick, there
// is a chance that the function will run slightly before it. If this
// happens, we will display the same time for two seconds in a row - not
// good. By scheduling 100 ms after the tick, we will always be about 100 ms
// late, but we are also very likely to display a new time every second.
var ms = now.getUTCMilliseconds();
setTimeout( function () {
setTimeout( function () {
showTime( $target );
showTime( $target );
Linia 28: Linia 99:


function liveClock() {
function liveClock() {
// Set CSS styles. We do this here instead of on the CSS page because some
// wikis load this page directly, without loading the accompanying CSS.
mw.util.addCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );
mw.util.addCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );


if ( window.UTCLiveClockConfig === undefined ) {
// Reset whitespace that was set in the peer CSS gadget; this prevents the
window.UTCLiveClockConfig = {};
// effect of the p-personal menu jumping to the left when the JavaScript
}
// loads.
var portletId = UTCLiveClockConfig.portletId || 'p-personal';
$( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' );
var nextNode = UTCLiveClockConfig.nextNodeId ? document.getElementById( UTCLiveClockConfig.nextNodeId ) : undefined;
$( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );
UTCLiveClockConfig.node = mw.util.addPortletLink(
 
portletId,
// Add the portlet link.
mw.config.get( 'wgScript' ) + '?title=' + encodeURIComponent( mw.config.get( 'wgPageName' ) ) + '&action=purge',
var node = mw.util.addPortletLink(
'p-personal',
mw.util.getUrl( null, { action: 'purge' } ),
'',
'',
'utcdate',
'utcdate'
null,
null,
nextNode
);
);
if ( !UTCLiveClockConfig.node ) {
if ( !node ) {
return;
return;
}
}


showTime();
// Purge the page when the clock is clicked. We have to do this through the
// API, as purge URLs now make people click through a confirmation screen.
$( node ).on( 'click', function ( e ) {
new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
location.reload();
}, function () {
mw.notify( 'Purge failed', { type: 'error' } );
} );
e.preventDefault();
} );
 
// Show the clock.
showTime( $( node ).find( 'a:first' ) );
}
}
$( liveClock );
$( liveClock );
 
} );
} )( mediaWiki, jQuery );

Aktualna wersja na dzień 14:12, 27 sie 2020

/**
 * Warning! Global gadget file!
 * 
 * This gadget adds a clock in the personal toolbar that shows the current time
 * in UTC (or a different timezone of your choosing), and also provides a link
 * to purge the current page.
 *
 * Revision: July 2020
 * Source: https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js
 *
 * Installation:
 * 
 * 1. Copy the JS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.js
 * to the page [[MediaWiki:Gadget-UTCLiveClock.js]] on your wiki.
 * 
 * 2. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock.css
 * to the page [[MediaWiki:Gadget-UTCLiveClock.css]] on your wiki.
 * 
 * 3. Copy the CSS page at https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock-pagestyles.css
 * to the page [[MediaWiki:Gadget-UTCLiveClock-pagestyles.css]] on your wiki.
 * 
 * 4. Add a description of the gadget to the page [[MediaWiki:Gadget-UTCLiveClock]]
 * on your wiki. You can use https://www.mediawiki.org/wiki/MediaWiki:Gadget-UTCLiveClock
 * as a template.
 * 
 * 5. Add the following code to your wiki's [[MediaWiki:Gadgets-definition]]:
 * 
 *     * UTCLiveClock[ResourceLoader|type=general|dependencies=mediawiki.util,mediawiki.api|peers=UTCLiveClock-pagestyles]|UTCLiveClock.js|UTCLiveClock.css
 *     * UTCLiveClock-pagestyles[hidden|skins=vector,monobook]|UTCLiveClock-pagestyles.css
 * 
 * 
 * To set the timezone used to one other than UTC, set window.LiveClockTimeZone to
 * the desired timezone. For example, adding the following to your common.js
 *      window.LiveClockTimeZone = 'America/Los_Angeles';
 * would result in the local time in Los Angeles being shown. See
 * [[:w:List of tz database time zones]] for valid options (use the TZ database name).
 */
/*global mw, $ */
mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {

function padWithZeroes( num ) {
	// Pad a number with zeroes. The number must be an integer where
	// 0 <= num < 100.
	return num < 10 ? '0' + num.toString() : num.toString(); 
}

function showTime( $target ) {
	var now = new Date();
	
	var timezone = window.LiveClockTimeZone || 'UTC';

	// Set the time.
	var hh, mm, ss;
	if ( timezone === "UTC" ) {
		hh = now.getUTCHours();
		mm = now.getUTCMinutes();
		ss = now.getUTCSeconds();
	} else if ( timezone === "local" ) {
		hh = now.getHours();
		mm = now.getMinutes();
		ss = now.getSeconds();
	} else {
		var newNow;
		try {
			newNow = new Date(
				now.toLocaleString(
					"en-US",
					{ timeZone: timezone }
				)
			);
			hh = newNow.getHours();
			mm = newNow.getMinutes();
			ss = newNow.getSeconds();
		} catch ( err ) {
			console.log( "LiveClock - error creating Date object with timezone '" + timezone + "': " + err.name);
			timezone = "UTC";
			newNow = now;
			hh = now.getUTCHours();
			mm = now.getUTCMinutes();
			ss = now.getUTCSeconds();
		}
	}
	var time = padWithZeroes( hh ) + ':' + padWithZeroes( mm ) + ':' + padWithZeroes( ss );
	$target.text( time );

	// Schedule the next time change.
	// 
	// We schedule the change for 100 ms _after_ the next clock tick. The delay
	// from setTimeout is not precise, and if we aim exactly for the tick, there
	// is a chance that the function will run slightly before it. If this
	// happens, we will display the same time for two seconds in a row - not
	// good. By scheduling 100 ms after the tick, we will always be about 100 ms
	// late, but we are also very likely to display a new time every second.
	var ms = now.getUTCMilliseconds();
	setTimeout( function () {
		showTime( $target );
	}, 1100 - ms );
}

function liveClock() {
	// Set CSS styles. We do this here instead of on the CSS page because some
	// wikis load this page directly, without loading the accompanying CSS.
	mw.util.addCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );

	// Reset whitespace that was set in the peer CSS gadget; this prevents the
	// effect of the p-personal menu jumping to the left when the JavaScript
	// loads.
	$( '.client-js > body.skin-vector #p-personal ul' ).css( 'margin-right', 'initial' );
	$( '.client-js > body.skin-monobook #p-personal ul' ).css( 'margin-right', 'initial' );

	// Add the portlet link.
	var node = mw.util.addPortletLink(
		'p-personal',
		mw.util.getUrl( null, { action: 'purge' } ),
		'',
		'utcdate'
	);
	if ( !node ) {
		return;
	}

	// Purge the page when the clock is clicked. We have to do this through the
	// API, as purge URLs now make people click through a confirmation screen.
	$( node ).on( 'click', function ( e ) {
		new mw.Api().post( { action: 'purge', titles: mw.config.get( 'wgPageName' ) } ).then( function () {
			location.reload();
		}, function () {
			mw.notify( 'Purge failed', { type: 'error' } );
		} );
		e.preventDefault();
	} );

	// Show the clock.
	showTime( $( node ).find( 'a:first' ) );
}

$( liveClock );
} );