Difference between revisions of "MediaWiki:Common.js"

From University Innovation Fellows
Jump to navigation Jump to search
Line 123: Line 123:
 
mw.loader.load('/index.php?title=MediaWiki:Gadget-2020Cohort.js&action=raw&ctype=text/javascript');
 
mw.loader.load('/index.php?title=MediaWiki:Gadget-2020Cohort.js&action=raw&ctype=text/javascript');
  
// Gadget - CreateFellowPage
+
// Gadget - CreateFellow
$( document ).ready( function (){
+
mw.loader.load('/index.php?title=MediaWiki:Gadget-CreateFellow.js&action=raw&ctype=text/javascript');
 
 
function init() {
 
// Remove the default content
 
$('#mw-content-text > p').remove();
 
    $('#firstHeading').text('Create Fellow Page');
 
 
// Create widget
 
var bio = new OO.ui.MultilineTextInputWidget( {
 
            placeholder: 'Bio',
 
            autosize: true,
 
            rows: 10
 
        } ),
 
        achievements = new OO.ui.MultilineTextInputWidget( {
 
            placeholder: 'Achievements',
 
            autosize: true,
 
            rows: 10
 
        } ),
 
        social = new OO.ui.MultilineTextInputWidget( {
 
            placeholder: 'Social media profiles',
 
            autosize: true,
 
            rows: 10
 
        } ),
 
        name = new OO.ui.TextInputWidget( {
 
            placeholder: 'Name'
 
        } ),
 
        cohort = new OO.ui.TextInputWidget( {
 
            placeholder: 'Cohort'
 
        } ),
 
        school = new OO.ui.TextInputWidget( {
 
            placeholder: 'School'
 
        } ),
 
        major = new OO.ui.TextInputWidget( {
 
            placeholder: 'Majoring in'
 
        } ),
 
        country = new OO.ui.TextInputWidget( {
 
            placeholder: 'Country'
 
        } ),
 
createBtn = new OO.ui.ButtonWidget( {
 
    label: 'Create Page',
 
    flags: [ 'progressive', 'primary' ]
 
} ),
 
cancelBtn = new OO.ui.ButtonWidget( {
 
    label: 'Cancel',
 
    flags: [ 'primary', 'destructive' ],
 
    href:  mw.config.get( 'wgServer' )
 
} ),
 
label1 = $('<p>').text('Name:').css('font-weight','bold' ),
 
label2 = $('<p>').text('Cohort:').css('font-weight','bold' ),
 
label3 = $('<p>').text('School:').css('font-weight','bold' ),
 
label4 = $('<p>').text('Majoring in:').css('font-weight','bold' ),
 
label5 = $('<p>').text('Country:').css('font-weight','bold' ),
 
label6 = $('<p>').text('Bio:').css('font-weight','bold' ),
 
label7 = $('<p>').text('Achievements:').css('font-weight','bold' ),
 
label8 = $('<p>').text('Social media profiles:').css('font-weight','bold' ),
 
divMainBox = $('<div />').css( {
 
'box-shadow': '0 .5rem 1rem rgba(0, 0, 0, .15)',
 
'border': '1px solid',
 
'padding': '1rem',
 
'margin-bottom': '3rem',
 
'border-radius': '.5rem'
 
});
 
 
divMainBox.append(
 
            label1, name.$element,
 
            label2, cohort.$element,
 
            label3, school.$element,
 
            label4, major.$element,
 
            label5, country.$element,
 
            label6, bio.$element,
 
            label7, achievements.$element,
 
            label8, social.$element,
 
            '<br/>',
 
            createBtn.$element,
 
            cancelBtn.$element
 
);
 
   
 
$( '#mw-content-text' ).append( divMainBox );
 
 
 
// Create button click event handler
 
createBtn.on( 'click', function() {
 
nameResp = name.getValue().trim(),
 
cohortResp = cohort.getValue().trim();
 
schoolResp = school.getValue().trim(),
 
majorResp = major.getValue().trim();
 
countryResp = country.getValue().trim(),
 
bioResp = bio.getValue().trim();
 
achievementsResp = achievements.getValue().trim(),
 
socialResp = social.getValue().trim();
 
 
 
// Check whether the all fields are enough to run the function
 
if( nameResp !== "" && cohortResp !== ""
 
&& schoolResp !== "" && majorResp !== "" && countryResp !== "" ) {
 
 
api = new mw.Api();
 
 
 
wikiContent = "==Intro==\n";
 
wikiContent += ";Name: " + nameResp + "\n";
 
wikiContent += ";Cohort: " + cohortResp + "\n";
 
wikiContent += ";School: " + schoolResp + "\n";
 
wikiContent += ";Majoring in: " + majorResp + "\n";
 
wikiContent += ";Country: " + countryResp + "\n";
 
wikiContent += "==Bio==\n" + bioResp + "\n";
 
wikiContent += "==Achievements==\n" + achievementsResp + "\n";
 
wikiContent += "==Social media profiles==\n" + socialResp + "\n";
 
 
 
pageTitle = "Fellow:" + nameResp;
 
 
api.get( {
 
formatversion: 2,
 
action: 'query',
 
titles: pageTitle,
 
redirects: true
 
}, { async: false } ).then( function ( response ) {
 
var page = response.query.pages[ 0 ];
 
if ( page.missing || page.invalid ) {
 
doEdit( pageTitle, wikiContent );
 
} else {
 
api.get( {
 
formatversion: 2,
 
action: 'query',
 
titles: pageTitle + " (" + cohortResp + ")",
 
redirects: true
 
}, { async: false } ).then( function ( response ) {
 
var page = response.query.pages[ 0 ];
 
if ( page.missing || page.invalid ) {
 
doEdit( pageTitle + " (" + cohortResp + ")", wikiContent );
 
} else {
 
doEdit( pageTitle + " (" + schoolResp + ", " + cohortResp + ")", wikiContent );
 
}
 
});
 
}
 
} );
 
} else {
 
mw.notify( "Some fields have no text :( Please fill them." );
 
return;
 
}
 
 
 
    });
 
   
 
    function doEdit(title, wikiContent){
 
    // Edit the page
 
var editParams = {
 
action: 'edit',
 
title: title,
 
text: wikiContent,
 
summary: "Created Fellow Page",
 
format: 'json'
 
};
 
 
api.postWithToken( 'csrf', editParams, { async: false } ).done( function ( res ) {
 
if( res.edit.result === "Success"){
 
mw.notify( title + " page has created :)");
 
window.location.replace( mw.config.get( 'wgServer' ) + "/wiki/" + title );
 
} else{
 
mw.notify( "Something went worng :(" );
 
}
 
} );
 
    }
 
}
 
 
if ( mw.config.get('wgCanonicalSpecialPageName') === 'Blankpage' && mw.config.get('wgTitle').split('/', 2)[1] === 'CreateFellowPage' ) {
 
mw.loader.using( ['oojs-ui-core', 'mediawiki.api', 'mediawiki.notify', 'mediawiki.Title'], init );
 
}
 
});
 

