Difference between revisions of "MediaWiki:Common.js"

From University Innovation Fellows
Jump to navigation Jump to search
 
(48 intermediate revisions by 3 users not shown)
Line 1: Line 1:
window.protectedNamespace = [ "2020", "2020_talk", "2021", "2022", "2023", "2024", "2025" ];
+
window.protectedNamespace = [ "2020", "2020_talk", "2021", "2021_talk", "2022", "2022_talk", "2023", "2024", "2025" ];
 +
 
 +
// $( document ).ready( function(){
 +
// $('body').addClass('_responsive');
 +
// console.log('Yes we can edit by jQuery');
 +
// });
  
 
$( document ).ready( function(){
 
$( document ).ready( function(){
Line 9: Line 14:
 
// Open all UIF link in new tab
 
// Open all UIF link in new tab
 
$('#p-UIF').children().find('a').attr('target', '_blank');
 
$('#p-UIF').children().find('a').attr('target', '_blank');
+
 
 +
// Hide login link from nav
 
if ( mw.config.get( 'wgPageName' ) === "Special:Badtitle" ){
 
if ( mw.config.get( 'wgPageName' ) === "Special:Badtitle" ){
 
$('#pt-login').hide();
 
$('#pt-login').hide();
 +
}
 +
 +
// Remove the navigation from Training namespaces
 +
if( mw.config.get("wgCanonicalNamespace") === "Training" && !(mw.config.get( 'wgUserGroups' ).indexOf("bureaucrat") !== -1) ) {
 +
$("#left-navigation").empty();
 +
$("#right-navigation").empty();
 +
}
 +
 +
// Redirect Topic page to their parent pages
 +
if( mw.config.get( 'wgNamespaceNumber') === mw.config.get( 'wgNamespaceIds' ).topic ){
 +
location.replace( mw.config.get( 'wgServer') + $("#contentSub").children().attr("href") );
 
}
 
}
  
Line 24: Line 41:
 
});
 
});
 
}
 
}
 
// CategoryTree
 
$('.CategoryTreeLabelPage').first().prev().before("---------------------------<br>");
 
$('.CategoryTreeEmptyBullet').each( function(){
 
if( $(this).next().next().val() === undefined){
 
$(this).parent().hide();
 
}
 
});
 
 
 
 
// CategoryTree Sorting
 
// CategoryTree Sorting
Line 38: Line 47:
  
 
items.sort( function(a, b){
 
items.sort( function(a, b){
if( $( a ).find(".CategoryTreeLabelNs14").length === 0){
+
        if (
return 0;
+
              $(a).find(".CategoryTreeLabelNs14").length === 0
}
+
            && $(a).find(".CategoryTreeLabelNs3762").length === 0
 +
            && $(a).find(".CategoryTreeLabelNs3752").length === 0
 +
            && $(a).find(".CategoryTreeLabelNs3756").length === 0
 +
        ) {
 +
            return 0;
 +
        }
  
 
// Get text of the category link
 
// Get text of the category link
Line 52: Line 66:
 
// If nothing is found, status will remain same
 
// If nothing is found, status will remain same
 
if( firstMatch === null && secondMatch === null ) {  
 
if( firstMatch === null && secondMatch === null ) {  
return 0;
+
            if ( firstText.includes("Faculty Champion:") ){
 +
                // Now campare with second element
 +
                if( secondText.includes("School:") || secondText.includes("Priorities:") ){
 +
                    return -1;
 +
                } else {
 +
                    return 0;
 +
                }
 +
            } else if( firstText.includes("School:") ){
 +
                // Now campare with second element
 +
                if( secondText.includes("Faculty Champion:") ){
 +
                    return 1;
 +
                } else if( secondText.includes("Priorities:") ){
 +
                    return -1;
 +
                } else {
 +
                    return 0;
 +
                }
 +
            } else if( firstText.includes("Priorities:") ){
 +
                // Now campare with second element
 +
                if( secondText.includes("Faculty Champion:") || secondText.includes("School:") ){
 +
                    return 1;
 +
                } else {
 +
                    return 0;
 +
                }
 +
            } else {
 +
                return 0;
 +
            }
 
}
 
}
  
Line 67: Line 106:
 
});
 
});
 
container.append( items );
 
container.append( items );
 +
 +
// CategoryTree
 +
$('.CategoryTreeLabelPage').first().prev().before("---------------------------<br>");
 +
