MediaWiki:Common.js

From UBC Wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
 /*notification on update. comment out after use.
var ubcNotificationDesc = 'UBC Wiki will be under maintenance on<strong> Thursday, Aug 31</strong> from <strong>7:00pm-12:00am PST</strong>. During this period, UBC Wiki will be not be accessible. Concerns? Email <a href="mailto:lt.hub@ubc.ca">lt.hub@ubc.ca</a>';
var ubcNotificaitonTitle = 'Notice to wiki users';
var ubcNotificationMessage = '<aside class="top-header-notify">' +
'<h1>' + ubcNotificaitonTitle +'</h1>' +
'<p>' + ubcNotificationDesc + '</p>' +
'</aside>';

$( function () {
  $( '#mw-page-base' ).before( ubcNotificationMessage );
}());

*/

/* =============================== UBC HEADER ==================================== */

var ubcHeaderHtml = 
'<header id="ubc7-header" class="row-fluid" role="banner">' +
	'<div class="span1">' +
		'<div id="ubc7-logo">' +
			'<a href="http://www.ubc.ca" title="The University of British Columbia (UBC)">The University of British Columbia</a>' +
		'</div>' +
	'</div>' +
	'<div class="span9" id="ubc7-wordmark-block">' +
		'<div id="ubc7-wordmark">' +
			'<a href="http://www.ubc.ca/" title="The University of British Columbia (UBC)">The University of British Columbia</a>' +
		'</div>' +
		'<div id="ubc7-global-utility">' +
			'<button type="button" data-toggle="collapse" data-target="#ubc7-global-menu"><span>UBC Search</span></button>' +
			'<noscript>&lt;a id="ubc7-global-utility-no-script" href="http://www.ubc.ca/" title="UBC Search"&gt;UBC Search&lt;/a&gt;</noscript>' +
		'</div>' +
	'</div>' +
'</header>' +
'<div id="ubc7-unit" class="row-fluid expand">' +
	'<div class="span12">' +
		'<div id="ubc7-unit-name" class="ubc7-single-element">' +
		'<a href="http://wiki.ubc.ca" title="UBC Wiki"><span id="ubc7-unit-identifier">UBC Wiki</span></a>' +
		'</div>' +
	'</div>' +
'</div>';


$( function () {
  $( '#mw-page-base' ).before( ubcHeaderHtml );
}());


/* =============================== END UBC HEADER ==================================== */




  /** Test if an element has a certain class **************************************
  *
  * Description: Uses regular expressions and caching for better performance.
  * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
  */

  var hasClass = ( function() {
 	var reCache = {};
 	return function( element, className ) {
 		return ( reCache[className] ? reCache[className] : ( reCache[className] = new RegExp( "(?:\\s|^)" + className + "(?:\\s|$)" ) ) ).test( element.className );
 	};
 })();


/* =============================== MER Tag==================================== */
function MER_tag_navigation() {

	var mer_tag = jQuery("#MER-tag");

	if( typeof mer_tag[0] !== "undefined" ) {
		var tag_list = mer_tag.text().replace(/(<([^>]+)>)/ig,"").split(',');
		var num_of_tags = tag_list.length;
		var href, tagName, hrefString, html, tag;
                html = '';
		for (var i=0; i < num_of_tags; i++) {
                    
                    tag = tag_list[i].trim();
                    // if it is not a MER Tag skip 
		    if (tag.indexOf("MER Tag") == -1 ) 
                        continue;
                    // get just the tag
		    tagName = tag.substring(8); 

		    href  = "http://wiki.ubc.ca/Category:MER_Tag_"+tagName.replace(/ /g, "_");
		    html += '<em>See more questions about <strong><a href=' + href + '>'+tagName+'</a></strong>.</em><br />';
		    
		}	
		mer_tag.html(html);
	}
}

/** MER Course Tag Nav**/

function MER_coursetags(raw_tags){
    var stringList = raw_tags.innerHTML.split('---');
    var newContent = "";
    var allTags = []
    for (var i=0; i < stringList.length; i++){
        sublist = stringList[i].split(',');
        for (var j=0; j < sublist.length; j++){
            if (sublist[j].indexOf("MER Tag") !== -1){
                tagName = sublist[j].replace(" MER","MER").substring(8); // take care of optional leading space
                allTags.push(tagName)
            }
        }
    }

    // Reduce to only the unique tags
    var u = {}, uniqueTags = [];
    for(var i = 0, l = allTags.length; i < l; ++i){
        if(!u.hasOwnProperty(allTags[i])) {
            uniqueTags.push(allTags[i]);
            u[allTags[i]] = 1;
        }
    }

    uniqueSortedTags = uniqueTags.sort();
    for (var i=0; i < uniqueSortedTags.length; i++){
        hrefString = "http://wiki.ubc.ca/Category:MER_Tag_" + uniqueSortedTags[i].replace(/ /g, "_");
        newContent += '<a href=' + hrefString + '>' + uniqueSortedTags[i] + '</a></br>';
    }
    return newContent
}

