Jump to: navigation, search

Difference between revisions of "MediaWiki:Common.js"

 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
function ModifySidebar( action, section, name, link ) {
+
/* Any JavaScript here will be loaded for all users on every page load. */
try {
 
switch ( section ) {
 
case 'languages':
 
var target = 'p-lang';
 
break;
 
case 'toolbox':
 
var target = 'p-tb';
 
break;
 
case 'navigation':
 
var target = 'p-navigation';
 
break;
 
default:
 
var target = 'p-' + section;
 
break;
 
}
 
  
if ( action == 'add' ) {
+
function filterTable(){
var node = document.getElementById( target )
+
$("table.filterable").each(function(){
  .getElementsByTagName( 'div' )[0]
+
var i=0;
  .getElementsByTagName( 'ul' )[0];
+
var cols;
 
+
$(this).find("tr:first-child th, tr:first-child td").each(function(){
var aNode = document.createElement( 'a' );
+
if (!$(this).hasClass("unfilterable")){
var liNode = document.createElement( 'li' );
+
cols=[];
 
+
$(this).closest("table").find("tr td:nth-child("+(i+1)+")").each(function(){
aNode.appendChild( document.createTextNode( name ) );
+
cols.push($(this).text());
aNode.setAttribute( 'href', link );
+
});
liNode.appendChild( aNode );
+
cols = arrayUnique(cols);
liNode.className = 'plainlinks';
+
l=0;
node.appendChild( liNode );
+
for (j=0; j<cols.length; j++){
}
+
t=charLength(cols[j]);
 
+
if (l<t) l=t;
if ( action == 'remove' ) {
+
}
var list = document.getElementById( target )
+
$(this).css("position","relative");
  .getElementsByTagName( 'div' )[0]
+
$(this).html('<a href="javascript:void(0)" class="showFilterMenu">'+$(this).html()+'▼</a>');
  .getElementsByTagName( 'ul' )[0];
+
$(this).append($('<div class="filterMenu" style="position:absolute;top:'+$(this).height()+'px;left:0;width:'+(22+l*7)+'px;text-align:left;padding:5px;border:1px #000 solid;background:#ddd;z-index:1;display:none"></div>'));
 
+
for (j=0; j<cols.length; j++){
var listelements = list.getElementsByTagName( 'li' );
+
$(this).find(".filterMenu").append('<div><input type="checkbox" value="'+cols[j]+'" col="'+(i+1)+'" class="filterOption" checked>'+cols[j]+'</div>')
 
 
for ( var i = 0; i < listelements.length; i++ ) {
 
if (
 
listelements[i].getElementsByTagName( 'a' )[0].innerHTML == name ||
 
listelements[i].getElementsByTagName( 'a' )[0].href == link
 
)
 
{
 
list.removeChild( listelements[i] );
 
 
}
 
}
 
}
 
}
 +
i++;
 +
});
 +
$(this).find("tr:nth-child(n+1)").attr("condition", 0);
 +
});
 +
$(".showFilterMenu").click(function(){
 +
if ($(this).parent().find(".filterMenu:visible").length){
 +
$(".filterMenu").slideUp(150);
 +
}else{
 +
$(".filterMenu").slideUp(150);
 +
$(this).parent().find(".filterMenu").slideDown(150);
 
}
 
}
 
+
});
 
+
$(document).mouseup(function(e){
} catch( e ) {
+
var container = $(".filterMenu");
// let's just ignore what's happened
+
    if (!container.is(e.target) && container.has(e.target).length === 0){
return;
+
        container.slideUp(150);
}
+
    }
 +
});
 +
$(".filterOption").click(function(){
 +
col=$(this).attr("col");
 +
val=$(this).val();
 +
if ($(this).is(":checked")) chg=1; else chg=-1;
 +
$(this).closest("table").find("tr:nth-child(n+1)").each(function(){
 +
if ($(this).find("td:nth-child("+col+")").text()==val){
 +
var cond=$(this).attr("condition");
 +
cond=Number(cond)+chg;
 +
$(this).attr("condition", cond);
 +
if (cond==0) $(this).show();
 +
else $(this).hide();
 +
}
 +
});
 +
});
 
}
 
}
 
+
function arrayUnique(a) {
function CustomizeModificationsOfSidebar() {
+
    return a.reduce(function(p, c) {
          ModifySidebar( 'remove', 'views');
+
        if (p.indexOf(c) < 0) p.push(c);
 +
        return p;
 +
    }, []);
 +
};
 +
function charLength(s){
 +
return s.length+(encodeURI(s).split(/%..|./).length-1-s.length)/2;
 
}
 
}
 
+
filterTable();
jQuery( CustomizeModificationsOfSidebar );
 

Latest revision as of 08:51, 13 May 2020

/* Any JavaScript here will be loaded for all users on every page load. */

function filterTable(){
	$("table.filterable").each(function(){
		var i=0;
		var cols;
		$(this).find("tr:first-child th, tr:first-child td").each(function(){
			if (!$(this).hasClass("unfilterable")){
				cols=[];
				$(this).closest("table").find("tr td:nth-child("+(i+1)+")").each(function(){
					cols.push($(this).text());
				});
				cols = arrayUnique(cols);
				l=0;
				for (j=0; j<cols.length; j++){
					t=charLength(cols[j]);
					if (l<t) l=t;
				}
				$(this).css("position","relative");
				$(this).html('<a href="javascript:void(0)" class="showFilterMenu">'+$(this).html()+'▼</a>');
				$(this).append($('<div class="filterMenu" style="position:absolute;top:'+$(this).height()+'px;left:0;width:'+(22+l*7)+'px;text-align:left;padding:5px;border:1px #000 solid;background:#ddd;z-index:1;display:none"></div>'));
				for (j=0; j<cols.length; j++){
					$(this).find(".filterMenu").append('<div><input type="checkbox" value="'+cols[j]+'" col="'+(i+1)+'" class="filterOption" checked>'+cols[j]+'</div>')
				}
			}
			i++;
		});
		$(this).find("tr:nth-child(n+1)").attr("condition", 0);
	});
	$(".showFilterMenu").click(function(){
		if ($(this).parent().find(".filterMenu:visible").length){
			$(".filterMenu").slideUp(150);
		}else{
			$(".filterMenu").slideUp(150);
			$(this).parent().find(".filterMenu").slideDown(150);
		}
	});
	$(document).mouseup(function(e){
		var container = $(".filterMenu");
	    if (!container.is(e.target) && container.has(e.target).length === 0){
	        container.slideUp(150);
	    }
	});
	$(".filterOption").click(function(){
		col=$(this).attr("col");
		val=$(this).val();
		if ($(this).is(":checked")) chg=1; else chg=-1;
		$(this).closest("table").find("tr:nth-child(n+1)").each(function(){
			if ($(this).find("td:nth-child("+col+")").text()==val){
				var cond=$(this).attr("condition");
				cond=Number(cond)+chg;
				$(this).attr("condition", cond);
				if (cond==0) $(this).show();
				else $(this).hide();
			}
		});
	});
}
function arrayUnique(a) {
    return a.reduce(function(p, c) {
        if (p.indexOf(c) < 0) p.push(c);
        return p;
    }, []);
};
function charLength(s){
	return s.length+(encodeURI(s).split(/%..|./).length-1-s.length)/2;
}
filterTable();