プロジェクト:ウィキ技術部/スクリプト開発/trunk/SpecialSearchEnhanced.js

/*
 *
 * Change Special:Search to use a drop-down menu
 *
 * Created by: [[en:User:Gracenotes]]
 * Modified by: [[ja:User:Mizusumashi]]
 *
 */

function createArgInput(name, value)
{
    var input = document.createElement('input');
    
    input.name = name;
    input.value = value;
    input.type = 'hidden';
    
    return input;
}

function SpecialSearchEnhanced()
{
    var searchForm = document.forms['powersearch'];
    if(! searchForm){
        return;
    }
    var searchBox = searchForm.search;
    var searchEngines = [];
    var selectBox = document.createElement('select');
    var hiddenDiv = document.createElement('div');
    hiddenDiv.style.display = 'none';

    searchForm.onsubmit = function () {
        if( ! searchEngines[selectBox.selectedIndex][0] ){
            return true;
        }
        searchEngines[selectBox.selectedIndex][0]();
        if(window.event){
            window.event.returnValue = false;
        }
        return false;
    }

    selectBox.onchange = function (){
        searchEngines[selectBox.selectedIndex][1]();
        if(window.event){
            window.event.returnValue = false;
        }
        return false;
    }

    var nsCheckBoxs = [
        searchForm.ns0,
        searchForm.ns1,
        searchForm.ns2,
        searchForm.ns3,
        searchForm.ns4,
        searchForm.ns5,
        searchForm.ns6,
        searchForm.ns7,
        searchForm.ns8,
        searchForm.ns9,
        searchForm.ns10,
        searchForm.ns11,
        searchForm.ns12,
        searchForm.ns13,
        searchForm.ns14,
        searchForm.ns15,
        searchForm.ns100,
        searchForm.ns101
    ]
    
    var setNsCheckBoxs = function(disabled, color){
        for(var i in nsCheckBoxs){
            i.disabled = disabled;
            if (i.nextSibling && i.nextSibling.nestSibling && i.nextSibling.nextSibling.style){
                i.nextSibling.nextSibling.style.color = color;
            }
        }
    }
    
    var enableNsCheckBoxs = function(){ setNsCheckBoxs(false, 'black')}
    var disableNsCheckBoxs = function(){ setNsCheckBoxs(true, 'silver')}

    var setOption = function (name, submit, change) {
        var opt = document.createElement('option');
        opt.appendChild(document.createTextNode(name));
        selectBox.appendChild(opt);
        searchEngines[searchEngines.length] = [submit, change];
    }
    
    var setSearchEngineOption = function (name, action, title, args) {
        var form = document.createElement('form');

        form.action = action;
        form.method = 'get';
        form.style.display = 'none';
        
        var keyword = createArgInput(title, '');
        form.appendChild(keyword);
        
        for(var i = 0; i < args.length; i += 2){
            form.appendChild(createArgInput(args[i],args[i+1]));
        }

        var search = function(){
            keyword.value = searchForm.search.value;
            form.submit();
        }

        setOption(name, search, disableNsCheckBoxs);
        hiddenDiv.appendChild(form);

        return form;
    }

    setOption('MediaWiki 検索 ', false, enableNsCheckBoxs);
    setSearchEngineOption('AltaVista ',
                          'http://www.altavista.com/web/results',
                          'aqa',
                          ['aqmode', 's', 'rc', 'dmn', 'swd', 'ja.wikipedia.org']);
    setSearchEngineOption('goo ',
                          'http://search.goo.ne.jp/web.jsp',
                          'MT',
                          ['Domain', 'ja.wikipedia.org', 'DS', 'IN', 'IE', 'UTF-8']);
    setSearchEngineOption('GoodSearch ',
                          'http://www.goodsearch.com/Search.aspx',
                          'Keywords',
                          ['site', 'ja.wikipedia.org', 'charityid', '812661']);
    setSearchEngineOption('Google ',
                          'http://www.google.co.jp/search',
                          'q',
                          ['as_sitesearch', 'ja.wikipedia.org']);
    setSearchEngineOption('Wikiwix ',
                          'http://www.wikiwix.com/index.php',
                          'action',
                          ['lang', 'ja'/*, 'disp', 'article' */]);
    setSearchEngineOption('Windows Live ',
                          'http://search.live.com/results.aspx',
                          'q',
                          ['q1', 'site:ja.wikipedia.org']);
    setSearchEngineOption('Yahoo! ',
                          'http://search.yahoo.co.jp/search',
                          'p',
                          ['vs', 'ja.wikipedia.org', 'ei', 'UTF-8']);

    searchBox.style.marginLeft = '0px';
    selectBox.style.marginTop = '2px';
    selectBox.style.marginRight = '2px';
    searchForm.search.parentNode.insertBefore(selectBox,
                                              searchForm.search.nextSibling.nextSibling);
    searchForm.appendChild(hiddenDiv);
    
    enableNsCheckBoxs();
}

$(SpecialSearchEnhanced);