Revision as of 10:13, 23 June 2020

window.protectedNamespace = [ "2020", "2020_talk", "2021", "2022", "2023", "2024", "2025" ];

$( document ).ready( function(){
	if( mw.config.get('wgIsMainPage') )
	{
		$('#sbl-breadcrumbs').remove();
	}
	
	// Open all UIF link in new tab
	$('#p-UIF').children().find('a').attr('target', '_blank');
	
	if ( mw.config.get( 'wgPageName' ) === "Special:Badtitle" ){
		$('#pt-login').hide();
	}
	
	// CategoryTree
	$('.CategoryTreeLabelPage').first().prev().before("---------------------------<br>");
	$('.CategoryTreeEmptyBullet').each( function(){ 
		if( $(this).next().next().val() === undefined){
			$(this).parent().hide();
		}
	});
});


// Sidebar
$( document ).ready(function(){

	function ModifySidebar( section, name, link ) {		
		var node = document.getElementById( 'p-'+ section )
						   .getElementsByTagName( 'div' )[0]
						   .getElementsByTagName( 'ul' )[0];

		var aNode = document.createElement( 'a' );
		var liNode = document.createElement( 'li' );

		aNode.appendChild( document.createTextNode( name ) );
		aNode.setAttribute( 'href', link );
		liNode.appendChild( aNode );
		liNode.className = 'plainlinks';
		node.appendChild( liNode );
	}

	if( window.protectedNamespace.indexOf( mw.config.get( 'wgCanonicalNamespace' )) !== -1 ){
		$('#p-navigation > .body ul').empty();
		$('#p-tb').remove();
		$('#p-UIF').remove();
		$('#p-ratePage-vote-title').remove();
		
		if( !(mw.config.get( 'wgUserGroups' ).indexOf("bureaucrat") !== -1) ) {
			$("#left-navigation").empty();
			$("#right-navigation").empty();
		}
		
		// Navigation Links 
		if ( mw.config.get( 'wgTitle' ) === "Training Overview" ){
			ModifySidebar( 'navigation', 'Back to Main Page', 'https://universityinnovation.org/wiki/Main_Page' );
		} else {
			ModifySidebar( 'navigation', 'Back to Overview', 'https://universityinnovation.org/wiki/2020:Training_Overview' );
		}
		mw.loader.using( 'mediawiki.api', function(){
			var api = new mw.Api(),
				sections = [ 1, 2, 3 ],
				pageName = mw.config.get( 'wgCanonicalNamespace' ).slice(0,4) + ":Sidebar";

			sections.forEach(function(item){
				params = {
					"action": "parse",
					"format": "json",
					"page": pageName,
					"wrapoutputclass": "",
					"section": item,
					"disablelimitreport": 1,
					"disableeditsection": 1
				};
				
				api.get(params).done(function( data){
					$('a', data.parse.text['*']).each( function(){
						if( item == 1){
							// Session Links
							ModifySidebar( 'Sessions', $(this).text(), $( this).attr("href") );
						}
						else if( item ==2 ){
							// Resources Links
							ModifySidebar( 'Discussions', $(this).text(), $( this).attr("href") );
						}
						else if( item ==3 ){
							// Resources Links
							ModifySidebar( 'Resources', $(this).text(), $( this).attr("href") );
						}
					});
	
					// Make current page non clickable
					$('.portal .body > ul').children().each( function(){
					    link = $( this ).children()[0].href;
					    if ( link === window.location.href ) {
					        $( $( this ).children()[0]).css({
					            "pointer-events": "none",
					            "cursor": "default",
					            "color": "#000",
					            "font-weight": "bold"
					        });
					    }
					});
				});
			});

		});
	} else {
		$('#p-Sessions').remove();
		$('#p-Discussions').remove();	
		$('#p-Resources').remove();		
	}
});

// Gadget-HotCat
mw.loader.load( '/index.php?title=MediaWiki:Gadget-HotCat.js&action=raw&ctype=text/javascript' );

// Gadget - QRCode
mw.loader.load('/index.php?title=MediaWiki:QRCode.js&action=raw&ctype=text/javascript');

// Gadget - CreateFellow2020
mw.loader.load('/index.php?title=MediaWiki:Gadget-2020Cohort.js&action=raw&ctype=text/javascript');

// Gadget - CreateFellow
mw.loader.load('/index.php?title=MediaWiki:Gadget-CreateFellow.js&action=raw&ctype=text/javascript');