if(document.getElementById('MER-coursetags') != null){
    document.getElementById('MER-coursetags').innerHTML = MER_coursetags(document.getElementById('MER-coursetags'));
}

/* =============================== MER END ==================================== */

/**
 * Collapsible tables
 *
 * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
 *
 * @version 2.0.3 (2014-03-14)
 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
 * @author [[User:R. Koot]]
 * @author [[User:Krinkle]]
 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
 * is supported in MediaWiki core.
 */

var autoCollapse = 2;
var collapseCaption = 'hide';
var expandCaption = 'show';
var tableIndex = 0;

function collapseTable( tableIndex ) {
	var Button = document.getElementById( 'collapseButton' + tableIndex );
	var Table = document.getElementById( 'collapsibleTable' + tableIndex );

	if ( !Table || !Button ) {
		return false;
	}

	var Rows = Table.rows;
	var i;
	var $row0 = $(Rows[0]);

	if ( Button.firstChild.data === collapseCaption ) {
		for ( i = 1; i < Rows.length; i++ ) {
			Rows[i].style.display = 'none';
		}
		Button.firstChild.data = expandCaption;
	} else {
		for ( i = 1; i < Rows.length; i++ ) {
			Rows[i].style.display = $row0.css( 'display' );
		}
		Button.firstChild.data = collapseCaption;
	}
}

function createClickHandler( tableIndex ) {
	return function ( e ) {
		e.preventDefault();
		collapseTable( tableIndex );
	};
}

function createCollapseButtons( $content ) {
	var NavigationBoxes = {};
	var $Tables = $content.find( 'table' );
	var i;

	$Tables.each( function( i, table ) {
		if ( $(table).hasClass( 'collapsible' ) ) {

			/* only add button and increment count if there is a header row to work with */
			var HeaderRow = table.getElementsByTagName( 'tr' )[0];
			if ( !HeaderRow ) {
				return;
			}
			var Header = table.getElementsByTagName( 'th' )[0];
			if ( !Header ) {
				return;
			}

			NavigationBoxes[ tableIndex ] = table;
			table.setAttribute( 'id', 'collapsibleTable' + tableIndex );

			var Button     = document.createElement( 'span' );
			var ButtonLink = document.createElement( 'a' );
			var ButtonText = document.createTextNode( collapseCaption );
			// Styles are declared in [[MediaWiki:Common.css]]
			Button.className = 'collapseButton';

			ButtonLink.style.color = Header.style.color;
			ButtonLink.setAttribute( 'id', 'collapseButton' + tableIndex );
			ButtonLink.setAttribute( 'href', '#' );
			$( ButtonLink ).on( 'click', createClickHandler( tableIndex ) );
			ButtonLink.appendChild( ButtonText );

			Button.appendChild( document.createTextNode( '[' ) );
			Button.appendChild( ButtonLink );
			Button.appendChild( document.createTextNode( ']' ) );

			Header.insertBefore( Button, Header.firstChild );
			tableIndex++;
		}
	} );

	for ( i = 0;  i < tableIndex; i++ ) {
		if ( $( NavigationBoxes[i] ).hasClass( 'collapsed' ) ||
			( tableIndex >= autoCollapse && $( NavigationBoxes[i] ).hasClass( 'autocollapse' ) )
		) {
			collapseTable( i );
		}
	}
}

mw.hook( 'wikipage.content' ).add( createCollapseButtons );

/* =============================== Change the Visual Editor upload message ==================================== */
//wait for page loaded and get our variables
var insertTab = document.querySelector('.oo-ui-popupToolGroup-handle'),
    editPage = document.querySelector('#content'),
    editTab = document.querySelector('.ca-edit'),
    visualEditTab = document.querySelector('#ca-ve-edit a');
var observerOptions = {
    childList: true,
    attributes: true,
    subtree: true //Omit or set to false to observe only changes to the parent node.
};

//create a callback to delay our observer

// find our element and if it exists call it.
var changeVisualEditorUpLoadMessage = function() {

    var mediaUploadWrapper = document.querySelector('.ve-ui-mwMediaDialog-panel-search');

    if (mediaUploadWrapper) {

        var uploadMessage = 'Please go <a href="https://wiki.ubc.ca/Special:UploadWizard" title="Upload media" target="_blank">here</a> to upload media.',
            parentElement = mediaUploadWrapper.getElementsByClassName('oo-ui-bookletLayout-stackLayout')[0];
            childElement = parentElement.firstChild.innerHTML = uploadMessage,
            insertTab = document.querySelector('.ve-test-toolbar-insert');

    }
} // changeVisualEditorUpLoadMessage() //


var letTheDomLoad = function() {

    setTimeout(changeVisualEditorUpLoadMessage, 1000);

} // letTheDomLoad() //


//create the observer and start the watch
var doObserving = function() {

    var observer = new MutationObserver(letTheDomLoad);
    return observer.observe(editPage, observerOptions)

} // doObserveFunc() //



if (editPage) {
    doObserving();
}
/* =============================== END Change the Visual Editor upload message ==================================== */