MediaWiki:Common.js: Difference between revisions

From Tygron Preview Support Wiki
Jump to navigation Jump to search
mNo edit summary
mNo edit summary
Line 62: Line 62:
} );
} );


//# sourceURL=Common.js
<nowiki>//#sourceURL=Common.js</nowiki>

Revision as of 14:20, 15 June 2020

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

/* For every span, div, table, td, or th, if that element has the class "pops", then all anchor links (e.g. from [] or [[]] syntax) should  open in a new tab/window. */
$( function() {
     var pops = function( elems ) {
         for (var i=0; i<elems.length; i++) {
             if ( !(' '+elems[i].className+' ').match( / pops / ) ) continue;
             var anchs = elems[i].getElementsByTagName('a');
             for (var j=0; j<anchs.length; j++) anchs[j].target = '_blank';
         }
     };
     var bc = document.getElementById('bodyContent');
     var tags = ['span', 'div', 'table', 'td', 'th'];
     for (var i=0; i<tags.length; i++) pops( bc.getElementsByTagName( tags[i] ) );
 } );

/*Inject a search bar to filter elements in lists, where the list filterer template is used*/
$( function() {
    var filterFunction = function(searchterm) {
	var searchterm = this.parentElement.getElementsByClassName('list-filterer-input').value;
	var searchtarget = this.parentElement.getElementsByClassName(this.getAttribute('data-list'));
	var searchin = searchtarget.length>0?searchtarget:[this.parentElement];
	var el = 'li';
	var e = this.parentElement.getElementsByTagName(el);
throw new Error('test');
        for ( var i=0;i<searchin.length;i++) {
            for ( var i=0;i<e.length;i++ ) {
                if (e[i].title.includes(searchterm) || e[i].textContent.includes(searchterm) ) {
					e[i].style.display='';
				} else {
					e[i].style.display='none';
				}
            }
		}
	};
    var s = document.getElementsByClassName('list-filterer');
    for ( var i=0; i<s.length; i++ ) {
        if (s[i].getAttribute('data-ready')) continue;
        var input = document.createElement('input');
        var button = document.createElement('input');
		
        input.setAttribute('class', 'list-filterer-input ' +s[i].getAttribute('class'));
        input.setAttribute('type', 'text');
        input.setAttribute('placeholder', s[i].getAttribute('data-placeholder'));
		
        button.setAttribute('class', 'list-filterer-button ' +s[i].getAttribute('class'));
        button.setAttribute('type', 'submit');
        button.setAttribute('value', s[i].getAttribute('data-value'));
		if (s[i].getAttribute('data-list')) {
		    button.setAttribute('data-list', s[i].getAttribute('data-list'));
		}
		
	    s[i].insertBefore(input, null);
	    s[i].insertBefore(button, null);
		input.onchange = filterFunction;
		button.onclick = filterFunction;
		
        s[i].setAttribute('data-ready', 'ready');
        input.setAttribute('data-ready', 'ready');
        button.setAttribute('data-ready', 'ready');
    }
} );

<nowiki>//#sourceURL=Common.js</nowiki>