$('.CategoryTreeEmptyBullet').each( function(){
 +
if( $(this).next().next().val() === undefined){
 +
$(this).parent().hide();
 +
}
 +
});
 
});
 
});
  
 +
$( function(){
 +
pageId = $("#embedWikiPage");
 +
page = pageId.attr("data-page");
 +
pageId.css( {
 +
"overflow": "scroll",
 +
"height": pageId.attr("data-height"),
 +
"width": pageId.attr("data-width")
 +
});
 +
loadWikiPage( page );
 +
 +
function loadWikiPage( page ){
 +
pageId.load("/wiki/"+ page + " .mw-parser-output", function(){
 +
$( this ).find( ".mw-editsection" ).hide();
 +
$( this ).find( "#toc" ).hide();
 +
$( this ).scrollTop(0);
 +
anchors = $( this ).find("a");
 +
anchors.on("click", function(e){
 +
link = $( this )[0].href.split("https://universityinnovation.org/wiki/");
 +
if( link.length > 1 ){
 +
newPage = link.pop();
 +
if( newPage.includes("Main_Page") || newPage.includes("File:")
 +
|| newPage.includes("Special:") || newPage.includes("Category:")
 +
|| newPage.includes("#") || newPage.includes("Talk:") ){
 +
$( this ).attr('target', '_blank').get(0).click();
 +
} else {
 +
e. preventDefault();
 +
loadWikiPage( newPage );
 +
}
 +
} else {
 +
$( this ).attr('target', '_blank').get(0).click();
 +
}
 +
});
 +
});
 +
}
 +
});
  
  
Line 105: Line 187:
 
ModifySidebar( 'navigation', 'Back to Main Page', 'https://universityinnovation.org/wiki/Main_Page' );
 
ModifySidebar( 'navigation', 'Back to Main Page', 'https://universityinnovation.org/wiki/Main_Page' );
 
} else {
 
} else {
ModifySidebar( 'navigation', 'Back to Overview', 'https://universityinnovation.org/wiki/2020:Training_Overview' );
+
ModifySidebar( 'navigation', 'Back to Overview', 'https://universityinnovation.org/wiki/'+ mw.config.get( 'wgCanonicalNamespace' ).slice(0,4) + ':Training_Overview' );
 
}
 
}
 
