MediaWiki:Live EriCSSon.js

mw.loader.load( ['jquery.ui'] );

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 = "

" ;

h += "

" ;

if ( self.isSet ( self.use_css ) ) {

h += "Current CSS :
" + self.use_css ;

h += "
Make default" ;

h += "" ;

} else {

h += "No CSS loaded" ;

}

h += "

" ;

h += "

" ;

h += "Load CSS page:
" ;

h += "" ;

h += "" ;

h += "

" ;

h += "

" ;

h += "

" ;

$('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 () } ) ;