Difference between revisions of "MediaWiki:Gadget-UTCLiveClock.js"

From translatewiki.net
Jump to navigation Jump to search
(jQuery- and RL-compliant version :))
(No need to use window. when checking or using variables, only when assigning it; caching $target; making the functions local (no need to make them global) and in the correct order)
Line 1: Line 1:
  +
/* Warning! Global gadget file! */
(function( $ ) {
 
$( document ).ready( function() {
+
( function( $, undefined ) {
appendCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );
 
   
  +
function showTime( $target ) {
if ( typeof( window.UTCLiveClockConfig ) == 'undefined' ) {
 
  +
var dateNode = UTCLiveClockConfig.node;
window.UTCLiveClockConfig = {};
 
}
 
window.liveClock = {};
 
var portletId = window.UTCLiveClockConfig.portletId || 'p-personal';
 
var nextNode = window.UTCLiveClockConfig.nextNodeId ? document.getElementById( UTCLiveClockConfig.nextNodeId ) : undefined;
 
window.liveClock.node = addPortletLink(
 
portletId,
 
wgScript + '?title=' + encodeURIComponent( wgPageName ) + '&action=purge',
 
'',
 
'utcdate',
 
undefined,
 
undefined,
 
nextNode
 
);
 
if( !window.liveClock.node ) {
 
return;
 
}
 
 
window.showTime();
 
});
 
})( jQuery );
 
 
window.showTime = function() {
 
var dateNode = window.liveClock.node;
 
 
if( !dateNode ) {
 
if( !dateNode ) {
 
return;
 
return;
Line 36: Line 12:
 
var mm = now.getUTCMinutes();
 
var mm = now.getUTCMinutes();
 
var ss = now.getUTCSeconds();
 
var ss = now.getUTCSeconds();
  +
if ( typeof $target === 'undefined' ) {
  +
console.log( '$target is now being defined.' );
  +
$target = $( dateNode ).find( 'a:first' );
  +
}
 
var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss );
 
var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss );
  +
$target.text( time );
dateNode.firstChild.replaceChild( document.createTextNode( time ), dateNode.firstChild.firstChild );
 
   
window.setTimeout( window.showTime, 1000 );
+
setTimeout( function(){
  +
showTime( $target );
  +
}, 1000 );
 
}
 
}
  +
  +
function liveClock() {
  +
appendCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );
  +
  +
if ( typeof( UTCLiveClockConfig ) === 'undefined' ) {
  +
window.UTCLiveClockConfig = {};
  +
}
  +
var portletId = UTCLiveClockConfig.portletId || 'p-personal';
  +
var nextNode = UTCLiveClockConfig.nextNodeId ? document.getElementById( UTCLiveClockConfig.nextNodeId ) : undefined;
  +
UTCLiveClockConfig.node = addPortletLink(
  +
portletId,
  +
wgScript + '?title=' + encodeURIComponent( wgPageName ) + '&action=purge',
  +
'',
  +
'utcdate',
  +
undefined,
  +
undefined,
  +
nextNode
  +
);
  +
if( !UTCLiveClockConfig.node ) {
  +
return;
  +
}
  +
  +
showTime();
  +
}
  +
$( document ).ready( liveClock );
  +
  +
} )( jQuery );

Revision as of 15:25, 1 February 2011

/* Warning! Global gadget file! */
( function( $, undefined ) {

function showTime( $target ) {
	var dateNode = UTCLiveClockConfig.node;
	if( !dateNode ) {
		return;
	}

	var now = new Date();
	var hh = now.getUTCHours();
	var mm = now.getUTCMinutes();
	var ss = now.getUTCSeconds();
	if ( typeof $target === 'undefined' ) {
		console.log( '$target is now being defined.' );
		$target = $( dateNode ).find( 'a:first' );
	}
	var time = ( hh < 10 ? '0' + hh : hh ) + ':' + ( mm < 10 ? '0' + mm : mm ) + ':' + ( ss < 10 ? '0' + ss : ss );
	$target.text( time );

	setTimeout( function(){
		showTime( $target );	
	}, 1000 );
}

function liveClock() {
	appendCSS( '#utcdate a { font-weight:bolder; font-size:120%; }' );

	if ( typeof( UTCLiveClockConfig ) === 'undefined' ) {
		window.UTCLiveClockConfig = {};
	}
	var portletId = UTCLiveClockConfig.portletId || 'p-personal';
	var nextNode = UTCLiveClockConfig.nextNodeId ? document.getElementById( UTCLiveClockConfig.nextNodeId ) : undefined;
	UTCLiveClockConfig.node = addPortletLink(
		portletId,
		wgScript + '?title=' + encodeURIComponent( wgPageName ) + '&action=purge',
		'',
		'utcdate',
		undefined,
		undefined,
		nextNode
	);
	if( !UTCLiveClockConfig.node ) {
		return;
	}

	showTime();
}
$( document ).ready( liveClock );

} )( jQuery );