Jump to content

MediaWiki:Live EriCSSon.js

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by Krenair (talk | contribs) at 13:41, 15 July 2014 (Maintenance: Replacing use of mw.util.wikiGetlink with mw.util.getUrl per request on talk page). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
mw.loader.load( ['jquery.ui.dialog'] );

if ( typeof ( $ ) === 'undefined' ) $ = $j ;

var live_ericsson = {

	use_css_cookie : 'Live_EriCSSon_useCSS' ,

	init : function () {
		
//		if ( mw.config.get('wgNamespaceNumber') != 0 ) return ;
//		if ( mw.config.get('wgAction') != 'view' ) return ;
		var self = this ;
		var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'Live EriCSSon','t-live-ericsson');
		$(portletLink).click ( function () {
			self.options() ;
			return false ;
		} ) ;
		self.run() ;
	} ,
	
	run : function () {
		var self = this ;
		var use_css = self.getURLParameter ( 'useCSS' ) ;
		if ( !self.isSet ( use_css ) ) {
			use_css = $.cookie( self.use_css_cookie ) ;
			if ( !self.isSet ( use_css ) ) return ;
		}
		self.use_css = use_css ;
		var api = new mw.Api();
		api.get( {
			action: 'query',
			prop: 'revisions' ,
			titles: use_css ,
			rvprop: 'content'
		}, {
			ok: function () {
				var a = arguments[0] ;
				if ( undefined === a || undefined === a.query ) return ;
				var pages = a.query.pages ;
				if ( undefined === pages ) return ;
				$.each ( pages , function ( page , data ) {
					var css = data.revisions[0]['*'] ;
					mw.util.addCSS ( css ) ;
				} ) ;
			}
		} );		
		
		if ( use_css != ($.cookie( self.use_css_cookie)||'') ) {
			$('a').each ( function ( k , v ) {
				var href = $(v).attr('href') || '' ;
				if ( href.match ( /^\/wiki\// ) ) $(v).attr('href',href+'?useCSS='+use_css) ;
				else if ( href.match ( /^\/w\// ) ) $(v).attr('href',href+'&useCSS='+use_css) ;
			} ) ;
		} ;
	} ,

	options : function () {
		var self = this ;
		$('#live_ericsson_dialog').remove() ;
		var h = "<div id='live_ericsson_dialog' title='Live EriCSSon - Settings'>" ;

		h += "<div style='border-bottom:1px solid #DDDDDD;padding-bottom:5px;margin-bottom:5px'>" ;
		if ( self.isSet ( self.use_css ) ) {
			h += "Current CSS :<br/>" + self.use_css ;
			h += "<br/><a href='#' onclick='live_ericsson.makeCurrentDefault();return false'>Make default</a>" ;
			h += "<span id='live_ericsson_clear_link' style='display:none'> | <a href='#' onclick='live_ericsson.clearDefault();return false'>Clear current default</a></span>" ;
		} else {
			h += "<i>No CSS loaded</i>" ;
		}
		h += "</div>" ;
		
		h += "<div style='border-bottom:1px solid #DDDDDD;padding-bottom:5px;margin-bottom:5px'>" ;
		h += "Load CSS page:<br/>" ;
		h += "<input type='text' size='30' id='live_ericsson_text_input' value='User:Magnus_Manske/explosion.css' />" ;
		h += "<button onclick='live_ericsson.reloadPageWithNewCSS()'>Reload with this CSS</button>" ;
		h += "</div>" ;
		
		h += "<div id='live_ericsson_message'></div>" ;
		h += "</div>" ;
		$('body').append ( h ) ;
		$('#live_ericsson_dialog').dialog ( {
			open: function(event, ui) { $('#live_ericsson_dialog a').blur().css({'font-weight':'bold'}) ; }
		} ) ;

		var def = $.cookie( self.use_css_cookie ) ;
		if ( self.isSet ( def ) ) $('#live_ericsson_clear_link').show() ;

	} ,
	
	reloadPageWithNewCSS : function () {
		var ncss = $('#live_ericsson_text_input').val() ;
		var url = mw.util.getUrl ( mw.config.get( 'wgPageName' ) ) ;
		url += "?useCSS=" + ncss ;
		window.location.href = url ;
	} ,
	
	isSet : function ( x ) {
		if ( undefined === x ) return false ;
		if ( null === x ) return false ;
		if ( 'null' === x ) return false ;
		return true ;
	} ,
	
	clearDefault : function () {
		var self = this ;
		$.cookie( self.use_css_cookie , null , {
		 expires: 90, // days
		 path: '/' // domain-wide, entire wiki
		} );
		$('#live_ericsson_message').html ( "Default CSS has been removed for this wiki, for this computer." ) ;
		$('#live_ericsson_clear_link').hide() ;
//		console.log ( $.cookie( self.use_css_cookie ) ) ;
	} ,
	
	makeCurrentDefault : function () {
		var self = this ;
		$.cookie( self.use_css_cookie , self.use_css , {
		 expires: 90, // days
		 path: '/' // domain-wide, entire wiki
		} );
		$('#live_ericsson_message').html ( self.use_css + " is now default CSS for this wiki, on this computer." ) ;
		$('#live_ericsson_clear_link').show() ;
//		console.log ( $.cookie( self.use_css_cookie ) ) ;
	} ,
	
	getURLParameter : function(name) {
		return decodeURIComponent(
			(RegExp('\\b' + name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
		);
	} ,

	the_end : ''
} ;

$( function () { live_ericsson.init () } ) ;