MediaWiki:Common.js/Search.js

/** * Adds a "Mods" section in Special:Search so users can more * easily find content, but searches the main namespace by default * * Designed for Terraria Wiki, will not work properly on other wikis */ if (!window.TerrariaSearch) { (function (mw, $, ns) {       // code to run on Special:Search page only        ns.pageSpecialSearch = function  {            var fadeInMilliseconds = 600;

// make Articles search main namespace only var tabs = $('ul.search-tabs').children, articles = tabs.first, link = articles.children('a'), href = link.attr('href'); link.attr('href', href.replace("&ns114=1", "")); link.attr('title', "Search in (Main)");

// add Mods link after Articles articles.after('Mods'); $('#mods-tab').fadeIn(fadeInMilliseconds); // make sure that the Articles and Mods tabs are "selected" properly // without this, the Advanced tab would be selected instead var loc = window.location.href, index = loc.indexOf('ns'); if (-1 === loc.indexOf('advanced=1') && loc.lastIndexOf('ns') === index) { if (loc.indexOf('ns0') === index) { tabs.removeClass('selected'); tabs.first.removeClass('normal').addClass('selected'); } else if (loc.indexOf('ns114') === index) { tabs.removeClass('selected'); tabs.first.next.removeClass('normal').addClass('selected'); }           }            // Search main namespace only when using the search bar -- for Special:Search page if (articles.hasClass('selected')) { var searchBar = $('.WikiaSearch'); searchBar.children('input[name="ns114"]').remove; if (0 == searchBar.children('input[name="ns0"]').length) { searchBar.prepend(''); }           }

// In the list of results, give page titles their namespace prefix $('.result').each(function {                var link = $(this).find('a:first'),                    url = link.attr('href'),                    page = url.substr(31),                    pageIndexOfColon = page.indexOf(":"),                    namespace;                if ("http://terraria.wikia.com/wiki/" === url.slice(0, 31) && -1 != pageIndexOfColon) {                    namespace = page.substr(0, pageIndexOfColon).replace(/_/g, " ");                    link.css('display', 'none');                    link.text(namespace + ":" + link.text);                    link.fadeIn(fadeInMilliseconds);                }            }); };

// code to run for the search bar on other pages ns.pageOther = function { // Search main namespace only when using the search bar -- for non-Special:Search pages $('#WikiaSearch input[type="submit"]').before(''); }

// main init method to be called on pageload ns.init = function { if ("Search" === mw.config.get('wgCanonicalSpecialPageName')) { ns.pageSpecialSearch; } else { ns.pageOther; }       };

// on pageload handler $(ns.init); }(window.mediaWiki, window.jQuery, window.TerrariaSearch = {})); }