User:Genewikiplus/SWL infobox.js

// // SWL_portlet_infobox.js // If the page contain SWLs, it adds a show/hide portlet in the "ca-views" area (top right corner of the page). // Clicking on it produces an infobox collecting all SWLs, and also highlights them throughout the page. // Links to target pages added. // // Salvatore Loguercio, 2011 // email:salvatore.loguercio@Gmail.com // if($(".swl").length!=0) {   // if there are SWLs on the page var title = document.title.split("-")[0]; function makeLink(str) { return "" + str + ""; } function makeTable {        // make an infobox with all SWLs, displayed as "predicate-object" var swlArray = $('.swl'); // initialize array; var content = $.makeArray(swlArray).map(function (elem) {   // map on every swl found    var inner1 = elem.firstChild,        inner2,        result = [];    if (inner1) {        var inner2 = inner1.firstChild;    }    var line = '  ' +             [makeLink(inner1.className), "" + inner2.title + ""].join('  ')             + ' \n';	// table formatting stuff; links to target pages added as well.    return line;  }); return ' '; // make a pretty infobox, same style as PBB infoboxes. } // toggle function function toggle(obj) { var el = document.getElementById(obj); if ( el.style.display != 'none' ) { el.style.display = 'none'; // hide the infobox $(".swl").css("border",""); // hide the borders on SWLs }	else { el.style.display = '';                          // show infobox.. $(".swl").css("border","3px solid lightgreen"); // ..and borders } } var swltab=' ' + makeTable.replace(/,/g, '') + ' ';  // instantiate makeTable, as a with an id to be used by the toggle function. Initially not displayed. $('#siteSub').append(swltab); // Add it right below the page title // Create portlet link*; var portletLink = mw.util.addPortletLink( 'p-views', '#',       'Semantic Wiki Links', 'ca-SWL', 'Shows Semantic Wiki Links on the current page','',document.getElementById("ca-watch") ); // Bind click handler $(portletLink).click( function( e ) {       e.preventDefault;        toggle("swltab"); }); } // * see http://www.mediawiki.org/wiki/ResourceLoader/Default_modules#addPortletLink