mw.loader.using( 'mediawiki.api', function(){
 
mw.loader.using( 'mediawiki.api', function(){
Line 163: Line 245:
  
 
// Gadget-HotCat
 
// Gadget-HotCat
mw.loader.load( '/index.php?title=MediaWiki:Gadget-HotCat.js&action=raw&ctype=text/javascript' );
+
if( mw.config.get("wgUserGroups").indexOf("bureaucrat") !== -1) {
 +
mw.loader.load( '/index.php?title=MediaWiki:Gadget-HotCat.js&action=raw&ctype=text/javascript' );
 +
}
  
 
// Gadget - QRCode
 
// Gadget - QRCode
mw.loader.load('/index.php?title=MediaWiki:QRCode.js&action=raw&ctype=text/javascript');
+
// mw.loader.load('/index.php?title=MediaWiki:QRCode.js&action=raw&ctype=text/javascript');
  
 
// Gadget - CreateFellow2020
 
// Gadget - CreateFellow2020
Line 173: Line 257:
 
// Gadget - UpdateWikiBio
 
// Gadget - UpdateWikiBio
 
mw.loader.load('/index.php?title=MediaWiki:Gadget-UpdateWikiBio.js&action=raw&ctype=text/javascript');
 
mw.loader.load('/index.php?title=MediaWiki:Gadget-UpdateWikiBio.js&action=raw&ctype=text/javascript');
 +
 +
 +
//_responsive chabges

Latest revision as of 08:28, 31 August 2021

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

// $( document ).ready( function(){
// 	$('body').addClass('_responsive');
// 	console.log('Yes we can edit by jQuery');
// });

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

	// Hide login link from nav
	if ( mw.config.get( 'wgPageName' ) === "Special:Badtitle" ){
		$('#pt-login').hide();
	}

	// Remove the navigation from Training namespaces
	if( mw.config.get("wgCanonicalNamespace") === "Training" && !(mw.config.get( 'wgUserGroups' ).indexOf("bureaucrat") !== -1) ) {
		$("#left-navigation").empty();
		$("#right-navigation").empty();
	}

	// Redirect Topic page to their parent pages
	if( mw.config.get( 'wgNamespaceNumber') === mw.config.get( 'wgNamespaceIds' ).topic ){
		location.replace( mw.config.get( 'wgServer') + $("#contentSub").children().attr("href") );
	}

	// Image copy link widget
	if( mw.config.get("wgCanonicalNamespace") === "File" ){
		mw.loader.using('mediawiki.widgets').then( function (){
			var copyImageLink = new mw.widgets.CopyTextLayout( {
				title: 'Copy image link', 
				copyText: mw.config.get( 'wgServer' ) + "/wiki/" + mw.config.get("wgPageName")
			} );
			$( '#firstHeading' ).after( copyImageLink.$element );
		});
	}
	
	// CategoryTree Sorting
	var container = $( $( "div.CategoryTreeSection" ).first().children().last()[0] ),
		items = container.children();

	items.sort( function(a, b){
        if (
               $(a).find(".CategoryTreeLabelNs14").length === 0
            && $(a).find(".CategoryTreeLabelNs3762").length === 0 
            && $(a).find(".CategoryTreeLabelNs3752").length === 0 
            && $(a).find(".CategoryTreeLabelNs3756").length === 0
        ) {
            return 0;
        }

		// Get text of the category link
		firstText = $(a).find("a").text();
		secondText = $(b).find("a").text();

		// Regex to get the cohort year
		firstMatch = /(\d{4})/ig.exec( firstText.trim() );
		secondMatch = /(\d{4})/ig.exec( secondText.trim() );

		// If nothing is found, status will remain same
		if( firstMatch === null && secondMatch === null ) { 
            if ( firstText.includes("Faculty Champion:") ){
                // Now campare with second element
                if( secondText.includes("School:") || secondText.includes("Priorities:") ){
                    return -1;
                } else {
                    return 0;
                }
            } else if( firstText.includes("School:") ){
                // Now campare with second element
                if( secondText.includes("Faculty Champion:") ){
                    return 1;
                } else if( secondText.includes("Priorities:") ){
                    return -1;
                } else {
                    return 0;
                }
            } else if( firstText.includes("Priorities:") ){
                // Now campare with second element
                if( secondText.includes("Faculty Champion:") || secondText.includes("School:") ){
                    return 1;
                } else {
                    return 0;
                }
            } else {
                return 0;
            }
		}

		// If first element match
		if( firstMatch !== null) {
			if( secondMatch !== null ){
				return firstMatch[1] > secondMatch[1] ? -1 : 1;
			} else {
				return -1;
			}
		} else {
			return secondMatch !== null ? 1 : 0;
		}
	});
	container.append( items );
	
	// CategoryTree
	$('.CategoryTreeLabelPage').first().prev().before("---------------------------<br>");
	$('.CategoryTreeEmptyBullet').each( function(){ 
		if( $(this).next().next().val() === undefined){
			$(this).parent().hide();
		}
	});
});

$( function(){
	pageId = $("#embedWikiPage");
	page = pageId.attr("data-page");
	pageId.css( {
		"overflow": "scroll",
		"height": pageId.attr("data-height"),
		"width": pageId.attr("data-width")
	});
	loadWikiPage( page );

	function loadWikiPage( page ){
		pageId.load("/wiki/"+ page + " .mw-parser-output", function(){
			$( this ).find( ".mw-editsection" ).hide();
			$( this ).find( "#toc" ).hide();
			$( this ).scrollTop(0);
			anchors = $( this ).find("a");
			anchors.on("click", function(e){
				link = $( this )[0].href.split("https://universityinnovation.org/wiki/");
				if( link.length > 1 ){
					newPage = link.pop();
					if( newPage.includes("Main_Page") || newPage.includes("File:") 
						|| newPage.includes("Special:") || newPage.includes("Category:")
						|| newPage.includes("#") || newPage.includes("Talk:") ){
						$( this ).attr('target', '_blank').get(0).click();
					} else {
						e. preventDefault();	
						loadWikiPage( newPage );
					}
				} else {
					$( this ).attr('target', '_blank').get(0).click();
				}
			});
		});
	}
});


// 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 ){
		$("#firstHeading").text( mw.config.get( 'wgTitle' ) );
		$('#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/'+ mw.config.get( 'wgCanonicalNamespace' ).slice(0,4) + ':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
if( mw.config.get("wgUserGroups").indexOf("bureaucrat") !== -1) {
	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 - UpdateWikiBio
mw.loader.load('/index.php?title=MediaWiki:Gadget-UpdateWikiBio.js&action=raw&ctype=text/javascript');


//_responsive chabges